diff --git a/tests/requirements.txt b/tests/requirements.txt index 55e3ccec..08051620 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,7 +4,6 @@ pytest-cov pypng~=0.0.20 pyzbar~=0.1.8 qrcode-artistic -opencv-python; platform.python_implementation != 'PyPy' Pillow importlib-metadata>=3.6.0; python_version < '3.10' diff --git a/tests/test_encode_decode.py b/tests/test_encode_decode.py index bedd372e..d8bdca5b 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -13,19 +13,19 @@ import io import pytest import segno -_cv_available = True -try: - import cv2 as cv - import numpy as np -except (ImportError): - _cv_available = False +import platform try: from pyzbar.pyzbar import decode as zbardecode except (ImportError, FileNotFoundError): # The latter may occur under Windows pytestmark = pytest.mark.skip +_libc, _ = platform.libc_ver() +IS_MUSL = _libc != 'glibc' + -def decode_zbar(qrcode): +def decode(qrcode): + if qrcode.is_micro: + raise Exception('Cannot decode Micro QR codes') scale = 3 width, height = qrcode.symbol_size(scale=scale) out = io.BytesIO() @@ -37,29 +37,9 @@ def decode_zbar(qrcode): return decoded[0].data.decode('utf-8') -def decode_cv(qrcode): - out = io.BytesIO() - qrcode.save(out, scale=3, kind='png') - out.seek(0) - img = cv.imdecode(np.frombuffer(out.getvalue(), np.uint8), flags=cv.IMREAD_COLOR) - detector = cv.QRCodeDetector() - decoded, points, qrcode_bin = detector.detectAndDecode(img) - return decoded or None - - -def decode(qrcode): - if qrcode.is_micro: - raise Exception('Cannot decode Micro QR codes') - # OpenCV does not support Kanji - if _cv_available and qrcode.mode != 'kanji': - return decode_cv(qrcode) - return decode_zbar(qrcode) - - @pytest.mark.parametrize('content, mode', [('漢字', 'kanji'), ('続きを読む', 'kanji'), - ('Märchenbücher', 'byte'), ('汉字', 'byte'), ]) def test_encode_decode(content, mode): @@ -68,15 +48,21 @@ def test_encode_decode(content, mode): assert content == decode(qr) +@pytest.mark.skipif(IS_MUSL, reason="zbar does not support latin1 with musl") # See +def test_encode_decode_latin1(): + content = 'Märchenbücher' + qr = segno.make_qr(content) + assert 'byte' == qr.mode + assert content == decode(qr) + + def test_stackoverflow_issue(): # See # and content = 'Thomsôn Gonçalves Ámaral,325.432.123-21' qr = segno.make(content, encoding='utf-8') assert 'byte' == qr.mode - decoded = decode(qr) - if not _cv_available: - decoded = decoded.encode('shift-jis').decode('utf-8') + decoded = decode(qr).encode('shift-jis').decode('utf-8') assert content == decoded @@ -94,15 +80,6 @@ def test_pyqrcode_issue76(): assert content == decode(qr) -@pytest.mark.skipif(not _cv_available, reason="Requires OpenCV") -@pytest.mark.parametrize('encoding', [None, 'latin1', 'ISO-8859-1', 'utf-8']) -def test_issue134(encoding): - # See - content = 'Märchen' - qr = segno.make(content, encoding=encoding, micro=False) - assert 'byte' == qr.mode - assert content == decode(qr) - if __name__ == '__main__': pytest.main([__file__]) diff --git a/tests/test_issue_109_bytes.py b/tests/test_issue_109_bytes.py index d16d1e81..92714e56 100644 --- a/tests/test_issue_109_bytes.py +++ b/tests/test_issue_109_bytes.py @@ -11,6 +11,7 @@ Requires pyzbar and additional libs (libzbar0). """ +import platform import io import pytest import segno @@ -19,6 +20,11 @@ except (ImportError, FileNotFoundError): # The latter may occur under Windows pytestmark = pytest.mark.skip +_libc, _ = platform.libc_ver() + +if _libc != 'glibc': # Does not work with zbar/musl + pytestmark = pytest.mark.skip + def decode(qrcode): scale = 3