Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Implements fetch_next_block.
Browse files Browse the repository at this point in the history
  • Loading branch information
shipunyc committed Dec 9, 2014
1 parent 53b663e commit 3bf69fb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
4 changes: 2 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
# built documents.
#
# The short X.Y version.
version = '0.9.2-preview'
version = '0.9.3-preview'
# The full version, including alpha/beta/rc tags.
release = '0.9.2-preview'
release = '0.9.3-preview'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion pydocumentdb/http_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class Versions:
"""Constants of versions.
"""
CurrentVersion = '2014-08-21'
UserAgent = 'documentdb-python-sdk-0.9.2'
UserAgent = 'documentdb-python-sdk-0.9.3'


class Delimiters:
Expand Down
14 changes: 9 additions & 5 deletions pydocumentdb/query_iterable.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def __init__(self, iterable):
self._iterable = iterable
self._finished = False
self._current = -1
self._iterable._results = []
self._iterable._continuation = None
self._iterable._has_started = False

def __iter__(self):
# Always returns self
Expand All @@ -53,19 +56,20 @@ def next(self):
if self._current < len(self._iterable._results):
return self._iterable._results[self._current]
else:
if self._iterable._fetch_next_block():
if self._iterable.fetch_next_block():
self._current = 0
return self._iterable._results[self._current]
self._finished = True
raise StopIteration

# Also support Python 3.x iteration
__next__ = next

def _fetch_next_block(self):
def fetch_next_block(self):
"""Fetches more items.
:Returns:
int, number of fetched items.
list of fetched items.
"""
fetched_items = []
Expand All @@ -74,9 +78,9 @@ def _fetch_next_block(self):
self._has_started = True
self._options['continuation'] = self._continuation
(fetched_items, response_headers) = self._fetch_function(self._options)
self._results.extend(fetched_items)
self._results = fetched_items
self._continuation = response_headers.get(
http_constants.HttpHeaders.Continuation)
if fetched_items:
break
return len(fetched_items)
return fetched_items
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import setuptools

setup(name='pydocumentdb',
version='0.9.2-preview',
version='0.9.3-preview',
description='Azure DocumentDB Python SDK',
author="Microsoft",
author_email="docdbteam@microsoft.onmicrosoft.com",
Expand Down
38 changes: 27 additions & 11 deletions test/crud_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,33 +903,49 @@ def __CreateResources(client):
results = client.ReadDocuments(resources['coll']['_self'],
{'maxItemCount':2})
docs = list(iter(results))
self.assertEqual(len(docs),
3,
self.assertEqual(3,
len(docs),
'QueryIterable should return all documents' +
' using continuation')
self.assertEqual(docs[0]['id'], resources['doc1']['id'])
self.assertEqual(docs[1]['id'], resources['doc2']['id'])
self.assertEqual(docs[2]['id'], resources['doc3']['id'])
self.assertEqual(resources['doc1']['id'], docs[0]['id'])
self.assertEqual(resources['doc2']['id'], docs[1]['id'])
self.assertEqual(resources['doc3']['id'], docs[2]['id'])

# Validate QueryIterable iterator with 'for'.
counter = 0
# test QueryIterable with 'for'.
for doc in iter(results):
counter += 1
if counter == 1:
self.assertEqual(doc['id'],
resources['doc1']['id'],
self.assertEqual(resources['doc1']['id'],
doc['id'],
'first document should be doc1')
elif counter == 2:
self.assertEqual(doc['id'],
resources['doc2']['id'],
self.assertEqual(resources['doc2']['id'],
doc['id'],
'second document should be doc2')
elif counter == 3:
self.assertEqual(doc['id'],
resources['doc3']['id'],
self.assertEqual(resources['doc3']['id'],
doc['id'],
'third document should be doc3')
self.assertEqual(counter, 3)

# Get query results page by page.
results = client.ReadDocuments(resources['coll']['_self'],
{'maxItemCount':2})
first_block = results.fetch_next_block()
self.assertEqual(2,
len(first_block),
'First block should have 2 entries.')
self.assertEqual(resources['doc1']['id'], first_block[0]['id'])
self.assertEqual(resources['doc2']['id'], first_block[1]['id'])
self.assertEqual(1,
len(results.fetch_next_block()),
'Second block should have 1 entry.')
self.assertEqual(0,
len(results.fetch_next_block()),
'Then its empty.')

def test_trigger_functionality(self):
triggers_in_collection1 = [
{
Expand Down

0 comments on commit 3bf69fb

Please sign in to comment.