Skip to content

Commit

Permalink
Add namespace to query ctor / clone.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver committed Oct 19, 2014
1 parent 6a732f1 commit b7bee41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
17 changes: 15 additions & 2 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class Query(object):
:type dataset: :class:`gcloud.datastore.dataset.Dataset`
:param dataset: The dataset to query.
:type namespace: string or None
:param dataset: The namespace to which to restrict results.
"""

OPERATORS = {
Expand All @@ -52,8 +55,9 @@ class Query(object):
}
"""Mapping of operator strings and their protobuf equivalents."""

def __init__(self, kind=None, dataset=None):
def __init__(self, kind=None, dataset=None, namespace=None):
self._dataset = dataset
self._namespace = namespace
self._pb = datastore_pb.Query()
self._cursor = None

Expand All @@ -66,11 +70,20 @@ def _clone(self):
:rtype: :class:`gcloud.datastore.query.Query`
:returns: a copy of 'self'.
"""
clone = self.__class__(dataset=self._dataset)
clone = self.__class__(dataset=self._dataset,
namespace=self._namespace)
clone._pb.CopyFrom(self._pb)
clone._cursor = self._cursor
return clone

def namespace(self):
"""This query's namespace
:rtype: string or None
:returns: the namespace assigned to this query
"""
return self._namespace

def to_protobuf(self):
"""Convert :class:`Query` instance to :class:`.datastore_v1_pb2.Query`.
Expand Down
13 changes: 9 additions & 4 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@ def _getTargetClass(self):

return Query

def _makeOne(self, kind=None, dataset=None):
return self._getTargetClass()(kind, dataset)
def _makeOne(self, kind=None, dataset=None, namespace=None):
return self._getTargetClass()(kind, dataset, namespace)

def test_ctor_defaults(self):
query = self._getTargetClass()()
self.assertEqual(query.dataset(), None)
self.assertEqual(list(query.kind()), [])
self.assertEqual(query.limit(), 0)
self.assertEqual(query.namespace(), None)

def test_ctor_explicit(self):
from gcloud.datastore.dataset import Dataset

_DATASET = 'DATASET'
_KIND = 'KIND'
_NAMESPACE = 'NAMESPACE'
dataset = Dataset(_DATASET)
query = self._makeOne(_KIND, dataset)
query = self._makeOne(_KIND, dataset, _NAMESPACE)
self.assertTrue(query.dataset() is dataset)
kq_pb, = list(query.kind())
self.assertEqual(kq_pb.name, _KIND)
self.assertEqual(query.namespace(), _NAMESPACE)

def test__clone(self):
from gcloud.datastore.dataset import Dataset

_DATASET = 'DATASET'
_KIND = 'KIND'
_CURSOR = 'DEADBEEF'
_NAMESPACE = 'NAMESPACE'
dataset = Dataset(_DATASET)
query = self._makeOne(_KIND, dataset)
query = self._makeOne(_KIND, dataset, _NAMESPACE)
query._cursor = _CURSOR
clone = query._clone()
self.assertFalse(clone is query)
self.assertTrue(isinstance(clone, self._getTargetClass()))
self.assertTrue(clone.dataset() is dataset)
self.assertEqual(clone.namespace(), _NAMESPACE)
kq_pb, = list(clone.kind())
self.assertEqual(kq_pb.name, _KIND)
self.assertEqual(clone._cursor, _CURSOR)
Expand Down

0 comments on commit b7bee41

Please sign in to comment.