|
| 1 | +"""Pytest tests for the refresh of the derived data before a build. |
| 2 | +
|
| 3 | +`loopstructural.main.derived_refresh` imports `m2l_api` only when it |
| 4 | +calculates, so the tests put a fake `m2l_api` in `sys.modules` and run in the |
| 5 | +fast tests/unit/ job. |
| 6 | +""" |
| 7 | + |
| 8 | +import sys |
| 9 | +from types import ModuleType, SimpleNamespace |
| 10 | + |
| 11 | +import pandas as pd |
| 12 | +import pytest |
| 13 | + |
| 14 | +from loopstructural.main import derived_data, layer_roles |
| 15 | +from loopstructural.main.derived_refresh import BASAL_CONTACT_UNIT_FIELD, DerivedRefresh |
| 16 | + |
| 17 | +UNITS = ['young', 'old'] |
| 18 | + |
| 19 | + |
| 20 | +class FakeLayer: |
| 21 | + def __init__(self, name): |
| 22 | + self._name = name |
| 23 | + |
| 24 | + def name(self): |
| 25 | + return self._name |
| 26 | + |
| 27 | + |
| 28 | +class FakeDerived: |
| 29 | + def __init__(self): |
| 30 | + self.recorded = [] |
| 31 | + |
| 32 | + def record(self, name, inputs): |
| 33 | + self.recorded.append((name, inputs)) |
| 34 | + |
| 35 | + |
| 36 | +class FakeDataManager: |
| 37 | + def __init__(self): |
| 38 | + self.layer_roles = SimpleNamespace(contacts_source=layer_roles.CONTACTS_FROM_GEOLOGY) |
| 39 | + self.derived = FakeDerived() |
| 40 | + self.old_contacts = FakeLayer('Basal contacts') |
| 41 | + self.roles = { |
| 42 | + layer_roles.GEOLOGY: FakeLayer('geology'), |
| 43 | + layer_roles.GEOLOGY_UNIT_FIELD: 'UNITNAME', |
| 44 | + layer_roles.BASAL_CONTACTS: self.old_contacts, |
| 45 | + } |
| 46 | + self.settings = { |
| 47 | + 'basal_contacts_widget': {}, |
| 48 | + 'thickness_calculator_widget': { |
| 49 | + 'calculator_type': 'InterpolatedStructure', |
| 50 | + 'basal_contacts_layer': 'Basal contacts', |
| 51 | + 'basal_unit_field': 'basal_unit', |
| 52 | + }, |
| 53 | + } |
| 54 | + self.stratigraphy_updates = [] |
| 55 | + self.applied = [] |
| 56 | + |
| 57 | + def get_widget_settings(self, name, default=None): |
| 58 | + return self.settings.get(name, default) |
| 59 | + |
| 60 | + def get_layer_role(self, role): |
| 61 | + return self.roles.get(role) |
| 62 | + |
| 63 | + def find_layer_by_name(self, name): |
| 64 | + return self.old_contacts if name == 'Basal contacts' else FakeLayer(name) |
| 65 | + |
| 66 | + def basal_contacts_inputs(self): |
| 67 | + return {'geology': 'geology', 'unit_field': 'UNITNAME', 'unit_order': UNITS} |
| 68 | + |
| 69 | + def thickness_inputs(self): |
| 70 | + return {'contacts': 'calculated', 'unit_order': UNITS} |
| 71 | + |
| 72 | + def get_model_crs(self): |
| 73 | + return SimpleNamespace(isValid=lambda: True) |
| 74 | + |
| 75 | + def get_stratigraphic_unit_names(self): |
| 76 | + return list(UNITS) |
| 77 | + |
| 78 | + def get_stratigraphic_unit_colours(self): |
| 79 | + return {} |
| 80 | + |
| 81 | + def update_stratigraphy(self, basal_contacts=None, unit_name_field=None): |
| 82 | + self.stratigraphy_updates.append((basal_contacts, unit_name_field)) |
| 83 | + |
| 84 | + def apply_calculated_thicknesses(self, values): |
| 85 | + self.applied.append(values) |
| 86 | + return values, [] |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture |
| 90 | +def contacts(): |
| 91 | + return pd.DataFrame({BASAL_CONTACT_UNIT_FIELD: ['young'], 'X': [1.0]}) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.fixture |
| 95 | +def m2l_api(monkeypatch, contacts): |
| 96 | + """A fake `m2l_api` that records the calls.""" |
| 97 | + module = ModuleType('loopstructural.main.m2l_api') |
| 98 | + module.calls = [] |
| 99 | + |
| 100 | + def extract_basal_contacts(updater=None, **params): |
| 101 | + module.calls.append(('contacts', params)) |
| 102 | + return {'basal_contacts': contacts} |
| 103 | + |
| 104 | + def calculate_thickness(updater=None, **params): |
| 105 | + module.calls.append(('thickness', params)) |
| 106 | + return {'thicknesses': pd.DataFrame({'name': ['young'], 'ThicknessMedian': [10.0]})} |
| 107 | + |
| 108 | + module.extract_basal_contacts = extract_basal_contacts |
| 109 | + module.calculate_thickness = calculate_thickness |
| 110 | + monkeypatch.setitem(sys.modules, 'loopstructural.main.m2l_api', module) |
| 111 | + return module |
| 112 | + |
| 113 | + |
| 114 | +@pytest.fixture |
| 115 | +def dm(): |
| 116 | + return FakeDataManager() |
| 117 | + |
| 118 | + |
| 119 | +def _refresh(dm, names): |
| 120 | + refresh = DerivedRefresh(dm, None, names) |
| 121 | + refresh._update_contacts_layer = lambda contacts: None |
| 122 | + return refresh |
| 123 | + |
| 124 | + |
| 125 | +BOTH = [derived_data.BASAL_CONTACTS, derived_data.THICKNESS] |
| 126 | + |
| 127 | + |
| 128 | +class TestRefreshOrder: |
| 129 | + def test_the_inputs_are_read_from_the_data_manager(self, dm, m2l_api): |
| 130 | + refresh = _refresh(dm, BOTH) |
| 131 | + assert refresh._contacts['inputs'] == dm.basal_contacts_inputs() |
| 132 | + assert refresh._thickness['inputs'] == dm.thickness_inputs() |
| 133 | + |
| 134 | + def test_the_thickness_uses_the_new_contacts_not_the_old_layer(self, dm, m2l_api, contacts): |
| 135 | + _refresh(dm, BOTH).run() |
| 136 | + assert [name for name, _params in m2l_api.calls] == ['contacts', 'thickness'] |
| 137 | + params = m2l_api.calls[1][1] |
| 138 | + assert params['basal_contacts'] is contacts |
| 139 | + assert params['basal_contacts'] is not dm.old_contacts |
| 140 | + assert params['basal_contacts_unit_name'] is None |
| 141 | + |
| 142 | + def test_the_thickness_alone_uses_the_contacts_layer(self, dm, m2l_api): |
| 143 | + _refresh(dm, [derived_data.THICKNESS]).run() |
| 144 | + params = m2l_api.calls[0][1] |
| 145 | + assert params['basal_contacts'] is dm.old_contacts |
| 146 | + assert params['basal_contacts_unit_name'] == 'basal_unit' |
| 147 | + |
| 148 | + |
| 149 | +class TestThreads: |
| 150 | + def test_run_does_not_change_the_data_manager(self, dm, m2l_api): |
| 151 | + _refresh(dm, BOTH).run() |
| 152 | + assert dm.stratigraphy_updates == [] |
| 153 | + assert dm.applied == [] |
| 154 | + assert dm.derived.recorded == [] |
| 155 | + |
| 156 | + def test_finish_puts_the_contacts_in_the_model_and_records_the_inputs( |
| 157 | + self, dm, m2l_api, contacts |
| 158 | + ): |
| 159 | + refresh = _refresh(dm, BOTH) |
| 160 | + refresh.run() |
| 161 | + refresh.finish() |
| 162 | + assert dm.stratigraphy_updates == [(contacts, BASAL_CONTACT_UNIT_FIELD)] |
| 163 | + assert dm.applied == [{'young': 10.0}] |
| 164 | + assert [name for name, _inputs in dm.derived.recorded] == BOTH |
0 commit comments