pytest-neon 2.0.0__tar.gz → 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/PKG-INFO +18 -8
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/README.md +17 -7
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/pyproject.toml +1 -1
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/src/pytest_neon/__init__.py +1 -1
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/src/pytest_neon/plugin.py +26 -3
- pytest_neon-2.1.0/tests/test_xdist_worker_support.py +189 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/uv.lock +1 -1
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/.env.example +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/.github/workflows/release.yml +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/.github/workflows/tests.yml +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/.gitignore +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/.neon +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/CLAUDE.md +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/LICENSE +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/src/pytest_neon/py.typed +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/conftest.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_branch_lifecycle.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_cli_options.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_env_var.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_fixture_errors.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_integration.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_migrations.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_readwrite_readonly_fixtures.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_reset_behavior.py +0 -0
- {pytest_neon-2.0.0 → pytest_neon-2.1.0}/tests/test_skip_behavior.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytest-neon
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.1.0
|
|
4
4
|
Summary: Pytest plugin for Neon database branch isolation in tests
|
|
5
5
|
Project-URL: Homepage, https://github.com/ZainRizvi/pytest-neon
|
|
6
6
|
Project-URL: Repository, https://github.com/ZainRizvi/pytest-neon
|
|
@@ -489,16 +489,26 @@ The `neon_branch_readwrite` fixture uses Neon's branch restore API to reset data
|
|
|
489
489
|
|
|
490
490
|
This is similar to database transactions but at the branch level.
|
|
491
491
|
|
|
492
|
-
##
|
|
492
|
+
## Parallel Test Execution (pytest-xdist)
|
|
493
493
|
|
|
494
|
-
|
|
494
|
+
This plugin supports parallel test execution with [pytest-xdist](https://pytest-xdist.readthedocs.io/). Each xdist worker automatically gets its own isolated branch.
|
|
495
495
|
|
|
496
|
-
|
|
496
|
+
```bash
|
|
497
|
+
# Run tests in parallel with 4 workers
|
|
498
|
+
pip install pytest-xdist
|
|
499
|
+
pytest -n 4
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
**How it works:**
|
|
503
|
+
- Each xdist worker (gw0, gw1, gw2, etc.) creates its own branch
|
|
504
|
+
- Branches are named with the worker ID suffix (e.g., `-test-gw0`, `-test-gw1`)
|
|
505
|
+
- Workers run tests in parallel without database state interference
|
|
506
|
+
- All branches are cleaned up after the test session
|
|
497
507
|
|
|
498
|
-
|
|
499
|
-
-
|
|
500
|
-
-
|
|
501
|
-
-
|
|
508
|
+
**Cost implications:**
|
|
509
|
+
- Running with `-n 4` creates 4 branches (one per worker) plus the migration branch
|
|
510
|
+
- Choose your parallelism level based on your Neon plan's branch limits
|
|
511
|
+
- Each worker's branch is reset after each test using the fast reset operation (~0.5s)
|
|
502
512
|
|
|
503
513
|
## Troubleshooting
|
|
504
514
|
|
|
@@ -444,16 +444,26 @@ The `neon_branch_readwrite` fixture uses Neon's branch restore API to reset data
|
|
|
444
444
|
|
|
445
445
|
This is similar to database transactions but at the branch level.
|
|
446
446
|
|
|
447
|
-
##
|
|
447
|
+
## Parallel Test Execution (pytest-xdist)
|
|
448
448
|
|
|
449
|
-
|
|
449
|
+
This plugin supports parallel test execution with [pytest-xdist](https://pytest-xdist.readthedocs.io/). Each xdist worker automatically gets its own isolated branch.
|
|
450
450
|
|
|
451
|
-
|
|
451
|
+
```bash
|
|
452
|
+
# Run tests in parallel with 4 workers
|
|
453
|
+
pip install pytest-xdist
|
|
454
|
+
pytest -n 4
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
**How it works:**
|
|
458
|
+
- Each xdist worker (gw0, gw1, gw2, etc.) creates its own branch
|
|
459
|
+
- Branches are named with the worker ID suffix (e.g., `-test-gw0`, `-test-gw1`)
|
|
460
|
+
- Workers run tests in parallel without database state interference
|
|
461
|
+
- All branches are cleaned up after the test session
|
|
452
462
|
|
|
453
|
-
|
|
454
|
-
-
|
|
455
|
-
-
|
|
456
|
-
-
|
|
463
|
+
**Cost implications:**
|
|
464
|
+
- Running with `-n 4` creates 4 branches (one per worker) plus the migration branch
|
|
465
|
+
- Choose your parallelism level based on your Neon plan's branch limits
|
|
466
|
+
- Each worker's branch is reset after each test using the fast reset operation (~0.5s)
|
|
457
467
|
|
|
458
468
|
## Troubleshooting
|
|
459
469
|
|
|
@@ -56,6 +56,17 @@ DEFAULT_BRANCH_EXPIRY_SECONDS = 600
|
|
|
56
56
|
_MIGRATIONS_NOT_DEFINED = object()
|
|
57
57
|
|
|
58
58
|
|
|
59
|
+
def _get_xdist_worker_id() -> str:
|
|
60
|
+
"""
|
|
61
|
+
Get the pytest-xdist worker ID, or "main" if not running under xdist.
|
|
62
|
+
|
|
63
|
+
When running tests in parallel with pytest-xdist, each worker process
|
|
64
|
+
gets a unique ID (gw0, gw1, gw2, etc.). This is used to create separate
|
|
65
|
+
branches per worker to avoid database state pollution between parallel tests.
|
|
66
|
+
"""
|
|
67
|
+
return os.environ.get("PYTEST_XDIST_WORKER", "main")
|
|
68
|
+
|
|
69
|
+
|
|
59
70
|
def _get_schema_fingerprint(connection_string: str) -> tuple[tuple[Any, ...], ...]:
|
|
60
71
|
"""
|
|
61
72
|
Get a fingerprint of the database schema for change detection.
|
|
@@ -527,6 +538,12 @@ def _neon_branch_for_reset(
|
|
|
527
538
|
session, avoiding issues with Python's module caching (e.g., SQLAlchemy
|
|
528
539
|
engines created at import time would otherwise point to stale branches).
|
|
529
540
|
|
|
541
|
+
Parallel Test Support (pytest-xdist):
|
|
542
|
+
When running tests in parallel with pytest-xdist, each worker gets its
|
|
543
|
+
own branch. This prevents database state pollution between tests running
|
|
544
|
+
concurrently on different workers. The worker ID is included in the
|
|
545
|
+
branch name suffix (e.g., "-test-gw0", "-test-gw1").
|
|
546
|
+
|
|
530
547
|
Smart Migration Detection:
|
|
531
548
|
This fixture implements a cost-optimization strategy:
|
|
532
549
|
|
|
@@ -558,15 +575,21 @@ def _neon_branch_for_reset(
|
|
|
558
575
|
# Assume migrations changed something to be safe
|
|
559
576
|
schema_changed = True
|
|
560
577
|
|
|
578
|
+
# Get worker ID for parallel test support
|
|
579
|
+
# Each xdist worker gets its own branch to avoid state pollution
|
|
580
|
+
worker_id = _get_xdist_worker_id()
|
|
581
|
+
branch_suffix = f"-test-{worker_id}"
|
|
582
|
+
|
|
561
583
|
# Only create a child branch if migrations actually modified the schema
|
|
562
|
-
if
|
|
584
|
+
# OR if we're running under xdist (each worker needs its own branch)
|
|
585
|
+
if schema_changed or worker_id != "main":
|
|
563
586
|
yield from _create_neon_branch(
|
|
564
587
|
request,
|
|
565
588
|
parent_branch_id_override=_neon_migration_branch.branch_id,
|
|
566
|
-
branch_name_suffix=
|
|
589
|
+
branch_name_suffix=branch_suffix,
|
|
567
590
|
)
|
|
568
591
|
else:
|
|
569
|
-
# No schema changes - reuse the migration branch directly
|
|
592
|
+
# No schema changes and not parallel - reuse the migration branch directly
|
|
570
593
|
# This saves creating an unnecessary branch
|
|
571
594
|
yield _neon_migration_branch
|
|
572
595
|
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Tests for pytest-xdist parallel worker support."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TestXdistBranchIsolation:
|
|
5
|
+
"""Test that parallel workers get separate branches."""
|
|
6
|
+
|
|
7
|
+
def test_xdist_worker_creates_branch_even_without_migrations(
|
|
8
|
+
self, pytester, monkeypatch
|
|
9
|
+
):
|
|
10
|
+
"""Even without schema changes, xdist workers should get their own branch."""
|
|
11
|
+
monkeypatch.setenv("PYTEST_XDIST_WORKER", "gw0")
|
|
12
|
+
|
|
13
|
+
pytester.makeconftest(
|
|
14
|
+
"""
|
|
15
|
+
import os
|
|
16
|
+
import pytest
|
|
17
|
+
from pytest_neon.plugin import (
|
|
18
|
+
NeonBranch,
|
|
19
|
+
_get_xdist_worker_id,
|
|
20
|
+
_MIGRATIONS_NOT_DEFINED,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
branch_creation_calls = []
|
|
24
|
+
|
|
25
|
+
@pytest.fixture(scope="session")
|
|
26
|
+
def _neon_migration_branch(request):
|
|
27
|
+
# Store empty fingerprint to simulate no psycopg available
|
|
28
|
+
request.config._neon_pre_migration_fingerprint = ()
|
|
29
|
+
|
|
30
|
+
return NeonBranch(
|
|
31
|
+
branch_id="br-migration-123",
|
|
32
|
+
project_id="proj-mock",
|
|
33
|
+
connection_string="postgresql://mock:mock@migration.neon.tech/mockdb",
|
|
34
|
+
host="migration.neon.tech",
|
|
35
|
+
parent_id="br-parent-000",
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
@pytest.fixture(scope="session")
|
|
39
|
+
def neon_apply_migrations(_neon_migration_branch):
|
|
40
|
+
# Return sentinel to simulate NO migrations defined
|
|
41
|
+
return _MIGRATIONS_NOT_DEFINED
|
|
42
|
+
|
|
43
|
+
@pytest.fixture(scope="session")
|
|
44
|
+
def _neon_branch_for_reset(
|
|
45
|
+
request, _neon_migration_branch, neon_apply_migrations
|
|
46
|
+
):
|
|
47
|
+
# Replicate the real logic
|
|
48
|
+
sentinel = _MIGRATIONS_NOT_DEFINED
|
|
49
|
+
migrations_defined = neon_apply_migrations is not sentinel
|
|
50
|
+
fingerprint_key = "_neon_pre_migration_fingerprint"
|
|
51
|
+
pre_fp = getattr(request.config, fingerprint_key, ())
|
|
52
|
+
schema_changed = False
|
|
53
|
+
|
|
54
|
+
if migrations_defined and pre_fp:
|
|
55
|
+
schema_changed = False # Simplified
|
|
56
|
+
elif migrations_defined and not pre_fp:
|
|
57
|
+
schema_changed = True
|
|
58
|
+
|
|
59
|
+
worker_id = _get_xdist_worker_id()
|
|
60
|
+
suffix = f"-test-{worker_id}"
|
|
61
|
+
|
|
62
|
+
# Key: create branch even without schema changes when xdist
|
|
63
|
+
if schema_changed or worker_id != "main":
|
|
64
|
+
branch_creation_calls.append(f"created-branch{suffix}")
|
|
65
|
+
conn = f"postgresql://mock:mock@t{suffix}.neon.tech/db"
|
|
66
|
+
branch_info = NeonBranch(
|
|
67
|
+
branch_id=f"br-test{suffix}",
|
|
68
|
+
project_id="proj-mock",
|
|
69
|
+
connection_string=conn,
|
|
70
|
+
host=f"t{suffix}.neon.tech",
|
|
71
|
+
parent_id=_neon_migration_branch.branch_id,
|
|
72
|
+
)
|
|
73
|
+
else:
|
|
74
|
+
branch_creation_calls.append("reused-migration-branch")
|
|
75
|
+
branch_info = _neon_migration_branch
|
|
76
|
+
|
|
77
|
+
os.environ["DATABASE_URL"] = branch_info.connection_string
|
|
78
|
+
try:
|
|
79
|
+
yield branch_info
|
|
80
|
+
finally:
|
|
81
|
+
os.environ.pop("DATABASE_URL", None)
|
|
82
|
+
|
|
83
|
+
@pytest.fixture(scope="session", autouse=True)
|
|
84
|
+
def verify_branch_created():
|
|
85
|
+
yield
|
|
86
|
+
# Under xdist, should create a new branch
|
|
87
|
+
assert branch_creation_calls == ["created-branch-test-gw0"]
|
|
88
|
+
"""
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
pytester.makepyfile(
|
|
92
|
+
"""
|
|
93
|
+
def test_xdist_creates_branch(_neon_branch_for_reset):
|
|
94
|
+
# Just trigger the fixture
|
|
95
|
+
assert _neon_branch_for_reset is not None
|
|
96
|
+
"""
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
result = pytester.runpytest("-v")
|
|
100
|
+
result.assert_outcomes(passed=1)
|
|
101
|
+
|
|
102
|
+
def test_non_xdist_reuses_migration_branch_without_migrations(
|
|
103
|
+
self, pytester, monkeypatch
|
|
104
|
+
):
|
|
105
|
+
"""Without xdist and without migrations, should reuse migration branch."""
|
|
106
|
+
monkeypatch.delenv("PYTEST_XDIST_WORKER", raising=False)
|
|
107
|
+
|
|
108
|
+
pytester.makeconftest(
|
|
109
|
+
"""
|
|
110
|
+
import os
|
|
111
|
+
import pytest
|
|
112
|
+
from pytest_neon.plugin import (
|
|
113
|
+
NeonBranch,
|
|
114
|
+
_get_xdist_worker_id,
|
|
115
|
+
_MIGRATIONS_NOT_DEFINED,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
branch_creation_calls = []
|
|
119
|
+
|
|
120
|
+
@pytest.fixture(scope="session")
|
|
121
|
+
def _neon_migration_branch(request):
|
|
122
|
+
request.config._neon_pre_migration_fingerprint = ()
|
|
123
|
+
return NeonBranch(
|
|
124
|
+
branch_id="br-migration-123",
|
|
125
|
+
project_id="proj-mock",
|
|
126
|
+
connection_string="postgresql://mock:mock@migration.neon.tech/mockdb",
|
|
127
|
+
host="migration.neon.tech",
|
|
128
|
+
parent_id="br-parent-000",
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
@pytest.fixture(scope="session")
|
|
132
|
+
def neon_apply_migrations(_neon_migration_branch):
|
|
133
|
+
return _MIGRATIONS_NOT_DEFINED
|
|
134
|
+
|
|
135
|
+
@pytest.fixture(scope="session")
|
|
136
|
+
def _neon_branch_for_reset(
|
|
137
|
+
request, _neon_migration_branch, neon_apply_migrations
|
|
138
|
+
):
|
|
139
|
+
sentinel = _MIGRATIONS_NOT_DEFINED
|
|
140
|
+
migrations_defined = neon_apply_migrations is not sentinel
|
|
141
|
+
fingerprint_key = "_neon_pre_migration_fingerprint"
|
|
142
|
+
pre_fp = getattr(request.config, fingerprint_key, ())
|
|
143
|
+
schema_changed = False
|
|
144
|
+
|
|
145
|
+
if migrations_defined and pre_fp:
|
|
146
|
+
schema_changed = False
|
|
147
|
+
elif migrations_defined and not pre_fp:
|
|
148
|
+
schema_changed = True
|
|
149
|
+
|
|
150
|
+
worker_id = _get_xdist_worker_id()
|
|
151
|
+
suffix = f"-test-{worker_id}"
|
|
152
|
+
|
|
153
|
+
if schema_changed or worker_id != "main":
|
|
154
|
+
branch_creation_calls.append(f"created-branch{suffix}")
|
|
155
|
+
conn = f"postgresql://mock:mock@t{suffix}.neon.tech/db"
|
|
156
|
+
branch_info = NeonBranch(
|
|
157
|
+
branch_id=f"br-test{suffix}",
|
|
158
|
+
project_id="proj-mock",
|
|
159
|
+
connection_string=conn,
|
|
160
|
+
host=f"t{suffix}.neon.tech",
|
|
161
|
+
parent_id=_neon_migration_branch.branch_id,
|
|
162
|
+
)
|
|
163
|
+
else:
|
|
164
|
+
branch_creation_calls.append("reused-migration-branch")
|
|
165
|
+
branch_info = _neon_migration_branch
|
|
166
|
+
|
|
167
|
+
os.environ["DATABASE_URL"] = branch_info.connection_string
|
|
168
|
+
try:
|
|
169
|
+
yield branch_info
|
|
170
|
+
finally:
|
|
171
|
+
os.environ.pop("DATABASE_URL", None)
|
|
172
|
+
|
|
173
|
+
@pytest.fixture(scope="session", autouse=True)
|
|
174
|
+
def verify_branch_reused():
|
|
175
|
+
yield
|
|
176
|
+
# Without xdist, should reuse migration branch
|
|
177
|
+
assert branch_creation_calls == ["reused-migration-branch"]
|
|
178
|
+
"""
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
pytester.makepyfile(
|
|
182
|
+
"""
|
|
183
|
+
def test_no_xdist_reuses_branch(_neon_branch_for_reset):
|
|
184
|
+
assert _neon_branch_for_reset is not None
|
|
185
|
+
"""
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
result = pytester.runpytest("-v")
|
|
189
|
+
result.assert_outcomes(passed=1)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|