2016-08-30 10:53:31 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-08-14 23:03:36 +00:00
|
|
|
import escpos.printer as printer
|
|
|
|
from escpos.capabilities import BARCODE_B, Profile
|
|
|
|
from escpos.exceptions import BarcodeCodeError, BarcodeTypeError
|
|
|
|
|
2016-08-30 10:53:31 +00:00
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"bctype,data,expected",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"EAN13",
|
|
|
|
"4006381333931",
|
|
|
|
b"\x1ba\x01\x1dh@\x1dw\x03\x1df\x00\x1dH\x02\x1dk\x024006381333931\x00",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2016-08-30 10:53:31 +00:00
|
|
|
def test_barcode(bctype, data, expected):
|
2021-10-30 16:15:22 +00:00
|
|
|
"""should generate different barcode types correctly."""
|
2016-08-30 10:53:31 +00:00
|
|
|
instance = printer.Dummy()
|
|
|
|
instance.barcode(data, bctype)
|
|
|
|
assert instance.output == expected
|
|
|
|
|
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"bctype,supports_b",
|
|
|
|
[
|
|
|
|
("invalid", True),
|
|
|
|
("CODE128", False),
|
|
|
|
],
|
|
|
|
)
|
2016-08-30 10:53:31 +00:00
|
|
|
def test_lacks_support(bctype, supports_b):
|
2021-10-30 16:15:22 +00:00
|
|
|
"""should raise an error if the barcode type is not supported."""
|
2016-08-30 10:53:31 +00:00
|
|
|
profile = Profile(features={BARCODE_B: supports_b})
|
|
|
|
instance = printer.Dummy(profile=profile)
|
|
|
|
with pytest.raises(BarcodeTypeError):
|
2021-10-30 16:15:22 +00:00
|
|
|
instance.barcode("test", bctype)
|
2016-08-30 10:53:31 +00:00
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
assert instance.output == b""
|
2017-10-08 18:05:18 +00:00
|
|
|
|
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"bctype,data",
|
|
|
|
[
|
|
|
|
("EAN13", "AA"),
|
|
|
|
("CODE128", "{D2354AA"),
|
|
|
|
],
|
|
|
|
)
|
2017-10-08 18:05:18 +00:00
|
|
|
def test_code_check(bctype, data):
|
2021-10-30 16:15:22 +00:00
|
|
|
"""should raise an error if the barcode code is invalid."""
|
2017-10-08 18:05:18 +00:00
|
|
|
instance = printer.Dummy()
|
|
|
|
with pytest.raises(BarcodeCodeError):
|
|
|
|
instance.barcode(data, bctype)
|
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
assert instance.output == b""
|