Advertisement
go6odn28

student_test

Dec 11th, 2023
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.42 KB | Software | 0 0
  1. # FOR TEST
  2. class Student:
  3.     def __init__(self, name: str, courses=None):
  4.         if courses is None:
  5.             courses = {}
  6.         self.name = name
  7.         self.courses = courses  # {course_name: [notes]}
  8.  
  9.     def enroll(self, course_name: str, notes, add_course_notes: str = ""):
  10.         if course_name in self.courses.keys():
  11.             [self.courses[course_name].append(x) for x in notes]
  12.             return "Course already added. Notes have been updated."
  13.  
  14.         if add_course_notes == "Y" or add_course_notes == "":
  15.             self.courses[course_name] = notes
  16.             return "Course and course notes have been added."
  17.  
  18.         self.courses[course_name] = []
  19.         return "Course has been added."
  20.  
  21.     def add_notes(self, course_name, notes):
  22.         if course_name in self.courses.keys():
  23.             self.courses[course_name].append(notes)
  24.             return "Notes have been updated"
  25.         raise Exception("Cannot add notes. Course not found.")
  26.  
  27.     def leave_course(self, course_name):
  28.         if course_name in self.courses.keys():
  29.             self.courses.pop(course_name)
  30.             return "Course has been removed"
  31.         raise Exception("Cannot remove course. Course not found.")
  32.  
  33.  
  34. ##################################################################
  35. ####################################################################
  36. # TEST CODE
  37.  
  38. from project.student import Student
  39.  
  40. from unittest import TestCase, main
  41.  
  42.  
  43. class TestStudent(TestCase):
  44.     def test_01_initialization_without_course_data(self):
  45.         student = Student("Gosho")
  46.         self.assertEqual("Gosho", student.name)
  47.         self.assertEqual({}, student.courses)
  48.  
  49.     def test_02_initialization_with_course_data(self):
  50.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  51.         self.assertEqual("Gosho", student.name)
  52.         self.assertEqual({"Python": ["P1", "P2"]}, student.courses)
  53.  
  54.     def test_03_enrol_method_add_notes_with_course_existed(self):
  55.         student = Student("Gosho", {"Python": ["P1", "P2"], "Javascript": ["J1"]})
  56.         student.enroll("Python", ["P3", "P4"])
  57.         self.assertEqual({"Python": ["P1", "P2", "P3", "P4"], "Javascript": ["J1"]}, student.courses)
  58.  
  59.     def test_04_enrol_method_add_notes_with_course_existed_returns_message(self):
  60.         student = Student("Gosho", {"Python": ["P1", "P2"], "Javascript": ["J1"]})
  61.         res = student.enroll("Python", ["P3", "P4"])
  62.         message = "Course already added. Notes have been updated."
  63.         self.assertEqual(message, res)
  64.  
  65.     def test_05_enrol_method_with_add_course_notes_equal_to_y(self):
  66.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  67.         student.enroll("Javascript", ["J1"], "Y")
  68.         self.assertEqual({"Python": ["P1", "P2"], "Javascript": ["J1"]}, student.courses)
  69.  
  70.     def test_06_enrol_method_with_empty_string(self):
  71.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  72.         student.enroll("Javascript", ["J1"], "")
  73.         self.assertEqual({"Python": ["P1", "P2"], "Javascript": ["J1"]}, student.courses)
  74.  
  75.     def test_07_enrol_method_return_message_when_add_course_equal_to_y_or_empty_string(self):
  76.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  77.         message = "Course and course notes have been added."
  78.  
  79.         # add_notes = ""
  80.         res = student.enroll("Javascript", ["J1"], "Y")
  81.         self.assertEqual(res, message)
  82.  
  83.         # add_notes = "Y"
  84.         res2 = student.enroll("Javascript", ["J1"], "")
  85.         self.assertEqual(res, message)
  86.  
  87.     def test_08_enrol_method_without_adding_notes(self):
  88.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  89.         student.enroll("Javascript", ["J1"], "x")
  90.         self.assertEqual({"Python": ["P1", "P2"], "Javascript": []}, student.courses)
  91.  
  92.     def test_09_enrol_method_without_adding_notes_and_returns_message(self):
  93.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  94.         message = "Course has been added."
  95.  
  96.         res = student.enroll("Javascript", ["J1"], "x")
  97.         self.assertEqual(res, message)
  98.  
  99.     def test_10_add_notes_with_existing_course_name(self):
  100.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  101.         message = "Notes have been updated"
  102.  
  103.         res = student.add_notes("Python", "P3")
  104.         self.assertEqual({"Python": ["P1", "P2", "P3"]}, student.courses)
  105.         self.assertEqual(message, res)
  106.  
  107.     def test_11_add_notes_with_not_existing_course_name(self):
  108.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  109.         message = "Cannot add notes. Course not found."
  110.  
  111.         with self.assertRaises(Exception) as ex:
  112.             student.add_notes("Javascript", "J1")
  113.         self.assertEqual(message, str(ex.exception))
  114.  
  115.     def test_12_leave_course_when_course_name_existed(self):
  116.         student = Student("Gosho", {"Python": ["P1", "P2"], "Javascript": ["J1"]})
  117.         message = "Course has been removed"
  118.  
  119.         res = student.leave_course("Javascript")
  120.         self.assertEqual({"Python": ["P1", "P2"]}, student.courses)
  121.         self.assertEqual(message, res)
  122.  
  123.     def test_13_leave_course_with_not_existed_course_name(self):
  124.         student = Student("Gosho", {"Python": ["P1", "P2"]})
  125.         message = "Cannot remove course. Course not found."
  126.  
  127.         with self.assertRaises(Exception) as ex:
  128.             student.leave_course("Javascript")
  129.         self.assertEqual(message, str(ex.exception))
  130.  
  131.  
  132. if __name__ == "__main__":
  133.     main()
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement