Skip to content

Commit c893acc

Browse files
authored
Merge pull request #51 from cloudblue/fix_resultset_mock_for_zero_results
Fix resultset mock for empty lists
2 parents a03bbcd + 33b063f commit c893acc

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

connect/client/testing/models/resourceset.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ def _mock_iteration(self, return_value, extra_headers):
9191
total = len(return_value)
9292
self._count = 0
9393

94+
if total == 0:
95+
url = self._build_full_url(
96+
request_kwargs['params']['limit'],
97+
0,
98+
request_kwargs['params'].get('search'),
99+
)
100+
headers = {'Content-Range': 'items 0-0/0'}
101+
102+
if extra_headers:
103+
headers.update(extra_headers)
104+
self._client.get(
105+
url,
106+
return_value=[],
107+
headers=headers,
108+
)
109+
return
110+
94111
def pages_iterator():
95112
for i in range(0, total, self._limit):
96113
yield return_value[i:i + self._limit], i
@@ -116,6 +133,23 @@ def _mock_slicing(self, return_value, extra_headers):
116133
total = len(return_value)
117134
self._count = 0
118135

136+
if total == 0:
137+
url = self._build_full_url(
138+
request_kwargs['params']['limit'],
139+
0,
140+
request_kwargs['params'].get('search'),
141+
)
142+
headers = {'Content-Range': 'items 0-0/0'}
143+
144+
if extra_headers:
145+
headers.update(extra_headers)
146+
self._client.get(
147+
url,
148+
return_value=[],
149+
headers=headers,
150+
)
151+
return
152+
119153
def pages_iterator():
120154
limit = request_kwargs['params']['limit']
121155
offset = request_kwargs['params']['offset']

tests/async_client/test_testing.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ async def test_iterate():
172172
assert [item async for item in client.products.all()] == return_value
173173
assert client.response.headers['X-Custom-Header'] == 'value'
174174

175+
with AsyncConnectClientMocker('http://localhost') as mocker:
176+
mocker.products.all().mock(return_value=[])
177+
178+
client = AsyncConnectClient('api_key', endpoint='http://localhost')
179+
180+
assert [item async for item in client.products.all()] == []
181+
182+
with AsyncConnectClientMocker('http://localhost') as mocker:
183+
mocker.products.all().mock(return_value=[], headers={'X-Custom-Header': 'value'})
184+
185+
client = AsyncConnectClient('api_key', endpoint='http://localhost')
186+
187+
assert [item async for item in client.products.all()] == []
188+
assert client.response.headers['X-Custom-Header'] == 'value'
189+
175190
with AsyncConnectClientMocker('http://localhost') as mocker:
176191
mocker.products.all().mock(return_value=return_value)
177192

@@ -265,6 +280,31 @@ async def test_slicing(total, start, stop):
265280
assert client.response.headers['X-Custom-Header'] == 'value'
266281

267282

283+
@pytest.mark.asyncio
284+
async def test_slicing_no_results():
285+
286+
with AsyncConnectClientMocker('http://localhost') as mocker:
287+
mocker.products.all()[0:3].mock(return_value=[])
288+
289+
client = AsyncConnectClient('api_key', endpoint='http://localhost')
290+
291+
assert [
292+
item async for item in client.products.all()[0:3]
293+
] == []
294+
295+
with AsyncConnectClientMocker('http://localhost') as mocker:
296+
mocker.products.all()[0:3].mock(
297+
return_value=[], headers={'X-Custom-Header': 'value'},
298+
)
299+
300+
client = AsyncConnectClient('api_key', endpoint='http://localhost')
301+
302+
assert [
303+
item async for item in client.products.all()[0:3]
304+
] == []
305+
assert client.response.headers['X-Custom-Header'] == 'value'
306+
307+
268308
@pytest.mark.asyncio
269309
async def test_count():
270310
with AsyncConnectClientMocker('http://localhost') as mocker:

tests/client/test_testing.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ def test_iterate():
161161
assert list(client.products.all()) == return_value
162162
assert client.response.headers['X-Custom-Header'] == 'value'
163163

164+
with ConnectClientMocker('http://localhost') as mocker:
165+
mocker.products.all().mock(return_value=[])
166+
167+
client = ConnectClient('api_key', endpoint='http://localhost')
168+
169+
assert list(client.products.all()) == []
170+
171+
with ConnectClientMocker('http://localhost') as mocker:
172+
mocker.products.all().mock(return_value=[], headers={'X-Custom-Header': 'value'})
173+
174+
client = ConnectClient('api_key', endpoint='http://localhost')
175+
176+
assert list(client.products.all()) == []
177+
assert client.response.headers['X-Custom-Header'] == 'value'
178+
164179
with ConnectClientMocker('http://localhost') as mocker:
165180
mocker.products.all().mock(return_value=return_value)
166181

@@ -267,6 +282,26 @@ def test_slicing(total, start, stop):
267282
assert client.response.headers['X-Custom-Header'] == 'value'
268283

269284

285+
def test_slicing_no_results():
286+
287+
with ConnectClientMocker('http://localhost') as mocker:
288+
mocker.products.all()[0:3].mock(return_value=[])
289+
290+
client = ConnectClient('api_key', endpoint='http://localhost')
291+
292+
assert list(client.products.all()[0:3]) == []
293+
294+
with ConnectClientMocker('http://localhost') as mocker:
295+
mocker.products.all()[0:3].mock(
296+
return_value=[], headers={'X-Custom-Header': 'value'},
297+
)
298+
299+
client = ConnectClient('api_key', endpoint='http://localhost')
300+
301+
assert list(client.products.all()[0:3]) == []
302+
assert client.response.headers['X-Custom-Header'] == 'value'
303+
304+
270305
def test_count():
271306
with ConnectClientMocker('http://localhost') as mocker:
272307
mocker.products.all().count(return_value=123)

0 commit comments

Comments
 (0)