Skip to content

Commit 1e7eae7

Browse files
committedMar 5, 2016
Add extra checks to provider test:
- test that fetching subset of attributes results in nulls for all other fields - test that geometry is fetched by default, but empty when NoGeometry flag is passed
1 parent c8a73da commit 1e7eae7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
 

‎tests/src/python/providertestbase.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,27 @@ def testGetFeaturesSubsetAttributes(self):
395395
for field, expected in tests.iteritems():
396396
result = set([f[field] for f in self.provider.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([field], self.provider.fields()))])
397397
self.assertEqual(result, expected, 'Expected {}, got {}'.format(expected, result))
398+
399+
def testGetFeaturesSubsetAttributes2(self):
400+
""" Test that other fields are NULL wen fetching subsets of attributes """
401+
402+
for field_to_fetch in ['pk', 'cnt', 'name', 'name2']:
403+
for f in self.provider.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([field_to_fetch], self.provider.fields())):
404+
# Check that all other fields are NULL
405+
for other_field in [field.name() for field in self.provider.fields() if field.name() != field_to_fetch]:
406+
self.assertEqual(f[other_field], NULL, 'Value for field "{}" was present when it should not have been fetched by request'.format(other_field))
407+
408+
def testGetFeaturesNoGeometry(self):
409+
""" Test that no geometry is present when fetching features without geometry"""
410+
411+
for f in self.provider.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)):
412+
self.assertFalse(f.constGeometry(), 'Expected no geometry, got one')
413+
414+
def testGetFeaturesNoGeometry(self):
415+
""" Test that geometry is present when fetching features without setting NoGeometry flag"""
416+
for f in self.provider.getFeatures(QgsFeatureRequest()):
417+
if f['pk'] == 3:
418+
# no geometry for this feature
419+
continue
420+
421+
assert f.constGeometry(), 'Expected geometry, got none'

‎tests/src/python/test_provider_memory.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ def setUpClass(cls):
106106
def tearDownClass(cls):
107107
"""Run after all tests"""
108108

109+
def testGetFeaturesSubsetAttributes2(self):
110+
""" Override and skip this test for memory provider, as it's actually more efficient for the memory provider to return
111+
its features as direct copies (due to implicit sharing of QgsFeature)
112+
"""
113+
pass
114+
115+
def testGetFeaturesNoGeometry(self):
116+
""" Override and skip this test for memory provider, as it's actually more efficient for the memory provider to return
117+
its features as direct copies (due to implicit sharing of QgsFeature)
118+
"""
119+
pass
120+
109121
def testCtors(self):
110122
testVectors = ["Point", "LineString", "Polygon", "MultiPoint", "MultiLineString", "MultiPolygon", "None"]
111123
for v in testVectors:
@@ -317,5 +329,17 @@ def setUpClass(cls):
317329
def tearDownClass(cls):
318330
"""Run after all tests"""
319331

332+
def testGetFeaturesSubsetAttributes2(self):
333+
""" Override and skip this test for memory provider, as it's actually more efficient for the memory provider to return
334+
its features as direct copies (due to implicit sharing of QgsFeature)
335+
"""
336+
pass
337+
338+
def testGetFeaturesNoGeometry(self):
339+
""" Override and skip this test for memory provider, as it's actually more efficient for the memory provider to return
340+
its features as direct copies (due to implicit sharing of QgsFeature)
341+
"""
342+
pass
343+
320344
if __name__ == '__main__':
321345
unittest.main()

0 commit comments

Comments
 (0)