Skip to content

Commit

Permalink
Merge pull request #918 from dhermes/fix-917
Browse files Browse the repository at this point in the history
Removing datastore key query filter restriction.
  • Loading branch information
dhermes committed Jun 10, 2015
2 parents 3e86aae + 3c1168c commit 319963f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
12 changes: 6 additions & 6 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def __init__(self,
self._kind = kind
self._namespace = namespace
self._ancestor = ancestor
self._filters = list(filters)
self._filters = []
# Verify filters passed in.
for property_name, operator, value in filters:
self.add_filter(property_name, operator, value)
self._projection = _ensure_tuple_or_list('projection', projection)
self._order = _ensure_tuple_or_list('order', order)
self._group_by = _ensure_tuple_or_list('group_by', group_by)
Expand Down Expand Up @@ -216,11 +219,8 @@ def add_filter(self, property_name, operator, value):
choices_message = 'Please use one of: =, <, <=, >, >=.'
raise ValueError(error_message, choices_message)

if property_name == '__key__':
if not isinstance(value, Key):
raise ValueError('Invalid key: "%s"' % value)
if operator != '=':
raise ValueError('Invalid operator for key: "%s"' % operator)
if property_name == '__key__' and not isinstance(value, Key):
raise ValueError('Invalid key: "%s"' % value)

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

Expand Down
12 changes: 10 additions & 2 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def test_ctor_bad_group_by(self):
self.assertRaises(TypeError, self._makeOne, _KIND, _DATASET,
group_by=BAD_GROUP_BY)

def test_ctor_bad_filters(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
FILTERS_CANT_UNPACK = [('one', 'two')]
self.assertRaises(ValueError, self._makeOne, _KIND, _DATASET,
filters=FILTERS_CANT_UNPACK)

def test_namespace_setter_w_non_string(self):
_DATASET = 'DATASET'
query = self._makeOne(dataset_id=_DATASET)
Expand Down Expand Up @@ -224,12 +231,13 @@ def test_add_filter___key__valid_key(self):
query.add_filter('__key__', '=', key)
self.assertEqual(query.filters, [('__key__', '=', key)])

def test_filter___key__invalid_operator(self):
def test_filter___key__not_equal_operator(self):
from gcloud.datastore.key import Key
_DATASET = 'DATASET'
key = Key('Foo', dataset_id='DATASET')
query = self._makeOne(dataset_id=_DATASET)
self.assertRaises(ValueError, query.add_filter, '__key__', '<', key)
query.add_filter('__key__', '<', key)
self.assertEqual(query.filters, [('__key__', '<', key)])

def test_filter___key__invalid_value(self):
_DATASET = 'DATASET'
Expand Down

0 comments on commit 319963f

Please sign in to comment.