Skip to content

Commit 6601fdc

Browse files
committed
add unit test for rest_get_paginated_async
1 parent 86fa041 commit 6601fdc

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

tests/unit/synapseclient/unit_test_client.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4272,3 +4272,113 @@ async def test_for_httpx_modified_user_agent_multiple_strings(self) -> None:
42724272
assert wrapped_rest_call.call_args[1]["headers"][
42734273
"User-Agent"
42744274
] == self.user_agent_httpx["User-Agent"] + " " + " ".join(user_agent)
4275+
4276+
4277+
class TestRestGetPaginatedAsync:
4278+
@pytest.fixture(autouse=True, scope="function")
4279+
def init_syn(self, syn: Synapse) -> None:
4280+
self.syn = syn
4281+
4282+
async def test_rest_get_paginated_async_with_results(self) -> None:
4283+
# Mock the rest_get_async method to return paginated results
4284+
mock_responses = [
4285+
{"results": [{"id": 1}, {"id": 2}, {"id": 3}]},
4286+
{"results": [{"id": 4}, {"id": 5}]},
4287+
{"results": []},
4288+
]
4289+
with patch.object(
4290+
self.syn, "rest_get_async", return_value=mock_responses
4291+
) as mock_rest_get:
4292+
# Test the paginated get
4293+
results = []
4294+
async for result in self.syn.rest_get_paginated_async("/test/uri", limit=3):
4295+
results.append(result)
4296+
4297+
# Verify results
4298+
assert len(results) == 5
4299+
assert [r["id"] for r in results] == [1, 2, 3, 4, 5]
4300+
4301+
# Verify rest_get_async was called with correct parameters
4302+
assert mock_rest_get.call_count == 3
4303+
call_list = [
4304+
call(uri="/test/uri", params={"offset": 0, "limit": 3}),
4305+
call(uri="/test/uri", params={"offset": 3, "limit": 3}),
4306+
call(uri="/test/uri", params={"offset": 6, "limit": 3}),
4307+
]
4308+
mock_rest_get.assert_has_calls(call_list)
4309+
4310+
async def test_rest_get_paginated_async_with_children(self) -> None:
4311+
# Mock the rest_get_async method to return paginated results with "children" key
4312+
mock_responses = [
4313+
{"children": [{"id": 1}, {"id": 2}]},
4314+
{"children": [{"id": 3}]},
4315+
{"children": []}, # Empty results to end pagination
4316+
]
4317+
4318+
with patch.object(
4319+
self.syn, "rest_get_async", return_value=mock_responses
4320+
) as mock_rest_get:
4321+
# Test the paginated get
4322+
results = []
4323+
async for result in self.syn.rest_get_paginated_async("/test/uri", limit=2):
4324+
results.append(result)
4325+
4326+
# Verify results
4327+
assert len(results) == 3
4328+
assert [r["id"] for r in results] == [1, 2, 3]
4329+
4330+
# Verify rest_get_async was called with correct parameters
4331+
assert mock_rest_get.call_count == 3
4332+
call_list = [
4333+
call(uri="/test/uri", params={"offset": 0, "limit": 2}),
4334+
call(uri="/test/uri", params={"offset": 2, "limit": 2}),
4335+
call(uri="/test/uri", params={"offset": 4, "limit": 2}),
4336+
]
4337+
mock_rest_get.assert_has_calls(call_list)
4338+
4339+
async def test_rest_get_paginated_async_empty_response(self) -> None:
4340+
# Mock the rest_get_async method to return empty results immediately
4341+
with patch.object(
4342+
self.syn, "rest_get_async", return_value={"results": []}
4343+
) as mock_rest_get:
4344+
# Test the paginated get
4345+
results = []
4346+
async for result in self.syn.rest_get_paginated_async("/test/uri"):
4347+
results.append(result)
4348+
4349+
# Verify no results were returned
4350+
assert len(results) == 0
4351+
4352+
# Verify rest_get_async was called once with default parameters
4353+
mock_rest_get.assert_called_once_with(
4354+
uri="/test/uri", params={"offset": 0, "limit": 20}
4355+
)
4356+
4357+
async def test_rest_get_paginated_async_custom_limit(self) -> None:
4358+
# Mock the rest_get_async method to return paginated results
4359+
mock_responses = [
4360+
{"results": [{"id": 1}, {"id": 2}]},
4361+
{"results": []}, # Empty results to end pagination
4362+
]
4363+
4364+
with patch.object(
4365+
self.syn, "rest_get_async", return_value=mock_responses
4366+
) as mock_rest_get:
4367+
# Test the paginated get with custom limit
4368+
results = []
4369+
async for result in self.syn.rest_get_paginated_async(
4370+
"/test/uri", limit=2, offset=5
4371+
):
4372+
results.append(result)
4373+
4374+
# Verify results
4375+
assert len(results) == 2
4376+
assert [r["id"] for r in results] == [1, 2]
4377+
4378+
# Verify rest_get_async was called with correct parameters
4379+
assert mock_rest_get.call_count == 2
4380+
call_list = [
4381+
call(uri="/test/uri", params={"offset": 5, "limit": 2}),
4382+
call(uri="/test/uri", params={"offset": 7, "limit": 2}),
4383+
]
4384+
mock_rest_get.assert_has_calls(call_list)

0 commit comments

Comments
 (0)