From c4ae79676421a8b6d304c9a74c785eafe1cf43e2 Mon Sep 17 00:00:00 2001 From: Florent de Labarre Date: Tue, 23 Jan 2018 00:56:02 +0100 Subject: [PATCH] [IMP] Add missing tests for graphics ZPL2 feature --- printer_zpl2/models/printing_label_zpl2.py | 3 +- .../tests/test_printing_label_zpl2.py | 95 +++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/printer_zpl2/models/printing_label_zpl2.py b/printer_zpl2/models/printing_label_zpl2.py index 27d668c..f81b0d9 100644 --- a/printer_zpl2/models/printing_label_zpl2.py +++ b/printer_zpl2/models/printing_label_zpl2.py @@ -121,8 +121,9 @@ class PrintingLabelZpl2(models.Model): zpl2.ARG_ROUNDING: component.rounding, }) elif component.component_type == 'graphic': + image = component.graphic_image or data pil_image = Image.open(io.BytesIO( - base64.b64decode(component.graphic_image or data))) + base64.b64decode(image))) if component.width and component.height: pil_image = pil_image.resize( (component.width, component.height)) diff --git a/printer_zpl2/tests/test_printing_label_zpl2.py b/printer_zpl2/tests/test_printing_label_zpl2.py index b845547..f1e95f5 100644 --- a/printer_zpl2/tests/test_printing_label_zpl2.py +++ b/printer_zpl2/tests/test_printing_label_zpl2.py @@ -3,10 +3,17 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import mock +import logging from odoo import exceptions from odoo.tests.common import TransactionCase +_logger = logging.getLogger(__name__) + +try: + import zpl2 +except ImportError: + _logger.debug('Cannot `import zpl2`.') model = 'odoo.addons.base_report_to_printer.models.printing_server' @@ -974,3 +981,91 @@ class TestPrintingLabelZpl2(TransactionCase): '^JUR\n' # Label end '^XZ'.format(contents=data)) + + def test_graphic_label_contents_blank(self): + """ Check contents of a image label """ + label = self.new_label() + data = 'R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==' + self.new_component({ + 'label_id': label.id, + 'component_type': 'graphic', + 'data': '"' + data + '"', + }) + contents = label._generate_zpl2_data(self.printer).decode("utf-8") + self.assertEqual( + contents, + "^XA\n" + "^PW480\n" + "^CI28\n" + "^LH10,10\n" + "^FO10,10^GFA,1.0,1.0,1.0,b'00'^FS\n" + "^JUR\n" + "^XZ") + + def test_graphic_label_contents_blank_rotated(self): + """ Check contents of image rotated label """ + label = self.new_label() + data = 'R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==' + self.new_component({ + 'label_id': label.id, + 'component_type': 'graphic', + 'data': '"' + data + '"', + 'height': 10, + 'width': 10, + 'reverse_print': 1, + 'orientation': zpl2.ORIENTATION_ROTATED, + }) + contents = label._generate_zpl2_data(self.printer).decode("utf-8") + self.assertEqual( + contents, + "^XA\n" + "^PW480\n" + "^CI28\n" + "^LH10,10\n" + "^FO10,10^GFA,20.0,20.0,2.0," + "b'FFC0FFC0FFC0FFC0FFC0FFC0FFC0FFC0FFC0FFC0'^FS\n" + "^JUR\n" + "^XZ") + + def test_graphic_label_contents_blank_inverted(self): + """ Check contents of a image inverted label """ + label = self.new_label() + data = 'R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==' + self.new_component({ + 'label_id': label.id, + 'component_type': 'graphic', + 'data': '"' + data + '"', + 'orientation': zpl2.ORIENTATION_INVERTED, + }) + contents = label._generate_zpl2_data(self.printer).decode("utf-8") + self.assertEqual( + contents, + "^XA\n" + "^PW480\n" + "^CI28\n" + "^LH10,10\n" + "^FO10,10^GFA,1.0,1.0,1.0,b'00'^FS\n" + "^JUR\n" + "^XZ") + + def test_graphic_label_contents_blank_bottom(self): + """ Check contents of a image bottom label """ + label = self.new_label() + data = 'R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==' + self.new_component({ + 'label_id': label.id, + 'component_type': 'graphic', + 'data': '"' + data + '"', + 'orientation': zpl2.ORIENTATION_BOTTOM_UP, + }) + contents = label._generate_zpl2_data(self.printer).decode("utf-8") + self.assertEqual( + contents, + "^XA\n" + "^PW480\n" + "^CI28\n" + "^LH10,10\n" + "^FO10,10^GFA,1.0,1.0,1.0,b'00'^FS\n" + "^JUR\n" + "^XZ") +