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 create_init_studs(self): id_base = 1 lista_studenti = ["Mario", "Dan", "Clara", "Flora", "Jane", "Jack", "Kis", "Barney", "Hector", "Ken"] for i in range(0, 10): s = Student(id_base, random.choice(lista_studenti)) id_base+=1 self._repoStudents.add(s)
def get_student_by_id(self, s_id): """ Returns a student from repo by id :params s_id = id-ul studentului """ key = Student(s_id , None) return self._repoStudents.search(key)
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_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_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 best_students(self): """ Creates statisctic! More precisely, it returns the students with the best averages """ stud_dict = {} for grade in self._repoGrades.get_all(): if (grade.get_id_stud()) not in stud_dict: stud_dict[grade.get_id_stud()] = [] stud_dict[grade.get_id_stud()].append(grade.get_grade()) res = [] for item in stud_dict.items(): average = sum(item[1])/len(item[1]) student = self._repoStudents.search(Student(item[0], "")) res.append(StudentDTO(student, average)) return res
def add_student(self, s_id, name, from_undo = False): """ Creates a student that will be added in the repoStudents :params s_id = id-ul noului elev creat :params name = numele noului elev creat :params from_undo: False if the function eas not called from the undo option, True otherwise """ student = Student(s_id, name) self._validatorStudents.validate_student(student) self._repoStudents.add(student) if not from_undo: function = FunctionCall(self.add_student, s_id, name , True) reverse_function = FunctionCall(self.remove_student, s_id , True) operation = Operation(function, reverse_function) self._undoService.record_operation(operation)
def remove_student(self, s_id, from_undo = False): """ Removes a student from repo :params s_id = id-ul studentului care va fi sters :params from_undo: False if the function eas not called from the undo option, True otherwise """ student = Student(s_id, None) deleted_student = self._repoStudents.search(student) self._repoStudents.remove_repo(student) if not from_undo: function = FunctionCall(self.remove_student, s_id, True) reverse_function = FunctionCall(self.add_student, s_id , deleted_student.get_name(), True) operation = Operation(function, reverse_function) cascaded_operation = CascadedOperation() cascaded_operation.add(operation) self._undoService.record_operation(cascaded_operation)
def grades_average(self): """ Calculates the average of the grades and shows the students that fail """ stud_dict = {} for grade in self._repoGrades.get_all(): if (grade.get_id_stud(), grade.get_id_disc()) not in stud_dict: stud_dict[(grade.get_id_stud(), grade.get_id_disc())] = [] stud_dict[(grade.get_id_stud(), grade.get_id_disc())].append(grade.get_grade()) res = [] for item in stud_dict.items(): average = sum(item[1])/len(item[1]) if average < 5: student = self._repoStudents.search(Student(item[0][0], "")) res.append(StudentDTO(student, average)) return res
def test_add_removebyID_average_SERVICE(self): #self._gradeService.give_grade(4, 5, 6) #grades = self._gradeService.get_grades() #self.assertEqual(len(grades) ,1) self.assertEqual(self._gradeService.get_grades_no(), 0) self._gradeService.give_grade(9, 3, 5) self._gradeService.give_grade(9, 4, 8) self.assertEqual(self._gradeService.get_grades_no(), 2) self._gradeService.remove_grade_by_idStud_and_grade(9, 3, 5) self.assertEqual(self._gradeService.get_grades_no(), 1) self._gradeService.remove_grade_by_disc_id(4) self.assertEqual(self._gradeService.get_grades_no(), 0) self._gradeService.give_grade(1, 8, 10) self._gradeService.remove_grade_by_student_id(1) self.assertEqual(self._gradeService.get_grades_no(), 0) self._gradeService.give_grade(1, 1, 1) grades = self._gradeService.get_grades() self.assertEqual(len(grades), 1) self._serviceStudent.add_student(1, "Jane") self._serviceStudent.add_student(2, "Marc") self._gradeService.give_grade(2, 2, 1) self._gradeService.give_grade(1, 3, 2) failing_students = self._gradeService.grades_average() self.assertEqual(len(failing_students), 3) average_disc = self._gradeService.average_student_discipline(2) self.assertEqual(average_disc, 1.0) best_studs = self._gradeService.best_students() self.assertEqual(len(best_studs), 2) self.assertEqual(best_studs[0].get_student(), Student(1, "Jane")) self.assertEqual(best_studs[0].get_stud_average(), 1.5) self._repoDisciplines.add(Discipline(2, "Ala")) self._repoDisciplines.add(Discipline(3, "Kaka")) grade = self._gradeService.find_by_id(2) self.assertEqual(grade.get_id_disc(), 2) best_disc = self._gradeService.best_disciplines() self.assertEqual(len(best_disc), 3) self.assertEqual(best_disc[0].get_discipline(), None) self.assertEqual(best_disc[1].get_disc_average(), 1.0)
def _ui_update_stud(self, params): if len(params) != 2: raise ValueError("No. params invalid! Must be 2!") s_id = int(params[0]) new_name = params[1] self._serviceStudents.update_student(Student(s_id, new_name))
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)