Dispatcharr/dispatcharr/settings_test.py
SergeantPanda 53fa1e42a0 enhancement(tests): introduce isolated backend test settings and improve test documentation
- Added `dispatcharr.settings_test` for isolated testing, automatically creating an empty PostgreSQL test database and ensuring transaction isolation during tests.
- Updated `manage.py` to switch to the test settings when running tests.
- Enhanced the CONTRIBUTING.md file with detailed instructions on running the backend test suite and handling Celery tasks in tests.
- Refactored test cases to use static methods for `worker_id` in `test_process_label.py` and adjusted assertions in `test_user_preferences.py` for better clarity and correctness.
2026-06-23 20:54:12 -05:00

58 lines
2.1 KiB
Python

"""
Django settings for running the backend test suite in isolation.
Always use this module instead of dispatcharr.settings when running tests:
python manage.py test
`manage.py` selects this module automatically for the ``test`` command.
Django creates a separate empty database (``test_<POSTGRES_DB>``) and runs
migrations — your live data under /data/db is not used.
Why not dispatcharr.settings?
- Production/AIO points at the live ``dispatcharr`` database.
- django-db-geventpool breaks TestCase transaction isolation on pooled connections.
SQLite (``TEST_USE_SQLITE=1``) is an optional fallback for machines without
Postgres; production and CI should use the default Postgres test database.
"""
import os
from dispatcharr.settings import * # noqa: F401,F403
# Fast password hashing for tests.
PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
# Do NOT run Celery tasks inline during tests. post_save signals on M3UAccount and
# EPGSource call .delay(); eager mode runs them inside TestCase transactions and
# closes/poisons the DB connection for subsequent queries in the same test.
CELERY_TASK_ALWAYS_EAGER = False
CELERY_TASK_EAGER_PROPAGATES = False
_use_sqlite = os.environ.get("TEST_USE_SQLITE", "").lower() in ("1", "true", "yes")
if _use_sqlite:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
}
else:
# Default: PostgreSQL with Django-managed test_dispatcharr (matches production).
# Uses the standard backend (not geventpool) so TestCase transactions isolate.
_pg_name = os.environ.get("POSTGRES_DB", "dispatcharr")
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": _pg_name,
"USER": os.environ.get("POSTGRES_USER", "dispatch"),
"PASSWORD": os.environ.get("POSTGRES_PASSWORD", "secret"),
"HOST": os.environ.get("POSTGRES_HOST", "localhost"),
"PORT": int(os.environ.get("POSTGRES_PORT", 5432)),
"TEST": {
"NAME": "test_" + _pg_name,
},
}
}