Skip to content
Permalink
Browse files

Improve spatialite provider test

  • Loading branch information
m-kuhn committed Apr 15, 2016
1 parent 1c6b298 commit 8269fc5096885a8844356102fdc83ca5cafcefcb
Showing with 25 additions and 33 deletions.
  1. +25 −33 tests/src/python/test_provider_spatialite.py
@@ -36,10 +36,6 @@
TEST_DATA_DIR = unitTestDataPath()


def die(error_message):
raise Exception(error_message)


class TestQgsSpatialiteProvider(unittest.TestCase, ProviderTestCase):

@classmethod
@@ -144,73 +140,69 @@ def partiallyCompiledFilters(self):
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"""

# 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 """

0 comments on commit 8269fc5

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