Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odudex committed Aug 30, 2024
1 parent a77a03a commit b202cf2
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/krux/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(self, board_io_pin, kendryte_gpio, maixpy_gpio):

def is_on(self):
"""Returns a boolean indicating if the circuit is currently on"""
if board.config["type"] == "wonder_mv":
return self.circuit.value() == 1

Check warning on line 38 in src/krux/light.py

View check run for this annotation

Codecov / codecov/patch

src/krux/light.py#L38

Added line #L38 was not covered by tests
return self.circuit.value() == 0

def turn_on(self):
Expand Down
11 changes: 10 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
board_dock,
board_cube,
board_m5stickv,
board_wonder_mv,
encode_to_string,
encode,
statvfs,
Expand Down Expand Up @@ -92,6 +93,14 @@ def cube(monkeypatch, mp_modules):
reset_krux_modules()


@pytest.fixture
def wonder_mv(monkeypatch, mp_modules):
import sys

monkeypatch.setitem(sys.modules, "board", board_wonder_mv())
reset_krux_modules()


@pytest.fixture(params=["amigo", "m5stickv", "dock", "cube"])
def all_devices(request):
def multiple_devices(request):
return request.getfixturevalue(request.param)
24 changes: 23 additions & 1 deletion tests/pages/test_mnemonic_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
MNEMONICS = [TEST_12_WORD_MNEMONIC, TEST_24_WORD_MNEMONIC]


def test_confirm_given_words(mocker, all_devices):
def test_confirm_given_words(mocker, multiple_devices):
from krux.pages.mnemonic_editor import MnemonicEditor
from krux.input import BUTTON_ENTER
import board
Expand All @@ -23,6 +23,24 @@ def test_confirm_given_words(mocker, all_devices):
assert ctx.input.wait_for_button.call_count == len(btn_sequence)


def test_loop_through_words(mocker, cube):
from krux.pages.mnemonic_editor import MnemonicEditor
from krux.input import BUTTON_PAGE, BUTTON_PAGE_PREV, BUTTON_ENTER

for mnemonic in MNEMONICS:
len_words = len(mnemonic.split())
btn_sequence = [
*([BUTTON_PAGE] * (len_words + 2)), # Loop forward
*([BUTTON_PAGE_PREV] * (len_words + 2)), # Loop backward
BUTTON_ENTER, # Confirm
]
ctx = create_ctx(mocker, btn_sequence)
mnemonic_editor = MnemonicEditor(ctx, mnemonic)
edited_mnemonic = mnemonic_editor.edit()
assert edited_mnemonic == mnemonic
assert ctx.input.wait_for_button.call_count == len(btn_sequence)


def test_edit_new_mnemonic_using_buttons(mocker, cube):
from krux.pages.mnemonic_editor import MnemonicEditor
from krux.input import BUTTON_ENTER, BUTTON_PAGE, BUTTON_PAGE_PREV
Expand Down Expand Up @@ -193,6 +211,9 @@ def test_edit_existing_mnemonic_using_touch(mocker, amigo):
1,
1,
1, # Confirm cabbage
25, # Try to "Go" with invalid checksum word
24, # Press "Esc"
0, # Give up to Esc
22, # index 23 = word 12
21, # Type v, e, n, d-> vendor
4,
Expand All @@ -208,6 +229,7 @@ def test_edit_existing_mnemonic_using_touch(mocker, amigo):
1,
1,
1, # Confirm cabbage
25, # Try to "Go" with invalid checksum word
23, # index 23 = word 24
22, # Type w, i, t -> witness
8,
Expand Down
4 changes: 2 additions & 2 deletions tests/pages/test_qr_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)


def test_capture_qr_code(mocker, all_devices, tdata):
def test_capture_qr_code(mocker, multiple_devices, tdata):
from krux.pages.qr_capture import QRCodeCapture
from krux.qr import FORMAT_PMOFN, FORMAT_UR
from ur.ur import UR
Expand Down Expand Up @@ -114,7 +114,7 @@ def toggle_antiglare():
)


def test_light_control(mocker, all_devices):
def test_light_control(mocker, multiple_devices):
from krux.pages.qr_capture import QRCodeCapture

time_mocker = TimeMocker(1001)
Expand Down
4 changes: 2 additions & 2 deletions tests/pages/test_tiny_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def test_scan_tiny_seed_12w(m5stickv, mocker):
assert " ".join(words) == TEST_12_WORDS


def test_scan_tiny_seed_24w(all_devices, mocker):
def test_scan_tiny_seed_24w(multiple_devices, mocker):
# This will be used when scanning 24 TinySeed
# First scanned page will be loaded to be edited, then proceed to scan second page
# Seed will be returned as its word index
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_scan_tiny_seed_24w(all_devices, mocker):
assert " ".join(words) == TEST_24_WORDS


def test_tinyscanner_initializes_tinyseed_with_label(all_devices, mocker):
def test_tinyscanner_initializes_tinyseed_with_label(multiple_devices, mocker):
import pytest
from krux.pages.tiny_seed import TinyScanner

Expand Down
60 changes: 60 additions & 0 deletions tests/shared_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,66 @@ def board_cube():
)


def board_wonder_mv():
return mock.MagicMock(
config={
"type": "wonder_mv",
"lcd": {
"dcx": 8,
"ss": 6,
"rst": 21,
"clk": 7,
"height": 240,
"width": 320,
"invert": 1,
"dir": 96,
"lcd_type": 0,
},
"sensor": {
"pin_sda": 9,
"cmos_href": 17,
"cmos_pclk": 15,
"cmos_xclk": 14,
"cmos_pwdn": 13,
"cmos_vsync": 12,
"reg_width": 16,
"i2c_num": 2,
"pin_clk": 10,
"cmos_rst": 11,
},
"sdcard": {
"sclk": 29,
"mosi": 30,
"miso": 31,
"cs": 32,
},
"board_info": {
"BOOT_KEY": 16,
"CONNEXT_A": 28,
"CONNEXT_B": 27,
"I2C_SDA": 19,
"I2C_SCL": 18,
"SPI_SCLK": 29,
"SPI_MOSI": 30,
"SPI_MISO": 31,
"SPI_CS": 32,
},
"krux": {
"pins": {
"BUTTON_A": 26,
"BUTTON_B": 47,
"LED_W": 25,
"TOUCH_IRQ": 41,
"I2C_SDA": 19,
"I2C_SCL": 18,
"BACKLIGHT": 23,
},
"display": {"touch": True, "font": [8, 16], "font_wide": [16, 16]},
},
}
)


def mock_context(mocker):
import board

Expand Down
2 changes: 1 addition & 1 deletion tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def string_width_px(string):
return string_width


def test_init(mocker, all_devices):
def test_init(mocker, multiple_devices):
mocker.patch("krux.display.lcd", new=mocker.MagicMock())
import krux
from krux.display import Display
Expand Down
19 changes: 19 additions & 0 deletions tests/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ def test_toggle_from_on(mocker, m5stickv):
light.toggle()

light.turn_off.assert_called()


def test_toggle_on_wonder_mv(mocker, wonder_mv):
mock_modules(mocker)
from krux.light import Light

light = Light()
mocker.spy(light, "turn_off")
mocker.spy(light, "turn_on")

# Toggle from on
mocker.patch.object(light, "is_on", new=lambda: True)
light.toggle()
light.turn_off.assert_called()

# Toggle again, now from off
mocker.patch.object(light, "is_on", new=lambda: False)
light.toggle()
light.turn_on.assert_called()
6 changes: 3 additions & 3 deletions tests/test_power.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def test_init(mocker, all_devices):
def test_init(mocker, multiple_devices):
from krux.power import PowerManager

manager = PowerManager()

assert isinstance(manager, PowerManager)


def test_pmu(mocker, all_devices):
def test_pmu(mocker, multiple_devices):
from krux.power import PowerManager
import board

Expand All @@ -20,7 +20,7 @@ def test_pmu(mocker, all_devices):
manager.has_battery() is True


def test_charge_remaining(mocker, all_devices):
def test_charge_remaining(mocker, multiple_devices):
from krux.power import PowerManager
import board

Expand Down

0 comments on commit b202cf2

Please sign in to comment.