-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prove multi-db tests don't restrict access
- Loading branch information
0 parents
commit 755aa3d
Showing
5 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |