Skip to content

Commit 3609a57

Browse files
committed
[test, toolbar] Include test to check build boundaries with invalid data and error is caught when executing the build boundaries model
1 parent 513f31a commit 3609a57

File tree

9 files changed

+398
-318
lines changed

9 files changed

+398
-318
lines changed

asistente_ladm_col/lib/geometry.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,14 @@ def build_boundaries(self, boundary_layer):
720720
"""
721721
:return: QgsVectorLayer
722722
"""
723-
params = {'boundaries': boundary_layer, 'native:refactorfields_2:built_boundaries': 'TEMPORARY_OUTPUT'}
724-
build_boundary_layer = processing.run("model:Build_Boundaries", params)['native:refactorfields_2:built_boundaries']
723+
feedback = CustomFeedbackWithErrors()
724+
try:
725+
params = {'boundaries': boundary_layer, 'native:refactorfields_2:built_boundaries': 'TEMPORARY_OUTPUT'}
726+
build_boundary_layer = processing.run("model:Build_Boundaries", params, feedback=feedback)['native:refactorfields_2:built_boundaries']
727+
except QgsProcessingException as e:
728+
self.logger.warning(__name__, QCoreApplication.translate("Geometry",
729+
"Error running the model to build boundaries. Details: {}".format(feedback.msg)))
730+
build_boundary_layer = QgsVectorLayer("LineString?crs={}".format(get_crs_authid(boundary_layer.sourceCrs())), "build_boundaries", "memory")
725731

726732
# For CTM12 the output layer remains without projection. The projection of the input layer is assigned
727733
if not build_boundary_layer.crs().authid():

asistente_ladm_col/lib/processing/models/model_build_boundaries.model3

Lines changed: 347 additions & 312 deletions
Large diffs are not rendered by default.

asistente_ladm_col/tests/config/test_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
'no_interlis_gpkg': 'static/gpkg/no_interlis.gpkg',
5555
'test_logic_quality_rules_gpkg': 'ladm/gpkg/test_logic_quality_rules_v1_1.gpkg',
5656
'test_valid_quality_rules_gpkg': 'ladm/gpkg/test_valid_quality_rules_v1_1.gpkg',
57-
'test_toolbar_gpkg': 'ladm/gpkg/test_toolbar_v1_1.gpkg',
58-
'test_toolbar_empty_geom_gpkg': 'ladm/gpkg/test_toolbar_v1_1_empty_geom.gpkg',
57+
'test_build_boundaries_gpkg': 'ladm/gpkg/test_build_boundaries_v1_1.gpkg',
58+
'test_build_boundaries_invalid_geom_gpkg': 'ladm/gpkg/test_build_boundaries_invalid_geom_v1_1.gpkg',
59+
'test_build_boundaries_empty_geom_gpkg': 'ladm/gpkg/test_build_boundaries_empty_geom_v1_1.gpkg',
60+
'test_build_boundaries_fix_geoms_gpkg': 'ladm/gpkg/test_build_boundaries_fix_geoms_v1_1.gpkg',
5961
'interlis_no_ladm_col_models_gpkg': 'static/gpkg/interlis_no_ladm_col_models.gpkg'
6062
}

asistente_ladm_col/tests/resources/db/ladm/gpkg/test_toolbar_v1_1_empty_geom.gpkg renamed to asistente_ladm_col/tests/resources/db/ladm/gpkg/test_build_boundaries_empty_geom_v1_1.gpkg

File renamed without changes.
Binary file not shown.
Binary file not shown.

asistente_ladm_col/tests/resources/db/ladm/gpkg/test_toolbar_v1_1.gpkg renamed to asistente_ladm_col/tests/resources/db/ladm/gpkg/test_build_boundaries_v1_1.gpkg

File renamed without changes.
Binary file not shown.

asistente_ladm_col/tests/test_build_boundaries.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def setUpClass(cls):
3333

3434
def test_build_boundaries(self):
3535
print('\nINFO: Validating build boundaries...')
36-
db_gpkg = get_copy_gpkg_conn('test_toolbar_gpkg')
36+
db_gpkg = get_copy_gpkg_conn('test_build_boundaries_gpkg')
3737
res, code, msg = db_gpkg.test_connection()
3838
self.assertTrue(res, msg)
3939

@@ -99,7 +99,7 @@ def test_build_boundaries(self):
9999

100100
def test_build_boundaries_with_empty_geom(self):
101101
print('\nINFO: Validating build boundaries, null geometries of the input layer are removed...')
102-
db_gpkg = get_copy_gpkg_conn('test_toolbar_empty_geom_gpkg')
102+
db_gpkg = get_copy_gpkg_conn('test_build_boundaries_empty_geom_gpkg')
103103
res, code, msg = db_gpkg.test_connection()
104104
self.assertTrue(res, msg)
105105

@@ -120,6 +120,43 @@ def test_build_boundaries_with_empty_geom(self):
120120
self.assertEqual(boundary_layer.selectedFeatureCount(), 0,
121121
'Null features count does not match for the layer boundary layer')
122122

123+
def test_build_boundaries_with_invalid_geom(self):
124+
print('\nINFO: Validating build boundaries, invalid geometry of the input layer are removed...')
125+
db_gpkg = get_copy_gpkg_conn('test_build_boundaries_invalid_geom_gpkg')
126+
res, code, msg = db_gpkg.test_connection()
127+
self.assertTrue(res, msg)
128+
129+
boundary_layer = self.app.core.get_layer(db_gpkg, db_gpkg.names.LC_BOUNDARY_T, load=True)
130+
self.assertEqual(boundary_layer.featureCount(), 16, 'Features count does not match for the layer boundary layer')
131+
132+
self.toolbar.build_boundaries(db_gpkg, True)
133+
134+
self.assertEqual(boundary_layer.featureCount(), 1, 'Features count does not match for the layer boundary layer')
135+
136+
exp = "is_empty_or_null($geometry)"
137+
boundary_layer.selectByExpression(exp)
138+
self.assertEqual(boundary_layer.selectedFeatureCount(), 0,
139+
'Null features count does not match for the layer boundary layer')
140+
141+
142+
def test_build_boundaries_with_invalid_geom_in_processing(self):
143+
print('\nINFO: Validating build boundaries, invalid geometries generated when running the processing algorithm are fixed...')
144+
db_gpkg = get_copy_gpkg_conn('test_build_boundaries_fix_geoms_gpkg')
145+
res, code, msg = db_gpkg.test_connection()
146+
self.assertTrue(res, msg)
147+
148+
boundary_layer = self.app.core.get_layer(db_gpkg, db_gpkg.names.LC_BOUNDARY_T, load=True)
149+
self.assertEqual(boundary_layer.featureCount(), 7, 'Features count does not match for the layer boundary layer')
150+
151+
self.toolbar.build_boundaries(db_gpkg, True)
152+
153+
self.assertEqual(boundary_layer.featureCount(), 17, 'Features count does not match for the layer boundary layer')
154+
155+
exp = "is_empty_or_null($geometry)"
156+
boundary_layer.selectByExpression(exp)
157+
self.assertEqual(boundary_layer.selectedFeatureCount(), 0,
158+
'Null features count does not match for the layer boundary layer')
159+
123160
def test_check_build_boundaries(self):
124161
print('\nINFO: Validation of the definition of boundaries...')
125162

0 commit comments

Comments
 (0)