Skip to content

Commit

Permalink
Restore virtual layer tests in PyQt5
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Sep 5, 2016
1 parent 7e6d641 commit b23e58a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 54 deletions.
2 changes: 0 additions & 2 deletions ci/travis/linux/qt5/blacklist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ PyQgsPalLabelingServer
PyQgsServer
PyQgsServerAccessControl
PyQgsSipCoverage
PyQgsVirtualLayerDefinition
PyQgsVirtualLayerProvider
qgis_composermapgridtest
qgis_composerutils
ProcessingGrass7AlgorithmsImageryTest
Expand Down
90 changes: 48 additions & 42 deletions tests/src/python/test_provider_virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
TEST_DATA_DIR = unitTestDataPath()


def toPercent(s):
return bytes(QUrl.toPercentEncoding(s)).decode()


class TestQgsVirtualLayerProvider(unittest.TestCase, ProviderTestCase):

@classmethod
Expand Down Expand Up @@ -137,7 +141,7 @@ def test_DynamicGeometry(self):
self.assertEqual(l1.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l1)

query = QUrl.toPercentEncoding("select *,makepoint(x,y) as geom from vtab1")
query = toPercent("select *,makepoint(x,y) as geom from vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&geometry=geom:point:0&uid=id" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)

Expand All @@ -163,7 +167,7 @@ def test_Query(self):
QgsMapLayerRegistry.instance().addMapLayer(l1)
ref_sum = sum(f.attributes()[0] for f in l1.getFeatures())

query = QUrl.toPercentEncoding("SELECT * FROM vtab1")
query = toPercent("SELECT * FROM vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&geometry=geometry:3:4326&query=%s&uid=OBJECTID" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3)
Expand All @@ -187,7 +191,7 @@ def test_Query(self):
self.assertEqual(ref_sum, ref_sum3)

# with two geometry columns
query = QUrl.toPercentEncoding("SELECT *,geometry as geom FROM vtab1")
query = toPercent("SELECT *,geometry as geom FROM vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=OBJECTID&geometry=geom:3:4326" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 3)
Expand All @@ -210,7 +214,7 @@ def test_Query(self):
self.assertEqual(ref_sum, ref_sum3)

# the same, without geometry
query = QUrl.toPercentEncoding("SELECT * FROM ww")
query = toPercent("SELECT * FROM ww")
l2 = QgsVectorLayer("?layer_ref=%s:ww&query=%s&uid=ObJeCtId&nogeometry" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry
Expand All @@ -230,7 +234,7 @@ def test_QueryUrlEncoding(self):
self.assertEqual(l1.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l1)

query = str(QUrl.toPercentEncoding("SELECT * FROM vtab1"))
query = toPercent("SELECT * FROM vtab1")
l2 = QgsVectorLayer("?layer_ref=%s&query=%s&uid=ObjectId&nogeometry" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)

Expand All @@ -241,7 +245,7 @@ def test_QueryTableName(self):
self.assertEqual(l1.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l1)

query = str(QUrl.toPercentEncoding("SELECT * FROM vt"))
query = toPercent("SELECT * FROM vt")
l2 = QgsVectorLayer("?layer_ref=%s:vt&query=%s&uid=ObJeCtId&nogeometry" % (l1.id(), query), "vtab", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry
Expand All @@ -258,7 +262,7 @@ def test_Join(self):
ref_sum = sum(f.attributes()[1] for f in l2.getFeatures())

# use a temporary file
query = QUrl.toPercentEncoding("select id,Pilots,vtab1.geometry from vtab1,vtab2 where intersects(vtab1.geometry,vtab2.geometry)")
query = toPercent("select id,Pilots,vtab1.geometry from vtab1,vtab2 where intersects(vtab1.geometry,vtab2.geometry)")
l3 = QgsVectorLayer("?layer_ref=%s&layer_ref=%s&uid=id&query=%s&geometry=geometry:1:4326" % (l1.id(), l2.id(), query), "vtab", "virtual", False)
self.assertEqual(l3.isValid(), True)
self.assertEqual(l3.dataProvider().wkbType(), 1)
Expand Down Expand Up @@ -296,25 +300,25 @@ def test_geometryTypes(self):
QgsMapLayerRegistry.instance().removeMapLayer(l.id())

def test_embeddedLayer(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
l = QgsVectorLayer("?layer=ogr:%s" % source, "vtab", "virtual", False)
self.assertEqual(l.isValid(), True)

l = QgsVectorLayer("?layer=ogr:%s:nn" % source, "vtab", "virtual", False)
self.assertEqual(l.isValid(), True)

def test_filter_rect(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))

query = QUrl.toPercentEncoding("select * from vtab where _search_frame_=BuildMbr(-2.10,49.38,-1.3,49.99,4326)")
query = toPercent("select * from vtab where _search_frame_=BuildMbr(-2.10,49.38,-1.3,49.99,4326)")
l2 = QgsVectorLayer("?layer=ogr:%s:vtab&query=%s&uid=objectid" % (source, query), "vtab2", "virtual", False)
self.assertEqual(l2.isValid(), True)
self.assertEqual(l2.dataProvider().featureCount(), 1)
a = [fit.attributes()[4] for fit in l2.getFeatures()]
self.assertEqual(a, [u"Basse-Normandie"])

def test_recursiveLayer(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
l = QgsVectorLayer("?layer=ogr:%s" % source, "vtab", "virtual", False)
self.assertEqual(l.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l)
Expand All @@ -333,7 +337,7 @@ def test_no_geometry(self):
self.assertEqual(l2.dataProvider().wkbType(), 100) # NoGeometry

def test_reopen(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
l = QgsVectorLayer("%s?layer=ogr:%s:vtab" % (tmp, source), "vtab2", "virtual", False)
self.assertEqual(l.isValid(), True)
Expand All @@ -344,7 +348,7 @@ def test_reopen(self):
self.assertEqual(l2.dataProvider().featureCount(), 4)

def test_reopen2(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
l = QgsVectorLayer("%s?layer=ogr:%s:vtab&nogeometry" % (tmp, source), "vtab2", "virtual", False)
self.assertEqual(l.isValid(), True)
Expand All @@ -355,9 +359,9 @@ def test_reopen2(self):
self.assertEqual(l2.dataProvider().featureCount(), 4)

def test_reopen3(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
query = QUrl.toPercentEncoding("SELECT * FROM vtab")
query = toPercent("SELECT * FROM vtab")
l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&geometry=geometry:3:4326" % (tmp, source, query), "vtab2", "virtual", False)
self.assertEqual(l.isValid(), True)

Expand All @@ -371,9 +375,9 @@ def test_reopen3(self):
self.assertEqual(suma, 3064.0)

def test_reopen4(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString()
query = QUrl.toPercentEncoding("SELECT * FROM vtab")
query = toPercent("SELECT * FROM vtab")
l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&nogeometry" % (tmp, source, query), "vtab2", "virtual", False)
self.assertEqual(l.isValid(), True)

Expand Down Expand Up @@ -406,7 +410,7 @@ def test_refLayers(self):

# cf qgis bug #12266
for i in range(10):
q = QUrl.toPercentEncoding("select * from t" + str(i))
q = toPercent("select * from t" + str(i))
l2 = QgsVectorLayer("?layer_ref=%s:t%d&query=%s&uid=id" % (l1.id(), i, q), "vtab", "virtual", False)
QgsMapLayerRegistry.instance().addMapLayer(l2)
self.assertEqual(l2.isValid(), True)
Expand Down Expand Up @@ -441,7 +445,7 @@ def test_sql2(self):
self.assertEqual(l2.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l2)

query = QUrl.toPercentEncoding("SELECT * FROM france_parts")
query = toPercent("SELECT * FROM france_parts")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual")
self.assertEqual(l4.isValid(), True)

Expand All @@ -457,7 +461,7 @@ def test_sql2(self):
self.assertEqual(n, 1)

# use uid
query = QUrl.toPercentEncoding("SELECT * FROM france_parts")
query = toPercent("SELECT * FROM france_parts")
l5 = QgsVectorLayer("?query=%s&geometry=geometry:polygon:4326&uid=ObjectId" % query, "tt", "virtual")
self.assertEqual(l5.isValid(), True)

Expand Down Expand Up @@ -499,14 +503,14 @@ def test_sql3(self):
QgsMapLayerRegistry.instance().addMapLayer(l2)

# unnamed column
query = QUrl.toPercentEncoding("SELECT count(*)")
query = toPercent("SELECT count(*)")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "count(*)")
self.assertEqual(l4.dataProvider().fields().at(0).type(), QVariant.Int)

def test_sql_field_types(self):
query = QUrl.toPercentEncoding("SELECT 42 as t, 'ok'||'ok' as t2, GeomFromText('') as t3, 3.14*2 as t4")
query = toPercent("SELECT 42 as t, 'ok'||'ok' as t2, GeomFromText('') as t3, 3.14*2 as t4")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "t")
Expand All @@ -519,7 +523,7 @@ def test_sql_field_types(self):
self.assertEqual(l4.dataProvider().fields().at(3).type(), QVariant.Double)

# with type annotations
query = QUrl.toPercentEncoding("SELECT '42.0' as t /*:real*/, 3 as t2/*:text */, GeomFromText('') as t3 /*:multiPoInT:4326 */, 3.14*2 as t4/*:int*/")
query = toPercent("SELECT '42.0' as t /*:real*/, 3 as t2/*:text */, GeomFromText('') as t3 /*:multiPoInT:4326 */, 3.14*2 as t4/*:int*/")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "t")
Expand All @@ -537,13 +541,13 @@ def test_sql_field_types(self):
self.assertEqual(f.attributes()[2], 6.28)

# with type annotations and url options
query = QUrl.toPercentEncoding("SELECT 1 as id /*:int*/, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/")
query = toPercent("SELECT 1 as id /*:int*/, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/")
l4 = QgsVectorLayer("?query=%s&geometry=geometry" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1) # point

# with type annotations and url options (2)
query = QUrl.toPercentEncoding("SELECT 1 as id /*:int*/, 3.14 as f, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/")
query = toPercent("SELECT 1 as id /*:int*/, 3.14 as f, geomfromtext('point(0 0)',4326) as geometry/*:point:4326*/")
l4 = QgsVectorLayer("?query=%s&geometry=geometry&field=id:text" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().fields().at(0).name(), "id")
Expand All @@ -553,18 +557,18 @@ def test_sql_field_types(self):
self.assertEqual(l4.dataProvider().wkbType(), 1) # point

def test_sql3b(self):
query = QUrl.toPercentEncoding("SELECT GeomFromText('POINT(0 0)') as geom")
query = toPercent("SELECT GeomFromText('POINT(0 0)') as geom")
l4 = QgsVectorLayer("?query=%s&geometry=geom" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1)

# forced geometry type
query = QUrl.toPercentEncoding("SELECT GeomFromText('POINT(0 0)') as geom")
query = toPercent("SELECT GeomFromText('POINT(0 0)') as geom")
l4 = QgsVectorLayer("?query=%s&geometry=geom:point:0" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1)

query = QUrl.toPercentEncoding("SELECT CastToPoint(GeomFromText('POINT(0 0)')) as geom")
query = toPercent("SELECT CastToPoint(GeomFromText('POINT(0 0)')) as geom")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
self.assertEqual(l4.dataProvider().wkbType(), 1)
Expand All @@ -574,7 +578,7 @@ def test_sql4(self):
self.assertEqual(l2.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l2)

query = QUrl.toPercentEncoding("SELECT OBJECTId from france_parts")
query = toPercent("SELECT OBJECTId from france_parts")
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
s = sum(f.attributes()[0] for f in l4.getFeatures())
Expand All @@ -586,15 +590,15 @@ def test_layer_name(self):
self.assertEqual(l2.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l2)

query = QUrl.toPercentEncoding('SELECT OBJECTId from "FranCe parts"')
query = toPercent('SELECT OBJECTId from "FranCe parts"')
l4 = QgsVectorLayer("?query=%s" % query, "tt", "virtual", False)
self.assertEqual(l4.isValid(), True)
s = sum(f.attributes()[0] for f in l4.getFeatures())
self.assertEqual(s, 10659)

def test_encoding(self):
# changes encoding on a shapefile (the only provider supporting setEncoding)
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "shp_latin1.dbf"))
source = toPercent(os.path.join(self.testDataDir, "shp_latin1.dbf"))
l = QgsVectorLayer("?layer=ogr:%s:fp:latin1" % source, "vtab", "virtual", False)
self.assertEqual(l.isValid(), True)

Expand All @@ -608,30 +612,30 @@ def test_encoding(self):
self.assertEqual(f.attributes()[1], u"accents \ufffd\ufffd\ufffd") # invalid unicode characters

def test_rowid(self):
source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp"))
query = QUrl.toPercentEncoding("select rowid as uid, * from vtab limit 1 offset 3")
source = toPercent(os.path.join(self.testDataDir, "france_parts.shp"))
query = toPercent("select rowid as uid, * from vtab limit 1 offset 3")
l = QgsVectorLayer("?layer=ogr:%s:vtab&query=%s" % (source, query), "vtab2", "virtual", False)
# the last line must have a fixed rowid (not an autoincrement)
for f in l.getFeatures():
lid = f.attributes()[0]
self.assertEqual(lid, 3)

def test_geometry_conversion(self):
query = QUrl.toPercentEncoding("select geomfromtext('multipoint((0 0),(1 1))') as geom")
query = toPercent("select geomfromtext('multipoint((0 0),(1 1))') as geom")
l = QgsVectorLayer("?query=%s&geometry=geom:multipoint:0" % query, "tt", "virtual", False)
self.assertEqual(l.isValid(), True)
for f in l.getFeatures():
self.assertEqual(f.geometry().exportToWkt().lower().startswith("multipoint"), True)
self.assertEqual("),(" in f.geometry().exportToWkt(), True) # has two points

query = QUrl.toPercentEncoding("select geomfromtext('multipolygon(((0 0,1 0,1 1,0 1,0 0)),((0 1,1 1,1 2,0 2,0 1)))') as geom")
query = toPercent("select geomfromtext('multipolygon(((0 0,1 0,1 1,0 1,0 0)),((0 1,1 1,1 2,0 2,0 1)))') as geom")
l = QgsVectorLayer("?query=%s&geometry=geom:multipolygon:0" % query, "tt", "virtual", False)
self.assertEqual(l.isValid(), True)
for f in l.getFeatures():
self.assertEqual(f.geometry().exportToWkt().lower().startswith("multipolygon"), True)
self.assertEqual(")),((" in f.geometry().exportToWkt(), True) # has two polygons

query = QUrl.toPercentEncoding("select geomfromtext('multilinestring((0 0,1 0,1 1,0 1,0 0),(0 1,1 1,1 2,0 2,0 1))') as geom")
query = toPercent("select geomfromtext('multilinestring((0 0,1 0,1 1,0 1,0 0),(0 1,1 1,1 2,0 2,0 1))') as geom")
l = QgsVectorLayer("?query=%s&geometry=geom:multilinestring:0" % query, "tt", "virtual", False)
self.assertEqual(l.isValid(), True)
for f in l.getFeatures():
Expand Down Expand Up @@ -668,21 +672,23 @@ def test_ProjectDependencies(self):
self.assertEqual(l1.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l1)

query = QUrl.toPercentEncoding("SELECT * FROM france_parts")
query = toPercent("SELECT * FROM france_parts")
l2 = QgsVectorLayer("?query=%s" % query, "aa", "virtual", False)
self.assertEqual(l2.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l2)

self.assertEqual(len(l2.dependencies()), 1)
self.assertEqual(l2.dependencies()[0].layerId().startswith('france_parts'), True)
ll0 = l2.dependencies().pop()
self.assertEqual(ll0.layerId().startswith('france_parts'), True)

query = QUrl.toPercentEncoding("SELECT t1.objectid, t2.name_0 FROM france_parts as t1, aa as t2")
query = toPercent("SELECT t1.objectid, t2.name_0 FROM france_parts as t1, aa as t2")
l3 = QgsVectorLayer("?query=%s" % query, "bb", "virtual", False)
self.assertEqual(l3.isValid(), True)
QgsMapLayerRegistry.instance().addMapLayer(l3)

self.assertEqual(len(l2.dependencies()), 1)
self.assertEqual(l2.dependencies()[0].layerId().startswith('france_parts'), True)
ll0 = l2.dependencies().pop()
self.assertEqual(ll0.layerId().startswith('france_parts'), True)

self.assertEqual(len(l3.dependencies()), 2)

Expand All @@ -691,7 +697,7 @@ def test_ProjectDependencies(self):
QgsProject.instance().setFileName(temp)
QgsProject.instance().write()

QgsMapLayerRegistry.instance().removeMapLayers([l1, l2])
QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])
QgsProject.instance().clear()

QgsProject.instance().setFileName(temp)
Expand Down Expand Up @@ -795,7 +801,7 @@ def test_joined_layers_conversion(self):
'LEFT JOIN {} AS j1 ON t."b_id"=j1."id" ' +
'LEFT JOIN {} AS j2 ON t."c_id"=j2."id"').format(v1.id(), v2.id(), v3.id()))

QgsMapLayerRegistry.instance().removeMapLayers([v1, v2, v3])
QgsMapLayerRegistry.instance().removeMapLayers([v1.id(), v2.id(), v3.id()])


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit b23e58a

Please sign in to comment.