|
| 1 | +"""Pytest tests for the contacts that the model reads with "Calculate from geology polygons". |
| 2 | +
|
| 3 | +The contacts layer of that mode is a memory layer that exists for display. It is |
| 4 | +empty or gone when the project is opened again, so the model must not read it |
| 5 | +again after the build calculated the contacts. |
| 6 | +""" |
| 7 | + |
| 8 | +from unittest.mock import Mock |
| 9 | + |
| 10 | +import geopandas as gpd |
| 11 | +import pytest |
| 12 | +from qgis.core import ( |
| 13 | + QgsCoordinateReferenceSystem, |
| 14 | + QgsFeature, |
| 15 | + QgsGeometry, |
| 16 | + QgsProject, |
| 17 | + QgsVectorLayer, |
| 18 | +) |
| 19 | +from shapely.geometry import LineString |
| 20 | + |
| 21 | +from loopstructural.main import layer_roles |
| 22 | +from loopstructural.main.data_manager import ModellingDataManager |
| 23 | +from loopstructural.main.derived_refresh import BASAL_CONTACT_UNIT_FIELD |
| 24 | +from loopstructural.main.model_manager import GeologicalModelManager |
| 25 | + |
| 26 | + |
| 27 | +class _DebugManager: |
| 28 | + def log(self, *args, **kwargs): |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def project(): |
| 34 | + QgsProject.instance().setCrs(QgsCoordinateReferenceSystem('EPSG:32755')) |
| 35 | + yield QgsProject.instance() |
| 36 | + QgsProject.instance().removeAllMapLayers() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def data_manager(project): |
| 41 | + manager = ModellingDataManager(project=project, mapCanvas=Mock(), logger=Mock()) |
| 42 | + manager.set_model_manager(GeologicalModelManager(debug_manager=_DebugManager())) |
| 43 | + column = manager.get_stratigraphic_column() |
| 44 | + column.clear(basement=False) |
| 45 | + for name in ('old', 'young'): |
| 46 | + column.add_unit(name=name, colour='#ff0000', where='top') |
| 47 | + return manager |
| 48 | + |
| 49 | + |
| 50 | +def _contacts(): |
| 51 | + lines = [LineString([(0, 0), (10, 0), (20, 5)]), LineString([(0, 50), (20, 50)])] |
| 52 | + return gpd.GeoDataFrame( |
| 53 | + {BASAL_CONTACT_UNIT_FIELD: ['old', 'young']}, geometry=lines, crs='EPSG:32755' |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _empty_contacts_layer(project): |
| 58 | + layer = QgsVectorLayer( |
| 59 | + f'LineString?crs=EPSG:32755&field={BASAL_CONTACT_UNIT_FIELD}:string', |
| 60 | + 'Basal contacts', |
| 61 | + 'memory', |
| 62 | + ) |
| 63 | + assert layer.isValid() |
| 64 | + project.addMapLayer(layer) |
| 65 | + return layer |
| 66 | + |
| 67 | + |
| 68 | +def _rows(manager): |
| 69 | + return { |
| 70 | + name: len(unit.get('contact', ())) |
| 71 | + for name, unit in manager._model_manager.stratigraphy.items() |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +class TestContactsFromGeology: |
| 76 | + def test_there_is_no_contact_data_before_the_first_calculation(self, data_manager): |
| 77 | + assert not data_manager.has_contact_data() |
| 78 | + |
| 79 | + def test_the_calculated_contacts_are_in_the_model(self, data_manager): |
| 80 | + data_manager.update_stratigraphy( |
| 81 | + basal_contacts=_contacts(), unit_name_field=BASAL_CONTACT_UNIT_FIELD |
| 82 | + ) |
| 83 | + assert data_manager.has_contact_data() |
| 84 | + assert _rows(data_manager)['old'] > 0 and _rows(data_manager)['young'] > 0 |
| 85 | + |
| 86 | + def test_a_later_update_keeps_the_calculated_contacts(self, data_manager): |
| 87 | + data_manager.update_stratigraphy( |
| 88 | + basal_contacts=_contacts(), unit_name_field=BASAL_CONTACT_UNIT_FIELD |
| 89 | + ) |
| 90 | + before = _rows(data_manager) |
| 91 | + # a change of the column or of the orientations updates the stratigraphy with no argument |
| 92 | + data_manager.update_stratigraphy() |
| 93 | + assert _rows(data_manager) == before |
| 94 | + |
| 95 | + def test_an_empty_contacts_layer_does_not_remove_the_contacts(self, data_manager, project): |
| 96 | + data_manager.update_stratigraphy( |
| 97 | + basal_contacts=_contacts(), unit_name_field=BASAL_CONTACT_UNIT_FIELD |
| 98 | + ) |
| 99 | + before = _rows(data_manager) |
| 100 | + # what the project has after it was opened again: the layer has no features |
| 101 | + data_manager.set_basal_contacts( |
| 102 | + _empty_contacts_layer(project), unitname_field=BASAL_CONTACT_UNIT_FIELD |
| 103 | + ) |
| 104 | + assert _rows(data_manager) == before |
| 105 | + |
| 106 | + def test_the_contacts_layer_is_read_once(self, data_manager, project): |
| 107 | + layer = _empty_contacts_layer(project) |
| 108 | + feature = QgsFeature(layer.fields()) |
| 109 | + feature.setGeometry(QgsGeometry.fromWkt('LineString (0 0, 10 0, 20 5)')) |
| 110 | + feature.setAttribute(BASAL_CONTACT_UNIT_FIELD, 'old') |
| 111 | + layer.dataProvider().addFeatures([feature]) |
| 112 | + data_manager.set_basal_contacts(layer, unitname_field=BASAL_CONTACT_UNIT_FIELD) |
| 113 | + assert _rows(data_manager)['old'] > 0 |
| 114 | + # the layer changes: the model keeps what was read, until the build calculates again |
| 115 | + layer.dataProvider().truncate() |
| 116 | + data_manager.update_stratigraphy() |
| 117 | + assert _rows(data_manager)['old'] > 0 |
| 118 | + |
| 119 | + def test_a_new_project_forgets_the_contacts(self, data_manager): |
| 120 | + data_manager.update_stratigraphy( |
| 121 | + basal_contacts=_contacts(), unit_name_field=BASAL_CONTACT_UNIT_FIELD |
| 122 | + ) |
| 123 | + data_manager.set_basal_contacts(None, unitname_field=None) |
| 124 | + data_manager.update_stratigraphy() |
| 125 | + assert data_manager._calculated_contacts is None |
| 126 | + |
| 127 | + |
| 128 | +class TestContactsFromLayer: |
| 129 | + def test_the_layer_of_the_user_is_not_remembered(self, data_manager, project): |
| 130 | + data_manager.layer_roles.contacts_source = layer_roles.CONTACTS_FROM_LAYER |
| 131 | + data_manager.set_basal_contacts( |
| 132 | + _empty_contacts_layer(project), unitname_field=BASAL_CONTACT_UNIT_FIELD |
| 133 | + ) |
| 134 | + assert data_manager._calculated_contacts is None |
| 135 | + |
| 136 | + |
| 137 | +class TestDeletedLayer: |
| 138 | + """A setting that points to a layer that QGIS deleted must not raise.""" |
| 139 | + |
| 140 | + @staticmethod |
| 141 | + def _delete(data_manager, project): |
| 142 | + layer = _empty_contacts_layer(project) |
| 143 | + data_manager.set_basal_contacts(layer, unitname_field=BASAL_CONTACT_UNIT_FIELD) |
| 144 | + project.removeMapLayer(layer.id()) |
| 145 | + return layer |
| 146 | + |
| 147 | + def test_the_unique_units_do_not_raise(self, data_manager, project): |
| 148 | + self._delete(data_manager, project) |
| 149 | + assert data_manager.calculate_unique_basal_units() == 0 |
| 150 | + assert data_manager.get_basal_contacts() is None |
| 151 | + |
| 152 | + def test_the_stratigraphy_update_does_not_raise(self, data_manager, project): |
| 153 | + self._delete(data_manager, project) |
| 154 | + data_manager.update_stratigraphy() |
| 155 | + |
| 156 | + def test_the_getter_gives_nothing_for_a_deleted_layer(self, data_manager, project): |
| 157 | + self._delete(data_manager, project) |
| 158 | + assert data_manager.get_basal_contacts() is None |
| 159 | + # the next layer works as usual |
| 160 | + layer = _empty_contacts_layer(project) |
| 161 | + data_manager.set_basal_contacts(layer, unitname_field=BASAL_CONTACT_UNIT_FIELD) |
| 162 | + assert data_manager.get_basal_contacts()['layer'] is layer |
0 commit comments