From 14c7dfe58fc8ca82e073d248eaa2b76d97ddcc80 Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Mon, 22 May 2017 21:20:22 +0200 Subject: [PATCH] Added unit tests for ImageWidthError --- src/escpos/escpos.py | 2 +- src/escpos/exceptions.py | 2 +- test/test_function_image.py | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 58bd81d..6f68872 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -101,7 +101,7 @@ class Escpos(object): im = EscposImage(img_source) try: - max_width = self.profile.profile_data['media']['width']['pixels'] + max_width = self.profile.profile_data['media']['width']['pixels'] if im.width > max_width: raise ImageWidthError('{} > {}'.format(im.width, max_width)) except KeyError: diff --git a/src/escpos/exceptions.py b/src/escpos/exceptions.py index 15c5ab6..0662792 100644 --- a/src/escpos/exceptions.py +++ b/src/escpos/exceptions.py @@ -107,7 +107,7 @@ class ImageSizeError(Error): class ImageWidthError(Error): """ Image width is too large. - + The return code for this exception is `41`. """ def __init__(self, msg=""): diff --git a/test/test_function_image.py b/test/test_function_image.py index 27b4fd7..5aac41b 100644 --- a/test/test_function_image.py +++ b/test/test_function_image.py @@ -12,9 +12,13 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals -import escpos.printer as printer +import pytest + from PIL import Image +import escpos.printer as printer +from escpos.exceptions import ImageWidthError + # Raster format print def test_bit_image_black(): @@ -139,3 +143,22 @@ def test_large_graphics(): instance = printer.Dummy() instance.image('test/resources/black_white.png', impl="bitImageRaster", fragment_height=1) assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\xc0\x1dv0\x00\x01\x00\x01\x00\x00') + + +def test_width_too_large(): + """ + Test printing an image that is too large in width. + """ + instance = printer.Dummy() + instance.profile.profile_data = { + 'media': { + 'width': { + 'pixels': 384 + } + } + } + + with pytest.raises(ImageWidthError): + instance.image(Image.new("RGB", (385, 200))) + + instance.image(Image.new("RGB", (384, 200))) \ No newline at end of file