Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # FOR TEST
- class Student:
- def __init__(self, name: str, courses=None):
- if courses is None:
- courses = {}
- self.name = name
- self.courses = courses # {course_name: [notes]}
- def enroll(self, course_name: str, notes, add_course_notes: str = ""):
- if course_name in self.courses.keys():
- [self.courses[course_name].append(x) for x in notes]
- return "Course already added. Notes have been updated."
- if add_course_notes == "Y" or add_course_notes == "":
- self.courses[course_name] = notes
- return "Course and course notes have been added."
- self.courses[course_name] = []
- return "Course has been added."
- def add_notes(self, course_name, notes):
- if course_name in self.courses.keys():
- self.courses[course_name].append(notes)
- return "Notes have been updated"
- raise Exception("Cannot add notes. Course not found.")
- def leave_course(self, course_name):
- if course_name in self.courses.keys():
- self.courses.pop(course_name)
- return "Course has been removed"
- raise Exception("Cannot remove course. Course not found.")
- ##################################################################
- ####################################################################
- # TEST CODE
- from project.student import Student
- from unittest import TestCase, main
- class TestStudent(TestCase):
- def test_01_initialization_without_course_data(self):
- student = Student("Gosho")
- self.assertEqual("Gosho", student.name)
- self.assertEqual({}, student.courses)
- def test_02_initialization_with_course_data(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- self.assertEqual("Gosho", student.name)
- self.assertEqual({"Python": ["P1", "P2"]}, student.courses)
- def test_03_enrol_method_add_notes_with_course_existed(self):
- student = Student("Gosho", {"Python": ["P1", "P2"], "Javascript": ["J1"]})
- student.enroll("Python", ["P3", "P4"])
- self.assertEqual({"Python": ["P1", "P2", "P3", "P4"], "Javascript": ["J1"]}, student.courses)
- def test_04_enrol_method_add_notes_with_course_existed_returns_message(self):
- student = Student("Gosho", {"Python": ["P1", "P2"], "Javascript": ["J1"]})
- res = student.enroll("Python", ["P3", "P4"])
- message = "Course already added. Notes have been updated."
- self.assertEqual(message, res)
- def test_05_enrol_method_with_add_course_notes_equal_to_y(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- student.enroll("Javascript", ["J1"], "Y")
- self.assertEqual({"Python": ["P1", "P2"], "Javascript": ["J1"]}, student.courses)
- def test_06_enrol_method_with_empty_string(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- student.enroll("Javascript", ["J1"], "")
- self.assertEqual({"Python": ["P1", "P2"], "Javascript": ["J1"]}, student.courses)
- def test_07_enrol_method_return_message_when_add_course_equal_to_y_or_empty_string(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- message = "Course and course notes have been added."
- # add_notes = ""
- res = student.enroll("Javascript", ["J1"], "Y")
- self.assertEqual(res, message)
- # add_notes = "Y"
- res2 = student.enroll("Javascript", ["J1"], "")
- self.assertEqual(res, message)
- def test_08_enrol_method_without_adding_notes(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- student.enroll("Javascript", ["J1"], "x")
- self.assertEqual({"Python": ["P1", "P2"], "Javascript": []}, student.courses)
- def test_09_enrol_method_without_adding_notes_and_returns_message(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- message = "Course has been added."
- res = student.enroll("Javascript", ["J1"], "x")
- self.assertEqual(res, message)
- def test_10_add_notes_with_existing_course_name(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- message = "Notes have been updated"
- res = student.add_notes("Python", "P3")
- self.assertEqual({"Python": ["P1", "P2", "P3"]}, student.courses)
- self.assertEqual(message, res)
- def test_11_add_notes_with_not_existing_course_name(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- message = "Cannot add notes. Course not found."
- with self.assertRaises(Exception) as ex:
- student.add_notes("Javascript", "J1")
- self.assertEqual(message, str(ex.exception))
- def test_12_leave_course_when_course_name_existed(self):
- student = Student("Gosho", {"Python": ["P1", "P2"], "Javascript": ["J1"]})
- message = "Course has been removed"
- res = student.leave_course("Javascript")
- self.assertEqual({"Python": ["P1", "P2"]}, student.courses)
- self.assertEqual(message, res)
- def test_13_leave_course_with_not_existed_course_name(self):
- student = Student("Gosho", {"Python": ["P1", "P2"]})
- message = "Cannot remove course. Course not found."
- with self.assertRaises(Exception) as ex:
- student.leave_course("Javascript")
- self.assertEqual(message, str(ex.exception))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement