Skip to content

Commit

Permalink
code review + one more test
Browse files Browse the repository at this point in the history
  • Loading branch information
tadeubas committed Nov 25, 2023
1 parent de99bfe commit 71f9339
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
9 changes: 3 additions & 6 deletions src/krux/qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ def max_qr_bytes(max_width):
max_width -= 2 # Subtract frame width
qr_version = (max_width - 17) // 4
try:
capacity = QR_CAPACITY[qr_version - 1]
return QR_CAPACITY[qr_version - 1]
except:
capacity = QR_CAPACITY[-1]
return capacity
return QR_CAPACITY[-1]


def find_min_num_parts(data, max_width, qr_format):
Expand Down Expand Up @@ -217,9 +216,7 @@ def find_min_num_parts(data, max_width, qr_format):
num_parts = (data_length + qr_capacity - 1) // qr_capacity
# For UR, part size will be the input for "max_fragment_len"
part_size = len(data.cbor) // num_parts
part_size = max(
part_size, UR_MIN_FRAGMENT_LENGTH
)
part_size = max(part_size, UR_MIN_FRAGMENT_LENGTH)
else:
raise ValueError("Invalid format type")
return num_parts, part_size
Expand Down
14 changes: 12 additions & 2 deletions tests/test_qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_to_qr_codes(mocker, m5stickv, tdata):
for case in cases:
mocker.patch(
"krux.display.lcd",
new=mocker.MagicMock(width=mocker.MagicMock(return_value=case[2]))
new=mocker.MagicMock(width=mocker.MagicMock(return_value=case[2])),
)
display = Display()
qr_data_width = display.qr_data_width()
Expand All @@ -154,7 +154,7 @@ def test_to_qr_codes(mocker, m5stickv, tdata):
if i == total - 1:
break
except Exception as e:
print("Error:",e)
print("Error:", e)
break
i += 1
assert len(codes) == expected_parts
Expand All @@ -168,3 +168,13 @@ def test_detect_plaintext_qr(mocker, m5stickv):
)

detect_format(PLAINTEXT_QR_DATA)


def test_find_min_num_parts(m5stickv):
from krux.qr import find_min_num_parts

with pytest.raises(ValueError) as raised_ex:
find_min_num_parts("", 10, "format unknown")

assert raised_ex.type is ValueError
assert raised_ex.value.args[0] == "Invalid format type"

0 comments on commit 71f9339

Please sign in to comment.