Skip to content

Commit

Permalink
Adding key_filter to datastore Query.
Browse files Browse the repository at this point in the history
Also updating system tests and system test helpers to
avoid direct use of __key__.

Fixes googleapis#1432.
  • Loading branch information
dhermes committed Jan 30, 2016
1 parent 6f58f4a commit ddc8f85
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ def keys_only(self):
"""Set the projection to include only keys."""
self._projection[:] = ['__key__']

def key_filter(self, key, operator='='):
"""Filter on a key.
:type key: :class:`gcloud.datastore.key.Key`
:param key: The key to filter on.
:type operator: string
:param operator: (Optional) One of ``=``, ``<``, ``<=``, ``>``, ``>=``.
Defaults to ``=``.
"""
self.add_filter('__key__', operator, key)

@property
def order(self):
"""Names of fields used to sort query results.
Expand Down
20 changes: 20 additions & 0 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ def test_keys_only(self):
query.keys_only()
self.assertEqual(query.projection, ['__key__'])

def test_key_filter_defaults(self):
from gcloud.datastore.key import Key

client = self._makeClient()
query = self._makeOne(client)
self.assertEqual(query.filters, [])
key = Key('Kind', 1234, project='project')
query.key_filter(key)
self.assertEqual(query.filters, [('__key__', '=', key)])

def test_key_filter_explicit(self):
from gcloud.datastore.key import Key

client = self._makeClient()
query = self._makeOne(client)
self.assertEqual(query.filters, [])
key = Key('Kind', 1234, project='project')
query.key_filter(key, operator='>')
self.assertEqual(query.filters, [('__key__', '>', key)])

def test_order_setter_empty(self):
query = self._makeOne(self._makeClient(), order=['foo', '-bar'])
query.order = []
Expand Down
3 changes: 2 additions & 1 deletion system_tests/clear_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def print_func(message):

def fetch_keys(kind, client, fetch_max=FETCH_MAX, query=None, cursor=None):
if query is None:
query = client.query(kind=kind, projection=['__key__'])
query = client.query(kind=kind)
query.keys_only()

iterator = query.fetch(limit=fetch_max, start_cursor=cursor)

Expand Down
4 changes: 2 additions & 2 deletions system_tests/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ def test_ancestor_query(self):
entities = list(filtered_query.fetch(limit=expected_matches + 1))
self.assertEqual(len(entities), expected_matches)

def test_query___key___filter(self):
def test_query_key_filter(self):
# Use the client for this test instead of the global.
rickard_key = self.CLIENT.key(*populate_datastore.RICKARD)

query = self._base_query()
query.add_filter('__key__', '=', rickard_key)
query.key_filter(rickard_key)
expected_matches = 1
# We expect 1, but allow the query to get 1 extra.
entities = list(query.fetch(limit=expected_matches + 1))
Expand Down

0 comments on commit ddc8f85

Please sign in to comment.