Skip to content

Commit

Permalink
split test cases out into separate assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Grainger committed Jul 12, 2017
1 parent 52efa00 commit d407545
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions test/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,51 +50,61 @@ class RenderTestCaseBase(TestCase):

def runTest(self):
symbology = self.conf.symbology
args = self.conf.args
img_prefix = join(IMG_ROOT, symbology)
for args in self.conf.cases:
img_filename, codestring = args[:2]
options = args[2] if len(args) > 2 else {}
render_options = dict((args[3] if len(args) > 3 else {}), scale=2.0)
generated = barcode(symbology, codestring, options, **render_options).convert('L')
loaded = Image.open(join(img_prefix, img_filename)).convert('L')
diff = None
try:
# image size comparison
self.assertEqual(generated.size, loaded.size)
# pixel-wize comparison
diff = ImageChops.difference(generated, loaded)
diff_bbox = diff.getbbox()
self.assertIsNone(diff_bbox)
except AssertionError as exc:
# generate and show diagnostics image
if diff:
# if diff exists, generate 3-row diagnostics image
lw, lh = loaded.size
gw, gh = generated.size
diag = Image.new('L', (max(lw, gw), (lh+gh+max(lh, gh))))
diag.paste(loaded, (0, 0, lw, lh))
diag.paste(generated, (0, lh, gw, lh+gh))
diag.paste(diff, (0, lh+gh, max(lw, gw), (lh+gh+max(lh, gh))))
else:
# else, just write generated image
diag = generated
sio_img = BytesIO()
diag.convert('L').save(sio_img, 'PNG')
# reopen sio_img
sio_img = BytesIO(sio_img.getvalue())
sio_uu = BytesIO()
uuencode(sio_img, sio_uu, name='diag_%s' % img_filename)
raise AssertionError(
'Image difference detected (%s)\n'
'uu of generated image:\n----\n%s----\n'
% (exc.args, sio_uu.getvalue()))
img_filename, codestring = args[:2]
options = args[2] if len(args) > 2 else {}
render_options = dict((args[3] if len(args) > 3 else {}), scale=2.0)
generated = barcode(symbology, codestring, options, **render_options).convert('L')
loaded = Image.open(join(img_prefix, img_filename)).convert('L')
diff = None
try:
# image size comparison
self.assertEqual(generated.size, loaded.size)
# pixel-wize comparison
diff = ImageChops.difference(generated, loaded)
diff_bbox = diff.getbbox()
self.assertIsNone(diff_bbox)
except AssertionError as exc:
# generate and show diagnostics image
if diff:
# if diff exists, generate 3-row diagnostics image
lw, lh = loaded.size
gw, gh = generated.size
diag = Image.new('L', (max(lw, gw), (lh+gh+max(lh, gh))))
diag.paste(loaded, (0, 0, lw, lh))
diag.paste(generated, (0, lh, gw, lh+gh))
diag.paste(diff, (0, lh+gh, max(lw, gw), (lh+gh+max(lh, gh))))
else:
# else, just write generated image
diag = generated
sio_img = BytesIO()
diag.convert('L').save(sio_img, 'PNG')
# reopen sio_img
sio_img = BytesIO(sio_img.getvalue())
sio_uu = BytesIO()
uuencode(sio_img, sio_uu, name='diag_%s' % img_filename)
raise AssertionError(
'Image difference detected (%s)\n'
'uu of generated image:\n----\n%s----\n'
% (exc.args, sio_uu.getvalue()))


def gen_render_test_case(symbology):
conf_mod = load_module(symbology, *find_module(symbology, ['test']))
test_case = type(symbology.capitalize()+'RenderTest',
(RenderTestCaseBase,), dict(conf=conf_mod))()
return test_case
for i, case in enumerate(conf_mod.cases):

class Conf(object):
symbology = conf_mod.symbology
args = case

RenderTestCase = type(
symbology.capitalize()+'RenderTest'+str(i),
(RenderTestCaseBase,),
dict(conf=Conf)
)

yield RenderTestCase()


def gen_test_images(symbology):
Expand All @@ -114,7 +124,8 @@ def gen_test_images(symbology):

def collect(ts):
for symbology in symbologies:
ts.addTest(gen_render_test_case(symbology))
for test in gen_render_test_case(symbology):
ts.addTest(test)


suite = collect(TestSuite())
Expand Down

0 comments on commit d407545

Please sign in to comment.