Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e4951c8
Use SHA1 of image file for check_graphic
marqh Oct 3, 2016
780276a
test file naming
marqh Oct 3, 2016
baae9d3
tweaks
marqh Oct 3, 2016
a574517
Initial json repo of image hashes.
bjlittle Oct 3, 2016
1563c66
Merge pull request #20 from bjlittle/json-image-repo
marqh Oct 4, 2016
f799dd4
f
marqh Oct 4, 2016
65fd0d5
Incorporate json store check-graphic usage
bjlittle Oct 4, 2016
19c33ae
Remove expected_fname and add imagerepo uris
bjlittle Oct 4, 2016
bdac68c
Cherry-pick expected hash from uri
bjlittle Oct 4, 2016
a7de297
fix repo lookup
bjlittle Oct 4, 2016
14a76b7
Fix experimental repo keys
bjlittle Oct 4, 2016
e6075ab
Add codecs utf-8 reader for pk3 json load
bjlittle Oct 4, 2016
c137581
Merge pull request #21 from bjlittle/check-graphic-with-json
marqh Oct 4, 2016
e83d0ab
test image resolution
marqh Oct 4, 2016
064a3b4
test tweak
marqh Oct 4, 2016
690a766
Indented image json store.
bjlittle Oct 5, 2016
7cba57a
Merge pull request #22 from bjlittle/format-json-repo
marqh Oct 5, 2016
67a8be9
error reporting
marqh Oct 5, 2016
908e8e0
Deal with multiple image hashes.
bjlittle Oct 5, 2016
e825ab8
Remove unwanted no difference result files.
bjlittle Oct 5, 2016
fe01819
try-ex
marqh Oct 5, 2016
5c907d8
six.moves
marqh Oct 5, 2016
5a83256
Manually introduce double hash.
bjlittle Oct 5, 2016
cdcb22c
turn down logging
marqh Oct 5, 2016
1a2eb91
ex
marqh Oct 5, 2016
eb2e7b7
Merge pull request #23 from bjlittle/check-graphic-with-multi-hashes
marqh Oct 5, 2016
4e05eb0
pep8
marqh Oct 5, 2016
77d9f86
new sha1 sums for mpl1.3
marqh Oct 5, 2016
3f89ab2
review actions
marqh Oct 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions lib/iris/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
from six.moves import (filter, input, map, range, zip) # noqa
import six

import codecs
import collections
import contextlib
import difflib
import filecmp
import functools
import gzip
import hashlib
import inspect
import json
import io
import logging
import os
Expand Down Expand Up @@ -620,16 +623,21 @@ def _unique_id(self):

"""
# Obtain a consistent ID for the current test.

# NB. unittest.TestCase.id() returns different values depending on
# whether the test has been run explicitly, or via test discovery.
# For example:
# python tests/test_plot.py => '__main__.TestContourf.test_tx'
# ird -t => 'iris.tests.test_plot.TestContourf.test_tx'
bits = self.id().split('.')[-3:]
bits = self.id().split('.')
if bits[0] == '__main__':
file_name = os.path.basename(sys.modules['__main__'].__file__)
floc = sys.modules['__main__'].__file__
path, file_name = os.path.split(os.path.abspath(floc))
bits[0] = os.path.splitext(file_name)[0]
folder, location = os.path.split(path)
bits = [location] + bits
while location != 'iris':
folder, location = os.path.split(folder)
bits = [location] + bits
test_id = '.'.join(bits)

# Derive the sequential assertion ID within the test
Expand All @@ -652,21 +660,22 @@ def _ensure_folder(self, path):
logger.warning('Creating folder: %s', dir_path)
os.makedirs(dir_path)

def check_graphic(self, tol=_DEFAULT_IMAGE_TOLERANCE):
"""Checks the CRC matches for the current matplotlib.pyplot figure, and closes the figure."""
def check_graphic(self, tol=None):
"""
Checks the CRC matches for the current matplotlib.pyplot figure,
and closes the figure.

"""

unique_id = self._unique_id()

figure = plt.gcf()

repo_fname = os.path.join(os.path.dirname(__file__), 'results', 'imagerepo.json')
with open(repo_fname, 'rb') as fi:
repo = json.load(codecs.getreader('utf-8')(fi))

try:
expected_fname = os.path.join(os.path.dirname(__file__),
'results', 'visual_tests',
unique_id + '.png')

if not os.path.isdir(os.path.dirname(expected_fname)):
os.makedirs(os.path.dirname(expected_fname))

#: The path where the images generated by the tests should go.
image_output_directory = os.path.join(os.path.dirname(__file__),
'result_image_comparison')
Expand Down Expand Up @@ -695,18 +704,38 @@ def check_graphic(self, tol=_DEFAULT_IMAGE_TOLERANCE):

figure.savefig(result_fname)

if not os.path.exists(expected_fname):
warnings.warn('Created image for test %s' % unique_id)
shutil.copy2(result_fname, expected_fname)
# XXX: Deal with a new test result i.e. it's not in the repo

err = mcompare.compare_images(expected_fname, result_fname, tol=tol)
# hash the created image using sha1
with open(result_fname, 'rb') as res_file:
sha1 = hashlib.sha1(res_file.read())
if unique_id not in repo:
wmsg = 'Created image {} for test {}.'
warnings.warn(wmsg.format(result_fname, unique_id))
else:
uris = repo[unique_id]
# Cherry-pick the registered expected hashes from the
# test case uri/s.
expected = [os.path.splitext(os.path.basename(uri))[0]
for uri in uris]

if sha1.hexdigest() not in expected:
emsg = 'Actual SHA1 {} not in expected {} for test {}.'
emsg = emsg.format(sha1.hexdigest(), expected, unique_id)
if _DISPLAY_FIGURES:
print('Image comparison would have failed. '
'Message: %s' % emsg)
else:
raise ValueError('Image comparison failed.'
' Message: %s' % emsg)

else:
# There is no difference between the actual and expected
# result, so remove the actual result file.
os.remove(result_fname)

if _DISPLAY_FIGURES:
if err:
print('Image comparison would have failed. Message: %s' % err)
plt.show()
else:
assert not err, 'Image comparison failed. Message: %s' % err

finally:
plt.close()
Expand Down
Loading