Skip to content

Commit

Permalink
Ignore a few test cases that require OpenCV on PyPy3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
heuer committed Feb 6, 2024
1 parent f8a6247 commit a8d90f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'

20 changes: 15 additions & 5 deletions tests/test_encode_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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',
Expand All @@ -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():
Expand All @@ -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 <https://github.com/heuer/segno/issues/134>
Expand Down
20 changes: 12 additions & 8 deletions tests/test_issue_105_epc_slash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', ['/',
Expand Down

0 comments on commit a8d90f5

Please sign in to comment.