Skip to content

Commit

Permalink
Fix json comparison with indented reference files
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed May 6, 2019
1 parent ea0a70b commit 425f672
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -2305,6 +2305,7 @@ namespace QgsWms
}
}
#ifdef QGISDEBUG
// This is only useful to generate human readable reference files for tests
return QByteArray::fromStdString( json.dump( 2 ) );
#else
return QByteArray::fromStdString( json.dump() );
Expand Down
16 changes: 15 additions & 1 deletion tests/src/python/test_qgsserver_wms.py
Expand Up @@ -17,6 +17,7 @@
__revision__ = '$Format:%H$'

import os
import json

# Needed on Qt 5 so that the serialization of XML is consistent among all executions
os.environ['QT_HASH_SEED'] = '1'
Expand Down Expand Up @@ -57,13 +58,26 @@ def wms_request(self, request, extra=None, project='test_project.qgs', version='
header, body = self._execute_request(query_string)
return (header, body, query_string)

def wms_request_compare(self, request, extra=None, reference_file=None, project='test_project.qgs', version='1.3.0', ignoreExtent=False):
def wms_request_compare(self, request, extra=None, reference_file=None, project='test_project.qgs', version='1.3.0', ignoreExtent=False, normalizeJson=False):
response_header, response_body, query_string = self.wms_request(request, extra, project, version)
response = response_header + response_body
reference_path = self.testdata_path + (request.lower() if not reference_file else reference_file) + '.txt'
self.store_reference(reference_path, response)
f = open(reference_path, 'rb')
expected = f.read()

def _n(r):
lines = r.split(b'\n')
b = lines[2:]
h = lines[:2]
try:
return b'\n'.join(h) + json.dumps(json.loads(b'\n'.join(b))).encode('utf8')
except:
return r

response = _n(response)
expected = _n(expected)

f.close()
response = re.sub(RE_STRIP_UNCHECKABLE, b'*****', response)
expected = re.sub(RE_STRIP_UNCHECKABLE, b'*****', expected)
Expand Down
54 changes: 36 additions & 18 deletions tests/src/python/test_qgsserver_wms_getfeatureinfo.py
Expand Up @@ -451,7 +451,8 @@ def testGetFeatureInfoJSON(self):
'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' +
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=testlayer%20%C3%A8%C3%A9&X=190&Y=320',
'wms_getfeatureinfo_json')
'wms_getfeatureinfo_json',
normalizeJson=True)

# simple test without geometry and info_format=application/geo+json
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -460,7 +461,8 @@ def testGetFeatureInfoJSON(self):
'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' +
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=testlayer%20%C3%A8%C3%A9&X=190&Y=320',
'wms_getfeatureinfo_geojson')
'wms_getfeatureinfo_geojson',
normalizeJson=True)

# test with several features and several layers
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -470,7 +472,8 @@ def testGetFeatureInfoJSON(self):
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=testlayer%20%C3%A8%C3%A9,fields_alias,exclude_attribute&' +
'X=190&Y=320&FEATURE_COUNT=2&FI_POINT_TOLERANCE=200',
'wms_getfeatureinfo_multiple_json')
'wms_getfeatureinfo_multiple_json',
normalizeJson=True)

# simple test with geometry
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -480,7 +483,8 @@ def testGetFeatureInfoJSON(self):
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=testlayer%20%C3%A8%C3%A9&X=190&Y=320&' +
'with_geometry=true',
'wms_getfeatureinfo_geometry_json')
'wms_getfeatureinfo_geometry_json',
normalizeJson=True)

# test with alias
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -489,7 +493,8 @@ def testGetFeatureInfoJSON(self):
'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' +
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=fields_alias&X=190&Y=320',
'wms_getfeatureinfo_alias_json')
'wms_getfeatureinfo_alias_json',
normalizeJson=True)

# test with excluded attributes
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -498,7 +503,8 @@ def testGetFeatureInfoJSON(self):
'width=600&height=400&srs=EPSG%3A3857&bbox=913190.6389747962%2C' +
'5606005.488876367%2C913235.426296057%2C5606035.347090538&' +
'query_layers=exclude_attribute&X=190&Y=320',
'wms_getfeatureinfo_exclude_attribute_json')
'wms_getfeatureinfo_exclude_attribute_json',
normalizeJson=True)

# test with raster layer
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -507,7 +513,8 @@ def testGetFeatureInfoJSON(self):
'width=500&height=500&srs=EPSG%3A3857&' +
'bbox=1989139.6,3522745.0,2015014.9,3537004.5&' +
'query_layers=landsat&X=250&Y=250',
'wms_getfeatureinfo_raster_json')
'wms_getfeatureinfo_raster_json',
normalizeJson=True)

def testGetFeatureInfoPostgresTypes(self):
# compare json list output with file
Expand All @@ -519,7 +526,8 @@ def testGetFeatureInfoPostgresTypes(self):
'&FILTER=json' +
urllib.parse.quote(':"pk" = 1'),
'get_postgres_types_json_list',
'test_project_postgres_types.qgs')
'test_project_postgres_types.qgs',
normalizeJson=True)

# compare dict output with file
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -530,7 +538,8 @@ def testGetFeatureInfoPostgresTypes(self):
'&FILTER=json' +
urllib.parse.quote(':"pk" = 2'),
'get_postgres_types_json_dict',
'test_project_postgres_types.qgs')
'test_project_postgres_types.qgs',
normalizeJson=True)

# compare decoded json field list
response_header, response_body, query_string = self.wms_request('GetFeatureInfo',
Expand Down Expand Up @@ -605,7 +614,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas',
'test_project_wms_grouped_layers.qgs')
'test_project_wms_grouped_layers.qgs',
normalizeJson=True)

# areas+and+symbols (nested)
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -617,7 +627,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# as-areas-short-name
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -629,7 +640,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# Top level: QGIS Server - Grouped Layer
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -641,7 +653,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_top',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# Multiple matches from 2 layer groups
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -653,7 +666,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_name_areas_cities',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# no_query group (nested)
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -665,7 +679,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_no_query',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# query_child group (nested)
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -677,7 +692,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_query_child',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# child_ok group (nested)
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -689,7 +705,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_query_child',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)

# as_areas_query_copy == as-areas-short-name-query-copy (nested)
self.wms_request_compare('GetFeatureInfo',
Expand All @@ -701,7 +718,8 @@ def testGetFeatureInfoGroupedLayers(self):
'&I=0&J=1' +
'&FEATURE_COUNT=10',
'wms_getfeatureinfo_group_query_child',
'test_project_wms_grouped_nested_layers.qgs')
'test_project_wms_grouped_nested_layers.qgs',
normalizeJson=True)


if __name__ == '__main__':
Expand Down

0 comments on commit 425f672

Please sign in to comment.