Skip to content

Commit

Permalink
Added Unit Test for Motor Cruise Control Toggle Functionality (#23)
Browse files Browse the repository at this point in the history
* Write test for battery relay status

* Added unit test for motor cruise control

* Apply setUp and tearDown

* added check for OFF state when array relay not held and cruise control toggled off

* Make tests functional

---------

Co-authored-by: Juho Kim <[email protected]>
Co-authored-by: alexlewis9 <[email protected]>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent dc446db commit 8ace232
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 17 deletions.
24 changes: 7 additions & 17 deletions revolution/tests/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from iclib.nhd_c12864a1z_fsw_fbw_htt import NHDC12864A1ZFSWFBWHTT
from periphery import GPIO, PWM, Serial

from revolution import (
from revolution import ( # noqa: F401
Application,
Contexts,
Debugger,
Expand All @@ -28,7 +28,7 @@
Miscellaneous,
Motor,
Power,
Telemetry,
# Telemetry,
)

CONTEXTS: Contexts = Contexts(
Expand Down Expand Up @@ -81,18 +81,8 @@
)
)

STEERING_WHEEL_MCP23S17: MCP23S17 = MCP23S17(
MagicMock(),
MagicMock(),
MagicMock(),
MagicMock(
mode=0b11,
max_speed=1e6,
bit_order='msb',
bits_per_word=8,
extra_flags=None,
transfer=lambda data: [0] * len(data),
),
STEERING_WHEEL_MCP23S17: MCP23S17 = MagicMock(
read_register=lambda *_: [0xFF],
)

SHIFT_SWITCH_PRB: PRB = MagicMock()
Expand All @@ -104,17 +94,17 @@
HORN_SWITCH_PRBS: PRBS = MagicMock()
BACKUP_CAMERA_CONTROL_SWITCH_PRBS: PRBS = MagicMock()
DISPLAY_BACKLIGHT_SWITCH_PRBS: PRBS = MagicMock()
BRAKE_SWITCH_GPIO: GPIO = MagicMock()
BRAKE_SWITCH_GPIO: GPIO = MagicMock(read=lambda *_: False)

ACCELERATION_INPUT_ROTARY_ENCODER_A_PRBS: PRBS = MagicMock()
ACCELERATION_INPUT_ROTARY_ENCODER_B_PRBS: PRBS = MagicMock()
DIRECTION_SWITCH_PRBS: PRBS = MagicMock()
REGENERATION_SWITCH_PRBS: PRBS = MagicMock()
VARIABLE_FIELD_MAGNET_UP_SWITCH_PRBS: PRBS = MagicMock()
VARIABLE_FIELD_MAGNET_DOWN_SWITCH_PRBS: PRBS = MagicMock()
CRUISE_CONTROL_SWITCH_PRBS: PRBS = MagicMock()
CRUISE_CONTROL_SWITCH_PRBS: PRBS = PRB.GPIOA_GP4

ARRAY_RELAY_SWITCH_PRBS: PRBS = MagicMock()
ARRAY_RELAY_SWITCH_PRBS: PRBS = PRB.GPIOA_GP0
BATTERY_RELAY_SWITCH_PRBS: PRBS = MagicMock()

INDICATOR_LIGHTS_PWM: PWM = MagicMock()
Expand Down
111 changes: 111 additions & 0 deletions revolution/tests/test_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from dataclasses import replace
from threading import Thread
from time import sleep
from typing import ClassVar
from unittest import TestCase, main

from door.threading2 import AcquirableDoor

from revolution.driver import Driver
from revolution.environment import Environment, Header, Message
from revolution.tests import configurations


class DriverTestCase(TestCase):
TIMEOUT: ClassVar[float] = 0.1

def setUp(self) -> None:
self.environment: Environment = Environment(
AcquirableDoor(replace(configurations.CONTEXTS)),
replace(configurations.PERIPHERIES),
replace(configurations.SETTINGS),
)
self.driver_thread: Thread = Thread(
target=Driver.main,
args=(self.environment,),
)

self.driver_thread.start()
sleep(self.TIMEOUT)

def tearDown(self) -> None:
self.environment.send_all(Message(Header.STOP))
self.driver_thread.join()

def test_array_relay_status(self) -> None:
with self.environment.contexts() as contexts:
self.assertFalse(contexts.power_array_relay_status_input)

(
configurations # type: ignore[method-assign]
.STEERING_WHEEL_MCP23S17
.read_register
) = lambda *_: [0]

sleep(self.TIMEOUT)

with self.environment.contexts() as contexts:
self.assertTrue(contexts.power_array_relay_status_input)

(
configurations # type: ignore[method-assign]
.STEERING_WHEEL_MCP23S17
.read_register
) = lambda *_: [0xFF]

sleep(self.TIMEOUT)

with self.environment.contexts() as contexts:
self.assertFalse(contexts.power_array_relay_status_input)

def test_motor_cruise_control_status(self) -> None:
with self.environment.contexts() as contexts:
self.assertFalse(contexts.motor_cruise_control_status_input)

(
configurations # type: ignore[method-assign]
.STEERING_WHEEL_MCP23S17
.read_register
) = lambda *_: [0]

sleep(self.TIMEOUT)

with self.environment.contexts() as contexts:
self.assertTrue(contexts.motor_cruise_control_status_input)

(
configurations # type: ignore[method-assign]
.STEERING_WHEEL_MCP23S17
.read_register
) = lambda *_: [0xFF]

sleep(self.TIMEOUT)

with self.environment.contexts() as contexts:
self.assertTrue(contexts.motor_cruise_control_status_input)

(
configurations # type: ignore[method-assign]
.STEERING_WHEEL_MCP23S17
.read_register
) = lambda *_: [0]

sleep(self.TIMEOUT)

with self.environment.contexts() as contexts:
self.assertFalse(contexts.motor_cruise_control_status_input)

(
configurations # type: ignore[method-assign]
.STEERING_WHEEL_MCP23S17
.read_register
) = lambda *_: [0xFF]

sleep(self.TIMEOUT)

with self.environment.contexts() as contexts:
self.assertFalse(contexts.motor_cruise_control_status_input)


if __name__ == '__main__':
main() # pragma: no cover

0 comments on commit 8ace232

Please sign in to comment.