diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 15ea03221b5ce175b0010cd319970fc063677f8c..47178de754150bf1c7e328e7d30be97de22fcd20 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -57,8 +57,10 @@ def app_token_auth(user1: User): @pytest.fixture(scope='session') def auth_dict(users_dict, invalid_user_auth): """ - Returns a dictionary of the form {user_label: (auth_headers, token)}. The key 'invalid' - contains an example of invalid credentials, and the key None contains (None, None). + Return a dict: user label -> (auth headers, token). + + The key 'invalid' contains invalid credentials. + The key None contains (None, None). """ auths = { label: (create_auth_headers(user.user_id), generate_upload_token(user)) diff --git a/tests/conftest.py b/tests/conftest.py index 3bc90ca6d804221d476c5b4c0797d3034685ecae..a0bcc9e2b354b0dc46b080b5e61684a9bf6ab442 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -73,11 +73,21 @@ pytest_plugins = ( def pytest_addoption(parser): help = 'Set this < 1.0 to speed up worker cleanup. May leave tasks running.' parser.addoption('--celery-inspect-timeout', type=float, default=1.0, help=help) - help = 'Only run tests with these fixtures and exclude ones prefixed with "!".' + help = ( + 'Only run tests with these fixtures and exclude ones prefixed with "!".' + 'Does not consider dynamically loaded fixtures (e.g. `request.getfixturevalue`).' + ) parser.addoption('--fixture-filters', nargs='+', help=help) -def pytest_collection_modifyitems(items, config): +def filter_tests_by_fixtures(items, config): + """Filter tests by fixture names based on CLI argument `--fixture-filters`. + + Will include tests that have all the fixtures in `--fixture-filters` + and exclude tests that have any of the fixtures prefixed with '!'. + + Does not consider dynamically loaded fixtures (e.g. `request.getfixturevalue`).""" + fixture_filters = config.getoption('fixture_filters') if not fixture_filters: return @@ -94,10 +104,16 @@ def pytest_collection_modifyitems(items, config): selected_items.append(item) else: deselected_items.append(item) + config.hook.pytest_deselected(items=deselected_items) items[:] = selected_items +def pytest_collection_modifyitems(items, config): + """Manipulate the list of test items (pytest hook).""" + filter_tests_by_fixtures(items, config) + + @pytest.fixture(scope='function') def tmp(): parent_directory = '.volumes' diff --git a/tests/fixtures/group_uploads.py b/tests/fixtures/group_uploads.py index 70e5859fa51f4089e919925fed1d146e291b23d2..9d0e7d3734ab02b5dc3d38497fd8dde4f39e9eb5 100644 --- a/tests/fixtures/group_uploads.py +++ b/tests/fixtures/group_uploads.py @@ -20,6 +20,7 @@ from nomad.utils.exampledata import ExampleData def group_upload_molds( convert_user_labels_to_ids, convert_group_labels_to_ids, user1, user2 ): + """Return a dict: upload label -> upload data (dict).""" default = {'main_author': user1} molds = { 'no_group': {}, @@ -87,12 +88,14 @@ def create_group_uploads_from_molds(group_upload_molds): @pytest.fixture(scope='module') def uploads_get_groups(create_group_uploads_from_molds, elastic_module, groups_module): + """Create and return uploads for testing get uploads with groups.""" labels = ('no_group', 'CGg2', 'RGg2', 'CGg123', 'RGg123', 'RGall') yield from create_group_uploads_from_molds(labels) @pytest.fixture def upload_full_agents(create_group_uploads_from_molds): + """Create and return an upload with all coauthor/reviewer user and groups filled.""" labels = ('full_agents',) yield from create_group_uploads_from_molds(labels) @@ -103,6 +106,7 @@ def uploads_agent_write_access( elastic_module, groups_module, ): + """Create and return uploads for testing agent write access.""" labels = ('C2', 'R2', 'CGg2', 'RGg2', 'RGall', 'full_agents') yield from create_group_uploads_from_molds(labels) @@ -113,6 +117,7 @@ def uploads_search_query_groups( elastic_module, groups_module, ): + """Create and return uploads for testing search query with groups.""" labels = ( 'embargo0', 'embargo3', diff --git a/tests/fixtures/groups.py b/tests/fixtures/groups.py index feb04117d179326df5c7f65325f31ba6addf3aa4..28352e974e9b15f6a00947a383d00f8f76c1be10 100644 --- a/tests/fixtures/groups.py +++ b/tests/fixtures/groups.py @@ -13,7 +13,7 @@ from tests.utils import fake_group_uuid, fake_user_uuid, generate_convert_label @pytest.fixture(scope='session') def group_molds(): - """Returns mapping from group label to field value dictionary.""" + """Return a dict: group label -> group data (dict).""" def old_group(owner, members): group_str = str(owner) + ''.join(str(m) for m in members) @@ -59,6 +59,7 @@ def group_molds(): @pytest.fixture(scope='session') def group_label_id_mapping(group_molds): + """Return a dict: group label -> group id.""" return {label: value.get('group_id') for label, value in group_molds.items()} @@ -92,9 +93,11 @@ def create_user_groups(group_molds): @pytest.fixture(scope='module') def groups_module(mongo_module, create_user_groups): + """Create and return predefined user groups for testing (module scope).""" return create_user_groups() @pytest.fixture def groups_function(mongo_function, create_user_groups): + """Create and return predefined user groups for testing (function scope).""" return create_user_groups() diff --git a/tests/fixtures/users.py b/tests/fixtures/users.py index 3d3742fd7e4723e7092322be9864990a0995da8f..55eac621c8b7b32cd7b354ec28c097776eb1aea7 100644 --- a/tests/fixtures/users.py +++ b/tests/fixtures/users.py @@ -16,6 +16,7 @@ admin_user_id = fake_user_uuid(0) def fake_user(num, first_name, last_name, *, email=None, **kwargs): + """Return a dict with test user data based on the number and name.""" if email is None: email = f'{first_name}.{last_name}@nomad-fairdi.tests.de'.lower() @@ -53,31 +54,37 @@ users = { @pytest.fixture(scope='session') def user_molds(): + """Return a dict: user labels -> user data (dict).""" return {f'user{i}': user for i, user in enumerate(users.values())} @pytest.fixture(scope='session') def user0(): + """Return the admin user object.""" return User(**users[fake_user_uuid(0)]) @pytest.fixture(scope='session') def user1(): + """Return the default user object.""" return User(**users[fake_user_uuid(1)]) @pytest.fixture(scope='session') def user2(): + """Return an alternative user object.""" return User(**users[fake_user_uuid(2)]) @pytest.fixture(scope='session') def users_dict(user_molds): + """Return a dict: user labels -> user objects.""" return {k: User(**v) for k, v in user_molds.items()} @pytest.fixture(scope='session') def user_label_id_mapping(user_molds): + """Return a dict: user labels -> user ids.""" return {label: value.get('user_id') for label, value in user_molds.items()} diff --git a/tests/utils.py b/tests/utils.py index 6a95a2154853794a8d0ba1c0ecf117ad73f7fdeb..6a6a2dcab56e808d5278695d2baebe54fa196809 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -162,13 +162,14 @@ def create_template_upload_file( def fake_user_uuid(handle): + """Return a test user uuid based on the handle.""" uuid = '00000000-0000-0000-0000-' + str(handle).rjust(12, '0') assert len(uuid) == 36 return uuid def fake_group_uuid(handle: Any): - """Returns a test user group uuid based on the handle.""" + """Return a test user group uuid based on the handle.""" uuid = str(handle).rjust(22, 'G') assert len(uuid) == 22 return uuid @@ -194,4 +195,7 @@ def generate_convert_label(mapping): def dict_to_params(d): + """Convert a dict to a list of pytest.param tuples with keys as ids. Return it. + + Can be used to make the parametrize decorator more concise.""" return [pytest.param(*item, id=id) for id, item in d.items()]