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

Makes operator checking deterministic in Query.filter. #294

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ class Query(object):
:param dataset: The namespace to which to restrict results.
"""

OPERATORS = {
'<': datastore_pb.PropertyFilter.LESS_THAN,
'<=': datastore_pb.PropertyFilter.LESS_THAN_OR_EQUAL,
'>': datastore_pb.PropertyFilter.GREATER_THAN,
'>=': datastore_pb.PropertyFilter.GREATER_THAN_OR_EQUAL,
'=': datastore_pb.PropertyFilter.EQUAL,
}
# NOTE: Order is very important here since operators that end with
# '<=' and '>=' also end with '='.
OPERATORS = (
('<=', datastore_pb.PropertyFilter.LESS_THAN_OR_EQUAL),
('>=', datastore_pb.PropertyFilter.GREATER_THAN_OR_EQUAL),
('<', datastore_pb.PropertyFilter.LESS_THAN),
('>', datastore_pb.PropertyFilter.GREATER_THAN),
('=', datastore_pb.PropertyFilter.EQUAL),
)
"""Mapping of operator strings and their protobuf equivalents."""

def __init__(self, kind=None, dataset=None, namespace=None):
Expand Down Expand Up @@ -132,10 +134,12 @@ def filter(self, expression, value):
property_name, operator = None, None
expression = expression.strip()

for operator_string in self.OPERATORS:
for operator_string, pb_op_enum in self.OPERATORS:
if expression.endswith(operator_string):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

operator = self.OPERATORS[operator_string]
operator = pb_op_enum
property_name = expression[0:-len(operator_string)].strip()
# After one match, we move on since >= and <= conflict with =.
break

if not operator or not property_name:
raise ValueError('Invalid expression: "%s"' % expression)
Expand Down
34 changes: 33 additions & 1 deletion gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,48 @@ def test_filter_w_unknown_operator(self):
self.assertRaises(ValueError, query.filter, 'firstname ~~', 'John')

def test_filter_w_known_operator(self):
from gcloud.datastore import datastore_v1_pb2 as datastore_pb

query = self._makeOne()
after = query.filter('firstname =', u'John')
self.assertFalse(after is query)
self.assertTrue(isinstance(after, self._getTargetClass()))
q_pb = after.to_protobuf()
self.assertEqual(q_pb.filter.composite_filter.operator, 1) # AND
self.assertEqual(q_pb.filter.composite_filter.operator,
datastore_pb.CompositeFilter.AND)
f_pb, = list(q_pb.filter.composite_filter.filter)
p_pb = f_pb.property_filter
self.assertEqual(p_pb.property.name, 'firstname')
self.assertEqual(p_pb.value.string_value, u'John')
self.assertEqual(p_pb.operator, datastore_pb.PropertyFilter.EQUAL)

def test_filter_w_all_operators(self):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

from gcloud.datastore import datastore_v1_pb2 as datastore_pb

query = self._makeOne()
query = query.filter('leq_prop <=', u'val1')
query = query.filter('geq_prop >=', u'val2')
query = query.filter('lt_prop <', u'val3')
query = query.filter('gt_prop >', u'val4')
query = query.filter('eq_prop =', u'val5')

query_pb = query.to_protobuf()
pb_values = [
('leq_prop', 'val1',
datastore_pb.PropertyFilter.LESS_THAN_OR_EQUAL),
('geq_prop', 'val2',
datastore_pb.PropertyFilter.GREATER_THAN_OR_EQUAL),
('lt_prop', 'val3', datastore_pb.PropertyFilter.LESS_THAN),
('gt_prop', 'val4', datastore_pb.PropertyFilter.GREATER_THAN),
('eq_prop', 'val5', datastore_pb.PropertyFilter.EQUAL),
]
query_filter = query_pb.filter.composite_filter.filter
for filter_pb, pb_value in zip(query_filter, pb_values):
name, val, filter_enum = pb_value
prop_filter = filter_pb.property_filter
self.assertEqual(prop_filter.property.name, name)
self.assertEqual(prop_filter.value.string_value, val)
self.assertEqual(prop_filter.operator, filter_enum)

def test_filter_w_known_operator_and_entity(self):
import operator
Expand Down