simonw / django-sql-dashboard

Django app for building dashboards using raw SQL queries
https://django-sql-dashboard.datasette.io/
Apache License 2.0
437 stars 37 forks source link

Upgrade to pytest-django 4.3.0 #109

Open simonw opened 3 years ago

simonw commented 3 years ago

The tests are currently failing. The difference looks to be pytest-django 4.3.0.

The last tests that passed show this:

platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
django: settings: config.settings (from env)
rootdir: /home/runner/work/django-sql-dashboard/django-sql-dashboard, configfile: pytest.ini
plugins: pythonpath-0.7.3, django-4.2.0

The tests that now fail show:

platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
django: settings: config.settings (from env)
rootdir: /home/runner/work/django-sql-dashboard/django-sql-dashboard, configfile: pytest.ini
plugins: pythonpath-0.7.3, django-4.3.0
simonw commented 3 years ago

Test failures look like this: E AssertionError: Database queries to 'dashboard' are not allowed in this test. Add 'dashboard' to pytest_django.fixtures._django_db_fixture_helper.<locals>.PytestDjangoTestCase.databases to ensure proper test isolation and silence this failure.

https://pytest-django.readthedocs.io/en/latest/changelog.html#v4-3-0-2021-05-15 says:

simonw commented 3 years ago

Their issue about this feature: https://github.com/pytest-dev/pytest-django/issues/924

simonw commented 3 years ago

For the moment I'm going to pin to 4.2.0 - I'll leave this issue open as an "upgrade to 4.3.0" task.

simonw commented 3 years ago

I tried fixing it like this but it didn't work:

diff --git a/conftest.py b/conftest.py
index 4354a71..aaa24e6 100644
--- a/conftest.py
+++ b/conftest.py
@@ -4,11 +4,13 @@ from django.contrib.auth.models import Permission
 from django_sql_dashboard.models import Dashboard

-@pytest.fixture
-def dashboard_db(settings, db):
+@pytest.fixture(autouse=True)
+def django_db_setup(django_db_setup, settings, request, django_db_blocker):
     settings.DATABASES["dashboard"]["OPTIONS"] = {
         "options": "-c default_transaction_read_only=on -c statement_timeout=100"
     }
+    django_db_blocker.unblock()
+    request.addfinalizer(django_db_blocker.restore)