|
1 | | -from unittest.mock import MagicMock |
2 | | - |
3 | 1 | import pytest |
4 | | - |
| 2 | +import logging |
| 3 | +from sd_protocols.sd_protocols import SDProtocols |
5 | 4 | from signalduino.parser.ms import MSParser |
6 | 5 | from signalduino.types import RawFrame |
7 | 6 |
|
8 | | - |
9 | | -@pytest.fixture |
10 | | -def ms_parser(mock_protocols, logger): |
11 | | - return MSParser(protocols=mock_protocols, logger=logger) |
12 | | - |
13 | | - |
14 | | -@pytest.mark.parametrize( |
15 | | - "line, expected_protocol, expected_payload, expected_rssi", |
16 | | - [ |
17 | | - ( |
18 | | - "MS;P1=502;P2=-9212;P3=-1939;P4=-3669;D=12131413141414131313131313141313131313131314141414141413131313141413131413;CP=1;SP=2;R=42;", |
19 | | - "2", |
20 | | - "sA018185020", |
21 | | - -53.0, |
22 | | - ), |
23 | | - # Add more valid test cases here |
24 | | - ], |
25 | | -) |
26 | | -def test_ms_parser_valid_messages(ms_parser, mock_protocols, line, expected_protocol, expected_payload, expected_rssi): |
27 | | - """Test valid MS messages.""" |
28 | | - frame = RawFrame(line=line) |
29 | | - demodulated = [{"protocol_id": expected_protocol, "payload": expected_payload}] |
30 | | - mock_protocols.demodulate.return_value = demodulated |
31 | | - |
32 | | - result = list(ms_parser.parse(frame)) |
33 | | - |
34 | | - mock_protocols.demodulate.assert_called_once() |
35 | | - assert len(result) == 1 |
36 | | - assert result[0].protocol_id == expected_protocol |
37 | | - assert result[0].payload == expected_payload |
38 | | - assert frame.rssi == expected_rssi |
39 | | - |
40 | | - |
41 | | -@pytest.mark.parametrize( |
42 | | - "line, log_message", |
43 | | - [ |
44 | | - ("MS;P1=-8043;D=212123;CP=2;SP=1;R=1q;", "Could not parse RSSI value: 1q"), |
45 | | - ("MS;P1=1;CP=1;R=42;", "Ignoring MS message without data (D)"), |
46 | | - ("FOO;P1=1;D=1;", "Not an MS message"), |
47 | | - ], |
48 | | -) |
49 | | -def test_ms_parser_corrupt_messages(ms_parser, mock_protocols, caplog, line, log_message): |
50 | | - """Test corrupt or invalid MS messages.""" |
51 | | - frame = RawFrame(line=line) |
52 | | - |
53 | | - with caplog.at_level("DEBUG"): |
54 | | - result = list(ms_parser.parse(frame)) |
55 | | - |
56 | | - assert not result |
57 | | - assert log_message in caplog.text |
| 7 | +class TestMSParser: |
| 8 | + @pytest.fixture |
| 9 | + def protocols(self): |
| 10 | + return SDProtocols() |
| 11 | + |
| 12 | + @pytest.fixture |
| 13 | + def parser(self, protocols): |
| 14 | + logger = logging.getLogger("TestMSParser") |
| 15 | + return MSParser(protocols, logger) |
| 16 | + |
| 17 | + def test_corrupt_ms_data_special_chars(self, parser): |
| 18 | + # testname: Corrupt MS data, special chars |
| 19 | + # input: MS;=0;L=L=-1020;L=H=935;S=L=-525;S=H=444;D=354133323044313642333731303246303541423044364430;C==487;L==89;R==24; |
| 20 | + |
| 21 | + line = "MS;=0;L=L=-1020;L=H=935;S=L=-525;S=H=444;D=354133323044313642333731303246303541423044364430;C==487;L==89;R==24;" |
| 22 | + frame = RawFrame(line) |
| 23 | + |
| 24 | + results = list(parser.parse(frame)) |
| 25 | + assert results == [] |
| 26 | + |
| 27 | + def test_corrupt_ms_data_structure_broken(self, parser): |
| 28 | + # testname: Corrupt MS data, special char and structure broken |
| 29 | + # input: MS;P1=;L=L=-1015;L=H=944;S=L=-512;S=H=456;D=353531313436304235313330433137433244353036423130;C==487;L==89;R==45; |
| 30 | + |
| 31 | + line = "MS;P1=;L=L=-1015;L=H=944;S=L=-512;S=H=456;D=353531313436304235313330433137433244353036423130;C==487;L==89;R==45;" |
| 32 | + frame = RawFrame(line) |
| 33 | + |
| 34 | + results = list(parser.parse(frame)) |
| 35 | + assert results == [] |
| 36 | + |
| 37 | + def test_corrupt_ms_data_invalid_rssi(self, parser): |
| 38 | + # testname: Corrupt MS data, R= Argument "1q" isn't numeric |
| 39 | + # input: MS;P1=-8043;P2=505;P3=-1979;P4=-3960;D=2121232323242424232423242323232323242324232424232324242323232323232323232323232323242423;CP=2;SP=1;R=1q; |
| 40 | + |
| 41 | + line = "MS;P1=-8043;P2=505;P3=-1979;P4=-3960;D=2121232323242424232423242323232323242324232424232324242323232323232323232323232323242423;CP=2;SP=1;R=1q;" |
| 42 | + frame = RawFrame(line) |
| 43 | + |
| 44 | + results = list(parser.parse(frame)) |
| 45 | + assert results == [] |
| 46 | + |
| 47 | + def test_correct_mc_cul_tcm_97001(self, parser): |
| 48 | + # testname: Correct MC CUL_TCM_97001 |
| 49 | + # input: MS;P1=502;P2=-9212;P3=-1939;P4=-3669;D=12131413141414131313131313141313131313131314141414141413131313141413131413;CP=1;SP=2; |
| 50 | + |
| 51 | + line = "MS;P1=502;P2=-9212;P3=-1939;P4=-3669;D=12131413141414131313131313141313131313131314141414141413131313141413131413;CP=1;SP=2;" |
| 52 | + frame = RawFrame(line) |
| 53 | + |
| 54 | + results = list(parser.parse(frame)) |
| 55 | + |
| 56 | + # Expect at least one result |
| 57 | + assert len(results) > 0 |
| 58 | + |
| 59 | + # Optional: Check if it matched Protocol 0 |
| 60 | + p0_match = any(r.protocol_id == '0' for r in results) |
| 61 | + assert p0_match |
0 commit comments