From b7bee412b03c367edd78e0b8810ab513804cc780 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Fri, 17 Oct 2014 11:21:03 -0400 Subject: [PATCH] Add namespace to query ctor / clone. --- gcloud/datastore/query.py | 17 +++++++++++++++-- gcloud/datastore/test_query.py | 13 +++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/gcloud/datastore/query.py b/gcloud/datastore/query.py index fa1961864340..0e6e29276e25 100644 --- a/gcloud/datastore/query.py +++ b/gcloud/datastore/query.py @@ -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 = { @@ -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 @@ -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`. diff --git a/gcloud/datastore/test_query.py b/gcloud/datastore/test_query.py index 9a5f016adbc6..1464ae93cd5c 100644 --- a/gcloud/datastore/test_query.py +++ b/gcloud/datastore/test_query.py @@ -8,25 +8,28 @@ 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 @@ -34,13 +37,15 @@ def test__clone(self): _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)