Skip to content
Permalink
Browse files

Dynamic format in tests

  • Loading branch information
m-kuhn committed Jun 9, 2020
1 parent ac3ae7f commit 78b02d5ce3689cbd118b782cdc9d5a4964b13a70
@@ -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")
@@ -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)
@@ -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())
@@ -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))

@@ -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({
@@ -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({

0 comments on commit 78b02d5

Please sign in to comment.
You can’t perform that action at this time.