Skip to content

Commit

Permalink
feat(datastore): add return query object in add filter method (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
HemangChothani committed Feb 27, 2020
1 parent 672ba3e commit 6a9efab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 8 additions & 4 deletions google/cloud/datastore/query.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 6a9efab

Please sign in to comment.