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

Removing datastore key query filter restriction. #918

Merged
merged 1 commit into from
Jun 10, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)])

This comment was marked as spam.


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