Controller Board Mock Class
This class implements the controller base class API for testing purposes, providing alternative methods useful for unit testing environments.
- class firmware.board_mock.BoardMock(watchdog_enabled: bool = True, verbose: bool = False, throw_rtc: bool = False, throw_http: bool = False, bad_http_get_status: bool = False, bad_http_put_status: bool = False, wifi_connect: bool = True, ds18x20_read_failure: bool = False, continue_running_after_first_iteration: bool = False, fixed_temperature_c: float = 1000, convert_temp_failure: bool = False, bad_ntp_timestamp: bool = False, label_missing_from_rom_hex_map: bool = False, label_missing_from_sensors: bool = False, empty_ds18x20_roms: bool = False, override_wifi_ssids: list | None = None)
Bases:
BoardBaseThis mock board class implements the required board API, with just enough implementation to mimic for testing.
- active(active: bool) None
Sets the Wi-Fi active status for this mock board
- Parameters:
active – The desired active mode, True or False
- Returns:
Nothing
- config(key: str) str
This mock config function is only designed to return the config for ‘ssid’, other keys will throw an error.
- Parameters:
key – The config key to request, which can only be ‘ssid’ for this mock class
- Returns:
The config value requested, which can only be the assigned ssid for this mock class
- connect(ssid: str, pw: str) None
This mock connect function will connect if the wifi_connect class argument was not False and the requested ssid is in the list of known ssids.
- Parameters:
ssid – The network ssid
pw – The network password
- Returns:
Nothing
- create_watchdog(timeout_ms: int) None
Mocks the watchdog creation by setting a flag to indicate that the watchdog is on patrol.
- Parameters:
timeout_ms – Not used in this mock class
- Returns:
Nothing
- developer_mode() bool
The developer flag is purely based on the passed in watchdog flag
- Returns:
Developer mode status; default is False,
- ds18x20_convert_temp() None
Mocks the convert_temp functionality, which prepares the DS18x20 sensors for reading. If the convert_temp_failure flag is active, this will raise an exception, otherwise it does nothing.
- Returns:
Nothing
- ds18x20_read_temp(rom: bytes) float
Mocks the functionality of reading the temperature for a specific ROM on the one-wire connection. If the ds18x20_read_failure flag is active, it will raise an OSError. If the fixed_temperature_c flag was active, then the provided value will be returned. Otherwise, this returns 20
- Parameters:
rom – Not used in this mock class, at this time anyway
- Returns:
The mocked sensed temperature in Celsius
- ds18x20_scan() list[bytes]
Mocks the DS18x20 scan step to return found sensor ROMs in bytes. If the empty_ds18x20_roms flag is active, it will return an empty list.
- Returns:
A list of byte strings, one for each sensor found.
- feed_watchdog() None
Mocks the watchdog feed step, but does not do anything here
- Returns:
Nothing
- get_ntp_timestamp() int | None
Mocks the NTP timestamp request by simply returning a Linux timestamp value for the current time. If bad_ntp_timestamp is active, it will return None to mimic a bad request.
- Returns:
A fixed timestamp value or None
- http_get(url: str) ResponseBase
Mocks an HTTP GET by creating a response object sensitive to control flags. If throw_http is active, it will result in an exception. If bad_http_get_status is active, it will return an erroneous status code.
- Parameters:
url – The URL to mock a GET request
- Returns:
A ResponseBase object
- http_put(url: str, headers: dict, json: dict) ResponseBase
Mocks an HTTP PUT by creating a response object sensitive to control flags. If throw_http is active, it will result in an exception. If bad_http_put_status is active, it will return an erroneous status code.
- Parameters:
url – The url to request
headers – Additional data, which could include branch name, data mime type, authentication, etc.
json – A dict payload to submit with the PUT request
- Returns:
A ResponseBase object
- ifconfig() tuple[str, str, str, str]
Returns a dummy ifconfig of a fake IP plus blanks for the other terms
- Returns:
A dummy tuple of network configuration (IP, Subnet, Gateway, DNS)
- isconnected() bool
Returns whether the mock board appears to be connected to the Wi-Fi network. If it has not been activated, it will fail. If it has, it returns whether it is connected.
- Returns:
True or False if connected or not
- load_json(json_readable_bytes) dict
Mocks the JSON reading function by simply returning a premade dictionary. If label_missing_from_rom_hex_map is active, there will be a missing ROM in the hex map. If the label_missing_from_sensors flag is active, there will be a missing sensor in the sensors map.
- Parameters:
json_readable_bytes – Not used in this mock class
- Returns:
A dictionary mimicking the configuration found on the repo
- localtime(linux_time_seconds: int | None = None) tuple
Mocks the localtime function by using datetime to return the current date and time, or the converted time if passed in.
- Parameters:
linux_time_seconds – An optional Linux timestamp
- Returns:
The localtime as a tuple (year, month, day, hour, minute, second, weekday, year-day)
- print(message: str) None
Mocks the print functionality by tracking the printed messages in a variable. This can be inspected by unit tests to ensure messages were printed to the terminal.
- Parameters:
message – Message to be printed
- Returns:
Nothing
- rtc_datetime(timestamp: tuple[int, int, int, int, int, int, int, int]) None
Mocks the RTC datetime assignment by simply unpacking the timestamp argument tuple. If throw_rtc is active, it will raise an exception.
- Parameters:
timestamp – A timestamp of (year, month, day, weekday, hour, minute, second, microsecond)
- Returns:
Nothing
- run_forever() bool
Specifies the run_forever mode for this mock class as False.
- Returns:
Bool of False for this mock class
- scan() list[tuple[bytes, bytes, int, int, int, int]]
This mock scan function simply returns a list of Wi-Fi network information with mostly dummy values
- Returns:
A list of network tuples, where each contains: (ssid, bssid, channel, RSSI, security, hidden)
- sleep(seconds: float) None
Mocks the sleep functionality by doing nothing.
- Parameters:
seconds – Ignored in this mock class
- Returns:
Nothing
- system_hang(seconds: int | None = None) None
The real system_hang function can hold indefinitely or for a fixed time. This mock function handles the finite time version by simply returning - not waiting. This mock function handles the infinite hang by calling an exception.
- Parameters:
seconds – An optional number of seconds to hang. If None, an exception will be raised.
- Returns:
Nothing
- ticks_diff(milliseconds_a: int, milliseconds_b: int) int
Mocks the ticks_diff function by always returning a large value, indicating time has passed. In the real function, there is functionality to try to handle time overflow for large values.
- Parameters:
milliseconds_a – The larger value from ticks_ms
milliseconds_b – The smaller value from ticks_ms
- Returns:
In this mock class, this returns a fixed large value
- ticks_ms() int
Mocks the ticks_ms function by keeping an internal clock and reporting the ms since it was created.
- Returns:
An increasing number of milliseconds.
- class firmware.board_mock.PinMock(pin_id: str | int, direction: int = 0, pull: int = 0)
Bases:
PinBaseThis mock Pin class implements the required Pin API, with just enough implementation to mimic for testing.
- IN = 0
A Pin IN value indicates the pin is in a sensor mode, awaiting data, such as for buttons
- OUT = 1
A Pin OUT value indicates the pin mode is being driven from the board, such as for LEDs
- PULL_UP = 0
PULL_UP indicates the pin is pulled up to VCC through a small resistor, yielding a ‘high’ value
- off() None
Sets the pin to an OFF state.
- on() None
Sets the pin to an ON state.
- toggle() None
Toggles the pin between ON and OFF states.
- value() int
Gets the current ON/OFF value of the Pin. :return: 1 if the Pin is ON, 0 otherwise
- class firmware.board_mock.ResponseMock(throw: bool = False, bad_status: bool = False)
Bases:
ResponseBaseThis mock response class implements the response API, but with behavior easily set up for testing.
- close() None
Since this response class is not actually holding any data, this does nothing
- Returns:
Nothing
- status_code
The status_code can be overridden by passing a bad_status