|
| 1 | +""" |
| 2 | +Pytest fixtures and hooks for shapelycpp precision alignment tests. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + pytest tests/ # run all tests |
| 6 | + pytest tests/ --cpp-module=my_module # custom C++ module name |
| 7 | + pytest tests/ --rtol=1e-10 --atol=1e-10 # custom tolerances |
| 8 | +""" |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import pytest |
| 12 | + |
| 13 | + |
| 14 | +def pytest_addoption(parser): |
| 15 | + parser.addoption( |
| 16 | + "--cpp-module", action="store", default=None, |
| 17 | + help="Python module name for the compiled C++ shapelycpp library (default: shapelycpp)", |
| 18 | + ) |
| 19 | + parser.addoption( |
| 20 | + "--rtol", action="store", type=float, default=1e-10, |
| 21 | + help="Relative tolerance for numerical comparisons (default: 1e-10)", |
| 22 | + ) |
| 23 | + parser.addoption( |
| 24 | + "--atol", action="store", type=float, default=1e-10, |
| 25 | + help="Absolute tolerance for numerical comparisons (default: 1e-10)", |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +# -- Lazy C++ module import --------------------------------------------------- |
| 30 | + |
| 31 | +_cpp_module = None |
| 32 | +_import_error = None |
| 33 | + |
| 34 | + |
| 35 | +def _resolve_module_name(config) -> str: |
| 36 | + import os |
| 37 | + cli = config.getoption("--cpp-module", default=None) |
| 38 | + if cli: |
| 39 | + return cli |
| 40 | + env = os.environ.get("SHAPELYCPP_MODULE") |
| 41 | + if env: |
| 42 | + return env |
| 43 | + return "shapelycpp" |
| 44 | + |
| 45 | + |
| 46 | +def get_cpp_module(request=None): |
| 47 | + """Return the compiled shapelycpp C++ module (lazy, cached).""" |
| 48 | + global _cpp_module, _import_error |
| 49 | + |
| 50 | + if _cpp_module is not None: |
| 51 | + return _cpp_module |
| 52 | + if _import_error is not None: |
| 53 | + raise _import_error |
| 54 | + |
| 55 | + if request is not None: |
| 56 | + modname = _resolve_module_name(request.config) |
| 57 | + else: |
| 58 | + import os |
| 59 | + modname = os.environ.get("SHAPELYCPP_MODULE", "shapelycpp") |
| 60 | + |
| 61 | + import importlib |
| 62 | + try: |
| 63 | + _cpp_module = importlib.import_module(modname) |
| 64 | + except ImportError as e: |
| 65 | + _import_error = e |
| 66 | + raise |
| 67 | + return _cpp_module |
| 68 | + |
| 69 | + |
| 70 | +# -- Fixtures ------------------------------------------------------------------ |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture(scope="session") |
| 74 | +def cpp(): |
| 75 | + """Session-scoped C++ module fixture.""" |
| 76 | + return get_cpp_module() |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture |
| 80 | +def rtol(request): |
| 81 | + return request.config.getoption("--rtol", default=1e-10) |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture |
| 85 | +def atol(request): |
| 86 | + return request.config.getoption("--atol", default=1e-10) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture(params=[np.float64, np.float32], ids=["float64", "float32"]) |
| 90 | +def dtype(request): |
| 91 | + """Parametrized fixture: each test runs once with float64 and once with float32.""" |
| 92 | + return request.param |
| 93 | + |
| 94 | + |
| 95 | +@pytest.fixture(params=[np.float64, np.float32], ids=["float64", "float32"]) |
| 96 | +def make(cpp, request): |
| 97 | + """Parametrized fixture providing float64/float32 geometry factories and classes.""" |
| 98 | + if request.param == np.float64: |
| 99 | + return { |
| 100 | + 'point': cpp.point, |
| 101 | + 'linestring': cpp.linestring, |
| 102 | + 'polygon': cpp.polygon, |
| 103 | + 'Point': cpp.Point, |
| 104 | + 'LineString': cpp.LineString, |
| 105 | + 'Polygon': cpp.Polygon, |
| 106 | + 'dtype': np.float64, |
| 107 | + } |
| 108 | + else: |
| 109 | + return { |
| 110 | + 'point': cpp.point_f32, |
| 111 | + 'linestring': cpp.linestring_f32, |
| 112 | + 'polygon': cpp.polygon_f32, |
| 113 | + 'Point': cpp.PointF32, |
| 114 | + 'LineString': cpp.LineStringF32, |
| 115 | + 'Polygon': cpp.PolygonF32, |
| 116 | + 'dtype': np.float32, |
| 117 | + } |
0 commit comments