Skip to content

Commit

Permalink
Dropped OpenCV in favour of skipping a few tests which are known to f…
Browse files Browse the repository at this point in the history
…ail with zbar/musl
  • Loading branch information
heuer committed Feb 7, 2024
1 parent 43872af commit 7fc8be3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
1 change: 0 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'

55 changes: 16 additions & 39 deletions tests/test_encode_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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 <https://github.com/heuer/segno/issues/134>
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 <https://stackoverflow.com/questions/63303624/generating-and-reading-qr-codes-with-special-characters>
# and <https://github.com/NaturalHistoryMuseum/pyzbar/issues/14>
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


Expand All @@ -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 <https://github.com/heuer/segno/issues/134>
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__])
6 changes: 6 additions & 0 deletions tests/test_issue_109_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Requires pyzbar and additional libs (libzbar0).
"""
import platform
import io
import pytest
import segno
Expand All @@ -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
Expand Down

0 comments on commit 7fc8be3

Please sign in to comment.