def delete(self, id): try: f = open("data/courses", "r") f1 = open("data/temp", "w") found = False for line in f: object = line.split(",") course = Course(int(object[0]), object[1], object[2]) self.validator.validate(course) if course.get_id() != id: s = str(course.get_id()) + "," + course.get_name( ) + "," + course.get_professor() + ",\n" f1.write(s) else: found = True f.close() f1.close() if found == False: raise CourseError("Course not found") f1 = open("data/temp", "r") f = open("data/courses", "w") for line in f1: f.write(line) f.close() f1.close() except IOError: raise ValueError("Cannot read course data")
class TestCourseController(unittest.TestCase): def setUp(self): courses = CourseRepository() self.course1 = Course(1, "Asc", "Vancea") self.course2 = Course(2, "Fp", "I") self.course3 = Course(3, "Logica", "Pop") courses.save(self.course1) courses.save(self.course2) self.course_controller = CourseController(courses) def test_get_all(self): self.assertEqual(self.course_controller.get_all()[0], self.course1) self.assertEqual(self.course_controller.get_all()[1], self.course2) def test_add(self): self.course_controller.add(self.course3.get_id(), self.course3.get_name(), self.course3.get_professor()) self.assertEqual(self.course_controller.get_all()[2].get_id(), self.course3.get_id()) self.assertEqual(self.course_controller.get_all()[2].get_name(), self.course3.get_name()) self.assertEqual(self.course_controller.get_all()[2].get_professor(), self.course3.get_professor()) self.assertRaises( CourseError, lambda: self.course_controller.add(1, "Algebra", "Modoi")) def test_search_id(self): self.assertEqual( self.course_controller.search_by_id(1).get_name(), "Asc") self.assertEqual( self.course_controller.search_by_id(1).get_professor(), "Vancea") self.assertRaises(CourseError, lambda: self.course_controller.search_by_id(4))
def find_by_id(self, id): try: f = open("data/courses", "r") for line in f: object = line.split(",") course = Course(int(object[0]), object[1], object[2]) self.validator.validate(course) if course.get_id() == id: f.close() return course return -1 except IOError: raise ValueError("Cannot read course data")
def save(self, course_to_add): try: f = open("data/courses", "a+") for line in f: object = line.split(",") course = Course(int(object[0]), object[1], object[2]) self.validator.validate(course) if course.get_id() == course_to_add.get_id(): raise CourseError("Duplicated Id's") s = str(course_to_add.get_id()) + "," + course_to_add.get_name( ) + "," + course_to_add.get_professor() + ",\n" f.write(s) f.close() except IOError: raise ValueError("Cannot read course data")
def find_index(self, course_to_add): try: f = open("data/courses", "r") index = 0 for line in f: object = line.split(",") course = Course(int(object[0]), object[1], object[2]) self.validator.validate(course) if course.get_id() == course_to_add.get_id(): f.close() return index else: index = index + 1 return -1 except IOError: raise ValueError("Cannot read course data")
def update_course(self, id, name, professor): ''' update_course -> None Updates a Course from the list of courses If the course doesn't exist or there id a validation error, raises CourseError :param id: int :param name: string :param professor: string ''' course = Course(id, name, professor) self.course_validator.validate(course) self.course_repository.update(course) grades_to_update = self.grade_repository.find_by_course( course.get_id()) for i in range(0, len(grades_to_update)): student = self.student_repository.find_by_id( grades_to_update[i].get_id_student()) grade = grades_to_update[i].get_grade() grade = self.create(student, course, grade) self.grade_repository.update(grade)
class TestCourseRepository(unittest.TestCase): def setUp(self): self.grades = GradeRepository() self.course1 = Course(1, "Asc", "Vancea") self.student1 = Student(1, "Vlad") self.student2 = Student(2, "Ion") self.course2 = Course(2, "Fp", "I") self.grade1 = Grade(self.student1, self.course1, 10) self.grade2 = Grade(self.student1, self.course2, 4) self.grade3 = Grade(self.student2, self.course1, 7) self.grade4 = Grade(self.student1, self.course1, 8) self.grades.save(self.grade1) self.grades.save(self.grade2) def test_save(self): self.grades.save(self.grade3) self.assertEqual(self.grades.find_all()[2].get_id_student(), self.grade3.get_student().get_id()) self.assertEqual(self.grades.find_all()[2].get_id_course(), self.grade3.get_course().get_id()) self.assertEqual(self.grades.find_all()[2].get_grade(), self.grade3.get_grade()) self.assertRaises(GradeError, lambda: self.grades.save(self.grade4)) def test_find_all(self): self.assertEqual(self.grades.find_all()[0].get_id_student(), self.grade1.get_student().get_id()) self.assertEqual(self.grades.find_all()[0].get_id_course(), self.grade1.get_course().get_id()) self.assertEqual(self.grades.find_all()[0].get_grade(), self.grade1.get_grade()) self.assertEqual(self.grades.find_all()[1].get_id_student(), self.grade2.get_student().get_id()) self.assertEqual(self.grades.find_all()[1].get_id_course(), self.grade2.get_course().get_id()) self.assertEqual(self.grades.find_all()[1].get_grade(), self.grade2.get_grade()) def test_update(self): self.grades.update(self.grade4) self.assertEqual(self.grades.find_all()[0].get_id_student(), self.grade4.get_student().get_id()) self.assertEqual(self.grades.find_all()[0].get_id_course(), self.grade4.get_course().get_id()) self.assertEqual(self.grades.find_all()[0].get_grade(), self.grade4.get_grade()) self.assertRaises(GradeError, lambda: self.grades.update(self.grade3)) def test_delete(self): self.grades.delete(1, 1) self.assertEqual(self.grades.find_all()[0].get_id_student(), self.grade2.get_student().get_id()) self.assertEqual(self.grades.find_all()[0].get_id_course(), self.grade2.get_course().get_id()) self.assertEqual(self.grades.find_all()[0].get_grade(), self.grade2.get_grade()) self.assertRaises(GradeError, lambda: self.grades.delete(4, 1)) self.assertRaises(GradeError, lambda: self.grades.delete(1, 4)) def test_find_by_id(self): self.assertEqual(self.grades.find_by_id(1, 1), self.grade1) self.assertEqual(self.grades.find_by_id(5, 1), -1) def test_find_index(self): self.assertEqual(self.grades.find_index(self.grade2), 1) self.assertEqual(self.grades.find_index(self.grade3), -1) def test_find_by_student(self): self.assertEqual( self.grades.find_by_student(1)[0].get_id_course(), self.course1.get_id()) self.assertEqual( self.grades.find_by_student(1)[0].get_course_name(), self.course1.get_name()) self.assertEqual(self.grades.find_by_student(1)[0].get_grade(), 10) self.assertEqual( self.grades.find_by_student(1)[1].get_id_course(), self.course2.get_id()) self.assertEqual( self.grades.find_by_student(1)[1].get_course_name(), self.course2.get_name()) self.assertEqual(self.grades.find_by_student(1)[1].get_grade(), 4) self.assertEqual(self.grades.find_by_student(5), []) repo = GradeRepository() self.assertEqual(repo.find_by_student(1), []) def test_find_by_course(self): self.grades.save(self.grade3) self.assertEqual( self.grades.find_by_course(1)[0].get_id_student(), self.student1.get_id()) self.assertEqual( self.grades.find_by_course(1)[1].get_id_student(), self.student2.get_id()) self.assertEqual( self.grades.find_by_course(1)[0].get_student_name(), self.student1.get_name()) self.assertEqual( self.grades.find_by_course(1)[1].get_student_name(), self.student2.get_name()) self.assertEqual(self.grades.find_by_course(1)[0].get_grade(), 10) self.assertEqual(self.grades.find_by_course(1)[1].get_grade(), 7) self.assertEqual(self.grades.find_by_course(5), [])
class TestGradeController(unittest.TestCase): def setUp(self): self.students = StudentRepository() courses = CourseRepository() grades = GradeRepository() self.course1 = Course(1, "Asc", "Vancea") self.student1 = Student(1, "Vlad") self.student2 = Student(2, "Alex") courses.save(self.course1) self.students.save(self.student1) self.students.save(self.student2) self.course2 = Course(2, "Fp", "I") courses.save(self.course2) self.grade1 = Grade(self.student1, self.course1, 10) self.grade2 = Grade(self.student1, self.course2, 4) grades.save(self.grade1) grades.save(self.grade2) self.grade_controller = GradeController(courses, self.students, grades) def test_get_all(self): self.assertEqual(self.grade_controller.get_all()[0].get_id_student(), self.grade1.get_student().get_id()) self.assertEqual(self.grade_controller.get_all()[0].get_id_course(), self.grade1.get_course().get_id()) self.assertEqual(self.grade_controller.get_all()[0].get_grade(), self.grade1.get_grade()) self.assertEqual(self.grade_controller.get_all()[1].get_id_student(), self.grade2.get_student().get_id()) self.assertEqual(self.grade_controller.get_all()[1].get_id_course(), self.grade2.get_course().get_id()) self.assertEqual(self.grade_controller.get_all()[1].get_grade(), self.grade2.get_grade()) def test_add(self): self.grade_controller.add(self.student2.get_id(), self.course1.get_id(), 10) self.assertEqual(self.grade_controller.get_all()[2].get_id_student(), self.student2.get_id()) self.assertEqual(self.grade_controller.get_all()[2].get_id_course(), self.course1.get_id()) self.assertEqual(self.grade_controller.get_all()[2].get_grade(), 10) self.assertRaises(GradeError, lambda: self.grade_controller.add(1, 1, 3.5)) self.assertRaises(GradeError, lambda: self.grade_controller.add(10, 1, 3.5)) self.assertRaises(GradeError, lambda: self.grade_controller.add(1, 10, 3.5)) self.assertRaises(GradeError, lambda: self.grade_controller.add(1, 10, "a")) def test_update_course(self): self.grade_controller.update_course(1, "Analiza", "Berinde") self.assertEqual(self.grade_controller.get_all()[0].get_course_name(), "Analiza") self.assertEqual(self.grade_controller.get_all()[0].get_professor(), "Berinde") self.assertRaises( CourseError, lambda: self.grade_controller.update_course( 10, "Analiza", "Berinde")) self.assertRaises( CourseError, lambda: self.grade_controller.update_course( "a", "Analiza", "Berinde")) def test_update_student(self): self.grade_controller.update_student(1, "Berinde") self.assertEqual(self.grade_controller.get_all()[0].get_student_name(), "Berinde") self.assertRaises( StudentError, lambda: self.grade_controller.update_student(10, "Berinde")) self.assertRaises( StudentError, lambda: self.grade_controller.update_student("a", "Berinde")) self.assertRaises( StudentError, lambda: self.grade_controller.update_student(1, "berinde")) def test_update_grade(self): self.grade_controller.update_grade(1, 1, 9) self.assertEqual(self.grade_controller.get_all()[0].get_grade(), 9) self.assertRaises( GradeError, lambda: self.grade_controller.update_grade(10, 1, 10)) self.assertRaises( GradeError, lambda: self.grade_controller.update_grade(1, 10, 10)) self.assertRaises( GradeError, lambda: self.grade_controller.update_grade("a", 10, 10)) self.assertRaises( GradeError, lambda: self.grade_controller.update_grade(1, 1, -10)) def test_remove_course(self): self.grade_controller.remove_course(1) self.assertEqual(self.grade_controller.get_all()[0].get_grade(), 4) self.assertEqual(self.grade_controller.get_all()[0].get_id_student(), 1) self.assertEqual(self.grade_controller.get_all()[0].get_id_course(), 2) self.assertRaises(CourseError, lambda: self.grade_controller.remove_course(10)) def test_remove_student(self): self.grade_controller.remove_student(1) self.assertEqual(self.grade_controller.get_all(), []) self.assertRaises(StudentError, lambda: self.grade_controller.remove_student(10)) def test_remove_grade(self): self.grade_controller.remove_grade(1, 1) self.assertEqual(self.grade_controller.get_all()[0].get_id_student(), 1) self.assertEqual(self.grade_controller.get_all()[0].get_id_course(), 2) self.assertRaises(GradeError, lambda: self.grade_controller.remove_grade(10, 1)) def test_best_students(self): student3 = Student(3, "Maria") self.students.save(student3) self.grade_controller.add(self.student2.get_id(), self.course1.get_id(), 10) self.grade_controller.add(self.student2.get_id(), self.course2.get_id(), 5) self.grade_controller.add(student3.get_id(), self.course1.get_id(), 7) self.grade_controller.add(student3.get_id(), self.course2.get_id(), 7) self.assertEqual(len(self.grade_controller.search_highest_grades()), 1) self.assertEqual( self.grade_controller.search_highest_grades()[0][0].get_id(), 2) self.assertEqual( self.grade_controller.search_highest_grades()[0][0].get_name(), "Alex") self.assertEqual(self.grade_controller.search_highest_grades()[0][1], 7.5) def test_failed_students(self): student3 = Student(3, "Maria") self.students.save(student3) self.grade_controller.add(self.student2.get_id(), self.course1.get_id(), 4) self.grade_controller.add(self.student2.get_id(), self.course2.get_id(), 5) self.grade_controller.add(student3.get_id(), self.course1.get_id(), 4) self.grade_controller.add(student3.get_id(), self.course2.get_id(), 7) self.assertEqual(self.grade_controller.failed_students()[0][0], "Vancea") self.assertEqual(self.grade_controller.failed_students()[1][0], "I") self.assertEqual(self.grade_controller.failed_students()[0][1], 2) self.assertEqual(self.grade_controller.failed_students()[1][1], 1) def test_search_course_grades(self): self.grade_controller.add(self.student2.get_id(), self.course1.get_id(), 4) self.assertEqual( self.grade_controller.search_course_grades(1)[1].get_id_student(), 1) self.assertEqual( self.grade_controller.search_course_grades(1) [0].get_student_name(), "Alex") self.assertEqual( self.grade_controller.search_course_grades(1) [1].get_student_name(), "Vlad") self.assertEqual( self.grade_controller.search_course_grades(1)[0].get_id_student(), 2) self.assertEqual(self.grade_controller.search_course_grades(4), [])