|
1 | 1 | import os
|
2 |
| -import sys |
3 |
| -import pytest |
4 |
| -from django.db import connections |
| 2 | +from pathlib import Path |
| 3 | +from django.conf import settings |
5 | 4 |
|
6 |
| -# Add the parent directory to sys.path |
7 |
| -sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 5 | +ROOT_DIR = Path(__file__).parent.parent |
| 6 | +PG_USER = os.environ.get("PG_USER", "postgres") |
8 | 7 |
|
9 | 8 |
|
10 |
| -@pytest.fixture(scope="session") |
11 |
| -def django_db_setup(django_db_setup, django_db_blocker): |
12 |
| - """ |
13 |
| - Fixture to set up the test database with the required tables. |
14 |
| - This extends the built-in django_db_setup fixture. |
15 |
| - """ |
16 |
| - # Import all test models |
17 |
| - from tests.test_models import ( |
18 |
| - MockObject, |
19 |
| - MockFKObject, |
20 |
| - MockBlankObject, |
21 |
| - ExtendedMockObject, |
22 |
| - LimitedMockObject, |
23 |
| - OverloadMockObject, |
24 |
| - SecondaryMockObject, |
25 |
| - UniqueMockObject, |
| 9 | +def pytest_configure(): |
| 10 | + settings.configure( |
| 11 | + DATABASES={ |
| 12 | + "default": { |
| 13 | + "HOST": "localhost", |
| 14 | + "PORT": 5432, |
| 15 | + "NAME": "test", |
| 16 | + "USER": PG_USER, |
| 17 | + "ENGINE": "django.db.backends.postgresql_psycopg2", |
| 18 | + }, |
| 19 | + "other": { |
| 20 | + "HOST": "localhost", |
| 21 | + "PORT": 5432, |
| 22 | + "NAME": "test_alternative", |
| 23 | + "USER": PG_USER, |
| 24 | + "ENGINE": "django.db.backends.postgresql_psycopg2", |
| 25 | + }, |
| 26 | + "sqlite": {"NAME": "sqlite", "ENGINE": "django.db.backends.sqlite3"}, |
| 27 | + "secondary": { |
| 28 | + "HOST": "localhost", |
| 29 | + "PORT": 5432, |
| 30 | + "NAME": "test_secondary", |
| 31 | + "USER": PG_USER, |
| 32 | + "ENGINE": "django.db.backends.postgresql_psycopg2", |
| 33 | + }, |
| 34 | + }, |
| 35 | + INSTALLED_APPS=("tests",), |
| 36 | + DATABASE_ROUTERS=["tests.router.CustomRouter"], |
| 37 | + DEFAULT_AUTO_FIELD="django.db.models.BigAutoField", |
| 38 | + LOGGING={ |
| 39 | + "version": 1, |
| 40 | + "disable_existing_loggers": False, |
| 41 | + "handlers": { |
| 42 | + "file": { |
| 43 | + "level": "DEBUG", |
| 44 | + "class": "logging.FileHandler", |
| 45 | + "filename": ROOT_DIR / "tests.log", |
| 46 | + }, |
| 47 | + }, |
| 48 | + "formatters": { |
| 49 | + "verbose": { |
| 50 | + "format": "%(levelname)s|%(asctime)s|%(module)s|%(message)s", |
| 51 | + "datefmt": "%d/%b/%Y %H:%M:%S", |
| 52 | + } |
| 53 | + }, |
| 54 | + "loggers": { |
| 55 | + "postgres_copy": { |
| 56 | + "handlers": ["file"], |
| 57 | + "level": "DEBUG", |
| 58 | + "propagate": True, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
26 | 62 | )
|
27 |
| - |
28 |
| - with django_db_blocker.unblock(): |
29 |
| - # Check if tables exist and create them if they don't |
30 |
| - for alias in connections: |
31 |
| - connection = connections[alias] |
32 |
| - |
33 |
| - # Get a list of all tables in the database |
34 |
| - with connection.cursor() as cursor: |
35 |
| - if connection.vendor == "postgresql": |
36 |
| - cursor.execute( |
37 |
| - "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" |
38 |
| - ) |
39 |
| - existing_tables = {row[0] for row in cursor.fetchall()} |
40 |
| - elif connection.vendor == "sqlite": |
41 |
| - cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") |
42 |
| - existing_tables = {row[0] for row in cursor.fetchall()} |
43 |
| - else: |
44 |
| - existing_tables = set() |
45 |
| - |
46 |
| - # Create tables that don't exist |
47 |
| - with connection.schema_editor() as schema_editor: |
48 |
| - for model in [ |
49 |
| - MockObject, |
50 |
| - MockFKObject, |
51 |
| - MockBlankObject, |
52 |
| - ExtendedMockObject, |
53 |
| - LimitedMockObject, |
54 |
| - OverloadMockObject, |
55 |
| - SecondaryMockObject, |
56 |
| - UniqueMockObject, |
57 |
| - ]: |
58 |
| - table_name = model._meta.db_table |
59 |
| - if table_name not in existing_tables: |
60 |
| - try: |
61 |
| - schema_editor.create_model(model) |
62 |
| - print(f"Created table {table_name} in {alias}") |
63 |
| - except Exception as e: |
64 |
| - print(f"Error creating {table_name} in {alias}: {e}") |
0 commit comments