From 46b254bc0add93dae74991928e8ef9cca703da9e Mon Sep 17 00:00:00 2001 From: Michael Billington Date: Sun, 20 Mar 2016 19:30:00 +1100 Subject: [PATCH] update encoding issue in QR code method, add ported test cases --- escpos/escpos.py | 4 +- test/test_function_qr_native.py | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 test/test_function_qr_native.py diff --git a/escpos/escpos.py b/escpos/escpos.py index bec1067..cabddbe 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -299,10 +299,10 @@ class Escpos(object): # Set error correction level: L, M, Q, or H self._send_2d_code_data(six.int2byte(69), cn, six.int2byte(48 + ec)); # Send content & print - self._send_2d_code_data(six.int2byte(80), cn, content, b'0'); + self._send_2d_code_data(six.int2byte(80), cn, content.encode('utf-8'), b'0'); self._send_2d_code_data(six.int2byte(81), cn, b'', b'0'); - def _send_2d_code_data(self, fn, cn, data, m=''): + def _send_2d_code_data(self, fn, cn, data, m=b''): """ Wrapper for GS ( k, to calculate and send correct data length. :param fn: Function to use. diff --git a/test/test_function_qr_native.py b/test/test_function_qr_native.py new file mode 100644 index 0000000..d316795 --- /dev/null +++ b/test/test_function_qr_native.py @@ -0,0 +1,81 @@ +#!/usr/bin/python +"""tests for panel button function + +:author: `Patrick Kanzler `_ +:organization: `python-escpos `_ +:copyright: Copyright (c) 2016 `python-escpos `_ +:license: GNU GPL v3 +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +from nose.tools import with_setup + +import escpos.printer as printer +import os +from escpos.constants import QR_ECLEVEL_H, QR_MODEL_1 + +devfile = 'testfile' + + +def setup_testfile(): + """create a testfile as devfile""" + fhandle = open(devfile, 'a') + try: + os.utime(devfile, None) + finally: + fhandle.close() + + +def teardown_testfile(): + """destroy testfile again""" + os.remove(devfile) + + +@with_setup(setup_testfile, teardown_testfile) +def test_function_qr_defaults(): + """test QR code with defaults""" + instance = printer.File(devfile=devfile) + instance.qr("1234", native=True) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0') + +@with_setup(setup_testfile, teardown_testfile) +def test_function_qr_empty(): + """test QR printing blank code""" + instance = printer.File(devfile=devfile) + instance.qr("", native=True) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'') + +@with_setup(setup_testfile, teardown_testfile) +def test_function_qr_ec(): + """test QR error correction setting""" + instance = printer.File(devfile=devfile) + instance.qr("1234", native=True, ec=QR_ECLEVEL_H) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0') + +@with_setup(setup_testfile, teardown_testfile) +def test_function_qr_size(): + """test QR box size""" + instance = printer.File(devfile=devfile) + instance.qr("1234", native=True, size=7) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0') + +@with_setup(setup_testfile, teardown_testfile) +def test_function_qr_model(): + """test QR model""" + instance = printer.File(devfile=devfile) + instance.qr("1234", native=True, model=QR_MODEL_1) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d(k\x07\x001P01234\x1d(k\x03\x001Q0') \ No newline at end of file