Skip to content

Commit

Permalink
Tests now start their own PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 13, 2021
1 parent 87aaf53 commit bef5de3
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 56 deletions.
7 changes: 7 additions & 0 deletions django_sql_dashboard/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpResponse


@staff_member_required
def dashboard(request):
return HttpResponse("<title>Django SQL Dashboard</title>")
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.isort]
profile = "black"
multi_line_output = 3


1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[pytest]
addopts = -p pytest_use_postgresql
DJANGO_SETTINGS_MODULE = config.settings
site_dirs = test_project/
18 changes: 18 additions & 0 deletions pytest_use_postgresql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import pytest
from dj_database_url import parse
from django.conf import settings
from testing.postgresql import Postgresql

_POSTGRESQL = Postgresql()


@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())


def pytest_unconfigure(config):
_POSTGRESQL.stop()
13 changes: 11 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from setuptools import setup
import os

from setuptools import setup

VERSION = "0.1"


Expand Down Expand Up @@ -28,7 +29,15 @@ def get_long_description():
version=VERSION,
packages=["django_sql_dashboard"],
install_requires=["Django"],
extras_require={"test": ["pytest", "pytest-django", "pytest-pythonpath"]},
extras_require={
"test": [
"pytest",
"pytest-django",
"pytest-pythonpath",
"dj-database-url",
"testing.postgresql",
]
},
tests_require=["django-sql-dashboard[test]"],
python_requires=">=3.6",
)
11 changes: 1 addition & 10 deletions test_project/config/asgi.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
"""
ASGI config for test_project project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

application = get_asgi_application()
40 changes: 8 additions & 32 deletions test_project/config/settings.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
"""
Django settings for test_project project.
Generated by 'django-admin startproject' using Django 3.1.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
import dj_database_url

BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "9k2s1#*8&jv7#9g=ek@+7fdttplhb5ba_hls)w9vx)=!89u&%-"

# SECURITY WARNING: don't run with debug turned on in production!
SECRET_KEY = "local-testing-insecure-secret"
DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["*"]


# Application definition
Expand All @@ -37,6 +21,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_sql_dashboard",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -67,19 +52,10 @@
},
]

WSGI_APPLICATION = "test_project.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
WSGI_APPLICATION = "config.wsgi.application"

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}

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

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
Expand Down
7 changes: 6 additions & 1 deletion test_project/config/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.urls import path
from django.contrib import admin
from django.http import HttpResponse
from django.urls import path

from django_sql_dashboard.views import dashboard

urlpatterns = [
path("dashboard", dashboard),
path("admin/", admin.site.urls),
path("200", lambda request: HttpResponse("Status 200")),
]
11 changes: 1 addition & 10 deletions test_project/config/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
"""
WSGI config for test_project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")

application = get_wsgi_application()
2 changes: 1 addition & 1 deletion test_project/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
10 changes: 10 additions & 0 deletions test_project/test_dashboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def test_staff_users_only(client):
response = client.get("/dashboard")
assert response.status_code == 302
assert response.url == "/admin/login/?next=/dashboard"


def test_staff_users_allowed(admin_client):
response = admin_client.get("/dashboard")
assert response.status_code == 200
assert b"<title>Django SQL Dashboard</title>" in response.content

0 comments on commit bef5de3

Please sign in to comment.