Skip to content

Commit

Permalink
Fix None value returned between batches (#50)
Browse files Browse the repository at this point in the history
* Fix None value returned between batches

* Fix test
  • Loading branch information
mgmacias95 authored May 21, 2021
1 parent 8de8932 commit 0368530
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
16 changes: 12 additions & 4 deletions tests/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def new_client(httpserver):

@pytest.fixture
def iterator_response(httpserver):
httpserver.expect_request(
httpserver.expect_ordered_request(
'/api/v3/dummy_collection/foo',
method='GET',
headers={'X-Apikey': 'dummy_api_key'}
Expand All @@ -41,7 +41,15 @@ def iterator_response(httpserver):
'id': 'dummy_id_3',
'type': 'dummy_type',
'attributes': {'order': 0}
}, {
}],
'meta': {'cursor': 3}
})
httpserver.expect_ordered_request(
'/api/v3/dummy_collection/foo',
method='GET',
headers={'X-Apikey': 'dummy_api_key'}
).respond_with_json({
'data': [{
'id': 'dummy_id_4',
'type': 'dummy_type',
'attributes': {'order': 0}
Expand All @@ -52,7 +60,7 @@ def iterator_response(httpserver):
def test_next(httpserver, iterator_response):
"""Tests iterator's next with a limit higher than the total of elements."""
with new_client(httpserver) as client:
it = client.iterator('/dummy_collection/foo', limit=10)
it = client.iterator('/dummy_collection/foo', limit=10, batch_size=3)
assert next(it).id == 'dummy_id_1'
assert next(it).id == 'dummy_id_2'

Expand Down Expand Up @@ -103,7 +111,7 @@ def test_next_limit(httpserver, iterator_response):
async def test_anext(httpserver, iterator_response):
"""Tests iterator's async next."""
async with new_client(httpserver) as client:
it = client.iterator('/dummy_collection/foo', limit=10)
it = client.iterator('/dummy_collection/foo', limit=10, batch_size=3)
assert (await it.__anext__()).id == 'dummy_id_1'

# iteration must start right where the next stayed
Expand Down
18 changes: 8 additions & 10 deletions vt/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,19 @@ def _iterate(self):
if len(self._items) == 0:
self._items, self._server_cursor = self._get_batch()
self._batch_cursor = 0
else:
item = self._items.pop(0)
self._count += 1
self._batch_cursor += 1
return Object.from_dict(item)
item = self._items.pop(0)
self._count += 1
self._batch_cursor += 1
return Object.from_dict(item)

async def _aiterate(self):
if len(self._items) == 0:
self._items, self._server_cursor = await self._get_batch_async()
self._batch_cursor = 0
else:
item = self._items.pop(0)
self._count += 1
self._batch_cursor += 1
return Object.from_dict(item)
item = self._items.pop(0)
self._count += 1
self._batch_cursor += 1
return Object.from_dict(item)

def __iter__(self):
if not self._items and self._count == 0: # iter called before next
Expand Down

0 comments on commit 0368530

Please sign in to comment.