Skip to content

Commit

Permalink
Dynamic format in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jun 10, 2020
1 parent ac3ae7f commit 78b02d5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
34 changes: 24 additions & 10 deletions tests/src/python/test_qgsserver.py
Expand Up @@ -174,19 +174,24 @@ def _result(self, data):

return data[1], headers

def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputJpg=False):
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputFormat='PNG'):

extFile = 'png'
if outputJpg:
if outputFormat == 'PNG':
extFile = 'png'
elif outputFormat == 'JPG':
extFile = 'jpg'
elif outputFormat == 'WEBP':
extFile = 'webp'
else:
raise RuntimeError('Yeah, new format implemented')

temp_image = os.path.join(tempfile.gettempdir(), "%s_result.%s" % (control_image, extFile))

with open(temp_image, "wb") as f:
f.write(image)

if outputJpg:
return (True, "QgsRenderChecker can't be used for JPG images")
if outputFormat != 'PNG':
return (True, "QgsRenderChecker can only be used for PNG")

control = QgsMultiRenderChecker()
control.setControlPathPrefix("qgis_server")
Expand All @@ -196,13 +201,22 @@ def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outpu
control.setSizeTolerance(max_size_diff.width(), max_size_diff.height())
return control.runTest(control_image, max_diff), control.report()

def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=QSize(), unittest_data_path='control_images', outputJpg=False):
def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=QSize(), unittest_data_path='control_images', outputFormat='PNG'):
"""
:param outputFormat: PNG, JPG or WEBP
"""

extFile = 'png'
contentType = 'image/png'
if outputJpg:
if outputFormat == 'PNG':
extFile = 'png'
contentType = 'image/png'
elif outputFormat == 'JPG':
extFile = 'jpg'
contentType = 'image/jpeg'
elif outputFormat == 'WEBP':
extFile = 'webp'
contentType = 'image/webp'
else:
raise RuntimeError('Yeah, new format implemented')

reference_path = unitTestDataPath(unittest_data_path) + '/qgis_server/' + image + '/' + image + '.' + extFile
self.store_reference(reference_path, response)
Expand All @@ -211,7 +225,7 @@ def _img_diff_error(self, response, headers, image, max_diff=100, max_size_diff=
headers.get("Content-Type"), contentType,
"Content type is wrong: %s instead of %s\n%s" % (headers.get("Content-Type"), contentType, response))

test, report = self._img_diff(response, image, max_diff, max_size_diff, outputJpg)
test, report = self._img_diff(response, image, max_diff, max_size_diff, outputFormat)

with open(os.path.join(tempfile.gettempdir(), image + "_result." + extFile), "rb") as rendered_file:
encoded_rendered_file = base64.b64encode(rendered_file.read())
Expand Down
11 changes: 8 additions & 3 deletions tests/src/python/test_qgsserver_accesscontrol.py
Expand Up @@ -200,11 +200,16 @@ def _post_restricted(self, data, query_string=None):
self._server.putenv("QGIS_PROJECT_FILE", '')
return result

def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputJpg=False):
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize(), outputFormat='PNG'):

extFile = 'png'
if outputJpg:
if outputFormat == 'PNG':
extFile = 'png'
elif outputFormat == 'JPG':
extFile = 'jpg'
elif outputFormat == 'WEBP':
extFile = 'webp'
else:
raise RuntimeError('Yeah, new format implemented')

temp_image = os.path.join(tempfile.gettempdir(), "%s_result.%s" % (control_image, extFile))

Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsserver_wms_getmap.py
Expand Up @@ -115,7 +115,7 @@ def test_wms_getmap_basic_mode(self):
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_Mode_16bit", 20000)
self._img_diff_error(r, h, "WMS_GetMap_Mode_16bit", 20000, outputFormat='WEBP')

def test_wms_getmap_basic(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/test_qgsserver_wms_getprint.py
Expand Up @@ -235,7 +235,7 @@ def test_wms_getprint_basic(self):
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetPrint_Basic", outputJpg=True)
self._img_diff_error(r, h, "WMS_GetPrint_Basic", outputFormat='JPG')

# Output PDF
qs = "?" + "&".join(["%s=%s" % i for i in list({
Expand Down

0 comments on commit 78b02d5

Please sign in to comment.