"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tests/src/python/test_provider_virtual.py" between
QGIS-final-3_14_15.tar.gz and QGIS-final-3_14_16.tar.gz

About: QGIS (Quantum GIS) is a Geographic Information System that supports vector, raster, and database formats. Latest release (3.14.x series).

test_provider_virtual.py  (QGIS-final-3_14_15):test_provider_virtual.py  (QGIS-final-3_14_16)
skipping to change at line 26 skipping to change at line 26
from qgis.core import (QgsVectorLayer, from qgis.core import (QgsVectorLayer,
QgsField, QgsField,
QgsFeature, QgsFeature,
QgsFeatureRequest, QgsFeatureRequest,
QgsGeometry, QgsGeometry,
QgsRectangle, QgsRectangle,
QgsVirtualLayerDefinition, QgsVirtualLayerDefinition,
QgsVirtualLayerDefinitionUtils, QgsVirtualLayerDefinitionUtils,
QgsWkbTypes, QgsWkbTypes,
QgsProject, QgsProject,
QgsVectorLayerJoinInfo QgsVectorLayerJoinInfo,
QgsVectorFileWriter,
QgsVirtualLayerDefinitionUtils
) )
from qgis.testing import start_app, unittest from qgis.testing import start_app, unittest
from utilities import unitTestDataPath from utilities import unitTestDataPath
from providertestbase import ProviderTestCase from providertestbase import ProviderTestCase
from qgis.PyQt.QtCore import QUrl, QVariant from qgis.PyQt.QtCore import QUrl, QVariant, QTemporaryDir
from qgis.utils import spatialite_connect from qgis.utils import spatialite_connect
import tempfile import tempfile
# Convenience instances in case you may need them # Convenience instances in case you may need them
start_app() start_app()
TEST_DATA_DIR = unitTestDataPath() TEST_DATA_DIR = unitTestDataPath()
def toPercent(s): def toPercent(s):
skipping to change at line 834 skipping to change at line 836
l1 = QgsVectorLayer(df.toString(), "vtab", "virtual", QgsVectorLayer.Lay erOptions(False)) l1 = QgsVectorLayer(df.toString(), "vtab", "virtual", QgsVectorLayer.Lay erOptions(False))
self.assertEqual(l1.isValid(), True) self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1) QgsProject.instance().addMapLayer(l1)
temp = os.path.join(self.testDataDir, "qgstestproject_relative_path_test .qgs") temp = os.path.join(self.testDataDir, "qgstestproject_relative_path_test .qgs")
QgsProject.instance().setFileName(temp) QgsProject.instance().setFileName(temp)
QgsProject.instance().write() QgsProject.instance().write()
QgsProject.instance().removeAllMapLayers()
QgsProject.instance().clear() QgsProject.instance().clear()
self.assertEqual(len(QgsProject.instance().mapLayers()), 0) self.assertEqual(len(QgsProject.instance().mapLayers()), 0)
# Check that virtual layer source is stored with relative path # Check that virtual layer source is stored with relative path
percent_path_relative = toPercent("./france_parts.shp") percent_path_relative = toPercent("./france_parts.shp")
with open(temp, 'r') as f: with open(temp, 'r') as f:
content = ''.join(f.readlines()) content = ''.join(f.readlines())
print(content)
self.assertTrue('<datasource>?layer=ogr:{}'.format(percent_path_rela tive) in content) self.assertTrue('<datasource>?layer=ogr:{}'.format(percent_path_rela tive) in content)
# Check that project is correctly re-read with all layers # Check that project is correctly re-read with all layers
QgsProject.instance().setFileName(temp) QgsProject.instance().setFileName(temp)
QgsProject.instance().read() QgsProject.instance().read()
print(QgsProject.instance().mapLayers()) print(QgsProject.instance().mapLayers())
self.assertEqual(len(QgsProject.instance().mapLayers()), 2) self.assertEqual(len(QgsProject.instance().mapLayers()), 2)
# Store absolute # Store absolute
QgsProject.instance().writeEntryBool('Paths', '/Absolute', True) QgsProject.instance().writeEntryBool('Paths', '/Absolute', True)
QgsProject.instance().write() QgsProject.instance().write()
QgsProject.instance().removeAllMapLayers()
QgsProject.instance().clear() QgsProject.instance().clear()
self.assertEqual(len(QgsProject.instance().mapLayers()), 0) self.assertEqual(len(QgsProject.instance().mapLayers()), 0)
# Check that virtual layer source is stored with absolute path # Check that virtual layer source is stored with absolute path
percent_path_absolute = toPercent(os.path.join(self.testDataDir, "france _parts.shp")) percent_path_absolute = toPercent(os.path.join(self.testDataDir, "france _parts.shp"))
with open(temp, 'r') as f: with open(temp, 'r') as f:
content = ''.join(f.readlines()) content = ''.join(f.readlines())
self.assertTrue('<datasource>?layer=ogr:{}'.format(percent_path_abso lute) in content) self.assertTrue('<datasource>?layer=ogr:{}'.format(percent_path_abso lute) in content)
# Check that project is correctly re-read with all layers # Check that project is correctly re-read with all layers
skipping to change at line 1165 skipping to change at line 1164
# Take an extent where east farthest point is excluded and second farthe st east is on the edge # Take an extent where east farthest point is excluded and second farthe st east is on the edge
# and should be returned # and should be returned
extent = QgsRectangle(-117.23257418909581418, 22.80020703933767834, -85. 6521739130433276, 46.87198067632875365) extent = QgsRectangle(-117.23257418909581418, 22.80020703933767834, -85. 6521739130433276, 46.87198067632875365)
r = QgsFeatureRequest(extent) r = QgsFeatureRequest(extent)
features = [feature for feature in vl.getFeatures(r)] features = [feature for feature in vl.getFeatures(r)]
self.assertEqual(len(features), 16) self.assertEqual(len(features), 16)
QgsProject.instance().removeMapLayer(pl) QgsProject.instance().removeMapLayer(pl)
def test_subset_string(self):
"""Test that subset strings are stored and restored correctly from the p
roject
See: GH #26189
"""
project = QgsProject.instance()
project.clear()
data_layer = QgsVectorLayer('Point?crs=epsg:4326&field=fid:integer&field
=value:integer&field=join_pk:integer', 'data', 'memory')
join_layer = QgsVectorLayer('NoGeometry?field=fid:integer&field=value:st
ring', 'join', 'memory')
tempdir = QTemporaryDir()
gpkg_path = os.path.join(tempdir.path(), 'test_subset.gpkg')
project_path = os.path.join(tempdir.path(), 'test_subset.qgs')
self.assertTrue(data_layer.isValid())
self.assertTrue(join_layer.isValid())
self.assertFalse(join_layer.isSpatial())
f = QgsFeature(data_layer.fields())
f.setAttributes([1, 20, 2])
f.setGeometry(QgsGeometry.fromWkt('point(9 45'))
self.assertTrue(data_layer.dataProvider().addFeature(f))
f = QgsFeature(data_layer.fields())
f.setAttributes([2, 10, 1])
f.setGeometry(QgsGeometry.fromWkt('point(9 45'))
self.assertTrue(data_layer.dataProvider().addFeature(f))
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = 'GPKG'
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile
options.layerName = 'data'
_, _ = QgsVectorFileWriter.writeAsVectorFormatV2(
data_layer,
gpkg_path,
data_layer.transformContext(),
options
)
f = QgsFeature(join_layer.fields())
f.setAttributes([1, "ten"])
self.assertTrue(join_layer.dataProvider().addFeature(f))
f.setAttributes([2, "twenty"])
self.assertTrue(join_layer.dataProvider().addFeature(f))
options.layerName = 'join'
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLaye
r
_, _ = QgsVectorFileWriter.writeAsVectorFormatV2(
join_layer,
gpkg_path,
join_layer.transformContext(),
options
)
gpkg_join_layer = QgsVectorLayer(gpkg_path + '|layername=join', 'join',
'ogr')
gpkg_data_layer = QgsVectorLayer(gpkg_path + '|layername=data', 'data',
'ogr')
self.assertTrue(gpkg_join_layer.isValid())
self.assertTrue(gpkg_data_layer.isValid())
self.assertEqual(gpkg_data_layer.featureCount(), 2)
self.assertEqual(gpkg_join_layer.featureCount(), 2)
self.assertTrue(project.addMapLayers([gpkg_data_layer, gpkg_join_layer])
)
joinInfo = QgsVectorLayerJoinInfo()
joinInfo.setTargetFieldName("join_pk")
joinInfo.setJoinLayer(gpkg_join_layer)
joinInfo.setJoinFieldName("fid")
gpkg_data_layer.addJoin(joinInfo)
self.assertEqual(len(gpkg_data_layer.fields()), 4)
self.assertTrue(project.write(project_path))
# Reload project
self.assertTrue(project.read(project_path))
gpkg_data_layer = project.mapLayersByName('data')[0]
gpkg_join_layer = project.mapLayersByName('join')[0]
self.assertEqual(gpkg_data_layer.vectorJoins()[0], joinInfo)
# Now set a subset filter -> virtual layer
virtual_def = QgsVirtualLayerDefinitionUtils.fromJoinedLayer(gpkg_data_l
ayer)
virtual = QgsVectorLayer(virtual_def.toString(), "virtual_data", "virtua
l")
self.assertTrue(virtual.isValid())
project.addMapLayers([virtual])
self.assertEqual(virtual.featureCount(), 2)
self.assertTrue(virtual.setSubsetString('"join_value" = \'twenty\''))
self.assertEqual(virtual.featureCount(), 1)
self.assertEqual([f.attributes() for f in virtual.getFeatures()], [[1, 2
0, 2, 'twenty']])
# Store and reload the project
self.assertTrue(project.write(project_path))
self.assertTrue(project.read(project_path))
gpkg_virtual_layer = project.mapLayersByName('virtual_data')[0]
self.assertEqual(gpkg_virtual_layer.featureCount(), 1)
self.assertEqual(gpkg_virtual_layer.subsetString(), '"join_value" = \'tw
enty\'')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
 End of changes. 6 change blocks. 
5 lines changed or deleted 113 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)