Skip to content
Permalink
Browse files

Also check geometry in tests

  • Loading branch information
nyalldawson committed Jun 2, 2018
1 parent d7d2a14 commit 314942d7a2f368f2760365ed9ada68e766cd6764
Showing with 24 additions and 7 deletions.
  1. +24 −7 tests/src/python/test_provider_ogr_sqlite.py
@@ -214,76 +214,93 @@ def testSubsetStringFids(self):
f.SetFID(0)
f.SetField(0, 1)
f.SetField(1, 11)
f.SetGeometry(ogr.CreateGeometryFromWkt('Point (0 0)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(1)
f.SetField(0, 1)
f.SetField(1, 12)
f.SetGeometry(ogr.CreateGeometryFromWkt('Point (1 1)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(2)
f.SetField(0, 1)
f.SetField(1, 13)
f.SetGeometry(ogr.CreateGeometryFromWkt('Point (2 2)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(3)
f.SetField(0, 2)
f.SetField(1, 14)
f.SetGeometry(ogr.CreateGeometryFromWkt('Point (3 3)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(4)
f.SetField(0, 2)
f.SetField(1, 15)
f.SetGeometry(ogr.CreateGeometryFromWkt('Point (4 4)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(5)
f.SetField(0, 2)
f.SetField(1, 16)
f.SetGeometry(ogr.CreateGeometryFromWkt('Point (5 5)'))
lyr.CreateFeature(f)
f = None
ds = None

vl = QgsVectorLayer(tmpfile, 'test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual([f.name() for f in vl.fields()], ['fid', 'type', 'value'])
original_fields = vl.fields()

vl = QgsVectorLayer(tmpfile + "|subset=type=2", 'test', 'ogr')
self.assertTrue(vl.isValid())

def run_checks():
self.assertEqual([f.name() for f in vl.fields()], ['fid', 'GEOMETRY', 'type', 'value'])
self.assertEqual([f.name() for f in vl.fields()], ['fid', 'type', 'value'])

req = QgsFeatureRequest()
req.setFilterExpression("value=16")
it = vl.getFeatures(req)
f = QgsFeature()
self.assertTrue(it.nextFeature(f))
self.assertEqual(f.id(), 5)
self.assertEqual(f.attributes(), [5, NULL, 2, 16])
self.assertEqual([field.name() for field in f.fields()], ['fid', 'GEOMETRY', 'type', 'value'])
self.assertEqual(f.attributes(), [5, 2, 16])
self.assertEqual([field.name() for field in f.fields()], ['fid', 'type', 'value'])
self.assertEqual(f.geometry().asWkt(), 'Point (5 5)')

req = QgsFeatureRequest()
req.setFilterFid(5)
it = vl.getFeatures(req)
f = QgsFeature()
self.assertTrue(it.nextFeature(f))
self.assertEqual(f.id(), 5)
self.assertEqual(f.attributes(), [5, NULL, 2, 16])
self.assertEqual([field.name() for field in f.fields()], ['fid', 'GEOMETRY', 'type', 'value'])
self.assertEqual(f.attributes(), [5, 2, 16])
self.assertEqual([field.name() for field in f.fields()], ['fid', 'type', 'value'])
self.assertEqual(f.geometry().asWkt(), 'Point (5 5)')

req = QgsFeatureRequest()
req.setFilterFids([5])
it = vl.getFeatures(req)
f = QgsFeature()
self.assertTrue(it.nextFeature(f))
self.assertEqual(f.id(), 5)
self.assertEqual(f.attributes(), [5, NULL, 2, 16])
self.assertEqual([field.name() for field in f.fields()], ['fid', 'GEOMETRY', 'type', 'value'])
self.assertEqual(f.attributes(), [5, 2, 16])
self.assertEqual([field.name() for field in f.fields()], ['fid', 'type', 'value'])
self.assertEqual(f.geometry().asWkt(), 'Point (5 5)')

# Ensure that orig_ogc_fid is still retrieved even if attribute subset is passed
req = QgsFeatureRequest()
req.setSubsetOfAttributes([])
it = vl.getFeatures(req)
ids = []
geoms = {}
while it.nextFeature(f):
ids.append(f.id())
geoms[f.id()] = f.geometry().asWkt()
self.assertCountEqual(ids, [3, 4, 5])
self.assertEqual(geoms, {3: 'Point (3 3)', 4: 'Point (4 4)', 5: 'Point (5 5)'})

run_checks()
# Check that subset string is correctly set on reload

0 comments on commit 314942d

Please sign in to comment.
You can’t perform that action at this time.