Skip to content

Commit 0910f99

Browse files
committed
Merge pull request #1433 from dhermes/fix-1432
Adding key_filter to datastore Query.
2 parents 6f58f4a + 2b0d245 commit 0910f99

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

gcloud/datastore/query.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ def keys_only(self):
248248
"""Set the projection to include only keys."""
249249
self._projection[:] = ['__key__']
250250

251+
def key_filter(self, key, operator='='):
252+
"""Filter on a key.
253+
254+
:type key: :class:`gcloud.datastore.key.Key`
255+
:param key: The key to filter on.
256+
257+
:type operator: string
258+
:param operator: (Optional) One of ``=``, ``<``, ``<=``, ``>``, ``>=``.
259+
Defaults to ``=``.
260+
"""
261+
self.add_filter('__key__', operator, key)
262+
251263
@property
252264
def order(self):
253265
"""Names of fields used to sort query results.

gcloud/datastore/test_query.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,26 @@ def test_keys_only(self):
244244
query.keys_only()
245245
self.assertEqual(query.projection, ['__key__'])
246246

247+
def test_key_filter_defaults(self):
248+
from gcloud.datastore.key import Key
249+
250+
client = self._makeClient()
251+
query = self._makeOne(client)
252+
self.assertEqual(query.filters, [])
253+
key = Key('Kind', 1234, project='project')
254+
query.key_filter(key)
255+
self.assertEqual(query.filters, [('__key__', '=', key)])
256+
257+
def test_key_filter_explicit(self):
258+
from gcloud.datastore.key import Key
259+
260+
client = self._makeClient()
261+
query = self._makeOne(client)
262+
self.assertEqual(query.filters, [])
263+
key = Key('Kind', 1234, project='project')
264+
query.key_filter(key, operator='>')
265+
self.assertEqual(query.filters, [('__key__', '>', key)])
266+
247267
def test_order_setter_empty(self):
248268
query = self._makeOne(self._makeClient(), order=['foo', '-bar'])
249269
query.order = []

system_tests/clear_datastore.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def print_func(message):
4242

4343
def fetch_keys(kind, client, fetch_max=FETCH_MAX, query=None, cursor=None):
4444
if query is None:
45-
query = client.query(kind=kind, projection=['__key__'])
45+
query = client.query(kind=kind)
46+
query.keys_only()
4647

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

@@ -89,7 +90,7 @@ def remove_kind(kind, client):
8990
def remove_all_entities(client=None):
9091
if client is None:
9192
# Get a client that uses the test dataset.
92-
client = datastore.Client(project=TESTS_DATASET)
93+
client = datastore.Client(project=os.getenv(TESTS_DATASET))
9394
for kind in ALL_KINDS:
9495
remove_kind(kind, client)
9596

system_tests/datastore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ def test_ancestor_query(self):
273273
entities = list(filtered_query.fetch(limit=expected_matches + 1))
274274
self.assertEqual(len(entities), expected_matches)
275275

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

280280
query = self._base_query()
281-
query.add_filter('__key__', '=', rickard_key)
281+
query.key_filter(rickard_key)
282282
expected_matches = 1
283283
# We expect 1, but allow the query to get 1 extra.
284284
entities = list(query.fetch(limit=expected_matches + 1))

system_tests/populate_datastore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def print_func(message):
9191
def add_characters(client=None):
9292
if client is None:
9393
# Get a client that uses the test dataset.
94-
client = datastore.Client(project=TESTS_DATASET)
94+
client = datastore.Client(project=os.getenv(TESTS_DATASET))
9595
with client.transaction() as xact:
9696
for key_path, character in zip(KEY_PATHS, CHARACTERS):
9797
if key_path[-1] != character['name']:

0 commit comments

Comments
 (0)