Skip to content

Commit

Permalink
Refactor code to import same decorator
Browse files Browse the repository at this point in the history
Add `requires_img_lib` decorator and use it in test_image and test_dataio.
Update test_image to reduce redundancies.
  • Loading branch information
mfkaptan committed Jul 7, 2014
1 parent 0e61e64 commit a373cd4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
4 changes: 2 additions & 2 deletions vispy/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from ._testing import (SkipTest, requires_application, requires_pyopengl, # noqa
from ._testing import (SkipTest, requires_application, requires_img_lib, # noqa
assert_is, assert_in, assert_not_in, has_backend, # noqa
glut_skip) # noqa
glut_skip, requires_pyopengl) # noqa
from ._runners import _tester # noqa
7 changes: 7 additions & 0 deletions vispy/testing/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,10 @@ def glut_skip():
if app.backend_name.lower() == 'glut':
raise SkipTest('GLUT unstable')
return # otherwise it's fine


def requires_img_lib():
"""Decorator for tests that require an image library"""
from ..util.dataio import _check_img_lib
has_img_lib = not all(c is None for c in _check_img_lib())
return np.testing.dec.skipif(not has_img_lib, 'imageio or PIL required')
10 changes: 3 additions & 7 deletions vispy/util/tests/test_dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
from nose.tools import assert_equal, assert_raises
from numpy.testing import assert_allclose, assert_array_equal

from vispy.util.dataio import (write_mesh, read_mesh, _check_img_lib, crate,
imsave, imread)
from vispy.util.dataio import write_mesh, read_mesh, crate, imsave, imread
from vispy.util._geom import _fast_cross_3d
from vispy.util import _TempDir
from vispy.testing import requires_img_lib

temp_dir = _TempDir()

has_img_lib = not all(c is None for c in _check_img_lib())
requires_img_lib = np.testing.dec.skipif(not has_img_lib, 'imageio or PIL '
'required')


def test_wavefront():
"""Test wavefront reader"""
Expand Down Expand Up @@ -42,7 +38,7 @@ def test_wavefront():
rtol=1e-10, atol=1e-10)


@requires_img_lib
@requires_img_lib()
def test_read_write_image():
"""Test reading and writing of images"""
fname = op.join(temp_dir, 'out.png')
Expand Down
38 changes: 17 additions & 21 deletions vispy/util/tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import numpy as np
from vispy.util import make_png
from vispy.util.dataio import _check_img_lib, imread
from numpy.testing import assert_array_equal
from os import path as op

has_img_lib = not all(c is None for c in _check_img_lib())
requires_img_lib = np.testing.dec.skipif(not has_img_lib, 'imageio or PIL '
'required')
from vispy.util import make_png, _TempDir
from vispy.util.dataio import imread
from vispy.testing import requires_img_lib

temp_dir = _TempDir()

@requires_img_lib

@requires_img_lib()
def test_make_png():
""" Test to ensure that make_png functions correctly.
Save random RGBA and RGB arrays onto disk as PNGs using make_png.
Read them back with an image library and check whether the array
saved is equal to the array read.
"""

## RGBA
# Create random RGBA array as type ubyte
rgba_save = np.random.randint(256, size=(100, 100, 4)).astype(np.ubyte)
# Save RGBA with make_png
with open('rgba.png', 'wb') as f1:
f1.write(make_png(rgba_save))
# Read back and check
rgba_read = imread('rgba.png', 'PNG')
assert_array_equal(rgba_save, rgba_read)

## RGB
# Get rid of the alpha for RGB
rgb_save = rgba_save[:, :, :3]
# Save RGB with make_png
with open('rgb.png', 'wb') as f2:
f2.write(make_png(rgb_save))
# Read back and check
rgb_read = imread('rgb.png', 'PNG')
assert_array_equal(rgb_save, rgb_read)
# Output file should be in temp
png_out = op.join(temp_dir, 'random.png')

for rgb_a in (rgba_save, rgb_save):
with open(png_out, 'wb') as f:
f.write(make_png(rgb_a)) # Save array with make_png

# Read back with a library and check equality
rgb_a_read = imread(png_out, 'PNG')
assert_array_equal(rgb_a, rgb_a_read)

0 comments on commit a373cd4

Please sign in to comment.