pytest-neon 2.2.0__py3-none-any.whl → 2.2.1__py3-none-any.whl
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/__init__.py +1 -1
- pytest_neon/plugin.py +180 -16
- {pytest_neon-2.2.0.dist-info → pytest_neon-2.2.1.dist-info}/METADATA +2 -1
- pytest_neon-2.2.1.dist-info/RECORD +8 -0
- pytest_neon-2.2.0.dist-info/RECORD +0 -8
- {pytest_neon-2.2.0.dist-info → pytest_neon-2.2.1.dist-info}/WHEEL +0 -0
- {pytest_neon-2.2.0.dist-info → pytest_neon-2.2.1.dist-info}/entry_points.txt +0 -0
- {pytest_neon-2.2.0.dist-info → pytest_neon-2.2.1.dist-info}/licenses/LICENSE +0 -0
pytest_neon/__init__.py
CHANGED
pytest_neon/plugin.py
CHANGED
|
@@ -36,17 +36,19 @@ For full documentation, see: https://github.com/ZainRizvi/pytest-neon
|
|
|
36
36
|
from __future__ import annotations
|
|
37
37
|
|
|
38
38
|
import contextlib
|
|
39
|
+
import json
|
|
39
40
|
import os
|
|
40
41
|
import random
|
|
41
42
|
import time
|
|
42
43
|
import warnings
|
|
43
44
|
from collections.abc import Callable, Generator
|
|
44
|
-
from dataclasses import dataclass
|
|
45
|
+
from dataclasses import asdict, dataclass
|
|
45
46
|
from datetime import datetime, timedelta, timezone
|
|
46
47
|
from typing import Any, TypeVar
|
|
47
48
|
|
|
48
49
|
import pytest
|
|
49
50
|
import requests
|
|
51
|
+
from filelock import FileLock
|
|
50
52
|
from neon_api import NeonAPI
|
|
51
53
|
from neon_api.exceptions import NeonAPIError
|
|
52
54
|
from neon_api.schema import EndpointState
|
|
@@ -806,9 +808,24 @@ def _wait_for_operations(
|
|
|
806
808
|
)
|
|
807
809
|
|
|
808
810
|
|
|
811
|
+
def _branch_to_dict(branch: NeonBranch) -> dict[str, Any]:
|
|
812
|
+
"""Convert NeonBranch to a JSON-serializable dict."""
|
|
813
|
+
return asdict(branch)
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
def _dict_to_branch(data: dict[str, Any]) -> NeonBranch:
|
|
817
|
+
"""Convert a dict back to NeonBranch."""
|
|
818
|
+
return NeonBranch(**data)
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
# Timeout for waiting for migrations to complete (seconds)
|
|
822
|
+
_MIGRATION_WAIT_TIMEOUT = 300 # 5 minutes
|
|
823
|
+
|
|
824
|
+
|
|
809
825
|
@pytest.fixture(scope="session")
|
|
810
826
|
def _neon_migration_branch(
|
|
811
827
|
request: pytest.FixtureRequest,
|
|
828
|
+
tmp_path_factory: pytest.TempPathFactory,
|
|
812
829
|
) -> Generator[NeonBranch, None, None]:
|
|
813
830
|
"""
|
|
814
831
|
Session-scoped branch where migrations are applied.
|
|
@@ -817,6 +834,13 @@ def _neon_migration_branch(
|
|
|
817
834
|
the parent for all test branches. Migrations run once per session
|
|
818
835
|
on this branch.
|
|
819
836
|
|
|
837
|
+
pytest-xdist Support:
|
|
838
|
+
When running with pytest-xdist, the first worker to acquire the lock
|
|
839
|
+
creates the migration branch. Other workers wait for migrations to
|
|
840
|
+
complete, then reuse the same branch. This avoids redundant API calls
|
|
841
|
+
and ensures migrations only run once. Only the creator cleans up the
|
|
842
|
+
branch at session end.
|
|
843
|
+
|
|
820
844
|
Note: The migration branch cannot have an expiry because Neon doesn't
|
|
821
845
|
allow creating child branches from branches with expiration dates.
|
|
822
846
|
Cleanup relies on the fixture teardown at session end.
|
|
@@ -826,25 +850,102 @@ def _neon_migration_branch(
|
|
|
826
850
|
it on request.config. After migrations run, _neon_branch_for_reset
|
|
827
851
|
compares the fingerprint to detect if the schema actually changed.
|
|
828
852
|
"""
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
853
|
+
config = request.config
|
|
854
|
+
worker_id = _get_xdist_worker_id()
|
|
855
|
+
is_xdist = worker_id != "main"
|
|
856
|
+
|
|
857
|
+
# Get env var name for DATABASE_URL
|
|
858
|
+
env_var_name = _get_config_value(
|
|
859
|
+
config, "neon_env_var", "", "neon_env_var", "DATABASE_URL"
|
|
834
860
|
)
|
|
835
|
-
branch = next(branch_gen)
|
|
836
861
|
|
|
837
|
-
#
|
|
838
|
-
#
|
|
839
|
-
|
|
840
|
-
|
|
862
|
+
# For xdist, use shared temp directory and filelock
|
|
863
|
+
# tmp_path_factory.getbasetemp().parent is shared across all workers
|
|
864
|
+
if is_xdist:
|
|
865
|
+
root_tmp_dir = tmp_path_factory.getbasetemp().parent
|
|
866
|
+
cache_file = root_tmp_dir / "neon_migration_branch.json"
|
|
867
|
+
lock_file = root_tmp_dir / "neon_migration_branch.lock"
|
|
868
|
+
else:
|
|
869
|
+
cache_file = None
|
|
870
|
+
lock_file = None
|
|
871
|
+
|
|
872
|
+
is_creator = False
|
|
873
|
+
branch: NeonBranch
|
|
874
|
+
branch_gen: Generator[NeonBranch, None, None] | None = None
|
|
875
|
+
original_env_value: str | None = None
|
|
876
|
+
|
|
877
|
+
if is_xdist:
|
|
878
|
+
assert cache_file is not None and lock_file is not None
|
|
879
|
+
with FileLock(str(lock_file)):
|
|
880
|
+
if cache_file.exists():
|
|
881
|
+
# Another worker already created the branch - reuse it
|
|
882
|
+
data = json.loads(cache_file.read_text())
|
|
883
|
+
branch = _dict_to_branch(data["branch"])
|
|
884
|
+
pre_migration_fingerprint = tuple(
|
|
885
|
+
tuple(row) for row in data["pre_migration_fingerprint"]
|
|
886
|
+
)
|
|
887
|
+
config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
|
|
888
|
+
|
|
889
|
+
# Set DATABASE_URL for this worker (not done by _create_neon_branch)
|
|
890
|
+
original_env_value = os.environ.get(env_var_name)
|
|
891
|
+
os.environ[env_var_name] = branch.connection_string
|
|
892
|
+
else:
|
|
893
|
+
# First worker - create branch and cache it
|
|
894
|
+
is_creator = True
|
|
895
|
+
branch_gen = _create_neon_branch(
|
|
896
|
+
request,
|
|
897
|
+
branch_expiry_override=0,
|
|
898
|
+
branch_name_suffix="-migrated",
|
|
899
|
+
)
|
|
900
|
+
branch = next(branch_gen)
|
|
901
|
+
|
|
902
|
+
# Capture schema fingerprint BEFORE migrations run
|
|
903
|
+
pre_migration_fingerprint = _get_schema_fingerprint(
|
|
904
|
+
branch.connection_string
|
|
905
|
+
)
|
|
906
|
+
config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
|
|
907
|
+
|
|
908
|
+
# Cache for other workers (they'll read this after lock released)
|
|
909
|
+
# Note: We cache now with pre-migration fingerprint. The branch
|
|
910
|
+
# content will have migrations applied by neon_apply_migrations.
|
|
911
|
+
cache_file.write_text(
|
|
912
|
+
json.dumps(
|
|
913
|
+
{
|
|
914
|
+
"branch": _branch_to_dict(branch),
|
|
915
|
+
"pre_migration_fingerprint": pre_migration_fingerprint,
|
|
916
|
+
}
|
|
917
|
+
)
|
|
918
|
+
)
|
|
919
|
+
else:
|
|
920
|
+
# Not using xdist - create branch normally
|
|
921
|
+
is_creator = True
|
|
922
|
+
branch_gen = _create_neon_branch(
|
|
923
|
+
request,
|
|
924
|
+
branch_expiry_override=0,
|
|
925
|
+
branch_name_suffix="-migrated",
|
|
926
|
+
)
|
|
927
|
+
branch = next(branch_gen)
|
|
928
|
+
|
|
929
|
+
# Capture schema fingerprint BEFORE migrations run
|
|
930
|
+
pre_migration_fingerprint = _get_schema_fingerprint(branch.connection_string)
|
|
931
|
+
config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
|
|
932
|
+
|
|
933
|
+
# Mark whether this worker is the creator (used by neon_apply_migrations)
|
|
934
|
+
config._neon_is_migration_creator = is_creator # type: ignore[attr-defined]
|
|
841
935
|
|
|
842
936
|
try:
|
|
843
937
|
yield branch
|
|
844
938
|
finally:
|
|
845
|
-
#
|
|
846
|
-
|
|
847
|
-
|
|
939
|
+
# Restore env var if we set it (non-creator workers)
|
|
940
|
+
if original_env_value is not None:
|
|
941
|
+
os.environ[env_var_name] = original_env_value
|
|
942
|
+
elif not is_creator and env_var_name in os.environ:
|
|
943
|
+
os.environ.pop(env_var_name, None)
|
|
944
|
+
|
|
945
|
+
# Only the creator cleans up the branch
|
|
946
|
+
if is_creator and branch_gen is not None:
|
|
947
|
+
with contextlib.suppress(StopIteration):
|
|
948
|
+
next(branch_gen)
|
|
848
949
|
|
|
849
950
|
|
|
850
951
|
@pytest.fixture(scope="session")
|
|
@@ -855,6 +956,12 @@ def neon_apply_migrations(_neon_migration_branch: NeonBranch) -> Any:
|
|
|
855
956
|
The migration branch is already created and DATABASE_URL is set.
|
|
856
957
|
Migrations run once per test session, before any tests execute.
|
|
857
958
|
|
|
959
|
+
pytest-xdist Support:
|
|
960
|
+
When running with pytest-xdist, migrations only run on the first
|
|
961
|
+
worker (the one that created the migration branch). Other workers
|
|
962
|
+
wait for migrations to complete before proceeding. This ensures
|
|
963
|
+
migrations run exactly once, even with parallel workers.
|
|
964
|
+
|
|
858
965
|
Smart Migration Detection:
|
|
859
966
|
The plugin automatically detects whether migrations actually modified
|
|
860
967
|
the database schema. If no schema changes occurred (or this fixture
|
|
@@ -897,11 +1004,67 @@ def neon_apply_migrations(_neon_migration_branch: NeonBranch) -> Any:
|
|
|
897
1004
|
return _MIGRATIONS_NOT_DEFINED
|
|
898
1005
|
|
|
899
1006
|
|
|
1007
|
+
@pytest.fixture(scope="session")
|
|
1008
|
+
def _neon_migrations_synchronized(
|
|
1009
|
+
request: pytest.FixtureRequest,
|
|
1010
|
+
tmp_path_factory: pytest.TempPathFactory,
|
|
1011
|
+
_neon_migration_branch: NeonBranch,
|
|
1012
|
+
neon_apply_migrations: Any,
|
|
1013
|
+
) -> Any:
|
|
1014
|
+
"""
|
|
1015
|
+
Internal fixture that synchronizes migrations across xdist workers.
|
|
1016
|
+
|
|
1017
|
+
This fixture ensures that:
|
|
1018
|
+
1. Only the creator worker runs migrations
|
|
1019
|
+
2. Other workers wait for migrations to complete before proceeding
|
|
1020
|
+
3. The return value from neon_apply_migrations is preserved for detection
|
|
1021
|
+
|
|
1022
|
+
Without xdist, this is a simple passthrough.
|
|
1023
|
+
"""
|
|
1024
|
+
config = request.config
|
|
1025
|
+
worker_id = _get_xdist_worker_id()
|
|
1026
|
+
is_xdist = worker_id != "main"
|
|
1027
|
+
is_creator = getattr(config, "_neon_is_migration_creator", True)
|
|
1028
|
+
|
|
1029
|
+
if not is_xdist:
|
|
1030
|
+
# Not using xdist - migrations already ran, just return the value
|
|
1031
|
+
return neon_apply_migrations
|
|
1032
|
+
|
|
1033
|
+
# For xdist, use a signal file to coordinate
|
|
1034
|
+
root_tmp_dir = tmp_path_factory.getbasetemp().parent
|
|
1035
|
+
migrations_done_file = root_tmp_dir / "neon_migrations_done"
|
|
1036
|
+
migrations_lock_file = root_tmp_dir / "neon_migrations.lock"
|
|
1037
|
+
|
|
1038
|
+
if is_creator:
|
|
1039
|
+
# Creator: migrations just ran via neon_apply_migrations dependency
|
|
1040
|
+
# Signal completion to other workers
|
|
1041
|
+
with FileLock(str(migrations_lock_file)):
|
|
1042
|
+
migrations_done_file.write_text("done")
|
|
1043
|
+
return neon_apply_migrations
|
|
1044
|
+
else:
|
|
1045
|
+
# Non-creator: wait for migrations to complete
|
|
1046
|
+
# The neon_apply_migrations fixture still runs but on already-migrated DB
|
|
1047
|
+
# (most migration tools handle this gracefully as a no-op)
|
|
1048
|
+
waited = 0.0
|
|
1049
|
+
poll_interval = 0.5
|
|
1050
|
+
while not migrations_done_file.exists():
|
|
1051
|
+
if waited >= _MIGRATION_WAIT_TIMEOUT:
|
|
1052
|
+
raise RuntimeError(
|
|
1053
|
+
f"Timeout waiting for migrations to complete after "
|
|
1054
|
+
f"{_MIGRATION_WAIT_TIMEOUT}s. The creator worker may have "
|
|
1055
|
+
f"failed or is still running migrations."
|
|
1056
|
+
)
|
|
1057
|
+
time.sleep(poll_interval)
|
|
1058
|
+
waited += poll_interval
|
|
1059
|
+
|
|
1060
|
+
return neon_apply_migrations
|
|
1061
|
+
|
|
1062
|
+
|
|
900
1063
|
@pytest.fixture(scope="session")
|
|
901
1064
|
def _neon_branch_for_reset(
|
|
902
1065
|
request: pytest.FixtureRequest,
|
|
903
1066
|
_neon_migration_branch: NeonBranch,
|
|
904
|
-
|
|
1067
|
+
_neon_migrations_synchronized: Any, # Ensures migrations complete; for detection
|
|
905
1068
|
) -> Generator[NeonBranch, None, None]:
|
|
906
1069
|
"""
|
|
907
1070
|
Internal fixture that creates a test branch from the migration branch.
|
|
@@ -931,7 +1094,8 @@ def _neon_branch_for_reset(
|
|
|
931
1094
|
- Migrations exist but are already applied (no schema changes)
|
|
932
1095
|
"""
|
|
933
1096
|
# Check if migrations fixture was overridden
|
|
934
|
-
|
|
1097
|
+
# _neon_migrations_synchronized passes through the neon_apply_migrations value
|
|
1098
|
+
migrations_defined = _neon_migrations_synchronized is not _MIGRATIONS_NOT_DEFINED
|
|
935
1099
|
|
|
936
1100
|
# Check if schema actually changed (if we have a pre-migration fingerprint)
|
|
937
1101
|
pre_fingerprint = getattr(request.config, "_neon_pre_migration_fingerprint", ())
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytest-neon
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.1
|
|
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
|
|
@@ -24,6 +24,7 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
24
24
|
Classifier: Topic :: Database
|
|
25
25
|
Classifier: Topic :: Software Development :: Testing
|
|
26
26
|
Requires-Python: >=3.9
|
|
27
|
+
Requires-Dist: filelock>=3.0
|
|
27
28
|
Requires-Dist: neon-api>=0.1.0
|
|
28
29
|
Requires-Dist: pytest>=7.0
|
|
29
30
|
Requires-Dist: requests>=2.20
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
pytest_neon/__init__.py,sha256=MShSVrrcshqn0Gk9s9zc4iMopsVfqCbDyKYJhOa5QYE,398
|
|
2
|
+
pytest_neon/plugin.py,sha256=kpfRodNlIY_6a11UbvtF-S0KFjaPHPPo-K3kiaEAGsA,55952
|
|
3
|
+
pytest_neon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
pytest_neon-2.2.1.dist-info/METADATA,sha256=NZEC6cCKDZ00UyyAjVQdyawyTjZ1MKL8oboOsw5ao0M,19266
|
|
5
|
+
pytest_neon-2.2.1.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
+
pytest_neon-2.2.1.dist-info/entry_points.txt,sha256=5U88Idj_G8-PSDb9VF3OwYFbGLHnGOo_GxgYvi0dtXw,37
|
|
7
|
+
pytest_neon-2.2.1.dist-info/licenses/LICENSE,sha256=aKKp_Ex4WBHTByY4BhXJ181dzB_qYhi2pCUmZ7Spn_0,1067
|
|
8
|
+
pytest_neon-2.2.1.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
pytest_neon/__init__.py,sha256=-HXVGp38c6Ddmq90i5eo6OFxKVzBo3igJ6R3DKA8A7M,398
|
|
2
|
-
pytest_neon/plugin.py,sha256=8y5LYvQossHJlPpJdh0DdZj8tFcO8OY45MMMfQytcRA,49269
|
|
3
|
-
pytest_neon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
pytest_neon-2.2.0.dist-info/METADATA,sha256=dMgATSjnaUH1gH1posidEa99b_IE30ikeVsRsYC3oE0,19237
|
|
5
|
-
pytest_neon-2.2.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
6
|
-
pytest_neon-2.2.0.dist-info/entry_points.txt,sha256=5U88Idj_G8-PSDb9VF3OwYFbGLHnGOo_GxgYvi0dtXw,37
|
|
7
|
-
pytest_neon-2.2.0.dist-info/licenses/LICENSE,sha256=aKKp_Ex4WBHTByY4BhXJ181dzB_qYhi2pCUmZ7Spn_0,1067
|
|
8
|
-
pytest_neon-2.2.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|