Coverage for .tox/py313/lib/python3.13/site-packages/pydalec/transport/mock.py: 78%

23 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-05-26 22:48 +0200

1"""In-memory synchronous mock transport for DALEC testing.""" 

2 

3from pydalec.transport.base import BaseTransport 

4 

5 

6class MockTransport(BaseTransport): 

7 """Mock synchronous transport that simulates DALEC responses.""" 

8 

9 def __init__( 

10 self, 

11 delay: float = 0.0, 

12 error_rate: float = 0.0, 

13 data_root_dir: str | None = None, 

14 max_file_size_kb: int = 51200, 

15 ): 

16 """Initialize mock behavior options for delay and error simulation.""" 

17 super().__init__() 

18 del data_root_dir, max_file_size_kb # TODO: implement file handling in the mock transport 

19 self.delay = delay 

20 self.error_rate = error_rate 

21 self._making_measurements = False 

22 self._connected = True 

23 

24 def start_measurements(self) -> None: 

25 """Start the background process for making and receiving measurements.""" 

26 self._making_measurements = True 

27 

28 def stop_measurements(self) -> None: 

29 """Stop the background process for making and receiving measurements.""" 

30 self._making_measurements = False 

31 

32 def send(self, data: str) -> None: 

33 """Store the latest command so a response can be generated.""" 

34 self._last_cmd: str = data.strip() 

35 

36 def disconnect(self) -> None: 

37 """Release mock transport resources.""" 

38 self._connected = False 

39 

40 def connect(self) -> None: 

41 """Reconnect the mock transport.""" 

42 self._connected = True 

43 

44 def __repr__(self) -> str: 

45 """Representation of Mock instance. 

46 

47 Returns: 

48 str: formatted string showing the host and port of the Mock instance. 

49 """ 

50 return self.__str__() 

51 

52 def __str__(self) -> str: 

53 """String representation of Mock instance. 

54 

55 Returns: 

56 str: formatted string showing the host and port of the Mock instance. 

57 """ 

58 return f'MockTransport with delay={self.delay}s and error_rate={self.error_rate:.2%}'