From b202cf2d70e71e5acbcb5fad43460b5501978444 Mon Sep 17 00:00:00 2001 From: odudex Date: Fri, 30 Aug 2024 11:50:51 -0300 Subject: [PATCH] add tests --- src/krux/light.py | 2 + tests/conftest.py | 11 +++++- tests/pages/test_mnemonic_editor.py | 24 +++++++++++- tests/pages/test_qr_capture.py | 4 +- tests/pages/test_tiny_seed.py | 4 +- tests/shared_mocks.py | 60 +++++++++++++++++++++++++++++ tests/test_display.py | 2 +- tests/test_light.py | 19 +++++++++ tests/test_power.py | 6 +-- 9 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/krux/light.py b/src/krux/light.py index 78c91de30..a6aebe6e7 100644 --- a/src/krux/light.py +++ b/src/krux/light.py @@ -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 return self.circuit.value() == 0 def turn_on(self): diff --git a/tests/conftest.py b/tests/conftest.py index 7b5ef33a3..4597507c2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,7 @@ board_dock, board_cube, board_m5stickv, + board_wonder_mv, encode_to_string, encode, statvfs, @@ -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) diff --git a/tests/pages/test_mnemonic_editor.py b/tests/pages/test_mnemonic_editor.py index be408deee..8f3486b22 100644 --- a/tests/pages/test_mnemonic_editor.py +++ b/tests/pages/test_mnemonic_editor.py @@ -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 @@ -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 @@ -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, @@ -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, diff --git a/tests/pages/test_qr_capture.py b/tests/pages/test_qr_capture.py index 3903324f9..2ef317a11 100644 --- a/tests/pages/test_qr_capture.py +++ b/tests/pages/test_qr_capture.py @@ -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 @@ -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) diff --git a/tests/pages/test_tiny_seed.py b/tests/pages/test_tiny_seed.py index 389749687..53ffa9748 100644 --- a/tests/pages/test_tiny_seed.py +++ b/tests/pages/test_tiny_seed.py @@ -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 @@ -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 diff --git a/tests/shared_mocks.py b/tests/shared_mocks.py index 70406b3c8..bc2b9ba6f 100644 --- a/tests/shared_mocks.py +++ b/tests/shared_mocks.py @@ -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 diff --git a/tests/test_display.py b/tests/test_display.py index d8285547e..2861d64c2 100644 --- a/tests/test_display.py +++ b/tests/test_display.py @@ -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 diff --git a/tests/test_light.py b/tests/test_light.py index 565d9e21c..3a4d34cb1 100644 --- a/tests/test_light.py +++ b/tests/test_light.py @@ -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() diff --git a/tests/test_power.py b/tests/test_power.py index d7b443d0a..ea91a4d64 100644 --- a/tests/test_power.py +++ b/tests/test_power.py @@ -1,4 +1,4 @@ -def test_init(mocker, all_devices): +def test_init(mocker, multiple_devices): from krux.power import PowerManager manager = PowerManager() @@ -6,7 +6,7 @@ def test_init(mocker, all_devices): assert isinstance(manager, PowerManager) -def test_pmu(mocker, all_devices): +def test_pmu(mocker, multiple_devices): from krux.power import PowerManager import board @@ -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