Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datastore): add return query object in add filter method #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions google/cloud/datastore/query.py
Expand Up @@ -220,8 +220,8 @@ def add_filter(self, property_name, operator, value):
>>> from google.cloud import datastore
>>> client = datastore.Client()
>>> query = client.query(kind='Person')
>>> query.add_filter('name', '=', 'James')
>>> query.add_filter('age', '>', 50)
>>> query = query.add_filter('name', '=', 'James')
>>> query = query.add_filter('age', '>', 50)

:type property_name: str
:param property_name: A property name.
Expand All @@ -235,6 +235,9 @@ def add_filter(self, property_name, operator, value):
:class:`google.cloud.datastore.key.Key`
:param value: The value to filter on.

:rtype: :class:`~google.cloud.datastore.query.Query`
:returns: A query object.

:raises: :class:`ValueError` if ``operation`` is not one of the
specified values, or if a filter names ``'__key__'`` but
passes an invalid value (a key is required).
Expand All @@ -248,6 +251,7 @@ def add_filter(self, property_name, operator, value):
raise ValueError('Invalid key: "%s"' % value)

self._filters.append((property_name, operator, value))
return self

@property
def projection(self):
Expand Down Expand Up @@ -348,8 +352,8 @@ def fetch(
>>> from google.cloud import datastore
>>> client = datastore.Client()
>>> query = client.query(kind='Person')
>>> query.add_filter('name', '=', 'Sally')
>>> list(query.fetch())
>>> result = query.add_filter('name', '=', 'Sally').fetch()
>>> list(result)
[<Entity object>, <Entity object>, ...]
>>> list(query.fetch(1))
[<Entity object>]
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_query.py
Expand Up @@ -214,6 +214,14 @@ def test_add_filter___key__valid_key(self):
query.add_filter("__key__", "=", key)
self.assertEqual(query.filters, [("__key__", "=", key)])

def test_add_filter_return_query_obj(self):
from google.cloud.datastore.query import Query

query = self._make_one(self._make_client())
query_obj = query.add_filter("firstname", "=", u"John")
self.assertIsInstance(query_obj, Query)
self.assertEqual(query_obj.filters, [("firstname", "=", u"John")])

def test_filter___key__not_equal_operator(self):
from google.cloud.datastore.key import Key

Expand Down