コード例 #1
0
 def test_add_searchById_searchByName_remove_update_Service(self):
     self.assertEqual(self._servStudent.get_no_students(), 0)
     self._servStudent.add_student(25, "Sparrow")
     self._servStudent.add_student(26, "Spar")
     self.assertEqual(self._servStudent.get_no_students(), 2)
     self._servStudent.remove_student(26)
     self.assertEqual(self._servStudent.get_no_students(), 1)
     student_found = self._servStudent.get_student_by_id(25)
     self.assertEqual(student_found.get_name(), "Sparrow")
     try:
         self._servStudent.add_student(-23, "Hans")
         assert (False)
     except ValidError as ve:
         assert (str(ve) == "id invalid!\n")
     try:
         self._servStudent.add_student(20, "")
         assert (False)
     except ValidError as ve:
         assert (str(ve) == "name invalid!\n")
     try:
         self._servStudent.add_student(25, "Jack")
         assert (False)
     except RepoError as re:
         assert (str(re) == "id existent!\n")
     try:
         self._servStudent.get_student_by_id(44)
         assert (False)
     except RepoError as re:
         assert (str(re) == "id inexistent!\n")
     new = Student(25, "Danielo")
     self._servStudent.update_student(new)
     self.assertEqual(new.get_name(), "Danielo")
     self._servStudent.add_student(55, "Ken")
     self._servStudent.add_student(56, "Ben")
     self._servStudent.add_student(57, "Men")
     self._servStudent.add_student(58, "Fen")
     students = self._servStudent.get_students_by_name("en")
     self.assertEqual(len(students), 4)
コード例 #2
0
class TesteStudent(unittest.TestCase):
    def setUp(self):
        self._student1 = Student(20, "A")
        self._student2 = Student(21, "B")
        self._student3 = Student(22, "C")
        self._bad_id_student = Student(-20, "B")
        self._bad_name_student = Student(20, "")
        self._validStud = ValidatorStudent()
        self._repo = Repo()
        self._undoService = ServiceUndo()
        self._servStudent = ServiceStudents(self._repo, self._validStud,
                                            self._undoService)

    def test_create_student(self):
        self.assertEqual(self._student1.get_name(), "A")
        self.assertEqual(self._student1.get_s_id(), 20)
        self._student3.set_s_id(15)
        self.assertEqual(self._student3.get_s_id(), 15)
        self._student1.set_name("Karl")
        self.assertEqual(self._student1.get_name(), "Karl")
        self._other_student_same_id = Student(20, "Bob")
        self.assertEqual(self._other_student_same_id.get_s_id(),
                         self._student1.get_s_id())

    def testSortingGnome(self):
        listastuds = []
        listastuds.append(self._student2)
        listastuds.append(self._student3)
        listastuds.append(self._student1)
        gnomeSort(listastuds)
        self.assertEqual(listastuds[0].get_s_id(), self._student1.get_s_id())
        self.assertEqual(listastuds[1].get_s_id(), self._student2.get_s_id())

    def testFiltering(self):
        listastuds = []
        listastuds.append(self._student2)
        listastuds.append(self._student3)
        listastuds.append(self._student1)
        res = filterList(listastuds)
        self.assertEqual(len(res), 1)

    def test_get_all(self):
        self._servStudent.add_student(1, "George")
        self._servStudent.add_student(2, "Marcu")
        students = self._servStudent.get_students()
        self.assertEqual(students[0].get_name(), "George")
        self.assertEqual(students[1].get_name(), "Marcu")
        self.assertEqual(len(students), 2)

    def test_validator_student(self):
        self._validStud.validate_student(self._student1)
        try:
            self._validStud.validate_student(self._bad_id_student)
            assert (False)
        except ValidError as ve:
            assert (str(ve) == "id invalid!\n")
        try:
            self._validStud.validate_student(self._bad_name_student)
            assert (False)
        except ValidError as ve:
            assert (str(ve) == "name invalid!\n")

    def test_add_search_remove_repo(self):
        self.assertEqual(self._repo.size(), 0)
        self._repo.add(self._student1)
        self._repo.add(self._student2)
        self.assertEqual(self._repo.size(), 2)
        self._repo.remove_repo(self._student2)
        self.assertEqual(self._repo.size(), 1)
        #In repo a ramas doar student1! Pay attention!
        self._other_student_same_id = Student(20, "Bob")
        key_student = Student(self._student1.get_s_id(), None)
        student_found = self._repo.search(key_student)
        self.assertEqual(student_found.get_name(), self._student1.get_name())
        try:
            self._repo.add(self._other_student_same_id)
            assert (False)
        except RepoError as re:
            assert (str(re) == "id existent!\n")
        self._other_student = Student(100, "Mane")
        try:
            self._repo.search(self._other_student)
            assert (False)
        except RepoError as re:
            assert (str(re) == "id inexistent!\n")

    def test_add_searchById_searchByName_remove_update_Service(self):
        self.assertEqual(self._servStudent.get_no_students(), 0)
        self._servStudent.add_student(25, "Sparrow")
        self._servStudent.add_student(26, "Spar")
        self.assertEqual(self._servStudent.get_no_students(), 2)
        self._servStudent.remove_student(26)
        self.assertEqual(self._servStudent.get_no_students(), 1)
        student_found = self._servStudent.get_student_by_id(25)
        self.assertEqual(student_found.get_name(), "Sparrow")
        try:
            self._servStudent.add_student(-23, "Hans")
            assert (False)
        except ValidError as ve:
            assert (str(ve) == "id invalid!\n")
        try:
            self._servStudent.add_student(20, "")
            assert (False)
        except ValidError as ve:
            assert (str(ve) == "name invalid!\n")
        try:
            self._servStudent.add_student(25, "Jack")
            assert (False)
        except RepoError as re:
            assert (str(re) == "id existent!\n")
        try:
            self._servStudent.get_student_by_id(44)
            assert (False)
        except RepoError as re:
            assert (str(re) == "id inexistent!\n")
        new = Student(25, "Danielo")
        self._servStudent.update_student(new)
        self.assertEqual(new.get_name(), "Danielo")
        self._servStudent.add_student(55, "Ken")
        self._servStudent.add_student(56, "Ben")
        self._servStudent.add_student(57, "Men")
        self._servStudent.add_student(58, "Fen")
        students = self._servStudent.get_students_by_name("en")
        self.assertEqual(len(students), 4)

    def test_create_init_studs(self):
        self._servStudent.create_init_studs()
        self.assertEqual(self._repo.size(), 10)