diff --git a/conftest.py b/conftest.py index e7d67e4185..41965bae8f 100644 --- a/conftest.py +++ b/conftest.py @@ -16,9 +16,8 @@ from db.deprecated.utils import get_pg_catalog_table from db.deprecated.metadata import get_empty_metadata -from fixtures.utils import create_scoped_fixtures - +@pytest.fixture(scope="session") def engine_cache(request): import logging logger = logging.getLogger(f'engine_cache-{request.scope}') @@ -40,15 +39,8 @@ def _get(db_name): logger.debug('exit') -# defines: -# FUN_engine_cache -# CLA_engine_cache -# MOD_engine_cache -# SES_engine_cache -create_scoped_fixtures(globals(), engine_cache) - - -def create_db(request, SES_engine_cache): +@pytest.fixture(scope="session") +def create_db(request, engine_cache): """ A factory for Postgres mathesar-installed databases. A fixture made of this method tears down created dbs when leaving scope. @@ -56,8 +48,6 @@ def create_db(request, SES_engine_cache): This method is used to create fixtures with different scopes, that's why it's not a fixture itself. """ - engine_cache = SES_engine_cache - import logging logger = logging.getLogger(f'create_db-{request.scope}') logger.debug('enter') @@ -89,14 +79,6 @@ def __create_db(db_name): logger.debug('exit') -# defines: -# FUN_create_db -# CLA_create_db -# MOD_create_db -# SES_create_db -create_scoped_fixtures(globals(), create_db) - - @pytest.fixture(scope="session") def worker_id(worker_id): """ @@ -136,12 +118,11 @@ def uid(get_uid): @pytest.fixture(scope="session", autouse=True) -def test_db_name(worker_id, SES_create_db): +def test_db_name(worker_id, create_db): """ A dynamic, yet non-random, db_name is used so that subsequent runs would automatically clean up test databases that we failed to tear down. """ - create_db = SES_create_db default_test_db_name = "mathesar_db_test" db_name = f"{default_test_db_name}_{worker_id}" create_db(db_name) @@ -149,8 +130,7 @@ def test_db_name(worker_id, SES_create_db): @pytest.fixture(scope="session") -def engine(test_db_name, SES_engine_cache): - engine_cache = SES_engine_cache +def engine(test_db_name, engine_cache): engine = engine_cache(test_db_name) add_custom_types_to_ischema_names(engine) return engine @@ -169,15 +149,13 @@ def engine_with_schema(engine, _test_schema_name, create_db_schema): @pytest.fixture -def create_db_schema(SES_engine_cache): +def create_db_schema(engine_cache): """ Creates a DB schema factory, making sure to track and clean up new instances. Schema setup and teardown is very fast, so we'll only use this fixture with the default "function" scope. """ - engine_cache = SES_engine_cache - import logging logger = logging.getLogger('create_db_schema') logger.debug('enter') diff --git a/fixtures/utils.py b/fixtures/utils.py deleted file mode 100644 index 716c4ce4ef..0000000000 --- a/fixtures/utils.py +++ /dev/null @@ -1,69 +0,0 @@ -import pytest - - -def get_fixture_value(request, fixture_impl_function): - """ - A way to have fixtures whose scope dynamically matches that of the caller. Pytest does not - provide dynamicly-scoped fixtures: this is a workaround for that. - - Scoped variants of the fixture must already have been created using `create_scoped_fixtures`. - """ - scope = request.scope - scoped_fixture_name = _get_scoped_fixture_name(fixture_impl_function, scope) - return request.getfixturevalue(scoped_fixture_name) - - -def create_scoped_fixtures(globals, fixture_impl_function): - """ - Simplifies creating multiple identical, but differently scoped, fixtures. - - For every pytest fixture scope, creates a fixture with that scope, with the body of - `fixture_impl_function`, and the name of `fixture_impl_function` prepended with `FUN_`, `CLA_`, - `MOD_` or `SES_` (depending on resulting scope), and then adds that fixture to the provided - `globals` dict. - - E.g. given a fixture implementation function named `create_db`, this will add 4 fixtures - to the passed `globals` dict: a function-scoped `FUN_create_db`, a class-scoped `CLA_create_db`, - a module-scoped `MOD_create_db`, and a session-scoped `SES_create_db`. - - Since this method is adding dynamically-named stuff to `globals()`, a developer might have a - hard time understanding where a given global member is being defined. To help with that, we - prepend a comment to the `create_scoped_fixtures` call that lists the global variables it is - expected to introduce, like so: - - ``` - # defines: - # FUN_create_dj_db - # CLA_create_dj_db - # MOD_create_dj_db - # SES_create_dj_db - create_scoped_fixtures(globals(), create_dj_db) - ``` - """ - for scope in ('function', 'class', 'module', 'session'): - scoped_fixture_name = _get_scoped_fixture_name(fixture_impl_function, scope) - globals[scoped_fixture_name] = pytest.fixture( - fixture_impl_function, - scope=scope, - name=scoped_fixture_name - ) - - -def _get_scoped_fixture_name(fixture_impl_function, scope): - """ - Produces names like "FUN_some_fixture", "FUN" signifying the "function" scope. - """ - shorthand = { - 'function': 'FUN', - 'class': 'CLA', - 'module': 'MOD', - 'session': 'SES', - }.get(scope) - assert shorthand is not None - fixture_impl_name = _get_function_name(fixture_impl_function) - scoped_fixture_name = shorthand + '_' + fixture_impl_name - return scoped_fixture_name - - -def _get_function_name(f): - return f.__name__ diff --git a/mathesar/tests/conftest.py b/mathesar/tests/conftest.py index 7497f030dc..bdccbb760d 100644 --- a/mathesar/tests/conftest.py +++ b/mathesar/tests/conftest.py @@ -12,8 +12,6 @@ from db import connection from mathesar.models.users import User -from fixtures.utils import create_scoped_fixtures - @pytest.fixture def mocked_responses(): @@ -52,16 +50,17 @@ def enable_db_access_for_all_tests(db): @pytest.fixture(scope="session", autouse=True) -def ignore_all_dbs_except_default(SES_dj_databases): +def ignore_all_dbs_except_default(dj_databases): """ Ignore the default test database: we're creating and tearing down our own databases dynamically. """ entry_name_to_keep = "default" - for entry_name in set(SES_dj_databases.keys()): + for entry_name in set(dj_databases.keys()): if entry_name != entry_name_to_keep: - del SES_dj_databases[entry_name] + del dj_databases[entry_name] +@pytest.fixture(scope="session") def dj_databases(): """ Returns django.conf.settings.DATABASES by reference. During cleanup, restores it to the state @@ -72,14 +71,6 @@ def dj_databases(): settings.DATABASES = dj_databases_deep_copy -# defines: -# FUN_dj_databases -# CLA_dj_databases -# MOD_dj_databases -# SES_dj_databases -create_scoped_fixtures(globals(), dj_databases) - - @pytest.fixture(scope='session') def patents_csv_filepath(): return 'mathesar/tests/data/patents.csv'