|
| 1 | +"""Tests for C4Director — get_item_variable_value, get_all_item_variable_value, |
| 2 | +and basic request wrappers. |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from unittest.mock import AsyncMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.asyncio |
| 12 | +async def test_get_item_variable_value_int(director): |
| 13 | + """Normal integer value is returned as-is.""" |
| 14 | + response = json.dumps([{"id": 100, "varName": "LIGHT_LEVEL", "value": 75}]) |
| 15 | + with patch.object( |
| 16 | + director, "send_get_request", new=AsyncMock(return_value=response) |
| 17 | + ): |
| 18 | + result = await director.get_item_variable_value(100, "LIGHT_LEVEL") |
| 19 | + assert result == 75 |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +async def test_get_item_variable_value_zero(director): |
| 24 | + """Zero is returned as 0, not confused with None or falsy.""" |
| 25 | + response = json.dumps([{"id": 100, "varName": "LIGHT_LEVEL", "value": 0}]) |
| 26 | + with patch.object( |
| 27 | + director, "send_get_request", new=AsyncMock(return_value=response) |
| 28 | + ): |
| 29 | + result = await director.get_item_variable_value(100, "LIGHT_LEVEL") |
| 30 | + assert result == 0 |
| 31 | + assert result is not None |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_get_item_variable_value_bool(director): |
| 36 | + """Boolean value is returned as a Python bool.""" |
| 37 | + response = json.dumps([{"id": 100, "varName": "IS_ON", "value": True}]) |
| 38 | + with patch.object( |
| 39 | + director, "send_get_request", new=AsyncMock(return_value=response) |
| 40 | + ): |
| 41 | + result = await director.get_item_variable_value(100, "IS_ON") |
| 42 | + assert result is True |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_get_item_variable_value_string(director): |
| 47 | + """String value is returned as-is.""" |
| 48 | + response = json.dumps( |
| 49 | + [{"id": 100, "varName": "PARTITION_STATE", "value": "ARMED_AWAY"}] |
| 50 | + ) |
| 51 | + with patch.object( |
| 52 | + director, "send_get_request", new=AsyncMock(return_value=response) |
| 53 | + ): |
| 54 | + result = await director.get_item_variable_value(100, "PARTITION_STATE") |
| 55 | + assert result == "ARMED_AWAY" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_get_item_variable_value_null(director): |
| 60 | + """JSON null value passes through as None (distinct from 'Undefined').""" |
| 61 | + response = json.dumps([{"id": 100, "varName": "OPTIONAL_VAR", "value": None}]) |
| 62 | + with patch.object( |
| 63 | + director, "send_get_request", new=AsyncMock(return_value=response) |
| 64 | + ): |
| 65 | + result = await director.get_item_variable_value(100, "OPTIONAL_VAR") |
| 66 | + assert result is None |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.asyncio |
| 70 | +async def test_get_item_variable_value_empty_response(director): |
| 71 | + """Empty list response raises ValueError.""" |
| 72 | + with patch.object(director, "send_get_request", new=AsyncMock(return_value="[]")): |
| 73 | + with pytest.raises(ValueError): |
| 74 | + await director.get_item_variable_value(100, "NONEXISTENT") |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_get_item_variable_value_invalid_format(director): |
| 79 | + """Non-list JSON response raises ValueError (2.0 guard).""" |
| 80 | + with patch.object( |
| 81 | + director, "send_get_request", new=AsyncMock(return_value='{"value": 1}') |
| 82 | + ): |
| 83 | + with pytest.raises(ValueError): |
| 84 | + await director.get_item_variable_value(100, "TEST") |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_get_item_variable_value_list_var_name(director): |
| 89 | + """List of var_names is joined with comma in the request URI.""" |
| 90 | + response = json.dumps([{"id": 100, "varName": "A", "value": 1}]) |
| 91 | + mock = AsyncMock(return_value=response) |
| 92 | + with patch.object(director, "send_get_request", new=mock): |
| 93 | + await director.get_item_variable_value(100, ["A", "B"]) |
| 94 | + uri = mock.call_args[0][0] |
| 95 | + assert "varnames=A,B" in uri |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.asyncio |
| 99 | +async def test_get_item_variable_value_tuple_var_name(director): |
| 100 | + """Tuple of var_names is joined with comma in the request URI.""" |
| 101 | + response = json.dumps([{"id": 100, "varName": "X", "value": 42}]) |
| 102 | + mock = AsyncMock(return_value=response) |
| 103 | + with patch.object(director, "send_get_request", new=mock): |
| 104 | + await director.get_item_variable_value(100, ("X", "Y")) |
| 105 | + uri = mock.call_args[0][0] |
| 106 | + assert "varnames=X,Y" in uri |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.asyncio |
| 110 | +async def test_get_all_item_variable_value_mixed(director): |
| 111 | + """get_all_item_variable_value normalizes Undefined values in-place.""" |
| 112 | + response = json.dumps( |
| 113 | + [ |
| 114 | + {"id": 1, "varName": "HUMIDITY", "value": "Undefined"}, |
| 115 | + {"id": 2, "varName": "HUMIDITY", "value": 45}, |
| 116 | + {"id": 3, "varName": "HUMIDITY", "value": 0}, |
| 117 | + ] |
| 118 | + ) |
| 119 | + with patch.object( |
| 120 | + director, "send_get_request", new=AsyncMock(return_value=response) |
| 121 | + ): |
| 122 | + result = await director.get_all_item_variable_value("HUMIDITY") |
| 123 | + assert result[0]["value"] is None |
| 124 | + assert result[1]["value"] == 45 |
| 125 | + assert result[2]["value"] == 0 |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.asyncio |
| 129 | +async def test_get_all_item_variable_value_empty(director): |
| 130 | + """Empty list response raises ValueError.""" |
| 131 | + with patch.object(director, "send_get_request", new=AsyncMock(return_value="[]")): |
| 132 | + with pytest.raises(ValueError): |
| 133 | + await director.get_all_item_variable_value("NONEXISTENT") |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.asyncio |
| 137 | +async def test_get_all_item_info_returns_parsed(director): |
| 138 | + """get_all_item_info returns parsed list (2.0 returns parsed JSON).""" |
| 139 | + raw = '[{"id": 1, "name": "Light"}]' |
| 140 | + with patch.object(director, "send_get_request", new=AsyncMock(return_value=raw)): |
| 141 | + result = await director.get_all_item_info() |
| 142 | + assert isinstance(result, list) |
| 143 | + assert result[0]["id"] == 1 |
0 commit comments