Skip to content

Commit

Permalink
[Qt6] [Tests] Fix PyQgsJsonUtils and PyQgsOGRProvider; re-enable test…
Browse files Browse the repository at this point in the history
…_core_ogcutils and PyQgsProviderRegistry
  • Loading branch information
rouault committed Feb 28, 2024
1 parent 150543c commit 668d7d6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
4 changes: 0 additions & 4 deletions .ci/test_blocklist_qt6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ test_core_compositionconverter
test_core_expression
test_core_labelingengine
test_core_layoutpicture
test_core_ogcutils
test_core_vectortilelayer
test_gui_processinggui
test_app_advanceddigitizing
Expand Down Expand Up @@ -47,7 +46,6 @@ PyQgsEditWidgets
PyQgsElevationProfileCanvas
PyQgsProject
PyQgsFloatingWidget
PyQgsJsonUtils
PyQgsLayoutHtml
PyQgsLineSymbolLayers
PyQgsMapBoxGlStyleConverter
Expand All @@ -59,7 +57,6 @@ PyQgsRasterAttributeTable
PyQgsRasterLayerRenderer
PyQgsShapefileProvider
PyQgsTextRenderer
PyQgsOGRProvider
PyQgsSpatialiteProvider
PyQgsSymbolLayerReadSld
PyQgsVectorLayerCache
Expand All @@ -79,4 +76,3 @@ ProcessingQgisAlgorithmsTestPt3
ProcessingQgisAlgorithmsTestPt4
ProcessingGdalAlgorithmsVectorTest
ProcessingGrassAlgorithmsImageryTest
PyQgsProviderRegistry
32 changes: 16 additions & 16 deletions tests/src/python/test_provider_ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from datetime import datetime

from osgeo import gdal, ogr # NOQA
from qgis.PyQt.QtCore import QByteArray, QTemporaryDir, QVariant
from qgis.PyQt.QtCore import QT_VERSION_STR, QByteArray, QTemporaryDir, QVariant
from qgis.PyQt.QtXml import QDomDocument
from qgis.core import (
NULL,
Expand Down Expand Up @@ -1092,7 +1092,10 @@ def testBoolFieldEvaluation(self):
self.assertEqual(vl.fields().at(0).name(), 'bool')
self.assertEqual(vl.fields().at(0).type(), QVariant.Bool)
self.assertEqual([f[0] for f in vl.getFeatures()], [True, False, NULL])
self.assertEqual([f[0].__class__.__name__ for f in vl.getFeatures()], ['bool', 'bool', 'QVariant'])
if int(QT_VERSION_STR.split('.')[0]) >= 6:
self.assertEqual([f[0].__class__.__name__ for f in vl.getFeatures()], ['bool', 'bool', 'NoneType'])
else:
self.assertEqual([f[0].__class__.__name__ for f in vl.getFeatures()], ['bool', 'bool', 'QVariant'])

def testReloadDataAndFeatureCount(self):

Expand Down Expand Up @@ -1155,8 +1158,7 @@ def testSpatialiteDefaultValues(self):

# Test default values
dp = vl.dataProvider()
# FIXME: should it be None?
self.assertTrue(dp.defaultValue(0).isNull())
self.assertEqual(dp.defaultValue(0), NULL)
self.assertIsNone(dp.defaultValue(1))
# FIXME: This fails because there is no backend-side evaluation in this provider
# self.assertTrue(dp.defaultValue(2).startswith(now.strftime('%Y-%m-%d')))
Expand Down Expand Up @@ -1404,7 +1406,16 @@ def testHTTPRequestsOverrider(self):
# Asked when ogr provider try to open. See QgsOgrProvider::QgsOgrProvider#453 open( OpenModeForceReadOnly );
handler.add('GET', '/collections/foo', 200, {'Content-Type': 'application/json'}, '{ "id": "foo" }')

if int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 9, 0):
# 3.8.3 not necessarily the minimum version
if int(gdal.VersionInfo('VERSION_NUM')) >= GDAL_COMPUTE_VERSION(3, 8, 3):
handler.add('GET', '/', 200, {'Content-Type': 'application/json'}, '{ "id": "foo" }')
handler.add('GET', '/api', 200, {'Content-Type': 'application/json'}, '{ "id": "foo" }')

handler.add('GET', '/collections/foo/items?limit=20', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')
handler.add('GET', '/collections/foo/items?limit=1000', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')
else:
# See QgsOgrProvider::open#4012 mOgrOrigLayer = QgsOgrProviderUtils::getLayer( mFilePath, false, options, mLayerName, errCause, true );
handler.add('GET', '/collections/foo/items?limit=10', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')
Expand All @@ -1417,17 +1428,6 @@ def testHTTPRequestsOverrider(self):
handler.add('GET', '/collections/foo/items?limit=10', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')

else:
handler.add('GET', '/', 200, {'Content-Type': 'application/json'}, '{ "id": "foo" }')
handler.add('GET', '/api', 200, {'Content-Type': 'application/json'}, '{ "id": "foo" }')

handler.add('GET', '/collections/foo/items?limit=20', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')
handler.add('GET', '/collections/foo/items?limit=1000', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')
handler.add('GET', '/collections/foo/items?limit=1000', 200, {'Content-Type': 'application/geo+json'},
'{ "type": "FeatureCollection", "features": [] }')

with mockedwebserver.install_http_handler(handler):
vl = QgsVectorLayer("OAPIF:http://127.0.0.1:%d/collections/foo" % port, 'test', 'ogr')
self.assertTrue(vl.isValid())
Expand Down
3 changes: 1 addition & 2 deletions tests/src/python/test_provider_spatialite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,8 +1561,7 @@ def testSpatialiteDefaultValues(self):

# Test default values
dp = vl.dataProvider()
# FIXME: should it be None?
self.assertTrue(dp.defaultValue(0).isNull())
self.assertEqual(dp.defaultValue(0), NULL)
self.assertIsNone(dp.defaultValue(1))
# FIXME: This fails because there is no backend-side evaluation in this provider
# self.assertTrue(dp.defaultValue(2).startswith(now.strftime('%Y-%m-%d')))
Expand Down
59 changes: 41 additions & 18 deletions tests/src/python/test_qgsjsonutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__date__ = '3/05/2016'
__copyright__ = 'Copyright 2016, The QGIS Project'

from qgis.PyQt.QtCore import QLocale, Qt, QTextCodec, QVariant
from qgis.PyQt.QtCore import QT_VERSION_STR, QLocale, Qt, QVariant
from qgis.core import (
NULL,
QgsCoordinateReferenceSystem,
Expand All @@ -31,7 +31,10 @@
from qgis.testing import start_app, QgisTestCase

start_app()
codec = QTextCodec.codecForName("System")

if int(QT_VERSION_STR.split('.')[0]) < 6:
from qgis.PyQt.QtCore import QTextCodec
codec = QTextCodec.codecForName("System")


class TestQgsJsonUtils(QgisTestCase):
Expand All @@ -42,17 +45,25 @@ def testStringToFeatureList(self):
fields.append(QgsField("name", QVariant.String))

# empty string
features = QgsJsonUtils.stringToFeatureList("", fields, codec)
if int(QT_VERSION_STR.split('.')[0]) >= 6:
features = QgsJsonUtils.stringToFeatureList("", fields)
else:
features = QgsJsonUtils.stringToFeatureList("", fields, codec)
self.assertEqual(features, [])

# bad string
features = QgsJsonUtils.stringToFeatureList("asdasdas", fields, codec)
if int(QT_VERSION_STR.split('.')[0]) >= 6:
features = QgsJsonUtils.stringToFeatureList("asdasdas", fields)
else:
features = QgsJsonUtils.stringToFeatureList("asdasdas", fields, codec)
self.assertEqual(features, [])

# geojson string with 1 feature
features = QgsJsonUtils.stringToFeatureList(
'{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands"}}',
fields, codec)
s = '{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands"}}'
if int(QT_VERSION_STR.split('.')[0]) >= 6:
features = QgsJsonUtils.stringToFeatureList(s, fields)
else:
features = QgsJsonUtils.stringToFeatureList(s, fields, codec)
self.assertEqual(len(features), 1)
self.assertFalse(features[0].geometry().isNull())
self.assertEqual(features[0].geometry().wkbType(), QgsWkbTypes.Type.Point)
Expand All @@ -62,9 +73,11 @@ def testStringToFeatureList(self):
self.assertEqual(features[0]['name'], "Dinagat Islands")

# geojson string with 2 features
features = QgsJsonUtils.stringToFeatureList(
'{ "type": "FeatureCollection","features":[{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands"}}, {\n"type": "Feature","geometry": {"type": "Point","coordinates": [110, 20]},"properties": {"name": "Henry Gale Island"}}]}',
fields, codec)
s = '{ "type": "FeatureCollection","features":[{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands"}}, {\n"type": "Feature","geometry": {"type": "Point","coordinates": [110, 20]},"properties": {"name": "Henry Gale Island"}}]}'
if int(QT_VERSION_STR.split('.')[0]) >= 6:
features = QgsJsonUtils.stringToFeatureList(s, fields)
else:
features = QgsJsonUtils.stringToFeatureList(s, fields, codec)
self.assertEqual(len(features), 2)
self.assertFalse(features[0].geometry().isNull())
self.assertEqual(features[0].geometry().wkbType(), QgsWkbTypes.Type.Point)
Expand Down Expand Up @@ -121,27 +134,37 @@ def testStringToFields(self):
"""test retrieving fields from GeoJSON strings"""

# empty string
fields = QgsJsonUtils.stringToFields("", codec)
if int(QT_VERSION_STR.split('.')[0]) >= 6:
fields = QgsJsonUtils.stringToFields("")
else:
fields = QgsJsonUtils.stringToFields("", codec)
self.assertEqual(fields.count(), 0)

# bad string
fields = QgsJsonUtils.stringToFields("asdasdas", codec)
if int(QT_VERSION_STR.split('.')[0]) >= 6:
fields = QgsJsonUtils.stringToFields("asdasdas")
else:
fields = QgsJsonUtils.stringToFields("asdasdas", codec)
self.assertEqual(fields.count(), 0)

# geojson string
fields = QgsJsonUtils.stringToFields(
'{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands","height":5.5}}',
codec)
s = '{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands","height":5.5}}'
if int(QT_VERSION_STR.split('.')[0]) >= 6:
fields = QgsJsonUtils.stringToFields(s)
else:
fields = QgsJsonUtils.stringToFields(s, codec)
self.assertEqual(fields.count(), 2)
self.assertEqual(fields[0].name(), "name")
self.assertEqual(fields[0].type(), QVariant.String)
self.assertEqual(fields[1].name(), "height")
self.assertEqual(fields[1].type(), QVariant.Double)

# geojson string with 2 features
fields = QgsJsonUtils.stringToFields(
'{ "type": "FeatureCollection","features":[{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands","height":5.5}}, {\n"type": "Feature","geometry": {"type": "Point","coordinates": [110, 20]},"properties": {"name": "Henry Gale Island","height":6.5}}]}',
codec)
s = '{ "type": "FeatureCollection","features":[{\n"type": "Feature","geometry": {"type": "Point","coordinates": [125, 10]},"properties": {"name": "Dinagat Islands","height":5.5}}, {\n"type": "Feature","geometry": {"type": "Point","coordinates": [110, 20]},"properties": {"name": "Henry Gale Island","height":6.5}}]}'
if int(QT_VERSION_STR.split('.')[0]) >= 6:
fields = QgsJsonUtils.stringToFields(s)
else:
fields = QgsJsonUtils.stringToFields(s, codec)
self.assertEqual(fields.count(), 2)
self.assertEqual(fields[0].name(), "name")
self.assertEqual(fields[0].type(), QVariant.String)
Expand Down

0 comments on commit 668d7d6

Please sign in to comment.