Skip to content

Commit

Permalink
Run tests against read-only 'dashboard' connection, refs #18
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 14, 2021
1 parent 87fc304 commit 88aae40
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
10 changes: 10 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


@pytest.fixture(scope="session")
def django_db_modify_db_settings():
from django.conf import settings

settings.DATABASES["dashboard"]["OPTIONS"] = {
"options": "-c default_transaction_read_only=on -c statement_timeout=100"
}
5 changes: 4 additions & 1 deletion django_sql_dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import time

from django.contrib.auth.decorators import permission_required
from django.db import connection, transaction
from django.db import connections, transaction
from django.shortcuts import get_object_or_404, render
from django.conf import settings

from .models import Dashboard

Expand All @@ -18,6 +19,8 @@ def _dashboard_index(
request, sql_queries, title=None, description=None, saved_dashboard=False
):
query_results = []
alias = getattr(settings, "DASHBOARD_DB_ALIAS", "dashboard")
connection = connections[alias]
with connection.cursor() as tables_cursor:
tables_cursor.execute(
"""
Expand Down
1 change: 1 addition & 0 deletions pytest_use_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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())
settings.DATABASES["dashboard"] = parse(_POSTGRESQL.url())


def pytest_unconfigure(config):
Expand Down
4 changes: 3 additions & 1 deletion test_project/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
WSGI_APPLICATION = "config.wsgi.application"


DATABASES = {"default": dj_database_url.config()}
DATABASES = {
"default": dj_database_url.config(),
}

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
Expand Down

0 comments on commit 88aae40

Please sign in to comment.