From 081a2c61fd5cb103e7606fddea034aca4b8cfc6c Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Fri, 15 Aug 2025 08:10:06 -0400 Subject: [PATCH 01/14] Figure out whats wrong --- tests/test_database.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/test_database.py b/tests/test_database.py index cb5b54a0..d0e477f3 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -492,3 +492,67 @@ def test_db_access_in_test_module(self, django_pytester: DjangoPytester) -> None 'or the "db" or "transactional_db" fixtures to enable it.' ] ) + + +def test_custom_django_db_setup(django_pytester: DjangoPytester) -> None: + pytest.importorskip("xdist") + pytest.importorskip("psycopg") + + django_pytester.makeconftest( + """ + import pytest + import psycopg + from django.conf import settings as django_settings + + def run_sql(query, fetch=False, db='default'): + conn = psycopg.connect( + user=django_settings.DATABASES[db]['USER'], + password=django_settings.DATABASES[db]['PASSWORD'], + host=django_settings.DATABASES[db]['HOST'], + port=django_settings.DATABASES['default']['PORT'] + ) + (cur := conn.cursor()).execute(query) + response = cur.fetchone() if fetch else None + conn.close() + return response + + @pytest.fixture(scope='session') + def django_db_createdb(request, django_db_createdb) -> bool: + db_name = f'test_{django_settings.DATABASES["default"]["NAME"]}' + if xdist_suffix := getattr(request.config, 'workerinput', {}).get('workerid'): + db_name = f'{db_name}_{xdist_suffix}' + db_exists = (result := run_sql(query=f"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname='{db_name}')", fetch=True)) and result and result[0] + if django_db_createdb or not db_exists: + run_sql('CREATE EXTENSION IF NOT EXISTS vector') + return django_db_createdb or not db_exists + + @pytest.fixture(scope='session') + def django_db_setup(django_db_setup, django_db_blocker, django_db_createdb) -> None: + del django_db_setup + if django_db_createdb: + with django_db_blocker.unblock(): + call_command('flush', '--noinput') + call_command('loaddata', *pathlib.Path().glob('tests/db_fixtures/**/*.yaml')) + """ + ) + + django_pytester.create_test_module( + """ + import pytest + from .app.models import Item + + @pytest.mark.django_db + def test_simple(): + assert Item.objects.count() == 0 + """ + ) + + result = django_pytester.runpytest_subprocess("-vv", "--reuse-db", "-n", "auto") + print(result.stdout.str()) + print(result.stderr.str()) + result.assert_outcomes(passed=1) + + result = django_pytester.runpytest_subprocess("-vv", "--reuse-db", "-n", "auto") + print(result.stdout.str()) + print(result.stderr.str()) + result.assert_outcomes(passed=1) From d1526afdc8685fd5d34d650a48aef64339538fe7 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Fri, 15 Aug 2025 08:12:58 -0400 Subject: [PATCH 02/14] Speed things up --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 5ffeeead..ef43a7f7 100644 --- a/tox.ini +++ b/tox.ini @@ -37,7 +37,7 @@ passenv = PYTEST_ADDOPTS,TERM,TEST_DB_USER,TEST_DB_PASSWORD,TEST_DB_HOST usedevelop = True commands = coverage: coverage erase - {env:PYTESTDJANGO_TEST_RUNNER:pytest} {posargs:tests} + {env:PYTESTDJANGO_TEST_RUNNER:pytest} {posargs:tests} tests/test_database.py::test_custom_django_db_setup -vv -s coverage: coverage combine coverage: coverage report coverage: coverage xml From f3ce321d7e352728a6f7a87340d8da4a163d78e5 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Fri, 15 Aug 2025 08:15:47 -0400 Subject: [PATCH 03/14] . --- tests/test_database.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_database.py b/tests/test_database.py index d0e477f3..9b7c8776 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -508,6 +508,7 @@ def run_sql(query, fetch=False, db='default'): conn = psycopg.connect( user=django_settings.DATABASES[db]['USER'], password=django_settings.DATABASES[db]['PASSWORD'], + database="postgres", host=django_settings.DATABASES[db]['HOST'], port=django_settings.DATABASES['default']['PORT'] ) From 346377ffce9e6cf82dd3bc5c63e8355e1180dd14 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Fri, 15 Aug 2025 08:19:17 -0400 Subject: [PATCH 04/14] . --- tests/test_database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_database.py b/tests/test_database.py index 9b7c8776..f9ddb9eb 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -508,7 +508,7 @@ def run_sql(query, fetch=False, db='default'): conn = psycopg.connect( user=django_settings.DATABASES[db]['USER'], password=django_settings.DATABASES[db]['PASSWORD'], - database="postgres", + dbname="postgres", host=django_settings.DATABASES[db]['HOST'], port=django_settings.DATABASES['default']['PORT'] ) From 46c2a80f417bfcc49abd7ccfd32aaad6bf33fa62 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Fri, 15 Aug 2025 08:22:32 -0400 Subject: [PATCH 05/14] . --- tests/test_database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_database.py b/tests/test_database.py index f9ddb9eb..15fa0b49 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -524,7 +524,7 @@ def django_db_createdb(request, django_db_createdb) -> bool: db_name = f'{db_name}_{xdist_suffix}' db_exists = (result := run_sql(query=f"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname='{db_name}')", fetch=True)) and result and result[0] if django_db_createdb or not db_exists: - run_sql('CREATE EXTENSION IF NOT EXISTS vector') + run_sql('CREATE EXTENSION IF NOT EXISTS postgis') return django_db_createdb or not db_exists @pytest.fixture(scope='session') From de5b18f86fb5f1edafa9d324baf7db244c21b3a2 Mon Sep 17 00:00:00 2001 From: kingbuzzman Date: Fri, 15 Aug 2025 08:25:23 -0400 Subject: [PATCH 06/14] . --- tests/test_database.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_database.py b/tests/test_database.py index 15fa0b49..ccd305f7 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -522,6 +522,7 @@ def django_db_createdb(request, django_db_createdb) -> bool: db_name = f'test_{django_settings.DATABASES["default"]["NAME"]}' if xdist_suffix := getattr(request.config, 'workerinput', {}).get('workerid'): db_name = f'{db_name}_{xdist_suffix}' + print(run_sql(query="SELECT * FROM pg_available_extensions;", fetch=True)) db_exists = (result := run_sql(query=f"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname='{db_name}')", fetch=True)) and result and result[0] if django_db_createdb or not db_exists: run_sql('CREATE EXTENSION IF NOT EXISTS postgis') From e53b46d9a303abf9776307a17e8bd67111cb3ae9 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Fri, 15 Aug 2025 13:49:44 +0100 Subject: [PATCH 07/14] Update test_database.py --- tests/test_database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_database.py b/tests/test_database.py index ccd305f7..7d64ab1a 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -525,7 +525,7 @@ def django_db_createdb(request, django_db_createdb) -> bool: print(run_sql(query="SELECT * FROM pg_available_extensions;", fetch=True)) db_exists = (result := run_sql(query=f"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname='{db_name}')", fetch=True)) and result and result[0] if django_db_createdb or not db_exists: - run_sql('CREATE EXTENSION IF NOT EXISTS postgis') + run_sql('CREATE EXTENSION IF NOT EXISTS hstore') return django_db_createdb or not db_exists @pytest.fixture(scope='session') From 110d73a6633e8e2ee95819c7d5c4adcf71102714 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:31:20 -0400 Subject: [PATCH 08/14] Update main.yml --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b6fb5cb8..19ed8ad6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,6 +49,7 @@ jobs: run: | sudo systemctl start postgresql.service sudo -u postgres createuser --createdb $USER + psql -U postgres -c "ALTER USER $USER WITH SUPERUSER" - name: Install dependencies run: | From 195d5a610ad6e752797624235d4e3ceedc5093d3 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:33:02 -0400 Subject: [PATCH 09/14] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 19ed8ad6..96b55484 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,7 +49,7 @@ jobs: run: | sudo systemctl start postgresql.service sudo -u postgres createuser --createdb $USER - psql -U postgres -c "ALTER USER $USER WITH SUPERUSER" + sudo -u postgres psql -c "ALTER USER $USER WITH SUPERUSER" - name: Install dependencies run: | From f1a0dfe5d87b78204519c148423c24c2fa6aa363 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:34:57 -0400 Subject: [PATCH 10/14] Update test_database.py --- tests/test_database.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_database.py b/tests/test_database.py index 7d64ab1a..d7203894 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -503,6 +503,7 @@ def test_custom_django_db_setup(django_pytester: DjangoPytester) -> None: import pytest import psycopg from django.conf import settings as django_settings + from django.core.management import call_command def run_sql(query, fetch=False, db='default'): conn = psycopg.connect( From 3fe4d63874422a5564f7fb7ad8b88b9d74bcfa17 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:35:22 -0400 Subject: [PATCH 11/14] Update test_database.py --- tests/test_database.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_database.py b/tests/test_database.py index d7203894..0e75bab4 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -535,7 +535,6 @@ def django_db_setup(django_db_setup, django_db_blocker, django_db_createdb) -> N if django_db_createdb: with django_db_blocker.unblock(): call_command('flush', '--noinput') - call_command('loaddata', *pathlib.Path().glob('tests/db_fixtures/**/*.yaml')) """ ) From 029def060e2013ac9ff802f9fc442f867f2a0560 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:43:20 -0400 Subject: [PATCH 12/14] Update test_database.py --- tests/test_database.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_database.py b/tests/test_database.py index 0e75bab4..8c10c0e1 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -549,12 +549,12 @@ def test_simple(): """ ) - result = django_pytester.runpytest_subprocess("-vv", "--reuse-db", "-n", "auto") + result = django_pytester.runpytest_subprocess("-vv", "-s", "--reuse-db", "-n", "auto") print(result.stdout.str()) print(result.stderr.str()) result.assert_outcomes(passed=1) - result = django_pytester.runpytest_subprocess("-vv", "--reuse-db", "-n", "auto") + result = django_pytester.runpytest_subprocess("-vv", "-s", "--reuse-db", "-n", "auto") print(result.stdout.str()) print(result.stderr.str()) result.assert_outcomes(passed=1) From e39319315f524c81a0376b871e58bceeaa94947f Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:46:20 -0400 Subject: [PATCH 13/14] Update test_database.py --- tests/test_database.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_database.py b/tests/test_database.py index 8c10c0e1..718f2abd 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -550,11 +550,9 @@ def test_simple(): ) result = django_pytester.runpytest_subprocess("-vv", "-s", "--reuse-db", "-n", "auto") - print(result.stdout.str()) - print(result.stderr.str()) + assert "Creating test database for alias 'default'" in result.stdout.str() result.assert_outcomes(passed=1) result = django_pytester.runpytest_subprocess("-vv", "-s", "--reuse-db", "-n", "auto") - print(result.stdout.str()) - print(result.stderr.str()) + assert "Got an error creating the test database: database" in result.stdout.str() result.assert_outcomes(passed=1) From 066b5cab9acb0e14cc484ed5d82b1afca39bed17 Mon Sep 17 00:00:00 2001 From: Javier Buzzi Date: Mon, 18 Aug 2025 03:47:16 -0400 Subject: [PATCH 14/14] Update test_database.py --- tests/test_database.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_database.py b/tests/test_database.py index 718f2abd..d68e0482 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -550,9 +550,9 @@ def test_simple(): ) result = django_pytester.runpytest_subprocess("-vv", "-s", "--reuse-db", "-n", "auto") - assert "Creating test database for alias 'default'" in result.stdout.str() + assert "Creating test database for alias 'default'" in result.stderr.str() result.assert_outcomes(passed=1) result = django_pytester.runpytest_subprocess("-vv", "-s", "--reuse-db", "-n", "auto") - assert "Got an error creating the test database: database" in result.stdout.str() + assert "Got an error creating the test database: database" in result.stderr.str() result.assert_outcomes(passed=1)