Skip to content

Commit 755aa3d

Browse files
committed
Prove multi-db tests don't restrict access
0 parents  commit 755aa3d

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

example_settings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
3+
env = os.environ
4+
5+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
6+
DATABASES = {
7+
"default": {
8+
"ENGINE": "django.db.backends.postgresql_psycopg2",
9+
"NAME": env.get("DB_NAME", default="example"),
10+
"USER": env.get("DB_USER", default=""),
11+
"PASSWORD": env.get("DB_PASSWORD", default=""),
12+
"HOST": env.get("DB_HOST", default=""),
13+
"PORT": env.get("DB_PORT", default=5432),
14+
"TEST": {"NAME": env.get("DB_TEST_NAME", default="example-test")},
15+
},
16+
"secondary": {
17+
"ENGINE": "django.db.backends.postgresql_psycopg2",
18+
"NAME": env.get("DB2_NAME", default="secondary"),
19+
"USER": env.get("DB2_USER", default=""),
20+
"PASSWORD": env.get("DB2_PASSWORD", default=""),
21+
"HOST": env.get("DB2_HOST", default=""),
22+
"PORT": env.get("DB2_PORT", default=5432),
23+
"TEST": {"NAME": env.get("DB2_TEST_NAME", default="example-secondary-test")},
24+
},
25+
}

manage.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_settings")
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == "__main__":
22+
main()

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tool.pytest.ini_options]
2+
DJANGO_SETTINGS_MODULE = "example_settings"
3+
python_files = ["test_example.py"]

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
django==5.0.6
2+
psycopg==3.2.1
3+
pytest-django==4.8.0
4+
pytest==8.2.2
5+
uv==0.2.18

test_example.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
from django.db.transaction import atomic
3+
4+
5+
@pytest.mark.django_db(databases=["default"])
6+
def test_wrong_db():
7+
with pytest.raises(RuntimeError, match="Database access not allowed"):
8+
with atomic(using="secondary"):
9+
pass

0 commit comments

Comments
 (0)