Skip to content

Commit 465bd87

Browse files
authored
feat: support IN/NOT_IN/NOT_EQUAL operators (#287)
1 parent db044b1 commit 465bd87

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

google/cloud/datastore/query.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Query(object):
8686
"<": query_pb2.PropertyFilter.Operator.LESS_THAN,
8787
">": query_pb2.PropertyFilter.Operator.GREATER_THAN,
8888
"=": query_pb2.PropertyFilter.Operator.EQUAL,
89+
"!=": query_pb2.PropertyFilter.Operator.NOT_EQUAL,
90+
"IN": query_pb2.PropertyFilter.Operator.IN,
91+
"NOT_IN": query_pb2.PropertyFilter.Operator.NOT_IN,
8992
}
9093
"""Mapping of operator strings and their protobuf equivalents."""
9194

@@ -215,7 +218,7 @@ def add_filter(self, property_name, operator, value):
215218
216219
where property is a property stored on the entity in the datastore
217220
and operator is one of ``OPERATORS``
218-
(ie, ``=``, ``<``, ``<=``, ``>``, ``>=``):
221+
(ie, ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``):
219222
220223
.. testsetup:: query-filter
221224
@@ -235,7 +238,7 @@ def add_filter(self, property_name, operator, value):
235238
:param property_name: A property name.
236239
237240
:type operator: str
238-
:param operator: One of ``=``, ``<``, ``<=``, ``>``, ``>=``.
241+
:param operator: One of ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``.
239242
240243
:type value: :class:`int`, :class:`str`, :class:`bool`,
241244
:class:`float`, :class:`NoneType`,
@@ -252,7 +255,7 @@ def add_filter(self, property_name, operator, value):
252255
"""
253256
if self.OPERATORS.get(operator) is None:
254257
error_message = 'Invalid expression: "%s"' % (operator,)
255-
choices_message = "Please use one of: =, <, <=, >, >=."
258+
choices_message = "Please use one of: =, <, <=, >, >=, !=, IN, NOT_IN."
256259
raise ValueError(error_message, choices_message)
257260

258261
if property_name == "__key__" and not isinstance(value, Key):
@@ -293,7 +296,7 @@ def key_filter(self, key, operator="="):
293296
:param key: The key to filter on.
294297
295298
:type operator: str
296-
:param operator: (Optional) One of ``=``, ``<``, ``<=``, ``>``, ``>=``.
299+
:param operator: (Optional) One of ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``.
297300
Defaults to ``=``.
298301
"""
299302
self.add_filter("__key__", operator, key)

tests/unit/test_query.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,18 @@ def test_query_add_filter_w_all_operators():
175175
query.add_filter("lt_prop", "<", "val3")
176176
query.add_filter("gt_prop", ">", "val4")
177177
query.add_filter("eq_prop", "=", "val5")
178-
assert len(query.filters) == 5
178+
query.add_filter("in_prop", "IN", ["val6"])
179+
query.add_filter("neq_prop", "!=", "val9")
180+
query.add_filter("not_in_prop", "NOT_IN", ["val13"])
181+
assert len(query.filters) == 8
179182
assert query.filters[0] == ("leq_prop", "<=", "val1")
180183
assert query.filters[1] == ("geq_prop", ">=", "val2")
181184
assert query.filters[2] == ("lt_prop", "<", "val3")
182185
assert query.filters[3] == ("gt_prop", ">", "val4")
183186
assert query.filters[4] == ("eq_prop", "=", "val5")
187+
assert query.filters[5] == ("in_prop", "IN", ["val6"])
188+
assert query.filters[6] == ("neq_prop", "!=", "val9")
189+
assert query.filters[7] == ("not_in_prop", "NOT_IN", ["val13"])
184190

185191

186192
def test_query_add_filter_w_known_operator_and_entity():

0 commit comments

Comments
 (0)