Skip to content

Commit d7632db

Browse files
committed
fix(client): determine the last_id by finding the maximum Id in the list
1 parent 6e5a30e commit d7632db

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/elmo/api/client.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,17 @@ def query(self, query):
643643
entries = response.json()
644644
_LOGGER.debug(f"Client | Query response: {entries}")
645645
items = {}
646+
647+
try:
648+
# Determine the last_id by finding the maximum Id in the list
649+
last_id = max(entry["Id"] for entry in entries)
650+
except (TypeError, ValueError) as err:
651+
_LOGGER.error("Client | Could not determine max Id from entries, defaulting to 0.")
652+
_LOGGER.debug(f"Client | Error: {err} | Entries: {entries}")
653+
last_id = 0
654+
646655
result = {
647-
"last_id": entries[-1]["Id"],
656+
"last_id": last_id,
648657
key_group: items,
649658
}
650659
try:

tests/test_client.py

+95
Original file line numberDiff line numberDiff line change
@@ -2562,3 +2562,98 @@ def test_client_get_alerts_missing_data(server):
25622562
with pytest.raises(ParseError):
25632563
client.query(query.ALERTS)
25642564
assert len(server.calls) == 1
2565+
2566+
2567+
def test_client_query_last_id_unordered(server, mocker):
2568+
"""Should determine the last_id correctly even if entries are unordered.
2569+
Regression test for: https://github.com/palazzem/econnect-python/issues/154
2570+
"""
2571+
html = """[
2572+
{
2573+
"Alarm": true,
2574+
"MemoryAlarm": false,
2575+
"Excluded": false,
2576+
"InUse": true,
2577+
"IsVideo": false,
2578+
"Id": 3,
2579+
"Index": 0,
2580+
"Element": 1,
2581+
"CommandId": 0,
2582+
"InProgress": false
2583+
},
2584+
{
2585+
"Alarm": false,
2586+
"MemoryAlarm": false,
2587+
"Excluded": true,
2588+
"InUse": true,
2589+
"IsVideo": false,
2590+
"Id": 5,
2591+
"Index": 2,
2592+
"Element": 3,
2593+
"CommandId": 0,
2594+
"InProgress": false
2595+
},
2596+
{
2597+
"Alarm": true,
2598+
"MemoryAlarm": false,
2599+
"Excluded": false,
2600+
"InUse": true,
2601+
"IsVideo": false,
2602+
"Id": 2,
2603+
"Index": 1,
2604+
"Element": 2,
2605+
"CommandId": 0,
2606+
"InProgress": false
2607+
}
2608+
]"""
2609+
server.add(responses.POST, "https://example.com/api/inputs", body=html, status=200)
2610+
client = ElmoClient(base_url="https://example.com", domain="domain")
2611+
client._session_id = "test"
2612+
mocker.patch.object(client, "_get_descriptions")
2613+
client._get_descriptions.return_value = {
2614+
10: {0: "Input 1", 1: "Input 2", 2: "Input 3"},
2615+
}
2616+
# Test
2617+
inputs = client.query(query.INPUTS)
2618+
assert inputs["last_id"] == 5
2619+
2620+
2621+
def test_client_query_last_id_type_error(server, mocker):
2622+
"""Should default last_id to 0 if a TypeError occurs during max()."""
2623+
html = """[
2624+
{
2625+
"Alarm": true,
2626+
"InUse": true,
2627+
"Id": "not-an-int",
2628+
"Index": 0,
2629+
"Element": 1
2630+
},
2631+
{
2632+
"Alarm": false,
2633+
"InUse": true,
2634+
"Id": 2,
2635+
"Index": 1,
2636+
"Element": 2
2637+
}
2638+
]"""
2639+
server.add(responses.POST, "https://example.com/api/inputs", body=html, status=200)
2640+
client = ElmoClient(base_url="https://example.com", domain="domain")
2641+
client._session_id = "test"
2642+
mocker.patch.object(client, "_get_descriptions")
2643+
client._get_descriptions.return_value = {10: {0: "Input 1", 1: "Input 2"}}
2644+
# Test
2645+
inputs = client.query(query.INPUTS)
2646+
assert inputs["last_id"] == 0
2647+
2648+
2649+
def test_client_query_last_id_value_error_empty(server, mocker):
2650+
"""Should default last_id to 0 if the entries list is empty (ValueError)."""
2651+
html = """[]"""
2652+
server.add(responses.POST, "https://example.com/api/inputs", body=html, status=200)
2653+
client = ElmoClient(base_url="https://example.com", domain="domain")
2654+
client._session_id = "test"
2655+
mocker.patch.object(client, "_get_descriptions")
2656+
client._get_descriptions.return_value = {}
2657+
# Test
2658+
inputs = client.query(query.INPUTS)
2659+
assert inputs["last_id"] == 0

0 commit comments

Comments
 (0)