pytest-neon 2.2.0__py3-none-any.whl → 2.2.2__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 CHANGED
@@ -9,7 +9,7 @@ from pytest_neon.plugin import (
9
9
  neon_engine,
10
10
  )
11
11
 
12
- __version__ = "2.2.0"
12
+ __version__ = "2.2.2"
13
13
  __all__ = [
14
14
  "NeonBranch",
15
15
  "neon_branch",
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,123 @@ 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
- # No expiry - Neon doesn't allow children from branches with expiry
830
- branch_gen = _create_neon_branch(
831
- request,
832
- branch_expiry_override=0,
833
- branch_name_suffix="-migrated",
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
- # Capture schema fingerprint BEFORE migrations run
838
- # This is stored on config so _neon_branch_for_reset can compare after
839
- pre_migration_fingerprint = _get_schema_fingerprint(branch.connection_string)
840
- request.config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
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
+ migrations_done_file = root_tmp_dir / "neon_migrations_done"
869
+ else:
870
+ cache_file = None
871
+ lock_file = None
872
+ migrations_done_file = None
873
+
874
+ is_creator = False
875
+ branch: NeonBranch
876
+ branch_gen: Generator[NeonBranch, None, None] | None = None
877
+ original_env_value: str | None = None
878
+
879
+ if is_xdist:
880
+ assert cache_file is not None and lock_file is not None
881
+ assert migrations_done_file is not None
882
+ with FileLock(str(lock_file)):
883
+ if cache_file.exists():
884
+ # Another worker already created the branch - reuse it
885
+ data = json.loads(cache_file.read_text())
886
+ branch = _dict_to_branch(data["branch"])
887
+ pre_migration_fingerprint = tuple(
888
+ tuple(row) for row in data["pre_migration_fingerprint"]
889
+ )
890
+ config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
891
+
892
+ # Set DATABASE_URL for this worker (not done by _create_neon_branch)
893
+ original_env_value = os.environ.get(env_var_name)
894
+ os.environ[env_var_name] = branch.connection_string
895
+ else:
896
+ # First worker - create branch and cache it
897
+ is_creator = True
898
+ branch_gen = _create_neon_branch(
899
+ request,
900
+ branch_expiry_override=0,
901
+ branch_name_suffix="-migrated",
902
+ )
903
+ branch = next(branch_gen)
904
+
905
+ # Capture schema fingerprint BEFORE migrations run
906
+ pre_migration_fingerprint = _get_schema_fingerprint(
907
+ branch.connection_string
908
+ )
909
+ config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
910
+
911
+ # Cache for other workers (they'll read this after lock released)
912
+ # Note: We cache now with pre-migration fingerprint. The branch
913
+ # content will have migrations applied by neon_apply_migrations.
914
+ cache_file.write_text(
915
+ json.dumps(
916
+ {
917
+ "branch": _branch_to_dict(branch),
918
+ "pre_migration_fingerprint": pre_migration_fingerprint,
919
+ }
920
+ )
921
+ )
922
+
923
+ # Non-creator workers must wait for migrations to complete BEFORE
924
+ # neon_apply_migrations runs, otherwise they'll try to run migrations
925
+ # concurrently on the same branch, causing race conditions.
926
+ if not is_creator:
927
+ waited = 0.0
928
+ poll_interval = 0.5
929
+ while not migrations_done_file.exists():
930
+ if waited >= _MIGRATION_WAIT_TIMEOUT:
931
+ raise RuntimeError(
932
+ f"Timeout waiting for migrations to complete after "
933
+ f"{_MIGRATION_WAIT_TIMEOUT}s. The creator worker may have "
934
+ f"failed or is still running migrations."
935
+ )
936
+ time.sleep(poll_interval)
937
+ waited += poll_interval
938
+ else:
939
+ # Not using xdist - create branch normally
940
+ is_creator = True
941
+ branch_gen = _create_neon_branch(
942
+ request,
943
+ branch_expiry_override=0,
944
+ branch_name_suffix="-migrated",
945
+ )
946
+ branch = next(branch_gen)
947
+
948
+ # Capture schema fingerprint BEFORE migrations run
949
+ pre_migration_fingerprint = _get_schema_fingerprint(branch.connection_string)
950
+ config._neon_pre_migration_fingerprint = pre_migration_fingerprint # type: ignore[attr-defined]
951
+
952
+ # Mark whether this worker is the creator (used by neon_apply_migrations)
953
+ config._neon_is_migration_creator = is_creator # type: ignore[attr-defined]
954
+ # Store migrations_done_file path for signaling after migrations complete
955
+ config._neon_migrations_done_file = migrations_done_file # type: ignore[attr-defined]
841
956
 
842
957
  try:
843
958
  yield branch
844
959
  finally:
845
- # Clean up by exhausting the generator (triggers branch deletion)
846
- with contextlib.suppress(StopIteration):
847
- next(branch_gen)
960
+ # Restore env var if we set it (non-creator workers)
961
+ if original_env_value is not None:
962
+ os.environ[env_var_name] = original_env_value
963
+ elif not is_creator and env_var_name in os.environ:
964
+ os.environ.pop(env_var_name, None)
965
+
966
+ # Only the creator cleans up the branch
967
+ if is_creator and branch_gen is not None:
968
+ with contextlib.suppress(StopIteration):
969
+ next(branch_gen)
848
970
 
849
971
 
850
972
  @pytest.fixture(scope="session")
@@ -855,6 +977,12 @@ def neon_apply_migrations(_neon_migration_branch: NeonBranch) -> Any:
855
977
  The migration branch is already created and DATABASE_URL is set.
856
978
  Migrations run once per test session, before any tests execute.
857
979
 
980
+ pytest-xdist Support:
981
+ When running with pytest-xdist, migrations only run on the first
982
+ worker (the one that created the migration branch). Other workers
983
+ wait for migrations to complete before proceeding. This ensures
984
+ migrations run exactly once, even with parallel workers.
985
+
858
986
  Smart Migration Detection:
859
987
  The plugin automatically detects whether migrations actually modified
860
988
  the database schema. If no schema changes occurred (or this fixture
@@ -897,11 +1025,41 @@ def neon_apply_migrations(_neon_migration_branch: NeonBranch) -> Any:
897
1025
  return _MIGRATIONS_NOT_DEFINED
898
1026
 
899
1027
 
1028
+ @pytest.fixture(scope="session")
1029
+ def _neon_migrations_synchronized(
1030
+ request: pytest.FixtureRequest,
1031
+ _neon_migration_branch: NeonBranch,
1032
+ neon_apply_migrations: Any,
1033
+ ) -> Any:
1034
+ """
1035
+ Internal fixture that synchronizes migrations across xdist workers.
1036
+
1037
+ This fixture ensures that:
1038
+ 1. Only the creator worker runs migrations (non-creators wait in
1039
+ _neon_migration_branch BEFORE neon_apply_migrations runs)
1040
+ 2. Creator signals completion after migrations finish
1041
+ 3. The return value from neon_apply_migrations is preserved for detection
1042
+
1043
+ Without xdist, this is a simple passthrough.
1044
+ """
1045
+ config = request.config
1046
+ is_creator = getattr(config, "_neon_is_migration_creator", True)
1047
+ migrations_done_file = getattr(config, "_neon_migrations_done_file", None)
1048
+
1049
+ if is_creator and migrations_done_file is not None:
1050
+ # Creator: migrations just ran via neon_apply_migrations dependency
1051
+ # Signal completion to other workers (who are waiting in
1052
+ # _neon_migration_branch)
1053
+ migrations_done_file.write_text("done")
1054
+
1055
+ return neon_apply_migrations
1056
+
1057
+
900
1058
  @pytest.fixture(scope="session")
901
1059
  def _neon_branch_for_reset(
902
1060
  request: pytest.FixtureRequest,
903
1061
  _neon_migration_branch: NeonBranch,
904
- neon_apply_migrations: Any, # Ensures migrations run first; value for detection
1062
+ _neon_migrations_synchronized: Any, # Ensures migrations complete; for detection
905
1063
  ) -> Generator[NeonBranch, None, None]:
906
1064
  """
907
1065
  Internal fixture that creates a test branch from the migration branch.
@@ -931,7 +1089,8 @@ def _neon_branch_for_reset(
931
1089
  - Migrations exist but are already applied (no schema changes)
932
1090
  """
933
1091
  # Check if migrations fixture was overridden
934
- migrations_defined = neon_apply_migrations is not _MIGRATIONS_NOT_DEFINED
1092
+ # _neon_migrations_synchronized passes through the neon_apply_migrations value
1093
+ migrations_defined = _neon_migrations_synchronized is not _MIGRATIONS_NOT_DEFINED
935
1094
 
936
1095
  # Check if schema actually changed (if we have a pre-migration fingerprint)
937
1096
  pre_fingerprint = getattr(request.config, "_neon_pre_migration_fingerprint", ())
@@ -1123,7 +1282,7 @@ def neon_branch(
1123
1282
  def neon_branch_shared(
1124
1283
  request: pytest.FixtureRequest,
1125
1284
  _neon_migration_branch: NeonBranch,
1126
- neon_apply_migrations: None, # Ensures migrations run first
1285
+ _neon_migrations_synchronized: Any, # Ensures migrations complete first
1127
1286
  ) -> Generator[NeonBranch, None, None]:
1128
1287
  """
1129
1288
  Provide a shared Neon database branch for all tests in a module.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-neon
3
- Version: 2.2.0
3
+ Version: 2.2.2
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=NYxWUqqq-U4rhtxH4fhgZ7F4-vaxQ51oYKhuMFGhTes,398
2
+ pytest_neon/plugin.py,sha256=8TciFbqE7mH86URTxSxw4sU8yqth17Mkrt2XOo0DZ-c,56023
3
+ pytest_neon/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ pytest_neon-2.2.2.dist-info/METADATA,sha256=Ga8VA-3kMmdhEj-oAer-qnOXkmZgC7GiBygh5HysiR4,19266
5
+ pytest_neon-2.2.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
6
+ pytest_neon-2.2.2.dist-info/entry_points.txt,sha256=5U88Idj_G8-PSDb9VF3OwYFbGLHnGOo_GxgYvi0dtXw,37
7
+ pytest_neon-2.2.2.dist-info/licenses/LICENSE,sha256=aKKp_Ex4WBHTByY4BhXJ181dzB_qYhi2pCUmZ7Spn_0,1067
8
+ pytest_neon-2.2.2.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,,