Skip to content

Commit 4f46b6a

Browse files
committed
Fix async
1 parent 695e3c0 commit 4f46b6a

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "airos"
7-
version = "0.2.1"
7+
version = "0.2.1a1"
88
license = "MIT"
99
description = "Ubiquity airOS module(s) for Python 3."
1010
readme = "README.md"

tests/test_discovery.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
from airos.exceptions import AirosDiscoveryError, AirosEndpointError
1010
import pytest
1111

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+
1223

1324
# Helper to load binary fixture
1425
async def _read_binary_fixture(fixture_name: str) -> bytes:
@@ -119,21 +130,48 @@ async def test_datagram_received_calls_callback(mock_airos_packet):
119130
protocol = AirosDiscoveryProtocol(mock_callback)
120131
host_ip = "192.168.1.3" # Sender IP
121132

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))
124134

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()
128137

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
131140

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+
)
137175

138176

139177
@pytest.mark.asyncio

0 commit comments

Comments
 (0)