def test_take_class_data_input(self, monkeypatch): test_class_name = 'my test class' test_take_student_name_input_returns = ['test_student', 'END'] take_student_avatar_return = 'my_student_avatar.jpg' test_class = Class( name=test_class_name, students=[ Student(name=test_take_student_name_input_returns[0], avatar_id=take_student_avatar_return), ]) test_take_student_name_input_return = ( name for name in test_take_student_name_input_returns) def mocked_take_student_name_input(new_class): if new_class.name != test_class_name: raise ValueError return next(test_take_student_name_input_return) def mocked_take_student_avatar(new_class, student_name): if (new_class.name, student_name) != ( test_class_name, test_take_student_name_input_returns[0]): raise ValueError return take_student_avatar_return monkeypatch.setattr(class_functions, 'take_student_avatar', mocked_take_student_avatar) monkeypatch.setattr(class_functions, 'take_student_name_input', mocked_take_student_name_input) assert take_class_data_input( test_class_name).json_dict() == test_class.json_dict()
def test_update_class(self, empty_json_database, test_full_class): test_json_database = empty_json_database test_class = Class(test_full_class.name, test_full_class.students) # create_class takes a NewClass object due to avatar moving machinery. test_class_new_class = NewClass(test_full_class.name, test_full_class.students) assert test_class.json_dict() == test_class_new_class.json_dict( ) # Ensure classes are the same. # Create class in database. test_json_database.create_class( NewClass(test_full_class.name, test_full_class.students)) # Ensure test_class in database test_class.id = test_class.name # Loaded class will have ids: test_loaded_class = test_full_class.json_dict() for student in test_loaded_class['students']: student['id'] = student['name'] assert test_json_database.load_class( test_class.name).json_dict() == test_loaded_class # Change class by adding student, update database: new_student = Student(name='new student') assert new_student not in test_class and new_student.name not in test_class # Confirm student not in class. test_class.add_student(new_student) assert test_json_database.update_class(test_class) is None # Look up name because new_student object itself is not in the loaded class object. assert new_student.name in test_json_database.load_class( test_class.name)
class TestTakeClassDataInput(TestCase): """Unittest used to mock multiple returns from student_name_input.""" def setUp(self) -> None: self.test_class_name = 'my test class' self.test_student_name_input_returns = ['test_student', 'END'] self.take_student_avatar_return = 'my_student_avatar.jpg' self.test_class = Class( name=self.test_class_name, students=[ Student(name=self.test_student_name_input_returns[0], avatar_filename=self.take_student_avatar_return), ]) @patch('dionysus_app.class_functions.take_student_avatar') @patch('dionysus_app.class_functions.take_student_name_input') def test_take_class_data_input(self, mock_take_student_name_input, mock_take_student_avatar): mock_take_student_name_input.side_effect = self.test_student_name_input_returns mock_take_student_avatar.return_value = self.take_student_avatar_return assert take_class_data_input( self.test_class_name).json_dict() == self.test_class.json_dict()