Skip to content

Commit 2857773

Browse files
committed
fix conflict in tests
1 parent bcc8a9b commit 2857773

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

examples/testing-patterns/django/conftest.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,19 @@
1616
from py_pglite import PGliteConfig, PGliteManager
1717

1818

19-
@pytest.fixture(scope="session")
20-
def pglite_manager():
21-
"""
22-
🎯 Base PGlite manager fixture
19+
# Removed pglite_manager fixture - let the Django backend handle PGlite lifecycle
20+
# This prevents conflicts with pytest-django's own database setup
2321

24-
Provides fresh PGlite instance for each test.
25-
Used by both Django and pytest-django patterns.
26-
"""
27-
manager = PGliteManager(PGliteConfig())
28-
manager.start()
29-
manager.wait_for_ready()
3022

31-
try:
32-
yield manager
33-
finally:
34-
manager.stop()
23+
@pytest.fixture(autouse=True)
24+
def _enable_db_access(db):
25+
"""Enable Django DB access for every test in this directory.
26+
27+
This autouse fixture ensures all tests can access the database
28+
without needing explicit @pytest.mark.django_db or db fixture.
29+
"""
30+
# Just importing the 'db' fixture is enough to enable database access
31+
pass
3532

3633

3734
@pytest.fixture(autouse=True)
@@ -54,17 +51,13 @@ def setup_django_mail():
5451

5552

5653
@pytest.fixture(scope="function")
57-
def configured_django(pglite_manager):
54+
def configured_django():
5855
"""
5956
🎯 Configured Django environment
6057
6158
Provides Django setup for both Django ORM and pytest-django testing.
62-
This is the main abstraction point for Django + py-pglite.
59+
This works with the PGlite instance managed by the Django backend.
6360
"""
64-
# Get connection details from PGlite
65-
conn_str = pglite_manager.config.get_connection_string()
66-
socket_dir = conn_str.split("host=")[1].split("&")[0].split("#")[0]
67-
6861
# Configure Django if not already configured
6962
if not settings.configured:
7063
settings.configure(
@@ -74,7 +67,7 @@ def configured_django(pglite_manager):
7467
"NAME": "postgres",
7568
"USER": "postgres",
7669
"PASSWORD": "postgres",
77-
"HOST": socket_dir,
70+
"HOST": "", # Let backend handle socket path
7871
"PORT": "",
7972
"OPTIONS": {},
8073
}
@@ -96,11 +89,11 @@ def configured_django(pglite_manager):
9689
if not hasattr(mail, "outbox"):
9790
mail.outbox = []
9891
else:
99-
# Update existing configuration with new PGlite connection
92+
# Ensure we're using the py-pglite backend
10093
settings.DATABASES["default"].update(
10194
{
10295
"ENGINE": "py_pglite.django.backend",
103-
"HOST": socket_dir,
96+
"HOST": "", # Let backend handle socket path
10497
}
10598
)
10699

py_pglite/django/backend/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ def _create_test_db(
5252
# Get or create PGlite manager for this database
5353
manager = self._get_pglite_manager(test_database_name)
5454

55-
# Start PGlite if not already running
55+
# Start PGlite if not already running (avoid killing existing processes)
5656
if not manager.is_running():
5757
manager.start()
5858
# Wait until PGlite is ready to accept connections
5959
manager.wait_for_ready()
60+
else:
61+
# Manager is already running, just ensure it's ready
62+
if verbosity >= 1:
63+
print(f"Using existing PGlite instance for {test_database_name}")
64+
# Brief wait to ensure stability
65+
time.sleep(0.5)
6066

6167
# Update connection settings to use PGlite
6268
self._update_connection_settings(test_database_name, manager)

0 commit comments

Comments
 (0)