mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-26 00:02:08 -05:00
Improve spatialite provider test
This commit is contained in:
parent
1c6b298833
commit
8269fc5096
@ -36,10 +36,6 @@ start_app()
|
||||
TEST_DATA_DIR = unitTestDataPath()
|
||||
|
||||
|
||||
def die(error_message):
|
||||
raise Exception(error_message)
|
||||
|
||||
|
||||
class TestQgsSpatialiteProvider(unittest.TestCase, ProviderTestCase):
|
||||
|
||||
@classmethod
|
||||
@ -144,35 +140,31 @@ class TestQgsSpatialiteProvider(unittest.TestCase, ProviderTestCase):
|
||||
def test_SplitFeature(self):
|
||||
"""Create spatialite database"""
|
||||
layer = QgsVectorLayer("dbname=%s table=test_pg (geometry)" % self.dbname, "test_pg", "spatialite")
|
||||
assert(layer.isValid())
|
||||
assert(layer.hasGeometryType())
|
||||
self.assertTrue(layer.isValid())
|
||||
self.assertTrue(layer.hasGeometryType())
|
||||
layer.startEditing()
|
||||
layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0) == 0 or die("error in split")
|
||||
layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0) == 0 or die("error in split")
|
||||
if not layer.commitChanges():
|
||||
die("this commit should work")
|
||||
layer.featureCount() == 4 or die("we should have 4 features after 2 split")
|
||||
self.assertEqual(layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0), 0)
|
||||
self.assertEqual(layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0), 0)
|
||||
self.assertTrue(layer.commitChanges())
|
||||
self.assertEqual(layer.featureCount(), 4)
|
||||
|
||||
def xtest_SplitFeatureWithFailedCommit(self):
|
||||
"""Create spatialite database"""
|
||||
layer = QgsVectorLayer("dbname=%s table=test_pg_mk (geometry)" % self.dbname, "test_pg_mk", "spatialite")
|
||||
assert(layer.isValid())
|
||||
assert(layer.hasGeometryType())
|
||||
self.assertTrue(layer.isValid())
|
||||
self.assertTrue(layer.hasGeometryType())
|
||||
layer.startEditing()
|
||||
layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0) == 0 or die("error in split")
|
||||
layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0) == 0 or die("error in split")
|
||||
if layer.commitChanges():
|
||||
die("this commit should fail")
|
||||
self.asserEqual(layer.splitFeatures([QgsPoint(0.5, -0.5), QgsPoint(0.5, 1.5)], 0), 0)
|
||||
self.asserEqual(layer.splitFeatures([QgsPoint(-0.5, 0.5), QgsPoint(1.5, 0.5)], 0), 0)
|
||||
self.assertFalse(layer.commitChanges())
|
||||
layer.rollBack()
|
||||
feat = QgsFeature()
|
||||
it = layer.getFeatures()
|
||||
it.nextFeature(feat)
|
||||
feat = next(layer.getFeatures())
|
||||
ref = [[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]]
|
||||
res = feat.geometry().asPolygon()
|
||||
for ring1, ring2 in zip(ref, res):
|
||||
for p1, p2 in zip(ring1, ring2):
|
||||
for c1, c2 in zip(p1, p2):
|
||||
c1 == c2 or die("polygon has been altered by failed edition")
|
||||
self.asserEqual(c1, c2)
|
||||
|
||||
def test_queries(self):
|
||||
"""Test loading of query-based layers"""
|
||||
@ -180,37 +172,37 @@ class TestQgsSpatialiteProvider(unittest.TestCase, ProviderTestCase):
|
||||
# a query with a geometry, but no unique id
|
||||
# the id will be autoincremented
|
||||
l = QgsVectorLayer("dbname=%s table='(select * from test_q)' (geometry)" % self.dbname, "test_pg_query1", "spatialite")
|
||||
assert(l.isValid())
|
||||
self.assertTrue(l.isValid())
|
||||
# the id() is autoincremented
|
||||
sum_id1 = sum(f.id() for f in l.getFeatures())
|
||||
# the attribute 'id' works
|
||||
sum_id2 = sum(f.attributes()[0] for f in l.getFeatures())
|
||||
assert(sum_id1 == 3) # 1+2
|
||||
assert(sum_id2 == 32) # 11 + 21
|
||||
self.assertEqual(sum_id1, 3) # 1+2
|
||||
self.assertEqual(sum_id2, 32) # 11 + 21
|
||||
|
||||
# and now with an id declared
|
||||
l = QgsVectorLayer("dbname=%s table='(select * from test_q)' (geometry) key='id'" % self.dbname, "test_pg_query1", "spatialite")
|
||||
assert(l.isValid())
|
||||
self.assertTrue(l.isValid())
|
||||
sum_id1 = sum(f.id() for f in l.getFeatures())
|
||||
sum_id2 = sum(f.attributes()[0] for f in l.getFeatures())
|
||||
assert(sum_id1 == 32)
|
||||
assert(sum_id2 == 32)
|
||||
self.assertEqual(sum_id1, 32)
|
||||
self.assertEqual(sum_id2, 32)
|
||||
|
||||
# a query, but no geometry
|
||||
l = QgsVectorLayer("dbname=%s table='(select id,name from test_q)' key='id'" % self.dbname, "test_pg_query1", "spatialite")
|
||||
assert(l.isValid())
|
||||
self.assertTrue(l.isValid())
|
||||
sum_id1 = sum(f.id() for f in l.getFeatures())
|
||||
sum_id2 = sum(f.attributes()[0] for f in l.getFeatures())
|
||||
assert(sum_id1 == 32)
|
||||
assert(sum_id2 == 32)
|
||||
self.assertEqual(sum_id1, 32)
|
||||
self.assertEqual(sum_id2, 32)
|
||||
|
||||
def test_case(self):
|
||||
"""Test case sensitivity issues"""
|
||||
l = QgsVectorLayer("dbname=%s table='test_n' (geometry) key='id'" % self.dbname, "test_n1", "spatialite")
|
||||
assert(l.isValid())
|
||||
assert(l.dataProvider().fields().count() == 2)
|
||||
self.assertTrue(l.isValid())
|
||||
self.assertEqual(l.dataProvider().fields().count(), 2)
|
||||
fields = [f.name() for f in l.dataProvider().fields()]
|
||||
assert('Geometry' not in fields)
|
||||
self.assertTrue('Geometry' not in fields)
|
||||
|
||||
def test_invalid_iterator(self):
|
||||
""" Test invalid iterator """
|
||||
|
Loading…
x
Reference in New Issue
Block a user