|
15 | 15 | import qgis # NOQA
|
16 | 16 |
|
17 | 17 | import os
|
| 18 | +import sys |
18 | 19 | import shutil
|
19 | 20 | import tempfile
|
20 | 21 |
|
|
31 | 32 | print("You should install pyspatialite to run the tests")
|
32 | 33 | raise ImportError
|
33 | 34 |
|
34 |
| -# Convenience instances in case you may need them |
35 |
| -start_app() |
| 35 | +# Pass no_exit=True: for some reason this crashes on exit on Travis MacOSX |
| 36 | +start_app(sys.platform != 'darwin') |
36 | 37 | TEST_DATA_DIR = unitTestDataPath()
|
37 | 38 |
|
38 | 39 |
|
39 |
| -def die(error_message): |
40 |
| - raise Exception(error_message) |
41 |
| - |
42 |
| - |
43 | 40 | class TestQgsSpatialiteProvider(unittest.TestCase, ProviderTestCase):
|
44 | 41 |
|
45 | 42 | @classmethod
|
@@ -144,73 +141,69 @@ def partiallyCompiledFilters(self):
|
144 | 141 | def test_SplitFeature(self):
|
145 | 142 | """Create spatialite database"""
|
146 | 143 | layer = QgsVectorLayer("dbname=%s table=test_pg (geometry)" % self.dbname, "test_pg", "spatialite")
|
147 |
| - assert(layer.isValid()) |
148 |
| - assert(layer.hasGeometryType()) |
| 144 | + self.assertTrue(layer.isValid()) |
| 145 | + self.assertTrue(layer.hasGeometryType()) |
149 | 146 | layer.startEditing()
|
150 |
| - layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0) == 0 or die("error in split") |
151 |
| - layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0) == 0 or die("error in split") |
152 |
| - if not layer.commitChanges(): |
153 |
| - die("this commit should work") |
154 |
| - layer.featureCount() == 4 or die("we should have 4 features after 2 split") |
| 147 | + self.assertEqual(layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0), 0) |
| 148 | + self.assertEqual(layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0), 0) |
| 149 | + self.assertTrue(layer.commitChanges()) |
| 150 | + self.assertEqual(layer.featureCount(), 4) |
155 | 151 |
|
156 | 152 | def xtest_SplitFeatureWithFailedCommit(self):
|
157 | 153 | """Create spatialite database"""
|
158 | 154 | layer = QgsVectorLayer("dbname=%s table=test_pg_mk (geometry)" % self.dbname, "test_pg_mk", "spatialite")
|
159 |
| - assert(layer.isValid()) |
160 |
| - assert(layer.hasGeometryType()) |
| 155 | + self.assertTrue(layer.isValid()) |
| 156 | + self.assertTrue(layer.hasGeometryType()) |
161 | 157 | layer.startEditing()
|
162 |
| - layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0) == 0 or die("error in split") |
163 |
| - layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0) == 0 or die("error in split") |
164 |
| - if layer.commitChanges(): |
165 |
| - die("this commit should fail") |
| 158 | + self.asserEqual(layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0), 0) |
| 159 | + self.asserEqual(layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0), 0) |
| 160 | + self.assertFalse(layer.commitChanges()) |
166 | 161 | layer.rollBack()
|
167 |
| - feat = QgsFeature() |
168 |
| - it = layer.getFeatures() |
169 |
| - it.nextFeature(feat) |
| 162 | + feat = next(layer.getFeatures()) |
170 | 163 | ref = [[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]]
|
171 | 164 | res = feat.geometry().asPolygon()
|
172 | 165 | for ring1, ring2 in zip(ref, res):
|
173 | 166 | for p1, p2 in zip(ring1, ring2):
|
174 | 167 | for c1, c2 in zip(p1, p2):
|
175 |
| - c1 == c2 or die("polygon has been altered by failed edition") |
| 168 | + self.asserEqual(c1, c2) |
176 | 169 |
|
177 | 170 | def test_queries(self):
|
178 | 171 | """Test loading of query-based layers"""
|
179 | 172 |
|
180 | 173 | # a query with a geometry, but no unique id
|
181 | 174 | # the id will be autoincremented
|
182 | 175 | l = QgsVectorLayer("dbname=%s table='(select * from test_q)' (geometry)" % self.dbname, "test_pg_query1", "spatialite")
|
183 |
| - assert(l.isValid()) |
| 176 | + self.assertTrue(l.isValid()) |
184 | 177 | # the id() is autoincremented
|
185 | 178 | sum_id1 = sum(f.id() for f in l.getFeatures())
|
186 | 179 | # the attribute 'id' works
|
187 | 180 | sum_id2 = sum(f.attributes()[0] for f in l.getFeatures())
|
188 |
| - assert(sum_id1 == 3) # 1+2 |
189 |
| - assert(sum_id2 == 32) # 11 + 21 |
| 181 | + self.assertEqual(sum_id1, 3) # 1+2 |
| 182 | + self.assertEqual(sum_id2, 32) # 11 + 21 |
190 | 183 |
|
191 | 184 | # and now with an id declared
|
192 | 185 | l = QgsVectorLayer("dbname=%s table='(select * from test_q)' (geometry) key='id'" % self.dbname, "test_pg_query1", "spatialite")
|
193 |
| - assert(l.isValid()) |
| 186 | + self.assertTrue(l.isValid()) |
194 | 187 | sum_id1 = sum(f.id() for f in l.getFeatures())
|
195 | 188 | sum_id2 = sum(f.attributes()[0] for f in l.getFeatures())
|
196 |
| - assert(sum_id1 == 32) |
197 |
| - assert(sum_id2 == 32) |
| 189 | + self.assertEqual(sum_id1, 32) |
| 190 | + self.assertEqual(sum_id2, 32) |
198 | 191 |
|
199 | 192 | # a query, but no geometry
|
200 | 193 | l = QgsVectorLayer("dbname=%s table='(select id,name from test_q)' key='id'" % self.dbname, "test_pg_query1", "spatialite")
|
201 |
| - assert(l.isValid()) |
| 194 | + self.assertTrue(l.isValid()) |
202 | 195 | sum_id1 = sum(f.id() for f in l.getFeatures())
|
203 | 196 | sum_id2 = sum(f.attributes()[0] for f in l.getFeatures())
|
204 |
| - assert(sum_id1 == 32) |
205 |
| - assert(sum_id2 == 32) |
| 197 | + self.assertEqual(sum_id1, 32) |
| 198 | + self.assertEqual(sum_id2, 32) |
206 | 199 |
|
207 | 200 | def test_case(self):
|
208 | 201 | """Test case sensitivity issues"""
|
209 | 202 | l = QgsVectorLayer("dbname=%s table='test_n' (geometry) key='id'" % self.dbname, "test_n1", "spatialite")
|
210 |
| - assert(l.isValid()) |
211 |
| - assert(l.dataProvider().fields().count() == 2) |
| 203 | + self.assertTrue(l.isValid()) |
| 204 | + self.assertEqual(l.dataProvider().fields().count(), 2) |
212 | 205 | fields = [f.name() for f in l.dataProvider().fields()]
|
213 |
| - assert('Geometry' not in fields) |
| 206 | + self.assertTrue('Geometry' not in fields) |
214 | 207 |
|
215 | 208 | def test_invalid_iterator(self):
|
216 | 209 | """ Test invalid iterator """
|
|
0 commit comments