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

Swapping the order of kind and dataset_id in Query constructor. #718

Merged
merged 1 commit into from
Mar 12, 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
9 changes: 5 additions & 4 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class Query(object):
"""Mapping of operator strings and their protobuf equivalents."""

def __init__(self,
dataset_id=None,
kind=None,
dataset_id=None,
namespace=None,
ancestor=None,
filters=(),
Expand Down Expand Up @@ -191,7 +191,8 @@ def add_filter(self, property_name, operator, value):
and operator is one of ``OPERATORS``
(ie, ``=``, ``<``, ``<=``, ``>``, ``>=``)::

>>> query = Query('Person')
>>> from gcloud import datastore
>>> query = datastore.Query('Person')
>>> query.add_filter('name', '=', 'James')
>>> query.add_filter('age', '>', 50)

Expand Down Expand Up @@ -297,8 +298,8 @@ def fetch(self, limit=None, offset=0, start_cursor=None, end_cursor=None,

For example::

>>> from gcloud.datastore.query import Query
>>> query = Query('dataset-id', 'Person')
>>> from gcloud import datastore
>>> query = datastore.Query('Person')
>>> query.add_filter('name', '=', 'Sally')
>>> list(query.fetch())
[<Entity object>, <Entity object>, ...]
Expand Down
66 changes: 33 additions & 33 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def test_ctor_explicit(self):
ORDER = ['foo', 'bar']
GROUP_BY = ['foo']
query = self._makeOne(
dataset_id=_DATASET,
kind=_KIND,
dataset_id=_DATASET,
namespace=_NAMESPACE,
ancestor=ancestor,
filters=FILTERS,
Expand All @@ -81,7 +81,7 @@ def test_ctor_explicit(self):

def test_namespace_setter_w_non_string(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)

def _assign(val):
query.namespace = val
Expand All @@ -91,14 +91,14 @@ def _assign(val):
def test_namespace_setter(self):
_DATASET = 'DATASET'
_NAMESPACE = 'NAMESPACE'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
query.namespace = _NAMESPACE
self.assertEqual(query.dataset_id, _DATASET)
self.assertEqual(query.namespace, _NAMESPACE)

def test_kind_setter_w_non_string(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)

def _assign(val):
query.kind = val
Expand All @@ -108,7 +108,7 @@ def _assign(val):
def test_kind_setter_wo_existing(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
query.kind = _KIND
self.assertEqual(query.dataset_id, _DATASET)
self.assertEqual(query.kind, _KIND)
Expand All @@ -117,15 +117,15 @@ def test_kind_setter_w_existing(self):
_DATASET = 'DATASET'
_KIND_BEFORE = 'KIND_BEFORE'
_KIND_AFTER = 'KIND_AFTER'
query = self._makeOne(_DATASET, _KIND_BEFORE)
query = self._makeOne(_KIND_BEFORE, _DATASET)
self.assertEqual(query.kind, _KIND_BEFORE)
query.kind = _KIND_AFTER
self.assertEqual(query.dataset_id, _DATASET)
self.assertEqual(query.kind, _KIND_AFTER)

def test_ancestor_setter_w_non_key(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)

def _assign(val):
query.ancestor = val
Expand All @@ -138,7 +138,7 @@ def test_ancestor_setter_w_key(self):
_DATASET = 'DATASET'
_NAME = u'NAME'
key = Key('KIND', 123, dataset_id='DATASET')
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
query.add_filter('name', '=', _NAME)
query.ancestor = key
self.assertEqual(query.ancestor.path, key.path)
Expand All @@ -147,25 +147,25 @@ def test_ancestor_deleter_w_key(self):
from gcloud.datastore.key import Key
_DATASET = 'DATASET'
key = Key('KIND', 123, dataset_id='DATASET')
query = self._makeOne(_DATASET, ancestor=key)
query = self._makeOne(dataset_id=_DATASET, ancestor=key)
del query.ancestor
self.assertTrue(query.ancestor is None)

def test_add_filter_setter_w_unknown_operator(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
self.assertRaises(ValueError, query.add_filter,
'firstname', '~~', 'John')

def test_add_filter_w_known_operator(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
query.add_filter('firstname', '=', u'John')
self.assertEqual(query.filters, [('firstname', '=', u'John')])

def test_add_filter_w_all_operators(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
query.add_filter('leq_prop', '<=', u'val1')
query.add_filter('geq_prop', '>=', u'val2')
query.add_filter('lt_prop', '<', u'val3')
Expand All @@ -181,7 +181,7 @@ def test_add_filter_w_all_operators(self):
def test_add_filter_w_known_operator_and_entity(self):
from gcloud.datastore.entity import Entity
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
other = Entity()
other['firstname'] = u'John'
other['lastname'] = u'Smith'
Expand All @@ -190,15 +190,15 @@ def test_add_filter_w_known_operator_and_entity(self):

def test_add_filter_w_whitespace_property_name(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
PROPERTY_NAME = ' property with lots of space '
query.add_filter(PROPERTY_NAME, '=', u'John')
self.assertEqual(query.filters, [(PROPERTY_NAME, '=', u'John')])

def test_add_filter___key__valid_key(self):
from gcloud.datastore.key import Key
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
key = Key('Foo', dataset_id='DATASET')
query.add_filter('__key__', '=', key)
self.assertEqual(query.filters, [('__key__', '=', key)])
Expand All @@ -207,32 +207,32 @@ def test_filter___key__invalid_operator(self):
from gcloud.datastore.key import Key
_DATASET = 'DATASET'
key = Key('Foo', dataset_id='DATASET')
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
self.assertRaises(ValueError, query.add_filter, '__key__', '<', key)

def test_filter___key__invalid_value(self):
_DATASET = 'DATASET'
query = self._makeOne(_DATASET)
query = self._makeOne(dataset_id=_DATASET)
self.assertRaises(ValueError, query.add_filter, '__key__', '=', None)

def test_projection_setter_empty(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.projection = []
self.assertEqual(query.projection, [])

def test_projection_setter_string(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.projection = 'field1'
self.assertEqual(query.projection, ['field1'])

def test_projection_setter_non_empty(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.projection = ['field1', 'field2']
self.assertEqual(query.projection, ['field1', 'field2'])

Expand All @@ -241,7 +241,7 @@ def test_projection_setter_multiple_calls(self):
_KIND = 'KIND'
_PROJECTION1 = ['field1', 'field2']
_PROJECTION2 = ['field3']
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.projection = _PROJECTION1
self.assertEqual(query.projection, _PROJECTION1)
query.projection = _PROJECTION2
Expand All @@ -250,56 +250,56 @@ def test_projection_setter_multiple_calls(self):
def test_keys_only(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.keys_only()
self.assertEqual(query.projection, ['__key__'])

def test_order_setter_empty(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND, order=['foo', '-bar'])
query = self._makeOne(_KIND, _DATASET, order=['foo', '-bar'])
query.order = []
self.assertEqual(query.order, [])

def test_order_setter_string(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.order = 'field'
self.assertEqual(query.order, ['field'])

def test_order_setter_single_item_list_desc(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.order = ['-field']
self.assertEqual(query.order, ['-field'])

def test_order_setter_multiple(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.order = ['foo', '-bar']
self.assertEqual(query.order, ['foo', '-bar'])

def test_group_by_setter_empty(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND, group_by=['foo', 'bar'])
query = self._makeOne(_KIND, _DATASET, group_by=['foo', 'bar'])
query.group_by = []
self.assertEqual(query.group_by, [])

def test_group_by_setter_string(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.group_by = 'field1'
self.assertEqual(query.group_by, ['field1'])

def test_group_by_setter_non_empty(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.group_by = ['field1', 'field2']
self.assertEqual(query.group_by, ['field1', 'field2'])

Expand All @@ -308,7 +308,7 @@ def test_group_by_multiple_calls(self):
_KIND = 'KIND'
_GROUP_BY1 = ['field1', 'field2']
_GROUP_BY2 = ['field3']
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
query.group_by = _GROUP_BY1
self.assertEqual(query.group_by, _GROUP_BY1)
query.group_by = _GROUP_BY2
Expand All @@ -317,7 +317,7 @@ def test_group_by_multiple_calls(self):
def test_fetch_defaults_wo_implicit_connection(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
self.assertRaises(ValueError, query.fetch)

def test_fetch_defaults_w_implicit_connection(self):
Expand All @@ -326,7 +326,7 @@ def test_fetch_defaults_w_implicit_connection(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
connection = _Connection()
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)

with _monkey_defaults(connection=connection):
iterator = query.fetch()
Expand All @@ -338,7 +338,7 @@ def test_fetch_explicit(self):
_DATASET = 'DATASET'
_KIND = 'KIND'
connection = _Connection()
query = self._makeOne(_DATASET, _KIND)
query = self._makeOne(_KIND, _DATASET)
iterator = query.fetch(limit=7, offset=8, connection=connection)
self.assertTrue(iterator._query is query)
self.assertEqual(iterator._limit, 7)
Expand Down