Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Add a mock interface, take two #4

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions turret_python_interface/mock_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Provides a mock Interface class for testing purposes, to decouple tests from requiring
the entire turret hardware stack.
"""

from math import tau, pi, sin
from pathlib import Path
from types import TracebackType
from typing import Optional, Type

from .interface import Interface
from .datamodel.telemetry_packet import TelemetryPacket


class MockInterface(Interface):
# noinspection PyMissingConstructor
def __init__(
self,
*,
serial_path: Path = Path("/") / "dev" / "ttyS0",
baud: int = 115200,
timeout=30,
):
# NOTE: we intentionally don't call the constructor of the superclass as it would
# init a serial connection; which is an implementation detail
# and we cannot mock it effectively.
# NOTE: the internal data members of this type are not mocked, as they are implementation
# details. mocking them is unnecessary.
self.t = 0
...

@property
def state(self) -> float:
# basically, we are reproducing a sine wave.
# Every time the state is observed, time progresses.

# preserve the current time
t = self.t
# increment t for the next tick
self.t += pi / 4
if self.t >= tau:
self.t = 0

return sin(t)

def __enter__(self) -> Interface:
...

def __exit__(
self,
__exc_type: Optional[Type[BaseException]],
__exc_value: Optional[BaseException],
__traceback: Optional[TracebackType],
) -> Optional[bool]:
...

def get_telemetry(self) -> TelemetryPacket:
return TelemetryPacket(turret_pos=self.state)