Skip to content

Commit

Permalink
Prove multi-db tests don't restrict access
Browse files Browse the repository at this point in the history
  • Loading branch information
meshy committed Jul 3, 2024
0 parents commit 755aa3d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
25 changes: 25 additions & 0 deletions example_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os

env = os.environ

# Build paths inside the project like this: BASE_DIR / 'subdir'.
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": env.get("DB_NAME", default="example"),
"USER": env.get("DB_USER", default=""),
"PASSWORD": env.get("DB_PASSWORD", default=""),
"HOST": env.get("DB_HOST", default=""),
"PORT": env.get("DB_PORT", default=5432),
"TEST": {"NAME": env.get("DB_TEST_NAME", default="example-test")},
},
"secondary": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": env.get("DB2_NAME", default="secondary"),
"USER": env.get("DB2_USER", default=""),
"PASSWORD": env.get("DB2_PASSWORD", default=""),
"HOST": env.get("DB2_HOST", default=""),
"PORT": env.get("DB2_PORT", default=5432),
"TEST": {"NAME": env.get("DB2_TEST_NAME", default="example-secondary-test")},
},
}
22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "example_settings"
python_files = ["test_example.py"]
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
django==5.0.6
psycopg==3.2.1
pytest-django==4.8.0
pytest==8.2.2
uv==0.2.18
9 changes: 9 additions & 0 deletions test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from django.db.transaction import atomic


@pytest.mark.django_db(databases=["default"])
def test_wrong_db():
with pytest.raises(RuntimeError, match="Database access not allowed"):
with atomic(using="secondary"):
pass

0 comments on commit 755aa3d

Please sign in to comment.