mirror of
https://github.com/python-kasa/python-kasa.git
synced 2026-07-08 14:52:03 +00:00
tests: add type annotations to CLI, protocol, and smartcam tests (#1687)
Add type annotations (`-> None` return types and parameter types) to test files in the `tests/cli/`, `tests/protocols/`, and `tests/smartcam/` directories. This enables mypy to check test function bodies, catching type errors that were previously hidden.
This commit is contained in:
@@ -26,7 +26,9 @@ DUMMY_MULTIPLE_QUERY = {
|
||||
ERRORS = [e for e in SmartErrorCode if e != 0]
|
||||
|
||||
|
||||
async def test_smart_queries(dummy_protocol, mocker: pytest_mock.MockerFixture):
|
||||
async def test_smart_queries(
|
||||
dummy_protocol: SmartProtocol, mocker: pytest_mock.MockerFixture
|
||||
) -> None:
|
||||
mock_response = {"result": {"great": "success"}, "error_code": 0}
|
||||
|
||||
mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response)
|
||||
@@ -42,7 +44,9 @@ async def test_smart_queries(dummy_protocol, mocker: pytest_mock.MockerFixture):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("error_code", ERRORS, ids=lambda e: e.name)
|
||||
async def test_smart_device_errors(dummy_protocol, mocker, error_code):
|
||||
async def test_smart_device_errors(
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture, error_code: SmartErrorCode
|
||||
) -> None:
|
||||
mock_response = {"result": {"great": "success"}, "error_code": error_code.value}
|
||||
|
||||
send_mock = mocker.patch.object(
|
||||
@@ -59,8 +63,11 @@ async def test_smart_device_errors(dummy_protocol, mocker, error_code):
|
||||
@pytest.mark.parametrize("error_code", [-13333, 13333])
|
||||
@pytest.mark.xdist_group(name="caplog")
|
||||
async def test_smart_device_unknown_errors(
|
||||
dummy_protocol, mocker, error_code, caplog: pytest.LogCaptureFixture
|
||||
):
|
||||
dummy_protocol: SmartProtocol,
|
||||
mocker: MockerFixture,
|
||||
error_code: int,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test handling of unknown error codes."""
|
||||
mock_response = {"result": {"great": "success"}, "error_code": error_code}
|
||||
|
||||
@@ -78,8 +85,8 @@ async def test_smart_device_unknown_errors(
|
||||
|
||||
@pytest.mark.parametrize("error_code", ERRORS, ids=lambda e: e.name)
|
||||
async def test_smart_device_errors_in_multiple_request(
|
||||
dummy_protocol, mocker, error_code
|
||||
):
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture, error_code: SmartErrorCode
|
||||
) -> None:
|
||||
mock_request = {
|
||||
"foobar1": {"foo": "bar", "bar": "foo"},
|
||||
"foobar2": {"foo": "bar", "bar": "foo"},
|
||||
@@ -113,10 +120,13 @@ async def test_smart_device_errors_in_multiple_request(
|
||||
@pytest.mark.parametrize("request_size", [1, 3, 5, 10])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 3, 4, 5])
|
||||
async def test_smart_device_multiple_request(
|
||||
dummy_protocol, mocker, request_size, batch_size
|
||||
):
|
||||
dummy_protocol: SmartProtocol,
|
||||
mocker: MockerFixture,
|
||||
request_size: int,
|
||||
batch_size: int,
|
||||
) -> None:
|
||||
requests = {}
|
||||
mock_response = {
|
||||
mock_response: dict = {
|
||||
"result": {"responses": []},
|
||||
"error_code": 0,
|
||||
}
|
||||
@@ -138,8 +148,8 @@ async def test_smart_device_multiple_request(
|
||||
|
||||
|
||||
async def test_smart_device_multiple_request_json_decode_failure(
|
||||
dummy_protocol, mocker
|
||||
):
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test the logic to disable multiple requests on JSON_DECODE_FAIL_ERROR."""
|
||||
requests = {}
|
||||
mock_responses = []
|
||||
@@ -169,8 +179,8 @@ async def test_smart_device_multiple_request_json_decode_failure(
|
||||
|
||||
|
||||
async def test_smart_device_multiple_request_json_decode_failure_twice(
|
||||
dummy_protocol, mocker
|
||||
):
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test the logic to disable multiple requests on JSON_DECODE_FAIL_ERROR."""
|
||||
requests = {}
|
||||
|
||||
@@ -196,8 +206,8 @@ async def test_smart_device_multiple_request_json_decode_failure_twice(
|
||||
|
||||
|
||||
async def test_smart_device_multiple_request_non_json_decode_failure(
|
||||
dummy_protocol, mocker
|
||||
):
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test the logic to disable multiple requests on JSON_DECODE_FAIL_ERROR.
|
||||
|
||||
Ensure other exception types behave as expected.
|
||||
@@ -225,7 +235,9 @@ async def test_smart_device_multiple_request_non_json_decode_failure(
|
||||
assert send_mock.call_count == 1
|
||||
|
||||
|
||||
async def test_childdevicewrapper_unwrapping(dummy_protocol, mocker):
|
||||
async def test_childdevicewrapper_unwrapping(
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test that responseData gets unwrapped correctly."""
|
||||
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
||||
mock_response = {"error_code": 0, "result": {"responseData": {"error_code": 0}}}
|
||||
@@ -235,7 +247,9 @@ async def test_childdevicewrapper_unwrapping(dummy_protocol, mocker):
|
||||
assert res == {"foobar": None}
|
||||
|
||||
|
||||
async def test_childdevicewrapper_unwrapping_with_payload(dummy_protocol, mocker):
|
||||
async def test_childdevicewrapper_unwrapping_with_payload(
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
||||
mock_response = {
|
||||
"error_code": 0,
|
||||
@@ -246,7 +260,9 @@ async def test_childdevicewrapper_unwrapping_with_payload(dummy_protocol, mocker
|
||||
assert res == {"foobar": {"bar": "bar"}}
|
||||
|
||||
|
||||
async def test_childdevicewrapper_error(dummy_protocol, mocker):
|
||||
async def test_childdevicewrapper_error(
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test that errors inside the responseData payload cause an exception."""
|
||||
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
||||
mock_response = {"error_code": 0, "result": {"responseData": {"error_code": -1001}}}
|
||||
@@ -256,7 +272,9 @@ async def test_childdevicewrapper_error(dummy_protocol, mocker):
|
||||
await wrapped_protocol.query(DUMMY_QUERY)
|
||||
|
||||
|
||||
async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, mocker):
|
||||
async def test_childdevicewrapper_unwrapping_multiplerequest(
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test that unwrapping multiplerequest works correctly."""
|
||||
mock_response = {
|
||||
"error_code": 0,
|
||||
@@ -285,7 +303,9 @@ async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, moc
|
||||
assert resp == {"get_device_info": {"foo": "bar"}, "second_command": {"bar": "foo"}}
|
||||
|
||||
|
||||
async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
|
||||
async def test_childdevicewrapper_multiplerequest_error(
|
||||
dummy_protocol: SmartProtocol, mocker: MockerFixture
|
||||
) -> None:
|
||||
"""Test that errors inside multipleRequest response of responseData raise an exception."""
|
||||
mock_response = {
|
||||
"error_code": 0,
|
||||
@@ -313,7 +333,9 @@ async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
|
||||
|
||||
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
||||
async def test_smart_protocol_lists_single_request(mocker, list_sum, batch_size):
|
||||
async def test_smart_protocol_lists_single_request(
|
||||
mocker: MockerFixture, list_sum: int, batch_size: int
|
||||
) -> None:
|
||||
child_device_list = [{"foo": i} for i in range(list_sum)]
|
||||
response = {
|
||||
"get_child_device_list": {
|
||||
@@ -341,7 +363,9 @@ async def test_smart_protocol_lists_single_request(mocker, list_sum, batch_size)
|
||||
|
||||
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
||||
async def test_smart_protocol_lists_multiple_request(mocker, list_sum, batch_size):
|
||||
async def test_smart_protocol_lists_multiple_request(
|
||||
mocker: MockerFixture, list_sum: int, batch_size: int
|
||||
) -> None:
|
||||
child_list = [{"foo": i} for i in range(list_sum)]
|
||||
response = {
|
||||
"get_child_device_list": {
|
||||
@@ -376,7 +400,9 @@ async def test_smart_protocol_lists_multiple_request(mocker, list_sum, batch_siz
|
||||
|
||||
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
||||
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
||||
async def test_smartcam_protocol_list_request(mocker, list_sum, batch_size):
|
||||
async def test_smartcam_protocol_list_request(
|
||||
mocker: MockerFixture, list_sum: int, batch_size: int
|
||||
) -> None:
|
||||
"""Test smartcam protocol list handling for lists."""
|
||||
child_list = [{"foo": i} for i in range(list_sum)]
|
||||
|
||||
@@ -414,7 +440,9 @@ async def test_smartcam_protocol_list_request(mocker, list_sum, batch_size):
|
||||
assert resp == response
|
||||
|
||||
|
||||
async def test_incomplete_list(mocker, caplog):
|
||||
async def test_incomplete_list(
|
||||
mocker: MockerFixture, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test for handling incomplete lists returned from queries."""
|
||||
info = {
|
||||
"get_preset_rules": {
|
||||
@@ -464,7 +492,7 @@ async def test_incomplete_list(mocker, caplog):
|
||||
@pytest.mark.xdist_group(name="caplog")
|
||||
async def test_smart_queries_redaction(
|
||||
dev: SmartDevice, caplog: pytest.LogCaptureFixture
|
||||
):
|
||||
) -> None:
|
||||
"""Test query sensitive info redaction."""
|
||||
if isinstance(dev.protocol._transport, FakeSmartTransport):
|
||||
device_id = "123456789ABCDEF"
|
||||
@@ -495,7 +523,7 @@ async def test_smart_queries_redaction(
|
||||
|
||||
async def test_no_method_returned_multiple(
|
||||
mocker: MockerFixture, caplog: pytest.LogCaptureFixture
|
||||
):
|
||||
) -> None:
|
||||
"""Test protocol handles multiple requests that don't return the method."""
|
||||
req = {
|
||||
"getDeviceInfo": {"device_info": {"name": ["basic_info", "info"]}},
|
||||
@@ -540,7 +568,7 @@ async def test_no_method_returned_multiple(
|
||||
|
||||
async def test_no_multiple_methods(
|
||||
mocker: MockerFixture, caplog: pytest.LogCaptureFixture
|
||||
):
|
||||
) -> None:
|
||||
"""Test protocol sends NO_MULTI methods as single call."""
|
||||
req = {
|
||||
"getDeviceInfo": {"device_info": {"name": ["basic_info", "info"]}},
|
||||
|
||||
Reference in New Issue
Block a user