Skip to content

Commit

Permalink
[tests] Add test for only update existing feature's geometry mode (GPKG)
Browse files Browse the repository at this point in the history
  • Loading branch information
gacarrillor committed Dec 3, 2023
1 parent f1fe632 commit 83f578a
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion tests/test_source_simple_polygons.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import nose2

from qgis.PyQt.QtCore import QDate
from qgis.core import (QgsVectorLayerUtils,
QgsGeometry,
QgsVectorLayer)
from qgis.testing import unittest, start_app
from qgis.testing.mocked import get_iface

import processing

from tests.utils import (CommonTests,
get_qgis_gpkg_layer,
APPENDED_COUNT)
APPENDED_COUNT,
SKIPPED_COUNT,
UPDATED_FEATURE_COUNT,
UPDATED_ONLY_GEOMETRY_COUNT)

start_app()

Expand Down Expand Up @@ -57,6 +66,46 @@ def test_skip_none(self):
output_layer, layer_path = get_qgis_gpkg_layer('target_simple_polygons')
self.common._test_skip_none('source_simple_polygons', output_layer, layer_path)

def test_only_update_geometry(self):
print('\nINFO: Validating only update geometry when duplicates are found...')
output_layer, layer_path = get_qgis_gpkg_layer('target_simple_polygons')
input_layer_path = "{}|layername={}".format(layer_path, 'source_simple_polygons')
input_layer = QgsVectorLayer(input_layer_path, 'layer name', 'ogr')

# Let's create a feature that will be updated later
geom = QgsGeometry()
attrs = {0: 2,
1: 'DEF',
2: 20,
3: QDate(84,2,19),
4: '0.1234'}
new_feature = QgsVectorLayerUtils().createFeature(output_layer, geom, attrs)
output_layer.dataProvider().addFeatures([new_feature])

res = processing.run("etl_load:appendfeaturestolayer",
{'SOURCE_LAYER': input_layer,
'SOURCE_FIELD': 'fid',
'TARGET_LAYER': output_layer,
'TARGET_FIELD': 'fid',
'ACTION_ON_DUPLICATE': 3}) # Only update geometries

self.assertIsNotNone(res['TARGET_LAYER'])
self.assertEqual(res[APPENDED_COUNT], 1)
self.assertIsNone(res[UPDATED_FEATURE_COUNT])
self.assertEqual(res[UPDATED_ONLY_GEOMETRY_COUNT], 1)
self.assertIsNone(res[SKIPPED_COUNT])

self.assertEqual(output_layer.featureCount(), 2)

# We should now have an updated geometry, not QgsGeometry() anymore
new_wkt = 'Polygon ((1001318.1368170625064522 1013949.56026844482403249, 1001353.78617076261434704 1013933.86301946395542473, 1001321.19123999250587076 1013859.39660056948196143, 1001285.90720375021919608 1013876.74864967621397227, 1001318.1368170625064522 1013949.56026844482403249))'
feature = output_layer.getFeature(2)
self.assertEqual(feature.geometry().asWkt(), new_wkt)

# Attrs should be intact
old_attrs = [2, 'DEF', 20, QDate(84, 2, 19), '0.1234']
self.assertEqual(feature.attributes(), old_attrs)

@classmethod
def tearDownClass(self):
print('INFO: Tear down simple_pol-simple_pol')
Expand Down

0 comments on commit 83f578a

Please sign in to comment.