Skip to content

Commit af444d5

Browse files
author
Samson Gebre
committed
test: add tests for _extract_pagingcookie and handle pagination exceptions
1 parent d28c960 commit af444d5

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

tests/unit/data/test_sql_parse.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,46 @@ def test_extract_pagingcookie_different_cookies_not_equal():
401401
def test_extract_pagingcookie_malformed_url_returns_none():
402402
"""Returns None gracefully when given a non-URL string."""
403403
assert _extract_pagingcookie("not a url at all !!!") is None
404+
405+
406+
def test_extract_pagingcookie_exception_returns_none():
407+
"""Returns None when an unexpected exception is raised during URL parsing (except branch)."""
408+
with patch("PowerPlatform.Dataverse.data._odata.urlparse", side_effect=RuntimeError("boom")):
409+
assert _extract_pagingcookie("https://org.example/?$skiptoken=x") is None
410+
411+
412+
def test_query_sql_request_exception_warns_and_returns_partial():
413+
"""When _request raises an exception mid-pagination a RuntimeWarning is emitted and
414+
the rows collected so far are returned."""
415+
client = _query_sql_client()
416+
page1 = _make_response([{"id": 1}], next_link="https://org.example/p2")
417+
418+
with (
419+
patch.object(client, "_execute_raw", return_value=page1),
420+
patch.object(client, "_build_sql", return_value=MagicMock()),
421+
patch.object(client, "_request", side_effect=ConnectionError("network timeout")),
422+
):
423+
with pytest.warns(RuntimeWarning, match="pagination stopped"):
424+
result = client._query_sql("SELECT id FROM account")
425+
426+
assert result == [{"id": 1}]
427+
428+
429+
def test_query_sql_non_dict_page_body_stops_pagination():
430+
"""When a pagination response contains valid JSON that is not a dict (e.g. a list),
431+
pagination stops silently and the rows collected so far are returned."""
432+
client = _query_sql_client()
433+
page1 = _make_response([{"id": 1}], next_link="https://org.example/p2")
434+
435+
bad_resp = MagicMock()
436+
bad_resp.json.return_value = [{"id": 2}] # a list, not a dict
437+
438+
with (
439+
patch.object(client, "_execute_raw", return_value=page1),
440+
patch.object(client, "_build_sql", return_value=MagicMock()),
441+
patch.object(client, "_request", return_value=bad_resp) as mock_req,
442+
):
443+
result = client._query_sql("SELECT id FROM account")
444+
445+
mock_req.assert_called_once_with("get", "https://org.example/p2")
446+
assert result == [{"id": 1}]

0 commit comments

Comments
 (0)