def test_add_notes_when_course_not_exist(self): student = Student("ivan", {"softuni": ["first", "second"]}) with self.assertRaises(Exception) as ex: student.add_notes("not_exist_course", "zero") self.assertEqual("Cannot add notes. Course not found.", str(ex.exception))
class TestStudent(unittest.TestCase): def setUp(self): self.st = Student("Kalin") def test_init(self): self.assertEqual(self.st.name, "Kalin") self.assertEqual(self.st.courses, {}) def test_add_non_existing_course_with_notes(self): self.st.courses = {} new_notes = self.st.enroll("Pyp", ["add those notes"]) self.assertEqual(self.st.courses, {"Pyp": ["add those notes"]}) self.assertEqual(new_notes, "Course and course notes have been added.") def test_add_non_existing_course_with_notes_and_y(self): self.st.courses = {} new_notes = self.st.enroll("Pyp", ["add those notes"], "Y") self.assertEqual(new_notes, "Course and course notes have been added.") def test_add_notes_to_existing_course(self): self.st.courses = {"Python": ["old notes"]} new_notes = self.st.enroll("Python", ["new notes"], "") self.assertEqual(self.st.courses, {"Python": ["new notes"]}) self.assertEqual(new_notes, "Course already added. Notes have been updated.") def test_add_notes_to_existing_course_with_y(self): self.st.courses = {"Python": ["old notes"]} new_notes = self.st.enroll("Python", ["new notes"], "Y") self.assertEqual(self.st.courses, {"Python": ["new notes"]}) self.assertEqual(new_notes, "Course already added. Notes have been updated.") def test_add_new_course(self): self.st.courses = {} new = self.st.enroll("Python", ["notes"], "Python") self.assertEqual(self.st.courses, {'Python': []}) self.assertEqual(new, "Course has been added.") def test_add_new_notes(self): self.st.courses = {'Python': []} new = self.st.add_notes("Python", "notes") self.assertEqual(self.st.courses, {'Python': ["notes"]}) self.assertEqual(new, "Notes have been updated") def test_add_notes_to_non_existing_course(self): self.st.courses = {"Python": ["no notes"]} with self.assertRaises(Exception) as ex: self.st.add_notes("Math", "adding this note") self.assertEqual("Cannot add notes. Course not found.", str(ex.exception)) def test_leave_existing_course(self): self.st.courses = {"Python": "no notes"} leave = self.st.leave_course("Python") self.assertEqual(self.st.courses, {}) self.assertEqual(leave, "Course has been removed") def test_leave_non_existing_course(self): self.st.courses = {"Python": "no notes"} with self.assertRaises(Exception) as ex: self.st.leave_course("Math") self.assertEqual("Cannot remove course. Course not found.", str(ex.exception))
class StudentTests(unittest.TestCase): def setUp(self): name = "Peter" courses = None self.student = Student(name, courses) def test_init(self): self.assertEqual(self.student.name, "Peter") self.assertEqual(self.student.courses, {}) def test_enroll_update_notes_when_course_existing(self): self.student.courses = {"MathForDevs": []} enroll_course = self.student.enroll( "MathForDevs", ["Basic Algebra", "Linear Algebra", "Vectors"], "Math") self.assertEqual(enroll_course, "Course already added. Notes have been updated.") def test_enroll_add_course_and_notes_when_course_note_is_Y(self): enroll_course = self.student.enroll( "DataScience", ["Statistics", "Combinatorics", "Hypothesis"], "Y") self.assertEqual(enroll_course, "Course and course notes have been added.") def test_enroll_add_course_and_notes_when_course_note_is_EmptyString(self): enroll_course = self.student.enroll( "ML", ["Sentiment Analysis", "Abnormalities Detection"], "") self.assertEqual(enroll_course, "Course and course notes have been added.") def test_enroll_add_new_course(self): enroll_course = self.student.enroll( "AI", ["TensorFlow.", "PyTorch", "Scikit Learn"], "AI") self.assertEqual(self.student.courses, {"AI": []}) self.assertEqual(enroll_course, "Course has been added.") def test_add_notes_when_course_existing(self): self.student.courses = {"MathForDevs": []} notes_for_add = self.student.add_notes( "MathForDevs", ["Calculus", "Statistics", "High School Math"]) self.assertEqual(notes_for_add, "Notes have been updated") def test_add_notes_when_course_not_existing(self): with self.assertRaises(Exception) as context: self.student.add_notes( "MathForDevs", ["Calculus", "Statistics", "High School Mat"]) expected_msg = str(context.exception) actual_msg = "Cannot add notes. Course not found." self.assertEqual(expected_msg, actual_msg) def test_leave_courses_when_course_existing(self): self.student.courses = {"MathForDevs": []} course_for_leaving = self.student.leave_course("MathForDevs") self.assertEqual(course_for_leaving, "Course has been removed") def test_leave_courses_when_course_not_existing(self): with self.assertRaises(Exception) as context: self.student.leave_course("MathForDevs") expected_msg = str(context.exception) actual_msg = "Cannot remove course. Course not found." self.assertEqual(expected_msg, actual_msg)
class TestStudent(unittest.TestCase): def setUp(self): self.student = Student('Vladi', courses={'Python': ['Note']}) def test_student_init_method_when_no_courses(self): student = Student('Vladi') self.assertEqual(student.courses, {}) self.assertEqual(student.name, 'Vladi') def test_student_init_method_when_courses_specified(self): courses = {'Python': ['Note']} self.assertEqual(self.student.courses, courses) self.assertEqual(self.student.name, 'Vladi') def test_student_enroll_method_when_course_already_enrolled(self): course_name = 'Python' notes = ['Note1', 'Note2'] student = Student('Vladi', courses={'Python': []}) result = student.enroll(course_name, notes=notes) expected_result = 'Course already added. Notes have been updated.' self.assertEqual(result, expected_result) def test_student_enroll_method_when_course_add_course_notes(self): student = Student('Vladi', courses={'Python': []}) course_name = 'C++' notes = ['Note'] add_course_notes = 'Y' result = student.enroll(course_name, notes=notes, add_course_notes=add_course_notes) expected_result = 'Course and course notes have been added.' self.assertEqual(result, expected_result) def test_student_enroll_method_when_course_not_enrolled_and_not_add_courses_argument(self): course_name = 'C++' student = Student('Vladi') notes = ['Note'] result = student.enroll(course_name, notes=notes, add_course_notes='N') expected_result = 'Course has been added.' self.assertEqual(result, expected_result) def test_student_add_notes_methods_when_course_in_list(self): result = self.student.add_notes('Python', 'Note2') expected_result = 'Notes have been updated' self.assertEqual(result, expected_result) def test_student_add_notes_method_when_course_not_in_list(self): with self.assertRaises(Exception) as ex: self.student.add_notes('C++', 'Note') self.assertIsNotNone(ex.exception) def test_student_leave_course_method_when_course_enrolled(self): result = self.student.leave_course('Python') expected_result = 'Course has been removed' self.assertEqual(result, expected_result) def test_student_leave_course_method_when_course_not_enrolled(self): with self.assertRaises(Exception) as ex: self.student.leave_course('C++') self.assertIsNotNone(ex.exception)
class StudentTests(unittest.TestCase): def setUp(self): courses = { "math": ["math notes", "more math"], "bio": ["bio notes", "more bio"] } self.student = Student("Anna", courses) def test_student_init__when_courses_are_none_expect_courses_to_be_empty_dict( self): student = Student("Anna") self.assertEqual("Anna", student.name) self.assertEqual({}, student.courses) def test_student_init__when_courses_are_not_none_expect_courses_to_be_filled( self): expected_courses = { "math": ["math notes", "more math"], "bio": ["bio notes", "more bio"] } self.assertEqual("Anna", self.student.name) self.assertEqual(expected_courses, self.student.courses) def test_student_enroll__when_course_already_in_courses_expect__msg_and_updated_notes( self): expected_msg = "Course already added. Notes have been updated." actual_msg = self.student.enroll("math", ["more", "and more"]) self.assertEqual(expected_msg, actual_msg) expected_courses = { "math": ["math notes", "more math", "more", "and more"], "bio": ["bio notes", "more bio"] } actual_courses = self.student.courses self.assertEqual(expected_courses, actual_courses) def test_student_enroll__when_course_not_in_courses_and_add_notes_is_y_expect_msg_and_added_course_with_notes( self): expected_msg = "Course and course notes have been added." actual_msg = self.student.enroll("chem", ["chem notes", "more chem"], "Y") self.assertEqual(expected_msg, actual_msg) expected_courses = { "math": ["math notes", "more math"], "bio": ["bio notes", "more bio"], "chem": ["chem notes", "more chem"] } actual_courses = self.student.courses self.assertEqual(expected_courses, actual_courses) def test_student_enroll__when_course_not_in_courses_and_add_notes_is_empty_expect_msg_and_added_course_with_notes( self): expected_msg = "Course and course notes have been added." actual_msg = self.student.enroll("chem", ["chem notes", "more chem"]) self.assertEqual(expected_msg, actual_msg) expected_courses = { "math": ["math notes", "more math"], "bio": ["bio notes", "more bio"], "chem": ["chem notes", "more chem"] } actual_courses = self.student.courses self.assertEqual(expected_courses, actual_courses) def test_student_enroll__when_course_not_in_courses_and_add_notes_is_not_y_or_empty_expect_msg_and_added_course_without_notes( self): expected_msg = "Course has been added." actual_msg = self.student.enroll("chem", ["chem notes", "more chem"], "N") self.assertEqual(expected_msg, actual_msg) expected_courses = { "math": ["math notes", "more math"], "bio": ["bio notes", "more bio"], "chem": [] } actual_courses = self.student.courses self.assertEqual(expected_courses, actual_courses) def test_student_add_notes__when_course_already_in_courses_expect__msg_and_updated_notes( self): expected_msg = "Notes have been updated" actual_msg = self.student.add_notes("math", "more") self.assertEqual(expected_msg, actual_msg) expected_courses = { "math": ["math notes", "more math", "more"], "bio": ["bio notes", "more bio"] } actual_courses = self.student.courses self.assertEqual(expected_courses, actual_courses) def test_student_add_notes__when_course_not_in_courses_expect_exception( self): with self.assertRaises(Exception) as ex: self.student.add_notes("chem", "notes") expected_msg = "Cannot add notes. Course not found." actual_msg = str(ex.exception) self.assertEqual(expected_msg, actual_msg) def test_student_leave_course__when_course_already_in_courses_expect__msg_and_remove_course_from_courses( self): expected_msg = "Course has been removed" actual_msg = self.student.leave_course("math") self.assertEqual(expected_msg, actual_msg) expected_courses = {"bio": ["bio notes", "more bio"]} actual_courses = self.student.courses self.assertEqual(expected_courses, actual_courses) def test_student_leave_course__when_course_not_in_courses_expect_exception( self): with self.assertRaises(Exception) as ex: self.student.leave_course("chem") expected_msg = "Cannot remove course. Course not found." actual_msg = str(ex.exception) self.assertEqual(expected_msg, actual_msg)