|
| 1 | +import os |
| 2 | +import pickle |
| 3 | +import importlib |
| 4 | +import pytest |
| 5 | + |
| 6 | +from qgis.core import QgsApplication, QgsProcessingContext, QgsProcessingFeedback |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture(scope="session", autouse=True) |
| 10 | +def qgis_app(): |
| 11 | + """Start a headless QGIS application for tests. |
| 12 | +
|
| 13 | + Requires QGIS_PREFIX_PATH to be set in the environment (pointing to the QGIS install). |
| 14 | + """ |
| 15 | + prefix = os.environ.get("QGIS_PREFIX_PATH") |
| 16 | + if not prefix: |
| 17 | + raise RuntimeError( |
| 18 | + "QGIS_PREFIX_PATH environment variable must be set to your QGIS install path for tests to run." |
| 19 | + ) |
| 20 | + |
| 21 | + app = QgsApplication([], False) |
| 22 | + app.setPrefixPath(prefix, True) |
| 23 | + app.initQgis() |
| 24 | + |
| 25 | + yield app |
| 26 | + |
| 27 | + app.exitQgis() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def qgis_context(): |
| 32 | + """Return a fresh processing context.""" |
| 33 | + return QgsProcessingContext() |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def feedback(): |
| 38 | + """Return a simple QgsProcessingFeedback instance for algorithms.""" |
| 39 | + return QgsProcessingFeedback() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def ensure_loopstructural(monkeypatch): |
| 44 | + """If the real LoopStructural classes are not available, inject minimal fakes into |
| 45 | + the algorithm module so tests can run without the external dependency. |
| 46 | +
|
| 47 | + The algorithm module imports FaultTopology and FaultRelationshipType at import-time |
| 48 | + and uses those module-level names; this fixture patches the algorithm module |
| 49 | + attributes when they are missing. |
| 50 | + """ |
| 51 | + mod_name = "loopstructural.processing.algorithms.modelling.add_fault_topology" |
| 52 | + mod = importlib.import_module(mod_name) |
| 53 | + |
| 54 | + if getattr(mod, "FaultTopology", None) is not None and getattr(mod, "FaultRelationshipType", None) is not None: |
| 55 | + # real dependency present; nothing to do |
| 56 | + return |
| 57 | + |
| 58 | + class _FakeFaultTopology: |
| 59 | + def __init__(self, strat_col=None): |
| 60 | + self.strat_col = strat_col |
| 61 | + self.faults = set() |
| 62 | + # store relationships in a dict for simple inspection |
| 63 | + self._rels = {} |
| 64 | + |
| 65 | + def add_fault(self, name): |
| 66 | + self.faults.add(name) |
| 67 | + |
| 68 | + def update_fault_relationship(self, a, b, rel): |
| 69 | + self._rels[(a, b)] = rel |
| 70 | + |
| 71 | + def __repr__(self): |
| 72 | + return f"FakeFaultTopology(faults={sorted(self.faults)})" |
| 73 | + |
| 74 | + class _FakeFaultRelationshipType: |
| 75 | + ABUTTING = 1 |
| 76 | + |
| 77 | + monkeypatch.setattr(mod, "FaultTopology", _FakeFaultTopology, raising=False) |
| 78 | + monkeypatch.setattr(mod, "FaultRelationshipType", _FakeFaultRelationshipType, raising=False) |
| 79 | + |
| 80 | + return |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture |
| 84 | +def simple_model_pickle(tmp_path): |
| 85 | + """Create and return a path to a simple pickled model object suitable for tests. |
| 86 | +
|
| 87 | + The returned object has a `features` attribute with simple feature-like objects |
| 88 | + exposing a `name` attribute so the algorithm can find faults. |
| 89 | + """ |
| 90 | + class DummyFeature: |
| 91 | + def __init__(self, name): |
| 92 | + self.name = name |
| 93 | + |
| 94 | + def __repr__(self): |
| 95 | + return f"DummyFeature({self.name})" |
| 96 | + |
| 97 | + class DummyModel: |
| 98 | + def __init__(self, names=("fault1", "fault2")): |
| 99 | + self.features = [DummyFeature(n) for n in names] |
| 100 | + |
| 101 | + def __repr__(self): |
| 102 | + return f"DummyModel(features={self.features})" |
| 103 | + |
| 104 | + model = DummyModel() |
| 105 | + path = tmp_path / "model.pkl" |
| 106 | + with open(path, "wb") as fh: |
| 107 | + pickle.dump(model, fh) |
| 108 | + |
| 109 | + return str(path) |
0 commit comments