def test_select_classlist(self, monkeypatch): test_class_options = { 1: ClassIdentifier('one', 'one'), 2: ClassIdentifier('two', 'two'), 3: ClassIdentifier('three', 'three') } selected_class = ClassIdentifier('some_class', 'some_class') def mocked_create_class_list_dict(): return test_class_options def mocked_display_class_selection_menu(class_options): if class_options != test_class_options: raise ValueError # Ensure called with correct arg. return None def mocked_take_class_selection(class_options): if class_options != test_class_options: raise ValueError # Ensure called with correct arg. return selected_class monkeypatch.setattr(class_functions, 'create_class_list_dict', mocked_create_class_list_dict) monkeypatch.setattr(class_functions, 'display_class_selection_menu', mocked_display_class_selection_menu) monkeypatch.setattr(class_functions, 'take_class_selection', mocked_take_class_selection) assert select_classlist() == selected_class.id
def get_classes(self) -> List[ClassIdentifier]: """ Return list of ClassIdentifiers for classes in the database. :return: List[ClassIdentifier] """ with self.session_scope() as session: return [ ClassIdentifier(*class_data) for class_data in session.query(self.Class.id, self.Class.name) ]
def get_classes(self) -> List[ClassIdentifier]: """ Return list of available classes in the database. List of ClassIdentifiers with form ClassIdentifier(id=class_name, name=class_name), since class_id in JSONDatabase is the class' name. :return: List[Tuple[str, str]] """ return [ClassIdentifier(class_name, class_name) for class_name in self._registry.list]
def get_classes(self) -> List[ClassIdentifier]: """ Return list of ClassIdentifiers for classes in the database. :return: List[ClassIdentifier] """ with self._connection() as conn: # get list of tuple/list pairs of id, name classes = conn.cursor().execute(""" SELECT id, name FROM class; """).fetchall() # convert to ClassIdentifiers return [ClassIdentifier(*class_data) for class_data in classes]
"""Test data sets for test_class_functions.py""" from dionysus_app.persistence.database import ClassIdentifier # Registry testing_registry_list = ['First class', 'Second class', 'Third class'] testing_registry_enumerated_dict = { 1: ClassIdentifier('First class', 'First class'), 2: ClassIdentifier('Second class', 'Second class'), 3: ClassIdentifier('Third class', 'Third class') } testing_registry_data_set = { 'registry_classlist': testing_registry_list, 'enumerated_dict': testing_registry_enumerated_dict } # new data storage formats # Add attributes to test expected output. test_class_name_only_name = "The_Knights_of_the_Round-table__we_don_t_say__Ni__" # Essentially clean_for_filename("The Knights of the Round-table: we don't say 'Ni!'") test_class_name_only_json_str_rep = ( '{\n' + f' "name": "{test_class_name_only_name}",\n' + f' "students": {[]},\n' + f' "id": "{test_class_name_only_name}"\n' + '}') test_class_name_only_json_dict_rep = { 'name': test_class_name_only_name, 'students': [], 'id': test_class_name_only_name }
class TestGetClasses: @pytest.mark.parametrize('registry_list, returned_value', [ ([], []), (['one class'], [ClassIdentifier(id='one class', name='one class')]), (['one class', 'another'], [ ClassIdentifier(id='one class', name='one class'), ClassIdentifier(id='another', name='another') ]), (['one class', 'another', 'so many'], [ ClassIdentifier(id='one class', name='one class'), ClassIdentifier(id='another', name='another'), ClassIdentifier(id='so many', name='so many') ]), pytest.param( ['tricky'], ['tricky'], marks=pytest.mark.xfail(reason='Wrong format return value.')), pytest.param( ['one class', 'another', 'so many'], [ ClassIdentifier(id='one class', name='one class'), ClassIdentifier(id='another', name='another'), ClassIdentifier(id='wrong', name='wrong') ], marks=pytest.mark.xfail(reason='Inconsistent class list.')), pytest.param(['one class', 'another', 'so many'], [ ClassIdentifier(id='one class', name='one class'), ClassIdentifier(id='another', name='another'), ClassIdentifier(id='so many', name='so many'), ClassIdentifier(id='wrong', name='wrong') ], marks=pytest.mark.xfail( reason='Inconsistent class list - extra class.')), pytest.param(['one class', 'another', 'so many'], [ ClassIdentifier(id='one class', name='one class'), ClassIdentifier(id='another', name='another') ], marks=pytest.mark.xfail( reason='Inconsistent class list - missing class.')), ]) def test_get_classes(self, empty_json_database, registry_list, returned_value): test_json_database = empty_json_database empty_json_database._registry.list = registry_list assert test_json_database.get_classes() == returned_value
class TestGetClasses: @pytest.mark.parametrize('existing_class_names, returned_value', [ ([], []), (['one class'], [ClassIdentifier(id=1, name='one class')]), (['one class', 'another'], [ ClassIdentifier(id=1, name='one class'), ClassIdentifier(id=2, name='another') ]), (['one class', 'another', 'so many'], [ ClassIdentifier(id=1, name='one class'), ClassIdentifier(id=2, name='another'), ClassIdentifier(id=3, name='so many') ]), pytest.param( ['tricky'], ['tricky'], marks=pytest.mark.xfail(reason='Wrong format return value.')), pytest.param(['one class', 'another', 'so many'], [ ClassIdentifier(id=1, name='one class'), ClassIdentifier(id=2, name='another'), ClassIdentifier(id=3, name='wrong') ], marks=pytest.mark.xfail( reason='Inconsistent class list.')), pytest.param(['one class', 'another', 'so many'], [ ClassIdentifier(id=1, name='one class'), ClassIdentifier(id=2, name='another'), ClassIdentifier(id=3, name='so many'), ClassIdentifier(id=4, name='wrong') ], marks=pytest.mark.xfail( reason='Inconsistent class list - extra class.')), pytest.param(['one class', 'another', 'so many'], [ ClassIdentifier(id=1, name='one class'), ClassIdentifier(id=2, name='another') ], marks=pytest.mark.xfail( reason='Inconsistent class list - missing class.')), ]) def test_get_classes(self, empty_sqlite_database, existing_class_names, returned_value): test_sqlite_database = empty_sqlite_database for class_name in existing_class_names: empty_sqlite_database.create_class(NewClass(name=class_name)) assert test_sqlite_database.get_classes() == returned_value