def test___iter___w_more(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) self._addQueryResults(connection, cursor=self._END, more=True) self._addQueryResults(connection) iterator = self._makeOne(query, client) entities = list(iterator) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 2) for entity in entities: self.assertEqual( entity.key.path, [{'kind': self._KIND, 'id': self._ID}]) self.assertEqual(entities[1]['foo'], u'Foo') qpb1 = _pb_from_query(query) qpb2 = _pb_from_query(query) qpb2.start_cursor = self._END EXPECTED1 = { 'project': self._PROJECT, 'query_pb': qpb1, 'namespace': self._NAMESPACE, 'transaction_id': None, } EXPECTED2 = { 'project': self._PROJECT, 'query_pb': qpb2, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(len(connection._called_with), 2) self.assertEqual(connection._called_with[0], EXPECTED1) self.assertEqual(connection._called_with[1], EXPECTED2)
def test___iter___w_limit(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) skip1 = 4 skip2 = 9 self._addQueryResults(connection, more=True, skipped_results=skip1, no_entity=True) self._addQueryResults(connection, more=True, skipped_results=skip2) self._addQueryResults(connection) offset = skip1 + skip2 iterator = self._makeOne(query, client, limit=2, offset=offset) entities = list(iterator) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 2) for entity in entities: self.assertEqual(entity.key.path, [{ 'kind': self._KIND, 'id': self._ID }]) qpb1 = _pb_from_query(query) qpb1.limit.value = 2 qpb1.offset = offset qpb2 = _pb_from_query(query) qpb2.start_cursor = self._END qpb2.limit.value = 2 qpb2.offset = offset - skip1 qpb3 = _pb_from_query(query) qpb3.start_cursor = self._END qpb3.limit.value = 1 EXPECTED1 = { 'project': self._PROJECT, 'query_pb': qpb1, 'namespace': self._NAMESPACE, 'transaction_id': None, } EXPECTED2 = { 'project': self._PROJECT, 'query_pb': qpb2, 'namespace': self._NAMESPACE, 'transaction_id': None, } EXPECTED3 = { 'project': self._PROJECT, 'query_pb': qpb3, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(len(connection._called_with), 3) self.assertEqual(connection._called_with[0], EXPECTED1) self.assertEqual(connection._called_with[1], EXPECTED2) self.assertEqual(connection._called_with[2], EXPECTED3)
def test___iter___w_limit(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) skip1 = 4 skip2 = 9 self._addQueryResults(connection, more=True, skipped_results=skip1, no_entity=True) self._addQueryResults(connection, more=True, skipped_results=skip2) self._addQueryResults(connection) offset = skip1 + skip2 iterator = self._makeOne(query, client, limit=2, offset=offset) entities = list(iterator) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 2) for entity in entities: self.assertEqual( entity.key.path, [{'kind': self._KIND, 'id': self._ID}]) qpb1 = _pb_from_query(query) qpb1.limit.value = 2 qpb1.offset = offset qpb2 = _pb_from_query(query) qpb2.start_cursor = self._END qpb2.limit.value = 2 qpb2.offset = offset - skip1 qpb3 = _pb_from_query(query) qpb3.start_cursor = self._END qpb3.limit.value = 1 EXPECTED1 = { 'project': self._PROJECT, 'query_pb': qpb1, 'namespace': self._NAMESPACE, 'transaction_id': None, } EXPECTED2 = { 'project': self._PROJECT, 'query_pb': qpb2, 'namespace': self._NAMESPACE, 'transaction_id': None, } EXPECTED3 = { 'project': self._PROJECT, 'query_pb': qpb3, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(len(connection._called_with), 3) self.assertEqual(connection._called_with[0], EXPECTED1) self.assertEqual(connection._called_with[1], EXPECTED2) self.assertEqual(connection._called_with[2], EXPECTED3)
def test_next_page_w_cursors_w_more(self): from base64 import urlsafe_b64decode from base64 import urlsafe_b64encode from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) self._addQueryResults(connection, cursor=self._END, more=True) iterator = self._makeOne(query, client) iterator._start_cursor = self._START iterator._end_cursor = self._END entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, urlsafe_b64encode(self._END)) self.assertTrue(more_results) self.assertTrue(iterator._more_results) self.assertEqual(iterator._end_cursor, None) self.assertEqual(urlsafe_b64decode(iterator._start_cursor), self._END) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{'kind': self._KIND, 'id': self._ID}]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.offset = 0 qpb.start_cursor = urlsafe_b64decode(self._START) qpb.end_cursor = urlsafe_b64decode(self._END) EXPECTED = { 'project': self._PROJECT, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test_next_page_no_cursors_no_more_w_offset_and_limit(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) self._addQueryResults(connection, cursor=b'') iterator = self._makeOne(query, client, 13, 29) entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, None) self.assertFalse(more_results) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{'kind': self._KIND, 'id': self._ID}]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.limit = 13 qpb.offset = 29 EXPECTED = { 'project': self._PROJECT, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test_next_page_no_cursors_no_more(self): from base64 import b64encode from gcloud.datastore.query import _pb_from_query connection = _Connection() query = _Query(self._DATASET, self._KIND, self._NAMESPACE) self._addQueryResults(connection) iterator = self._makeOne(query, connection) entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, b64encode(self._END)) self.assertFalse(more_results) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{'kind': self._KIND, 'id': self._ID}]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.offset = 0 EXPECTED = { 'dataset_id': self._DATASET, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test_next_page_w_cursors_w_more(self): from base64 import b64decode from base64 import b64encode from gcloud.datastore.query import _pb_from_query connection = _Connection() query = _Query(self._DATASET, self._KIND, self._NAMESPACE) self._addQueryResults(connection, cursor=self._END, more=True) iterator = self._makeOne(query, connection) iterator._start_cursor = self._START iterator._end_cursor = self._END entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, b64encode(self._END)) self.assertTrue(more_results) self.assertTrue(iterator._more_results) self.assertEqual(iterator._end_cursor, None) self.assertEqual(b64decode(iterator._start_cursor), self._END) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{ 'kind': self._KIND, 'id': self._ID }]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.offset = 0 qpb.start_cursor = b64decode(self._START) qpb.end_cursor = b64decode(self._END) EXPECTED = { 'dataset_id': self._DATASET, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test_next_page_no_cursors_no_more_w_offset_and_limit(self): from base64 import b64encode from gcloud.datastore.query import _pb_from_query connection = _Connection() query = _Query(self._DATASET, self._KIND, self._NAMESPACE) self._addQueryResults(connection) iterator = self._makeOne(query, connection, 13, 29) entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, b64encode(self._END)) self.assertFalse(more_results) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{ 'kind': self._KIND, 'id': self._ID }]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.limit = 13 qpb.offset = 29 EXPECTED = { 'dataset_id': self._DATASET, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test___iter___no_more(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) self._addQueryResults(connection) iterator = self._makeOne(query, client) entities = list(iterator) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{ 'kind': self._KIND, 'id': self._ID }]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.offset = 0 EXPECTED = { 'project': self._PROJECT, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test_next_page_no_cursors_no_more(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._DATASET, self._NAMESPACE) self._addQueryResults(connection, cursor=b'') iterator = self._makeOne(query, client) entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, None) self.assertFalse(more_results) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{ 'kind': self._KIND, 'id': self._ID }]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.offset = 0 EXPECTED = { 'dataset_id': self._DATASET, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test_next_page_no_cursors_no_more_w_offset_and_limit(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) skipped_results = object() self._addQueryResults(connection, cursor=b'', skipped_results=skipped_results) iterator = self._makeOne(query, client, 13, 29) entities, more_results, cursor = iterator.next_page() self.assertEqual(cursor, None) self.assertFalse(more_results) self.assertFalse(iterator._more_results) self.assertEqual(iterator._skipped_results, skipped_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{ 'kind': self._KIND, 'id': self._ID }]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.limit.value = 13 qpb.offset = 29 EXPECTED = { 'project': self._PROJECT, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def test___iter___no_more(self): from gcloud.datastore.query import _pb_from_query connection = _Connection() client = self._makeClient(connection) query = _Query(client, self._KIND, self._PROJECT, self._NAMESPACE) self._addQueryResults(connection) iterator = self._makeOne(query, client) entities = list(iterator) self.assertFalse(iterator._more_results) self.assertEqual(len(entities), 1) self.assertEqual(entities[0].key.path, [{'kind': self._KIND, 'id': self._ID}]) self.assertEqual(entities[0]['foo'], u'Foo') qpb = _pb_from_query(query) qpb.offset = 0 EXPECTED = { 'project': self._PROJECT, 'query_pb': qpb, 'namespace': self._NAMESPACE, 'transaction_id': None, } self.assertEqual(connection._called_with, [EXPECTED])
def _callFUT(self, query): from gcloud.datastore.query import _pb_from_query return _pb_from_query(query)