2016-03-19 13:48:29 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
"""test native QR code printing
|
|
|
|
|
|
|
|
:author: `Michael Billington <michael.billington@gmail.com>`_
|
|
|
|
:organization: `python-escpos <https://github.com/python-escpos>`_
|
2023-12-05 23:25:36 +00:00
|
|
|
:copyright: Copyright (c) 2023 `Michael Billington <michael.billington@gmail.com>`_
|
2017-01-29 23:39:43 +00:00
|
|
|
:license: MIT
|
2016-03-19 13:48:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2017-08-31 07:25:35 +00:00
|
|
|
import pytest
|
|
|
|
|
2016-03-19 13:48:29 +00:00
|
|
|
import escpos.printer as printer
|
|
|
|
from escpos.constants import QR_ECLEVEL_H, QR_MODEL_1
|
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_defaults():
|
|
|
|
"""Test QR code with defaults"""
|
|
|
|
instance = printer.Dummy()
|
2016-03-19 13:48:29 +00:00
|
|
|
instance.qr("1234", native=True)
|
2021-10-30 16:15:22 +00:00
|
|
|
expected = (
|
|
|
|
b"\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d"
|
|
|
|
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
|
|
|
|
)
|
|
|
|
assert instance.output == expected
|
|
|
|
|
2016-03-19 13:48:29 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_empty():
|
|
|
|
"""Test QR printing blank code"""
|
|
|
|
instance = printer.Dummy()
|
2016-03-19 13:48:29 +00:00
|
|
|
instance.qr("", native=True)
|
2021-10-30 16:15:22 +00:00
|
|
|
assert instance.output == b""
|
2016-03-19 13:48:29 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_ec():
|
|
|
|
"""Test QR error correction setting"""
|
|
|
|
instance = printer.Dummy()
|
2016-03-19 13:48:29 +00:00
|
|
|
instance.qr("1234", native=True, ec=QR_ECLEVEL_H)
|
2021-10-30 16:15:22 +00:00
|
|
|
expected = (
|
|
|
|
b"\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d"
|
|
|
|
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
|
|
|
|
)
|
|
|
|
assert instance.output == expected
|
2016-03-19 13:48:29 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_size():
|
|
|
|
"""Test QR box size"""
|
|
|
|
instance = printer.Dummy()
|
2016-03-19 13:48:29 +00:00
|
|
|
instance.qr("1234", native=True, size=7)
|
2021-10-30 16:15:22 +00:00
|
|
|
expected = (
|
|
|
|
b"\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d"
|
|
|
|
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
|
|
|
|
)
|
|
|
|
assert instance.output == expected
|
2016-03-19 13:48:29 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_model():
|
|
|
|
"""Test QR model"""
|
|
|
|
instance = printer.Dummy()
|
2016-03-19 13:48:29 +00:00
|
|
|
instance.qr("1234", native=True, model=QR_MODEL_1)
|
2021-10-30 16:15:22 +00:00
|
|
|
expected = (
|
|
|
|
b"\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d"
|
|
|
|
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
|
|
|
|
)
|
|
|
|
assert instance.output == expected
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_invalid_ec():
|
|
|
|
"""Test invalid QR error correction"""
|
|
|
|
instance = printer.Dummy()
|
2023-08-09 22:18:02 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
instance.qr("1234", native=True, ec=-1)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_invalid_size():
|
|
|
|
"""Test invalid QR size"""
|
|
|
|
instance = printer.Dummy()
|
2023-08-09 22:18:02 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
instance.qr("1234", native=True, size=0)
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_invalid_model():
|
|
|
|
"""Test invalid QR model"""
|
|
|
|
instance = printer.Dummy()
|
2023-08-09 22:18:02 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
instance.qr("1234", native=True, model="Hello")
|
2016-04-03 07:36:54 +00:00
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
|
2016-04-03 07:36:54 +00:00
|
|
|
def test_image_invalid_model():
|
|
|
|
"""Test unsupported QR model as image"""
|
|
|
|
instance = printer.Dummy()
|
2023-08-09 22:18:02 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
instance.qr("1234", native=False, model=QR_MODEL_1)
|
2017-08-31 07:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def instance():
|
|
|
|
return printer.Dummy()
|
|
|
|
|
|
|
|
|
|
|
|
def test_center_not_implementer(instance):
|
|
|
|
with pytest.raises(NotImplementedError):
|
2018-05-13 16:26:48 +00:00
|
|
|
instance.qr("test", center=True, native=True)
|