| 1 |
import pytest |
| 2 |
from unittest import mock |
| 3 |
from unittest.mock import AsyncMock, patch |
| 4 |
from blebox_uniapi.box import Box |
| 5 |
from blebox_uniapi import error |
| 6 |
from blebox_uniapi.jfollow import follow |
| 7 |
|
| 8 |
pytestmark = pytest.mark.asyncio |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture |
| 12 |
def mock_session(): |
| 13 |
return mock.MagicMock(host="172.1.2.3", port=80) |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture |
| 17 |
def sample_data(): |
| 18 |
return { |
| 19 |
"id": "abcd1234ef", |
| 20 |
"type": "airSensor", |
| 21 |
"deviceName": "foobar", |
| 22 |
"fv": "1.23", |
| 23 |
"hv": "4.56", |
| 24 |
"apiLevel": "20180403", |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def config(sample_data): |
| 30 |
return Box._match_device_config(sample_data) |
| 31 |
|
| 32 |
|
| 33 |
async def test_without_type(mock_session, sample_data, config): |
| 34 |
del sample_data["type"] |
| 35 |
|
| 36 |
with pytest.raises(error.UnsupportedBoxResponse, match="has no type"): |
| 37 |
Box(mock_session, sample_data, config, None) |
| 38 |
|
| 39 |
|
| 40 |
async def test_with_unknown_type(mock_session, sample_data): |
| 41 |
sample_data["type"] = "unknownBox" |
| 42 |
|
| 43 |
with pytest.raises(error.UnsupportedBoxResponse, match="not a supported type"): |
| 44 |
Box._match_device_config(sample_data) |
| 45 |
|
| 46 |
|
| 47 |
async def test_without_name(mock_session, sample_data, config): |
| 48 |
del sample_data["deviceName"] |
| 49 |
|
| 50 |
with pytest.raises(error.UnsupportedBoxResponse, match="has no name"): |
| 51 |
Box(mock_session, sample_data, config, None) |
| 52 |
|
| 53 |
|
| 54 |
async def test_without_firmware_version(mock_session, sample_data, config): |
| 55 |
del sample_data["fv"] |
| 56 |
|
| 57 |
with pytest.raises(error.UnsupportedBoxResponse, match="has no firmware version"): |
| 58 |
Box(mock_session, sample_data, config, None) |
| 59 |
|
| 60 |
|
| 61 |
async def test_without_hardware_version(mock_session, sample_data, config): |
| 62 |
del sample_data["hv"] |
| 63 |
|
| 64 |
with pytest.raises(error.UnsupportedBoxResponse, match="has no hardware version"): |
| 65 |
Box(mock_session, sample_data, config, None) |
| 66 |
|
| 67 |
|
| 68 |
async def test_without_api_level(mock_session, sample_data, config): |
| 69 |
del sample_data["apiLevel"] |
| 70 |
|
| 71 |
with pytest.raises(error.UnsupportedBoxVersion, match=r"unsupported version"): |
| 72 |
Box._match_device_config(sample_data) |
| 73 |
|
| 74 |
|
| 75 |
async def test_json_path_extraction(mock_session, sample_data, config): |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
assert follow(["foo"], "[0]") == "foo" |
| 83 |
assert follow([{"foo": "3", "value": 4}], "[?foo=='3'].value") == [4] |
| 84 |
|
| 85 |
|
| 86 |
assert follow([{"foo": "ab", "value": 4}], "[?foo=='bc'].value") == [] |
| 87 |
assert follow([{"value": 4}], "[1].value") is None |
| 88 |
assert follow({"value": 4}, "foo") is None |
| 89 |
assert follow({"foo": [4]}, "[?bar==`0`].value") is None |
| 90 |
|
| 91 |
|
| 92 |
async def test_missing_device_id(mock_session, sample_data, config): |
| 93 |
del sample_data["id"] |
| 94 |
with pytest.raises(error.UnsupportedBoxResponse, match="has no id"): |
| 95 |
Box(mock_session, sample_data, config, None) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
async def test_invalid_init(mock_session, sample_data, config): |
| 102 |
with mock.patch( |
| 103 |
"blebox_uniapi.sensor.SensorFactory.many_from_config", |
| 104 |
spec_set=True, |
| 105 |
autospec=True, |
| 106 |
) as mock_sensor: |
| 107 |
mock_sensor.side_effect = KeyError |
| 108 |
with pytest.raises( |
| 109 |
error.UnsupportedBoxResponse, match=r"Failed to initialize:" |
| 110 |
): |
| 111 |
Box(mock_session, sample_data, config, None) |
| 112 |
|
| 113 |
|
| 114 |
async def test_properties(mock_session, sample_data, config): |
| 115 |
box = Box(mock_session, sample_data, config, None) |
| 116 |
assert box.name == "foobar" |
| 117 |
assert box.last_data is None |
| 118 |
assert box.type == "airSensor" |
| 119 |
assert box.model == "airSensor" |
| 120 |
assert box.product == "airSensor" |
| 121 |
assert box.unique_id == "abcd1234ef" |
| 122 |
assert box.firmware_version == "1.23" |
| 123 |
assert box.hardware_version == "4.56" |
| 124 |
assert box.brand == "BleBox" |
| 125 |
assert box.api_version == 20180403 |
| 126 |
assert box.address == "172.1.2.3:80" |
| 127 |
|
| 128 |
|
| 129 |
async def test_field_validations(mock_session, sample_data, config): |
| 130 |
box = Box(mock_session, sample_data, config, None) |
| 131 |
|
| 132 |
with pytest.raises( |
| 133 |
error.BadFieldExceedsMax, |
| 134 |
match=r"foobar.field1 is 123 which exceeds max \(100\)", |
| 135 |
): |
| 136 |
box.check_int_range(123, "field1", 100, 0) |
| 137 |
|
| 138 |
with pytest.raises( |
| 139 |
error.BadFieldLessThanMin, |
| 140 |
match=r"foobar.field1 is 123 which is less than minimum \(200\)", |
| 141 |
): |
| 142 |
box.check_int_range(123, "field1", 300, 200) |
| 143 |
|
| 144 |
with pytest.raises(error.BadFieldMissing, match=r"foobar.field1 is missing"): |
| 145 |
box.check_int(None, "field1", 300, 200) |
| 146 |
|
| 147 |
with pytest.raises( |
| 148 |
error.BadFieldNotANumber, match=r"foobar.field1 is '123' which is not a number" |
| 149 |
): |
| 150 |
box.check_int("123", "field1", 300, 200) |
| 151 |
|
| 152 |
with pytest.raises(error.BadFieldMissing, match=r"foobar.field1 is missing"): |
| 153 |
box.check_hex_str(None, "field1", 300, 200) |
| 154 |
|
| 155 |
with pytest.raises( |
| 156 |
error.BadFieldNotAString, match=r"foobar.field1 is 123 which is not a string" |
| 157 |
): |
| 158 |
box.check_hex_str(123, "field1", 300, 200) |
| 159 |
|
| 160 |
with pytest.raises(error.BadFieldMissing, match=r"foobar.field1 is missing"): |
| 161 |
box.check_rgbw(None, "field1") |
| 162 |
|
| 163 |
with pytest.raises( |
| 164 |
error.BadFieldNotAString, match=r"foobar.field1 is 123 which is not a string" |
| 165 |
): |
| 166 |
box.check_rgbw(123, "field1") |
| 167 |
|
| 168 |
with pytest.raises( |
| 169 |
error.BadFieldNotRGBW, match=r"foobar.field1 is 123 which is not a rgbw string" |
| 170 |
): |
| 171 |
box.check_rgbw("123", "field1") |
| 172 |
|
| 173 |
|
| 174 |
async def test_available_firmware_version_none(mock_session, sample_data, config): |
| 175 |
box = Box(mock_session, sample_data, config, None) |
| 176 |
assert box.available_firmware_version is None |
| 177 |
|
| 178 |
|
| 179 |
async def test_available_firmware_version_set(mock_session, sample_data, config): |
| 180 |
sample_data["availableFv"] = "2.0" |
| 181 |
box = Box(mock_session, sample_data, config, None) |
| 182 |
assert box.available_firmware_version == "2.0" |
| 183 |
|
| 184 |
|
| 185 |
async def test_async_ota_update_calls_session(mock_session, sample_data, config): |
| 186 |
box = Box(mock_session, sample_data, config, None) |
| 187 |
mock_session.async_api_get = AsyncMock(return_value=None) |
| 188 |
await box.async_ota_update() |
| 189 |
mock_session.async_api_get.assert_called_once_with("/api/ota/update") |
| 190 |
|
| 191 |
|
| 192 |
async def test_async_ota_check_updates_firmware_versions( |
| 193 |
mock_session, sample_data, config |
| 194 |
): |
| 195 |
box = Box(mock_session, sample_data, config, None) |
| 196 |
mock_session.async_api_get_ota = AsyncMock(return_value=None) |
| 197 |
mock_session.async_api_get = AsyncMock( |
| 198 |
return_value={"availableFv": "2.0", "fv": "1.5"} |
| 199 |
) |
| 200 |
with patch("asyncio.sleep", new=AsyncMock()): |
| 201 |
await box.async_ota_check() |
| 202 |
assert box.available_firmware_version == "2.0" |
| 203 |
assert box.firmware_version == "1.5" |
| 204 |
|
| 205 |
|
| 206 |
async def test_async_ota_check_unwraps_device_key(mock_session, sample_data, config): |
| 207 |
box = Box(mock_session, sample_data, config, None) |
| 208 |
mock_session.async_api_get_ota = AsyncMock(return_value=None) |
| 209 |
mock_session.async_api_get = AsyncMock( |
| 210 |
return_value={"device": {"availableFv": "3.0", "fv": "2.5"}} |
| 211 |
) |
| 212 |
with patch("asyncio.sleep", new=AsyncMock()): |
| 213 |
await box.async_ota_check() |
| 214 |
assert box.available_firmware_version == "3.0" |
| 215 |
|
| 216 |
|
| 217 |
async def test_async_ota_check_raises_on_none_info_response( |
| 218 |
mock_session, sample_data, config |
| 219 |
): |
| 220 |
box = Box(mock_session, sample_data, config, None) |
| 221 |
mock_session.async_api_get_ota = AsyncMock(return_value=None) |
| 222 |
mock_session.async_api_get = AsyncMock(return_value=None) |
| 223 |
with patch("asyncio.sleep", new=AsyncMock()): |
| 224 |
with pytest.raises(error.UnsupportedBoxResponse): |
| 225 |
await box.async_ota_check() |
| 226 |
|
| 227 |
|
| 228 |
async def test_async_ota_check_returns_silently_when_no_available_fv( |
| 229 |
mock_session, sample_data, config |
| 230 |
): |
| 231 |
box = Box(mock_session, sample_data, config, None) |
| 232 |
mock_session.async_api_get_ota = AsyncMock(return_value=None) |
| 233 |
mock_session.async_api_get = AsyncMock(return_value={"fv": "1.0"}) |
| 234 |
with patch("asyncio.sleep", new=AsyncMock()): |
| 235 |
await box.async_ota_check() |
| 236 |
assert box.available_firmware_version is None |