Skip to content

Commit

Permalink
Additional testing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
emcconville committed Apr 24, 2019
1 parent 1fe9796 commit 99236a6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 82 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
omit =
wand/cdefs/*
wand/api.py
wand/display.py

Binary file added tests/assets/checks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 52 additions & 22 deletions tests/image_methods_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,12 @@ def test_despeckle(fx_asset):
@mark.slow
def test_distort(fx_asset):
"""Distort image."""
with Image(filename=str(fx_asset.join('mona-lisa.jpg'))) as img:
with Image(filename=str(fx_asset.join('checks.png'))) as img:
with Color('skyblue') as color:
img.matte_color = color
img.virtual_pixel = 'tile'
img.distort('perspective', (0, 0, 20, 60, 90, 0,
70, 63, 0, 90, 5, 83,
90, 90, 85, 88))
img.distort('perspective', (0, 0, 20, 60, 90, 0, 70, 63,
0, 90, 5, 83, 90, 90, 85, 88))
assert img[img.width - 1, 0] == color


Expand Down Expand Up @@ -811,6 +810,14 @@ def test_import_pixels(fx_asset):
dst.import_pixels(data=[0x00, 0xFF])


def test_kurtosis_channel():
with Image(filename='rose:') as img:
r = img.kurtosis_channel('red')
assert len(r) == 2
with raises(TypeError):
img.kurtosis_channel('unknown')


def test_level(fx_asset):
with Image(filename=str(fx_asset.join('gray_range.jpg'))) as img:
# Adjust the levels to make this image entirely black
Expand Down Expand Up @@ -919,6 +926,14 @@ def assert_equal_except_alpha(a, b):
assert_equal_except_alpha(img[x, y], img[x, y])


def test_mean_channel():
with Image(filename='rose:') as img:
r = img.mean_channel('red')
assert len(r) == 2
with raises(TypeError):
img.mean_channel('unknown')


def test_merge_layers(fx_asset):
for method in ['merge', 'flatten', 'mosaic']:
with Image(filename=str(fx_asset.join('cmyk.jpg'))) as img1:
Expand Down Expand Up @@ -1074,6 +1089,9 @@ def test(c1, c2):
test(left_bottom, img[0, -1])
test(right_top, img[-1, 0])
test(right_bottom, img[-1, -1])
assert img.negate(False, 'red')
with raises(ValueError):
img.negate(True, 'unknown')


def test_noise():
Expand All @@ -1099,6 +1117,8 @@ def test_normalize(display, fx_asset):
with img[-1, -1] as right_bottom:
assert (right_bottom.red == right_bottom.green ==
right_bottom.blue == 0)
with raises(ValueError):
img.normalize('unknown')


def test_normalize_channel(fx_asset):
Expand Down Expand Up @@ -1159,11 +1179,10 @@ def test_posterize(fx_asset):
@mark.slow
def test_quantize(fx_asset):
number_colors = 64
with Image(filename=str(fx_asset.join('mona-lisa.jpg'))) as img:
colors = set([color for row in img for color in row])
assert len(colors) > number_colors
with Image(width=100, height=100, pseudo='PLASMA:') as img:
assert img.colors > number_colors

with Image(filename=str(fx_asset.join('mona-lisa.jpg'))) as img:
with Image(width=100, height=100, pseudo='PLASMA:') as img:
with raises(TypeError):
img.quantize(str(number_colors), 'undefined', 0, True, True)

Expand All @@ -1180,9 +1199,15 @@ def test_quantize(fx_asset):
img.quantize(number_colors, 'undefined', 0, True, 1)

img.quantize(number_colors, 'undefined', 0, True, True)
colors = set([color for row in img for color in row])
assert colors
assert len(colors) <= number_colors
assert img.colors <= number_colors


def test_range_channel():
with Image(filename='rose:') as img:
minima, maxima = img.range_channel('red')
assert minima < maxima
with raises(TypeError):
img.range_channel('unknown')


def test_remap(fx_asset):
Expand Down Expand Up @@ -1559,17 +1584,8 @@ def test_threshold(fx_asset):
assert white.red_int8 == white.green_int8 == white.blue_int8 == 255
with img[0, btm] as black:
assert black.red_int8 == black.green_int8 == black.blue_int8 == 0


def test_tint(fx_asset):
with Image(filename='rose:') as img:
was = img.signature
img.tint('blue', 'blue')
assert was != img.signature
with raises(TypeError):
img.colorize(0xDEADBEEF, Color('blue'))
with raises(TypeError):
img.colorize(Color('blue'), 0xDEADBEEF)
with raises(ValueError):
img.threshold(0.5, channel='unknown')


def test_threshold_channel(fx_asset):
Expand All @@ -1588,6 +1604,17 @@ def test_threshold_channel(fx_asset):
red.green_int8 == red.blue_int8 == 0)


def test_tint(fx_asset):
with Image(filename='rose:') as img:
was = img.signature
img.tint('blue', 'blue')
assert was != img.signature
with raises(TypeError):
img.colorize(0xDEADBEEF, Color('blue'))
with raises(TypeError):
img.colorize(Color('blue'), 0xDEADBEEF)


@mark.parametrize(('args', 'kwargs', 'expected_size'), [
((), {'resize': '200%'}, (1600, 1200)),
((), {'resize': '200%x100%'}, (1600, 600)),
Expand Down Expand Up @@ -1692,6 +1719,8 @@ def test_transparentize(fx_asset):
with im[100, 100] as c:
assert c.red == c.green == c.blue == 0
assert 0.69 < c.alpha < 0.71
with raises(ValueError):
im.transparentize(-9)


def test_transpose(fx_asset):
Expand Down Expand Up @@ -1799,5 +1828,6 @@ def test_white_threshold(fx_asset):
was = img.signature
img.white_threshold(Color('gray(50%)'))
assert was != img.signature
assert img.white_threshold('gray(50%)')
with raises(TypeError):
img.white_threshold(0xDEADBEEF)
8 changes: 4 additions & 4 deletions tests/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_image_invalid_params():
def test_blank_image():
gray = Color('#ccc')
transparent = Color('transparent')
with raises(TypeError):
with raises(ValueError):
Image(width=0, height=0)
with Image(width=20, height=10) as img:
assert img[10, 5] == transparent
Expand All @@ -74,7 +74,7 @@ def test_raw_image(fx_asset):
for i in range(256) for j in range(256)])
with raises(ValueError):
Image(blob=b, depth=6)
with raises(TypeError):
with raises(ValueError):
Image(blob=b, depth=8, width=0, height=0, format="RGB")
with raises(TypeError):
Image(blob=b, depth=8, width=256, height=256, format=1)
Expand Down Expand Up @@ -330,11 +330,11 @@ def test_iterate(fx_asset):
with Image(filename=str(fx_asset.join('croptest.png'))) as img:
for i, row in enumerate(img):
assert len(row) == 300
if i % 3:
if i % 30:
continue # avoid slowness
if 100 <= i < 200:
for x, color in enumerate(row):
if x % 3:
if x % 30:
continue # avoid slowness
if 100 <= x < 200:
assert color == black
Expand Down
13 changes: 13 additions & 0 deletions wand/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def assert_unsigned_integer(subject, label=None):
raise ValueError(msg)


def assert_counting_number(subject, label):
assert_integer(subject, label)
if subject < 1:
fmt = "{0}={1} must be an natural number greater than 0"
msg = fmt.format(label, subject)
raise ValueError(msg)


def assert_bool(subject, label=None):
if not isinstance(subject, bool):
if label:
Expand Down Expand Up @@ -79,6 +87,11 @@ def assert_color(subject, label=None):
raise TypeError(msg)


def counting_numbers(**kwargs):
for label, subject in kwargs.items():
assert_counting_number(subject, label)


def in_list(options, label, **kwargs):
for subject_label, subject in kwargs.items():
if subject not in options:
Expand Down
Loading

0 comments on commit 99236a6

Please sign in to comment.