1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
"""Tests for the Traffic module."""
import importlib
from typing import Any
from unittest.mock import AsyncMock, Mock, call, patch
import pytest
from asusrouter.const import (
AR_CALL_GET_STATE,
AR_CALL_TRANSLATE_STATE,
HTTPStatus,
)
from asusrouter.modules.endpoint import EndpointTools
from asusrouter.modules.source import ARDataSource
import asusrouter.modules.traffic as traffic_module
from asusrouter.modules.traffic import (
ARTrafficSource,
ARTrafficSourceBackhaul,
ARTrafficSourceBetween,
ARTrafficSourceEthernet,
ARTrafficSourceWiFi,
ARTrafficType,
_check_state,
get_state,
translate_state,
)
from asusrouter.tools.identifiers import MacAddress
vtarget = MacAddress("00:11:22:33:44:55")
vtype = None
vtowards = MacAddress("11:22:33:44:55:66")
class TestARTrafficSource:
"""Class for testing ARTrafficSource."""
def test_init(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test the initialization."""
vtarget = "target"
vtype = "type"
target_calls: list[object] = []
type_calls: list[object] = []
# Keep original getters so reading still works
orig_target_get = ARTrafficSource.__dict__["target"].fget
orig_type_get = ARTrafficSource.__dict__["type"].fget
def fake_set_target(self: ARTrafficSource, value: Any) -> None:
"""Fake setter for target."""
target_calls.append(value)
# Emulate original behaviour enough for assertions
self._target = value
def fake_set_type(self: ARTrafficSource, value: Any) -> None:
"""Fake setter for type."""
type_calls.append(value)
self._type = value
# Replace the properties on the class
monkeypatch.setattr(
ARTrafficSource,
"target",
property(orig_target_get, fake_set_target),
raising=True,
)
monkeypatch.setattr(
ARTrafficSource,
"type",
property(orig_type_get, fake_set_type),
raising=True,
)
instance = ARTrafficSource(vtarget, vtype)
assert isinstance(instance, ARTrafficSource)
assert issubclass(instance.__class__, ARDataSource)
# Verify setters were invoked exactly once with expected values
assert target_calls == [vtarget]
assert type_calls == [vtype]
def test_repr(self) -> None:
"""Test the __repr__ method."""
traffic_type = ARTrafficType.WIFI
instance = ARTrafficSource("target", traffic_type)
assert repr(instance) == f"<ARTrafficSource type=`{traffic_type}`>"
def test_properties(self) -> None:
"""Test the properties."""
traffic_type = ARTrafficType.WIFI
instance = ARTrafficSource(vtarget, traffic_type)
assert instance.target == vtarget
assert instance.type == traffic_type
def test_setter_target(self) -> None:
"""Test the target setter."""
instance = ARTrafficSource(vtarget)
# Test with a real new target
instance.target = vtowards
assert instance.target == vtowards
# Test with an invalid new target
new_target_wrong = "string"
instance.target = new_target_wrong # type: ignore[assignment]
assert instance.target is None
def test_setter_type(self) -> None:
"""Test the type setter."""
traffic_type = ARTrafficType.WIFI
instance = ARTrafficSource(vtarget, traffic_type)
with patch(
"asusrouter.modules.traffic.ARTrafficType.from_value",
return_value=traffic_type,
) as mock_from_value:
instance.type = "string" # type: ignore[assignment]
mock_from_value.assert_called_once_with("string")
assert instance.type == traffic_type
class TestARTrafficSourceEthernet:
"""Class for testing ARTrafficSourceEthernet."""
def test_init(self) -> None:
"""Test the initialization."""
bh_flag = "true"
instance = ARTrafficSourceEthernet(vtarget, bh_flag=bh_flag)
assert isinstance(instance, ARTrafficSourceEthernet)
assert issubclass(instance.__class__, ARTrafficSource)
assert instance.target == vtarget
assert instance.type == ARTrafficType.ETHERNET
assert instance.bh_flag is True
@pytest.mark.parametrize(
("bh_input", "bh_flag"),
[
(None, False),
("true", True),
(False, False),
(object(), False),
],
)
def test_properties(self, bh_input: Any, bh_flag: bool) -> None:
"""Test the properties."""
instance = ARTrafficSourceEthernet(vtarget, bh_flag=bh_input)
assert instance.bh_flag is bh_flag
def test_setter_bh_flag(self) -> None:
"""Test the bh_flag setter."""
set_value = "string"
return_value = True
instance = ARTrafficSourceEthernet(vtarget)
with patch(
"asusrouter.modules.traffic.safe_bool_nn",
return_value=return_value,
) as mock_safe_bool:
instance.bh_flag = set_value # type: ignore[assignment]
assert instance.bh_flag is return_value
mock_safe_bool.assert_called_once_with(set_value)
class TestARTrafficSourceBetween:
"""Class for testing ARTrafficSourceBetween."""
def test_init(self) -> None:
"""Test the initialization."""
instance = ARTrafficSourceBetween(vtarget, towards=vtowards)
assert isinstance(instance, ARTrafficSourceBetween)
assert issubclass(instance.__class__, ARTrafficSource)
assert instance.target == vtarget
assert instance.type == ARTrafficType.UNKNOWN
assert instance.towards == vtowards
def test_properties(self) -> None:
"""Test the properties."""
instance = ARTrafficSourceBetween(vtarget, towards=vtowards)
assert instance.towards == vtowards
def test_setter_towards(self) -> None:
"""Test the towards setter."""
instance = ARTrafficSourceBetween(vtarget, towards=vtowards)
instance.towards = vtarget
assert instance.towards == vtarget
# Test with an invalid new towards
new_towards_wrong = "string"
instance.towards = new_towards_wrong # type: ignore[assignment]
assert instance.towards is None
class TestARTrafficSourceWiFi:
"""Class for testing ARTrafficSourceWiFi."""
def test_init(self) -> None:
"""Test the initialization."""
instance = ARTrafficSourceWiFi(vtarget, towards=vtowards)
assert isinstance(instance, ARTrafficSourceWiFi)
assert issubclass(instance.__class__, ARTrafficSourceBetween)
assert instance.target == vtarget
assert instance.towards == vtowards
assert instance.type == ARTrafficType.WIFI
class TestARTrafficSourceBackhaul:
"""Class for testing ARTrafficSourceBackhaul."""
def test_init(self) -> None:
"""Test the initialization."""
instance = ARTrafficSourceBackhaul(vtarget, towards=vtowards)
assert isinstance(instance, ARTrafficSourceBackhaul)
assert issubclass(instance.__class__, ARTrafficSourceBetween)
assert instance.target == vtarget
assert instance.towards == vtowards
assert instance.type == ARTrafficType.BACKHAUL
class TestCheckState:
"""Class for testing _check_state method."""
def test_not_traffic_source(self) -> None:
"""Test the _check_state method with a non-traffic source."""
with pytest.raises(TypeError, match="Expected `ARTrafficSource`, got"):
_check_state("value")
def test_no_target(self) -> None:
"""Test cases when no target is provided."""
source = ARTrafficSource(target=None)
with pytest.raises(
ValueError,
match="Traffic source must have a `target` property set",
):
_check_state(source)
def test_no_type(self) -> None:
"""Test cases when no type is provided."""
source = ARTrafficSource(target=vtarget)
with pytest.raises(
ValueError,
match="Traffic source must have a `type` property set",
):
_check_state(source)
@pytest.mark.parametrize(
"source",
[
ARTrafficSourceWiFi(target=vtarget, towards=None),
ARTrafficSourceBackhaul(target=vtarget, towards=None),
],
)
def test_no_towards(self, source: ARTrafficSourceBetween) -> None:
"""Test cases when no towards is provided when required."""
with pytest.raises(
ValueError,
match=f"Traffic source of type `{source.type}` requires "
"a `towards` property set",
):
_check_state(source)
class TestGetState:
"""Class for testing get_state method."""
@pytest.mark.asyncio
async def test_generic_case(self) -> None:
"""Test the generic case for get_state."""
callback = AsyncMock()
callback.return_value = True
source = ARTrafficSourceWiFi(target=vtarget, towards=vtowards)
request_type = "request"
with (
patch(
"asusrouter.modules.traffic._check_state"
) as mock_check_state,
patch(
"asusrouter.modules.traffic.isinstance",
return_value=False,
) as mock_isinstance,
patch(
"asusrouter.modules.traffic.get_request_type",
return_value=request_type,
) as mock_get_request_type,
patch(
"asusrouter.modules.traffic.dict_to_request",
) as mock_dict_to_request,
):
result = await get_state(callback, source)
assert result is True
mock_check_state.assert_called_once_with(source)
mock_isinstance.assert_has_calls(
[
# First call for ARTrafficSourceEthernet
call(source, ARTrafficSourceEthernet),
# Second call for ARTrafficSourceBackhaul
call(source, ARTrafficSourceBackhaul),
# Third call for ARTrafficSourceWiFi
call(source, ARTrafficSourceWiFi),
],
any_order=False,
)
mock_get_request_type.assert_called_once_with(
EndpointTools.TRAFFIC_WIFI
)
mock_dict_to_request.assert_called_once_with(
{
"node_mac": str(vtarget),
},
request_type=request_type,
)
@pytest.mark.asyncio
async def test_no_endpoint(self) -> None:
"""Test failure due to no endpoint found."""
callback = AsyncMock()
source = ARTrafficSource(target=vtarget, type=ARTrafficType.UNKNOWN)
with patch(
"asusrouter.modules.traffic._check_state"
) as mock_check_state:
with pytest.raises(
ValueError,
match=f"Cannot find endpoint for traffic type `{source.type}`",
):
await get_state(callback, source)
mock_check_state.assert_called_once_with(source)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("source", "endpoint", "expected_args"),
[
(
ARTrafficSourceEthernet(target=vtarget, bh_flag="true"),
EndpointTools.TRAFFIC_ETHERNET,
{
"node_mac": str(vtarget),
"is_bh": 1,
},
),
(
ARTrafficSourceEthernet(target=vtarget, bh_flag=False),
EndpointTools.TRAFFIC_ETHERNET,
{
"node_mac": str(vtarget),
"is_bh": 0,
},
),
(
ARTrafficSourceBackhaul(target=vtarget, towards=vtowards),
EndpointTools.TRAFFIC_BACKHAUL,
{
"node_mac": str(vtarget),
"sta_mac": str(vtowards),
},
),
(
ARTrafficSourceWiFi(target=vtarget, towards=vtowards),
EndpointTools.TRAFFIC_WIFI,
{
"node_mac": str(vtarget),
"band_mac": str(vtowards),
},
),
],
)
async def test_arguments(
self,
source: ARTrafficSource,
endpoint: EndpointTools,
expected_args: dict[str, Any],
) -> None:
"""Test the arguments passed to the request."""
callback = AsyncMock()
callback.return_value = True
request_type = "request"
def mock_dict_to_request(
*args: Any, **kwargs: Any
) -> tuple[tuple[Any], dict[Any, Any]]:
"""Mock dict to request."""
return args, kwargs
with (
patch(
"asusrouter.modules.traffic._check_state"
) as mock_check_state,
patch(
"asusrouter.modules.traffic.get_request_type",
return_value=request_type,
) as mock_get_request_type,
patch(
"asusrouter.modules.traffic.dict_to_request",
side_effect=mock_dict_to_request,
) as mock_dict_to_request,
):
result = await get_state(callback, source)
assert result is True
mock_check_state.assert_called_once_with(source)
mock_get_request_type.assert_called_once_with(endpoint)
mock_dict_to_request.assert_called_once_with(
expected_args,
request_type=request_type,
)
@pytest.mark.parametrize(
("input__dict", "output_dict"),
[
# No input
({}, {}),
# Real input
(
{
"data_rx": 1,
"data_tx": 2,
"data_avg_rx": 3,
"data_avg_tx": 4,
"phy_rx": 5,
"phy_tx": 6,
"error_status": 200,
},
{
"rx_speed": 1024.0,
"tx_speed": 2048.0,
"rx_speed_avg": 3072.0,
"tx_speed_avg": 4096.0,
"phy_rx": 5 * 2**20,
"phy_tx": 6 * 2**20,
"status": HTTPStatus.OK,
},
),
# Unknown value
({"unknown_key": "unknown_value"}, {"unknown_key": "unknown_value"}),
],
)
def test_translate_state(
input__dict: dict[str, Any], output_dict: dict[str, Any]
) -> None:
"""Test the translate_state method."""
result = translate_state(input__dict)
assert result == output_dict
def test_traffic_module_registers_callables(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Ensure traffic module registers callables on import."""
# Replace the registry method that is called at import time
mock_register = Mock()
monkeypatch.setattr(
"asusrouter.registry.ARCallableRegistry.register", mock_register
)
# Reload the module so top-level registration runs again under the mock
importlib.reload(traffic_module)
# Expect three registrations (Ethernet, WiFi, Backhaul)
assert mock_register.call_count == 3 # noqa: PLR2004
expected_kwargs = {
AR_CALL_GET_STATE: traffic_module.get_state,
AR_CALL_TRANSLATE_STATE: traffic_module.translate_state,
}
# Check each call used the expected target class and kwargs (order matters)
expected_targets = [
traffic_module.ARTrafficSourceEthernet,
traffic_module.ARTrafficSourceWiFi,
traffic_module.ARTrafficSourceBackhaul,
]
for idx, target in enumerate(expected_targets):
args, kwargs = mock_register.call_args_list[idx]
assert args[0] is target
assert kwargs == expected_kwargs
|