From 8d7730b7090d9b02da4513d0d78260ff131d5570 Mon Sep 17 00:00:00 2001 From: Dominik George <dominik.george@teckids.org> Date: Mon, 31 Jan 2022 16:22:08 +0100 Subject: [PATCH] Allow database configuration --- README.rst | 9 ++++----- pytest_django_testing_postgresql.py | 16 +++++++++++++--- setup.py | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index 2c0b8a0..b128664 100644 --- a/README.rst +++ b/README.rst @@ -24,11 +24,10 @@ auto-discovers it). The plugin re-uses the configuration used by `pytest-django`_. -Limitations ------------ - -Right now, this plugin can only reconfigure the `default` database. - +By default, only the ``default`` database is setup. You can modify this +by setting ``PYTEST_SETUP_DATABASES`` to a list of database aliases in +your ``settings`` module. The list can contain strings, or tuples if you +want some aliases to share the same database backend. .. _testing.postgresql: https://pypi.org/project/testing.postgresql/ .. _pytest-django: https://pypi.org/project/pytest-django/ diff --git a/pytest_django_testing_postgresql.py b/pytest_django_testing_postgresql.py index c6ed64e..5f5e0d0 100644 --- a/pytest_django_testing_postgresql.py +++ b/pytest_django_testing_postgresql.py @@ -5,13 +5,23 @@ from django.conf import settings import pytest from testing.postgresql import Postgresql -_POSTGRESQL = Postgresql() +_POSTGRESQLS = [] @pytest.hookimpl(tryfirst=True) def pytest_load_initial_conftests(early_config, parser, args): os.environ['DJANGO_SETTINGS_MODULE'] = early_config.getini('DJANGO_SETTINGS_MODULE') - settings.DATABASES['default'] = parse(_POSTGRESQL.url()) + + db_sets = getattr(settings, "PYTEST_SETUP_DATABASES", [("default",)]) + for db_set in db_sets: + postgresql = Postgresql() + if isinstance(db_set, str): + db_set = [db_set] + for db in db_set: + settings.DATABASES[db] = parse(postgresql.url()) + _POSTGRESQLS.append(postgresql) + def pytest_unconfigure(config): - _POSTGRESQL.stop() + for postgresql in _POSTGRESQLS: + postgresql.stop() diff --git a/setup.py b/setup.py index eb7022a..660f6bd 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ with open(path.join(this_directory, 'README.rst'), encoding='utf-8') as f: setup( name="pytest-django-testing-postgresql", - version='0.1.post0', + version='0.2', description='Use a temporary PostgreSQL database with pytest-django', long_description=long_description, author='Dominik George', @@ -16,7 +16,7 @@ setup( url='https://edugit.org/AlekSIS/libs/pytest-django-testing-postgresql', classifiers=[ - 'Development Status :: 3 - Alpha', + 'Development Status :: 4 - Beta', 'Framework :: Django', 'Framework :: Pytest', 'Intended Audience :: Developers', -- GitLab