|
9 | 9 | from airos.exceptions import AirosDiscoveryError, AirosEndpointError |
10 | 10 | import pytest |
11 | 11 |
|
| 12 | +expected_parsed_data_for_mock_packet = { |
| 13 | + "ip_address": "192.168.1.3", # This comes from the host_ip passed to datagram_received |
| 14 | + "mac_address": "01:23:45:67:89:CD", # From 0x06 TLV |
| 15 | + "hostname": "name", # From 0x0B TLV |
| 16 | + "model": "NanoStation 5AC loco", # From 0x0C TLV |
| 17 | + "firmware_version": "WA.V8.7.17", # From 0x03 TLV |
| 18 | + "uptime_seconds": 3231, # From 0x0A TLV (0x0C9F in hex) |
| 19 | + "ssid": "DemoSSID", # From 0x0D TLV |
| 20 | + "full_model_name": "NanoStation 5AC loco", # From 0x14 TLV |
| 21 | +} |
| 22 | + |
12 | 23 |
|
13 | 24 | # Helper to load binary fixture |
14 | 25 | async def _read_binary_fixture(fixture_name: str) -> bytes: |
@@ -119,21 +130,48 @@ async def test_datagram_received_calls_callback(mock_airos_packet): |
119 | 130 | protocol = AirosDiscoveryProtocol(mock_callback) |
120 | 131 | host_ip = "192.168.1.3" # Sender IP |
121 | 132 |
|
122 | | - with patch("asyncio.create_task") as mock_create_task: |
123 | | - protocol.datagram_received(mock_airos_packet, (host_ip, DISCOVERY_PORT)) |
| 133 | + protocol.datagram_received(mock_airos_packet, (host_ip, DISCOVERY_PORT)) |
124 | 134 |
|
125 | | - # Verify the task was created and get the coroutine |
126 | | - mock_create_task.assert_called_once() |
127 | | - task_coro = mock_create_task.call_args[0][0] |
| 135 | + # Assert that the mock_callback was called exactly once |
| 136 | + mock_callback.assert_called_once() |
128 | 137 |
|
129 | | - # Manually await the coroutine to test the callback |
130 | | - await task_coro |
| 138 | + # Get the arguments it was called with |
| 139 | + args, kwargs = mock_callback.call_args |
131 | 140 |
|
132 | | - mock_callback.assert_called_once() |
133 | | - called_args, _ = mock_callback.call_args |
134 | | - parsed_data = called_args[0] |
135 | | - assert parsed_data["ip_address"] == "192.168.1.3" |
136 | | - assert parsed_data["mac_address"] == "01:23:45:67:89:CD" # Verify scrubbed MAC |
| 141 | + # Assert that the callback was called with a single argument (the parsed data) |
| 142 | + assert len(args) == 1 |
| 143 | + assert not kwargs # No keyword arguments expected |
| 144 | + |
| 145 | + # Get the parsed data from the call |
| 146 | + actual_parsed_data = args[0] |
| 147 | + |
| 148 | + # Assert that the parsed data matches the expected structure and values |
| 149 | + # We need to make sure the IP address in the expected data reflects the host_ip |
| 150 | + expected_parsed_data_with_current_ip = expected_parsed_data_for_mock_packet.copy() |
| 151 | + expected_parsed_data_with_current_ip["ip_address"] = host_ip |
| 152 | + |
| 153 | + # For robust testing, you might want to only check the critical fields, |
| 154 | + # or ensure your `parse_airos_packet` is separately tested to be correct. |
| 155 | + # Here, we'll assert a subset of key fields for simplicity. |
| 156 | + assert ( |
| 157 | + actual_parsed_data.get("mac_address") |
| 158 | + == expected_parsed_data_with_current_ip["mac_address"] |
| 159 | + ) |
| 160 | + assert ( |
| 161 | + actual_parsed_data.get("hostname") |
| 162 | + == expected_parsed_data_with_current_ip["hostname"] |
| 163 | + ) |
| 164 | + assert ( |
| 165 | + actual_parsed_data.get("ip_address") |
| 166 | + == expected_parsed_data_with_current_ip["ip_address"] |
| 167 | + ) |
| 168 | + assert ( |
| 169 | + actual_parsed_data.get("model") == expected_parsed_data_with_current_ip["model"] |
| 170 | + ) |
| 171 | + assert ( |
| 172 | + actual_parsed_data.get("firmware_version") |
| 173 | + == expected_parsed_data_with_current_ip["firmware_version"] |
| 174 | + ) |
137 | 175 |
|
138 | 176 |
|
139 | 177 | @pytest.mark.asyncio |
|
0 commit comments