def testValidateStudent(self): val = StudentValidator() """ Student ID must exist """ st = Student("", "Ion", Address("Adr", 1, "Cluj")) try: val.validate(st) self.assertTrue(False) except ValidationException: pass """ Student name must exist """ st = Student("3", "", Address("Adr", 1, "Cluj")) try: val.validate(st) self.assertTrue(False) except ValidationException: pass """ Address must exist """ st = Student("3", "Ion", Address("", "Cluj", "")) try: val.validate(st) self.assertTrue(False) except ValidationException: pass
def testCreateStudent(self): st = Student("1", "Ion", Address("Cluj", "Cluj", "Adr2")) self.assertEqual(st.getId(), "1") self.assertEqual(st.getName(), "Ion") self.assertEqual(st.getAddr().getStreet(), "Adr2") st = Student("2", "Ion2", Address("Cluj", "Cluj", "Adr2")) self.assertEqual(st.getId(), "2") self.assertEqual(st.getName(), "Ion2") self.assertEqual(st.getAddr().getStreet(), "Adr2") self.assertEqual(st.getAddr().getCity(), "Cluj")
def testStudentIdentity(self): """ Student objects are defined by their identity and their lifecycle within the application """ st1 = Student("1", "Ion", Address("Adr", 1, "Cluj")) st2 = Student("1", "Ion", Address("Adr2", 1, "Cluj")) self.assertEqual(st1, st2) st1 = Student("1", "Popescu", Address("Adr", 1, "Cluj")) st2 = Student("2", "Popescu", Address("Adr2", 1, "Cluj")) self.assertNotEqual(st1, st2)
def testUpdate(self): rep = StudentRepository() st = Student("1", "Ion", Address("Cluj", "Cluj-Napoca", "Adr")) rep.store(st) st = Student("1", "Ionel", Address("Bihor", "Oradea", "Adr8")) rep.update("1", st) self.assertEqual(rep.find("1").getName(), "Ionel") self.assertEqual(rep.find("1").getAddr().getStreet(), "Adr8") try: rep.update("2", st) self.assertTrue(False) except ValueError: pass
def testDeleteStudent(self): rep = StudentRepository() st = Student("1", "Ion", Address("Adr", 1, "Cluj")) rep.store(st) st = Student("2", "Ion2", Address("Adr2", 1, "Cluj")) rep.store(st) self.assertEqual(rep.size(), 2) rep.remove("1") self.assertEqual(rep.size(), 1) try: rep.remove("3") self.assertTrue(False) except ValueError: pass
def testCreateGrade(self): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 9.5) self.assertEqual(gr.getStudent(), st) self.assertEqual(gr.getDiscipline(), "FP") self.assertEqual(gr.getGradeValue(), 9.5)
def testListStudent(self): rep = StudentRepository() st = Student("1", "Ion", Address("Tulcea", "Tulcea", "Adr1")) rep.store(st) st = Student("2", "Ion2", Address("Cluj", "Cluj-Napoca", "Adr2")) rep.store(st) allStudents = rep.getAll() self.assertEqual(len(allStudents), 2) st1 = rep.find("1") self.assertEqual(st1.getId(), "1") self.assertEqual(st1.getName(), "Ion") self.assertEqual(st1.getAddr().getStreet(), "Adr1") st2 = rep.find("2") self.assertEqual(st2.getId(), "2") self.assertEqual(st2.getName(), "Ion2") self.assertEqual(st2.getAddr().getStreet(), "Adr2")
def testGetGrades(self): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 10) rep = GradeRepository() rep.store(gr) gr = Grade(st, "SO", 9.5) rep.store(gr) grades = rep.getAll(st) self.assertEqual(grades[0].getStudent(), st) self.assertEqual(grades[0].getGradeValue(), 10) self.assertEqual(grades[0].getDiscipline(), "FP")
def testStoreStudent(self): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) rep = StudentRepository() """ Test adding students """ self.assertEqual(rep.size(), 0) rep.store(st) self.assertEqual(rep.size(), 1) st2 = Student("2", "Vasile", Address("Adr2", 1, "Cluj")) rep.store(st2) self.assertEqual(rep.size(), 2) """ Test with duplicate student id """ st3 = Student("2", "Ana", Address("Adr3", 1, "Cluj")) try: rep.store(st3) self.assertTrue(False) except DuplicateIDException: pass
def testValidateGrade(self): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) """ Grade must be between 1 and 10 """ gr = Grade(st, "FP", 11) val = GradeValidator() try: val.validate(gr) self.assertTrue(False) except ValidationException: pass
def __loadFromFile(self): try: f = open(self.__fName, "r") line = f.readline().strip() while line != "": attrs = line.split(";") student = Student(attrs[0], attrs[1], Address(attrs[2], attrs[3], attrs[4])) StudentRepository.store(self, student) line = f.readline().strip() except IOError: raise FileRepositoryException() finally: f.close()
def testGetStudentGrades(self): rep = GradeRepository() st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr = Grade(st, "FP", 10) rep.store(gr) gr = Grade(st, "SO", 9.5) rep.store(gr) studentGrades = rep.getAllForDisc("FP") self.assertEqual(len(studentGrades), 1) self.assertEqual(studentGrades[0].getStudentId(), "1") self.assertEqual(studentGrades[0].getGradeValue(), 10) self.assertEqual(studentGrades[0].getDiscipline(), "FP") st = Student("2", "Ionel", Address("Adr2", 22, "Cluj")) gr = Grade(st, "SO", 9.5) rep.store(gr) studentGrades = rep.getAllForDisc("SO") self.assertEqual(len(studentGrades), 2) self.assertEqual(studentGrades[0].getStudentId(), "1") self.assertEqual(studentGrades[1].getStudentId(), "2")
def testStoreGrade(self): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) rep = GradeRepository() self.assertEqual(rep.size(), 0) gr1 = Grade(st, "FP", 9.5) rep.store(gr1) self.assertEqual(rep.size(), 1) gr2 = rep.find(st, "FP") self.assertEqual(gr2, gr1) gr1 = Grade(st, "FP", 9) try: rep.store(gr1) self.assertTrue(False) except GradeAlreadyAssignedException: pass
def testIdentityGrade(self): st = Student("1", "Ion", Address("Adr", 1, "Cluj")) gr1 = Grade(st, "FP", 9.5) gr2 = Grade(st, "FP", 8.5) self.assertEqual(gr1, gr2)