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 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")
def find_by_professor(self, professor): try: f = open("data/courses", "r") courses = [] for line in f: object = line.split(",") course = Course(int(object[0]), object[1], object[2]) self.validator.validate(course) if course.get_professor() == professor: courses.append(course) return courses except IOError: raise ValueError("Cannot read course data")