|
14 | 14 |
|
15 | 15 | from qgis.core import QgsRectangle, QgsFeatureRequest, QgsFeature, QgsGeometry, NULL
|
16 | 16 |
|
| 17 | +from utilities import( |
| 18 | + compareWkt |
| 19 | +) |
| 20 | + |
17 | 21 |
|
18 | 22 | class ProviderTestCase(object):
|
19 | 23 |
|
| 24 | + def testGetFeatures(self): |
| 25 | + """ Test that expected results are returned when fetching all features """ |
| 26 | + |
| 27 | + # IMPORTANT - we do not use `for f in provider.getFeatures()` as we are also |
| 28 | + # testing that existing attributes & geometry in f are overwritten correctly |
| 29 | + # (for f in ... uses a new QgsFeature for every iteration) |
| 30 | + |
| 31 | + it = self.provider.getFeatures() |
| 32 | + f = QgsFeature() |
| 33 | + attributes = {} |
| 34 | + geometries = {} |
| 35 | + while it.nextFeature(f): |
| 36 | + # split off the first 5 attributes only - some provider test datasets will include |
| 37 | + # additional attributes which we ignore |
| 38 | + attrs = f.attributes()[0:5] |
| 39 | + # force the num_char attribute to be text - some providers (eg delimited text) will |
| 40 | + # automatically detect that this attribute contains numbers and set it as a numeric |
| 41 | + # field |
| 42 | + attrs[4] = str(attrs[4]) |
| 43 | + attributes[f['pk']] = attrs |
| 44 | + geometries[f['pk']] = f.constGeometry() and f.constGeometry().exportToWkt() |
| 45 | + |
| 46 | + expected_attributes = {5: [5, -200, NULL, 'NuLl', '5'], |
| 47 | + 3: [3, 300, 'Pear', 'PEaR', '3'], |
| 48 | + 1: [1, 100, 'Orange', 'oranGe', '1'], |
| 49 | + 2: [2, 200, 'Apple', 'Apple', '2'], |
| 50 | + 4: [4, 400, 'Honey', 'Honey', '4']} |
| 51 | + self.assertEqual(attributes, expected_attributes, 'Expected {}, got {}'.format(expected_attributes, attributes)) |
| 52 | + |
| 53 | + expected_geometries = {1: 'Point (-70.332 66.33)', |
| 54 | + 2: 'Point (-68.2 70.8)', |
| 55 | + 3: None, |
| 56 | + 4: 'Point(-65.32 78.3)', |
| 57 | + 5: 'Point(-71.123 78.23)'} |
| 58 | + for pk, geom in expected_geometries.iteritems(): |
| 59 | + if geom: |
| 60 | + assert compareWkt(geom, geometries[pk]), "Geometry {} mismatch Expected:\n{}\nGot:\n{}\n".format(pk, geom, geometries[pk].exportToWkt()) |
| 61 | + else: |
| 62 | + self.assertFalse(geometries[pk], 'Expected null geometry for {}'.format(pk)) |
| 63 | + |
20 | 64 | def assert_query(self, provider, expression, expected):
|
21 | 65 | result = set([f['pk'] for f in provider.getFeatures(QgsFeatureRequest().setFilterExpression(expression))])
|
22 | 66 | assert set(expected) == result, 'Expected {} and got {} when testing expression "{}"'.format(set(expected), result, expression)
|
|
0 commit comments