From a8d90f51106ac62409a85be484086f7fa254614f Mon Sep 17 00:00:00 2001 From: Lars Date: Wed, 7 Feb 2024 00:06:35 +0100 Subject: [PATCH] Ignore a few test cases that require OpenCV on PyPy3.9 --- tests/requirements.txt | 2 +- tests/test_encode_decode.py | 20 +++++++++++++++----- tests/test_issue_105_epc_slash.py | 20 ++++++++++++-------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index 0b3f8ca2..6db508f1 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,7 +4,7 @@ pytest-cov pypng~=0.0.20 pyzbar~=0.1.8 qrcode-artistic -opencv-python +opencv-python; python_version > '3.9' 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 40cd4465..bedd372e 100644 --- a/tests/test_encode_decode.py +++ b/tests/test_encode_decode.py @@ -11,17 +11,21 @@ Requires pyzbar and additional libs (libzbar0). """ import io -import cv2 as cv -import numpy as np import pytest import segno +_cv_available = True +try: + import cv2 as cv + import numpy as np +except (ImportError): + _cv_available = False try: from pyzbar.pyzbar import decode as zbardecode except (ImportError, FileNotFoundError): # The latter may occur under Windows pytestmark = pytest.mark.skip -def decode_kanji(qrcode): +def decode_zbar(qrcode): scale = 3 width, height = qrcode.symbol_size(scale=scale) out = io.BytesIO() @@ -47,7 +51,9 @@ def decode(qrcode): if qrcode.is_micro: raise Exception('Cannot decode Micro QR codes') # OpenCV does not support Kanji - return decode_cv(qrcode) if qrcode.mode != 'kanji' else decode_kanji(qrcode) + if _cv_available and qrcode.mode != 'kanji': + return decode_cv(qrcode) + return decode_zbar(qrcode) @pytest.mark.parametrize('content, mode', @@ -68,7 +74,10 @@ def test_stackoverflow_issue(): content = 'Thomsôn Gonçalves Ámaral,325.432.123-21' qr = segno.make(content, encoding='utf-8') assert 'byte' == qr.mode - assert content == decode(qr) + decoded = decode(qr) + if not _cv_available: + decoded = decoded.encode('shift-jis').decode('utf-8') + assert content == decoded def test_pyqrcode_issue76(): @@ -85,6 +94,7 @@ 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 diff --git a/tests/test_issue_105_epc_slash.py b/tests/test_issue_105_epc_slash.py index c8de32ad..ac36fc4e 100644 --- a/tests/test_issue_105_epc_slash.py +++ b/tests/test_issue_105_epc_slash.py @@ -13,19 +13,23 @@ """ import io import pytest -import cv2 as cv -import numpy as np from segno.helpers import make_epc_qr +try: + from pyzbar.pyzbar import decode as zbardecode +except (ImportError, FileNotFoundError): # The latter may occur under Windows + pytestmark = pytest.mark.skip def decode(qrcode): + scale = 3 + width, height = qrcode.symbol_size(scale=scale) 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 + for row in qrcode.matrix_iter(scale=scale): + out.write(bytearray(0x0 if b else 0xff for b in row)) + decoded = zbardecode((out.getvalue(), width, height)) + assert 1 == len(decoded) + assert 'QRCODE' == decoded[0].type + return decoded[0].data.decode('utf-8') @pytest.mark.parametrize('text', ['/',