Skip to content

Commit dc94eee

Browse files
authored
fix: paginator.fetchone() now returns None instead of a ClientException at the end of the iteration (#13)
1 parent 9af5374 commit dc94eee

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

hostingde/paginator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def fetchall(self) -> List[R]:
126126
"""
127127
return list(self)
128128

129-
def fetchone(self) -> R:
129+
def fetchone(self) -> Optional[R]:
130130
"""
131131
Only fetch a single resource from the API.
132132
@@ -135,7 +135,7 @@ def fetchone(self) -> R:
135135
try:
136136
return self.__next__()
137137
except StopIteration:
138-
raise ClientException('No entities found for given filter!')
138+
return None
139139

140140
def __len__(self):
141141
"""

tests/unit/test_paginator.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,49 @@ def test_fetchone():
241241
)
242242

243243
assert paginator.fetchone().content == '127.0.0.0'
244+
245+
246+
@responses.activate
247+
def test_fetchone_returns_none_after_last():
248+
api = login('https://example.de/api', 'token')
249+
250+
url = 'https://example.de/api/demo'
251+
252+
paginator: HostingDePaginator = HostingDePaginator(api, instance_class=Record, url=url)
253+
254+
responses.add(
255+
'POST',
256+
url,
257+
body=json.dumps(
258+
{
259+
"response": {
260+
"data": [
261+
Record.create_new_record('cloud.de', RecordType.A, f'127.0.0.{i}').to_json() for i in range(25)
262+
],
263+
"totalPages": 2,
264+
},
265+
"status": "success",
266+
}
267+
),
268+
)
269+
270+
responses.add(
271+
'POST',
272+
url,
273+
body=json.dumps(
274+
{
275+
"response": {
276+
"data": [
277+
Record.create_new_record('cloud.de', RecordType.A, f'127.0.0.{i}').to_json()
278+
for i in range(25, 41)
279+
],
280+
"totalPages": 2,
281+
},
282+
"status": "success",
283+
}
284+
),
285+
)
286+
287+
paginator.fetchall()
288+
289+
assert paginator.fetchone() is None

0 commit comments

Comments
 (0)