Skip to content

Commit 8d91fd3

Browse files
committed
Simplify
1 parent fa0ae5a commit 8d91fd3

File tree

10 files changed

+61
-357
lines changed

10 files changed

+61
-357
lines changed

.github/workflows/continuous-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
uv pip install ${{ matrix.psycopg }} django==${{ matrix.django }}
6969
7070
- name: Test
71-
run: uv run pytest tests
71+
run: uv run pytest tests --reuse-db --nomigrations
7272
env:
7373
PGPASSWORD: postgres
7474

manage.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ docs = [
102102

103103

104104
[tool.pytest.ini_options]
105-
addopts = "-p no:warnings --cov=postgres_copy --cov-branch -cov-report=term-missing:skip-covered --cov-context=test"
105+
addopts = "-p no:warnings --cov=postgres_copy --cov-branch -cov-report=term-missing:skip-covered --cov-context=test --reuse-db --nomigrations"
106106
testpaths = ["tests"]
107107
python_files = "test_*.py"
108-
DJANGO_SETTINGS_MODULE = "tests.settings"
109108

110109
[tool.coverage.run]
111110
source = ["postgres_copy"]

tests/conftest.py

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,62 @@
11
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
54

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")
87

98

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+
},
2662
)
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}")
File renamed without changes.

tests/test_models.py renamed to tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from postgres_copy import CopyManager, CopyMapping
55

6-
from .test_fields import MyIntegerField
6+
from .fields import MyIntegerField
77

88

99
class MockObject(models.Model):
File renamed without changes.

tests/settings.py

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)