@@ -401,3 +401,46 @@ def test_extract_pagingcookie_different_cookies_not_equal():
401401def 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