Skip to content

Commit

Permalink
ImageComparison decorator: add option to deactivate imgur upload
Browse files Browse the repository at this point in the history
this can be used in tests that should fail and then might attempt to
upload images which takes several seconds and is totally unwanted in
this case
  • Loading branch information
megies committed Oct 7, 2017
1 parent b9e2a94 commit c942ad4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
10 changes: 4 additions & 6 deletions obspy/core/tests/test_util_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ def test_image_comparison(self):
# image comparison that should raise
# avoid uploading the staged test fail image
# (after an estimate of 10000 uploads of it.. ;-))
with mock.patch.object(ImageComparison, '_upload_images',
new=mock.MagicMock(return_value='')):
with self.assertRaises(ImageComparisonException):
with ImageComparison(path, img_basename,
adjust_tolerance=False) as ic:
shutil.copy(img_fail, ic.name)
with self.assertRaises(ImageComparisonException):
with ImageComparison(path, img_basename, adjust_tolerance=False,
no_uploads=True) as ic:
shutil.copy(img_fail, ic.name)

# check that temp file is deleted
self.assertFalse(os.path.exists(ic.name))
Expand Down
14 changes: 12 additions & 2 deletions obspy/core/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ class ImageComparison(NamedTemporaryFile):
default will leave the style as is. You may wish to set it to
``'default'`` to enable the new style from Matplotlib 2.0, or some
alternate style, which will work back to Matplotlib 1.4.0.
:type no_uploads: bool
:param no_uploads: If set to ``True`` no uploads to imgur are attempted, no
matter what (e.g. any options to ``obspy-runtests`` that would normally
cause an upload attempt). This can be used to forcibly deactivate
upload attempts in image tests that are expected to fail.
The class should be used with Python's "with" statement. When setting up,
the matplotlib rcdefaults are set to ensure consistent image testing.
Expand Down Expand Up @@ -305,7 +310,8 @@ class ImageComparison(NamedTemporaryFile):
"""
def __init__(self, image_path, image_name, reltol=1,
adjust_tolerance=True, plt_close_all_enter=True,
plt_close_all_exit=True, style=None, *args, **kwargs):
plt_close_all_exit=True, style=None, no_uploads=False, *args,
**kwargs):
self.suffix = "." + image_name.split(".")[-1]
super(ImageComparison, self).__init__(suffix=self.suffix, *args,
**kwargs)
Expand All @@ -318,6 +324,7 @@ def __init__(self, image_path, image_name, reltol=1,
self.tol = reltol * 3.0
self.plt_close_all_enter = plt_close_all_enter
self.plt_close_all_exit = plt_close_all_exit
self.no_uploads = no_uploads

if (MATPLOTLIB_VERSION < [1, 4, 0] or
(MATPLOTLIB_VERSION[:2] == [1, 4] and style is None)):
Expand Down Expand Up @@ -561,11 +568,14 @@ def _copy_tempfiles(self):

def _upload_images(self):
"""
Uploads images to imgur.
Uploads images to imgur unless explicitly deactivated with option
`no_uploads` (to speed up tests that are expected to fail).
:returns: ``dict`` with links to uploaded images or ``str`` with
message if upload failed
"""
if self.no_uploads:
return "Upload to imgur deactivated with option 'no_uploads'."
try:
import pyimgur
# try to get imgur client id from environment
Expand Down
4 changes: 2 additions & 2 deletions obspy/taup/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def _test_plot_all(plot_all):
# same test should fail if plot_all=True
with self.assertRaises(ImageComparisonException):
with ImageComparison(
self.image_dir,
"traveltimes_plot_all_False.png") as ic:
self.image_dir, "traveltimes_plot_all_False.png",
no_uploads=True) as ic:
_test_plot_all(plot_all=True)
plt.savefig(ic.name)

Expand Down

0 comments on commit c942ad4

Please sign in to comment.