Coverage for tests/tests/test_devices.py: 0%

1#!/usr/bin/env python3

2"""RAMSES RF - Test the payload parsers on a per-device basis."""

3

4 from pathlib import Path, PurePath

5

6 import pytest

7

8 from ramses_tx import exceptions as exc

9 from ramses_tx.message import Message

10 from ramses_tx.packet import Packet

11

12 from .helpers import TEST_DIR

13

14 WORK_DIR = f"{TEST_DIR}/devices"

15

16

17 def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:

18 def id_fnc(param: Path) -> str:

19 return PurePath(param).name

20

21 metafunc.parametrize("f_name", sorted(Path(WORK_DIR).glob("*.log")), ids=id_fnc)

22

23

24 def _proc_log_line(log_line: str) -> None:

25 pkt_line, pkt_eval, *_ = list(

26 map(str.strip, log_line.split("#", maxsplit=1) + [""])

27 )

28

29 if not pkt_line:

30 return

31

32 try:

33 pkt = Packet.from_file(pkt_line[:26], pkt_line[27:])

34 except exc.PacketInvalid as err:

35 assert False, f"{pkt_line[27:]} < {err}"

36

37 try:

38 _ = Message(pkt)

39 except exc.PacketPayloadInvalid as err:

40 assert False, f"{pkt} < {err}"

41

42 # assert bool(msg._is_fragment) == pkt._is_fragment

43 # assert bool(msg._idx): dict == pkt._idx: Optional[bool | str] # not useful

44

45 if not pkt_eval:

46 return

47 try:

48 _ = eval(pkt_eval)

49 except SyntaxError:

50 if "{" in pkt_eval:

51 raise

52 return

53

54

55 def test_parsers_from_log_files(f_name: Path) -> None:

56 with open(f_name) as f:

57 while line := (f.readline()):

58 _proc_log_line(line)