We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 6f58f4a + 2b0d245 commit 0910f99Copy full SHA for 0910f99
gcloud/datastore/query.py
@@ -248,6 +248,18 @@ def keys_only(self):
248
"""Set the projection to include only keys."""
249
self._projection[:] = ['__key__']
250
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
263
@property
264
def order(self):
265
"""Names of fields used to sort query results.
gcloud/datastore/test_query.py
@@ -244,6 +244,26 @@ def test_keys_only(self):
244
query.keys_only()
245
self.assertEqual(query.projection, ['__key__'])
246
247
+ 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):
+ query.key_filter(key, operator='>')
+ self.assertEqual(query.filters, [('__key__', '>', key)])
266
267
def test_order_setter_empty(self):
268
query = self._makeOne(self._makeClient(), order=['foo', '-bar'])
269
query.order = []
system_tests/clear_datastore.py
@@ -42,7 +42,8 @@ def print_func(message):
42
43
def fetch_keys(kind, client, fetch_max=FETCH_MAX, query=None, cursor=None):
44
if query is None:
45
- query = client.query(kind=kind, projection=['__key__'])
+ query = client.query(kind=kind)
46
+ query.keys_only()
47
48
iterator = query.fetch(limit=fetch_max, start_cursor=cursor)
49
@@ -89,7 +90,7 @@ def remove_kind(kind, client):
89
90
def remove_all_entities(client=None):
91
if client is None:
92
# Get a client that uses the test dataset.
- client = datastore.Client(project=TESTS_DATASET)
93
+ client = datastore.Client(project=os.getenv(TESTS_DATASET))
94
for kind in ALL_KINDS:
95
remove_kind(kind, client)
96
system_tests/datastore.py
@@ -273,12 +273,12 @@ def test_ancestor_query(self):
273
entities = list(filtered_query.fetch(limit=expected_matches + 1))
274
self.assertEqual(len(entities), expected_matches)
275
276
- def test_query___key___filter(self):
+ def test_query_key_filter(self):
277
# Use the client for this test instead of the global.
278
rickard_key = self.CLIENT.key(*populate_datastore.RICKARD)
279
280
query = self._base_query()
281
- query.add_filter('__key__', '=', rickard_key)
+ query.key_filter(rickard_key)
282
expected_matches = 1
283
# We expect 1, but allow the query to get 1 extra.
284
entities = list(query.fetch(limit=expected_matches + 1))
system_tests/populate_datastore.py
@@ -91,7 +91,7 @@ def print_func(message):
def add_characters(client=None):
with client.transaction() as xact:
for key_path, character in zip(KEY_PATHS, CHARACTERS):
97
if key_path[-1] != character['name']:
0 commit comments