|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsServer GetFeatureInfo WMS with PG. |
| 3 | +
|
| 4 | +From build dir, run: ctest -R PyQgsServerWMSGetFeatureInfoPG -V |
| 5 | +
|
| 6 | +
|
| 7 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation; either version 2 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +""" |
| 13 | +__author__ = 'Alessandro Pasotti' |
| 14 | +__date__ = '22/01/2021' |
| 15 | +__copyright__ = 'Copyright 2021, The QGIS Project' |
| 16 | +# This will get replaced with a git SHA1 when you do a git archive |
| 17 | +__revision__ = '$Format:%H$' |
| 18 | + |
| 19 | +import os |
| 20 | +import tempfile |
| 21 | + |
| 22 | +# Needed on Qt 5 so that the serialization of XML is consistent among all |
| 23 | +# executions |
| 24 | +os.environ['QT_HASH_SEED'] = '1' |
| 25 | + |
| 26 | +os.environ['QGIS_CUSTOM_CONFIG_PATH'] = tempfile.mkdtemp('', 'QGIS-PythonTestConfigPath') |
| 27 | + |
| 28 | +import re |
| 29 | +import urllib.request |
| 30 | +import urllib.parse |
| 31 | +import urllib.error |
| 32 | + |
| 33 | +import xml.etree.ElementTree as ET |
| 34 | +import json |
| 35 | + |
| 36 | +from qgis.testing import unittest, start_app |
| 37 | +from qgis.PyQt.QtCore import QSize |
| 38 | + |
| 39 | +import osgeo.gdal # NOQA |
| 40 | + |
| 41 | +from test_qgsserver_wms import TestQgsServerWMSTestBase |
| 42 | +from qgis.core import QgsProject, QgsVectorLayer, QgsFeatureRequest, QgsExpression, QgsProviderRegistry |
| 43 | +from qgis.server import QgsBufferServerRequest, QgsBufferServerResponse |
| 44 | + |
| 45 | + |
| 46 | +class TestQgsServerWMSGetFeatureInfoPG(TestQgsServerWMSTestBase): |
| 47 | + """QGIS Server WMS Tests for GetFeatureInfo request""" |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def setUpClass(cls): |
| 51 | + |
| 52 | + super().setUpClass() |
| 53 | + |
| 54 | + cls.dbconn = 'service=qgis_test' |
| 55 | + if 'QGIS_PGTEST_DB' in os.environ: |
| 56 | + cls.dbconn = os.environ['QGIS_PGTEST_DB'] |
| 57 | + |
| 58 | + # Test layer |
| 59 | + md = QgsProviderRegistry.instance().providerMetadata('postgres') |
| 60 | + uri = cls.dbconn + 'dbname=qgis_test sslmode=disable ' |
| 61 | + conn = md.createConnection(uri, {}) |
| 62 | + conn.executeSql('DROP TABLE IF EXISTS "qgis_test"."someDataLong" CASCADE') |
| 63 | + conn.executeSql('SELECT * INTO "qgis_test"."someDataLong" FROM "qgis_test"."someData"') |
| 64 | + conn.executeSql('ALTER TABLE "qgis_test"."someDataLong" ALTER COLUMN "pk" TYPE bigint') |
| 65 | + conn.executeSql('ALTER TABLE "qgis_test"."someDataLong" ALTER COLUMN "pk" TYPE bigint') |
| 66 | + conn.executeSql('ALTER TABLE "qgis_test"."someDataLong" ALTER COLUMN "pk" SET NOT NULL') |
| 67 | + conn.executeSql('CREATE UNIQUE INDEX someDataLongIdx ON "qgis_test"."someDataLong" ("pk")') |
| 68 | + |
| 69 | + cls.vlconn = cls.dbconn + ' sslmode=disable key=\'pk\' checkPrimaryKeyUnicity=0 srid=4326 type=POINT table="qgis_test"."someDataLong" (geom) sql=' |
| 70 | + |
| 71 | + def _baseFilterTest(self, info_format): |
| 72 | + |
| 73 | + vl = QgsVectorLayer(self.vlconn, 'someData', 'postgres') |
| 74 | + self.assertTrue(vl.isValid()) |
| 75 | + |
| 76 | + # Pre-filtered |
| 77 | + vl2 = QgsVectorLayer(self.vlconn, 'someData', 'postgres') |
| 78 | + self.assertTrue(vl2.isValid()) |
| 79 | + [f for f in vl2.getFeatures(QgsFeatureRequest(QgsExpression('pk > 2')))] |
| 80 | + |
| 81 | + base_features_url = ('http://qgis/?SERVICE=WMS&REQUEST=GetFeatureInfo&' + |
| 82 | + 'LAYERS=someData&STYLES=&' + |
| 83 | + r'INFO_FORMAT={}&' + |
| 84 | + 'SRS=EPSG%3A4326&' + |
| 85 | + 'QUERY_LAYERS=someData&X=-1&Y=-1&' + |
| 86 | + 'FEATURE_COUNT=100&' |
| 87 | + 'FILTER=someData') |
| 88 | + |
| 89 | + two_feature_url = base_features_url + urllib.parse.quote(':"pk" = 2') |
| 90 | + |
| 91 | + p = QgsProject() |
| 92 | + p.addMapLayers([vl]) |
| 93 | + |
| 94 | + url = two_feature_url.format(urllib.parse.quote(info_format)) |
| 95 | + |
| 96 | + req = QgsBufferServerRequest(url) |
| 97 | + res = QgsBufferServerResponse() |
| 98 | + self.server.handleRequest(req, res, p) |
| 99 | + reference_body = bytes(res.body()).decode('utf8') |
| 100 | + |
| 101 | + # Pre-filter |
| 102 | + p = QgsProject() |
| 103 | + p.addMapLayers([vl2]) |
| 104 | + |
| 105 | + req = QgsBufferServerRequest(url) |
| 106 | + res = QgsBufferServerResponse() |
| 107 | + self.server.handleRequest(req, res, p) |
| 108 | + two_feature_body = bytes(res.body()).decode('utf8') |
| 109 | + |
| 110 | + self.assertEqual(reference_body, two_feature_body, info_format) |
| 111 | + |
| 112 | + def testGetFeatureInfoFilterPg(self): |
| 113 | + """Test issue GH #41124""" |
| 114 | + |
| 115 | + self._baseFilterTest('text/plain') |
| 116 | + self._baseFilterTest('text/html') |
| 117 | + self._baseFilterTest('text/xml') |
| 118 | + self._baseFilterTest('application/json') |
| 119 | + self._baseFilterTest('application/vnd.ogc.gml') |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + unittest.main() |
0 commit comments