| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,9 @@ class Query(object): | |||
| 41 | 41 | ||
| 42 | 42 | :type dataset: :class:`gcloud.datastore.dataset.Dataset` | |
| 43 | 43 | :param dataset: The dataset to query. | |
| 44 | + | ||
| 45 | + :type namespace: string or None | ||
| 46 | + :param dataset: The namespace to which to restrict results. | ||
| 44 | 47 | """ | |
| 45 | 48 | ||
| 46 | 49 | OPERATORS = { | |
@@ -52,8 +55,9 @@ class Query(object): | |||
| 52 | 55 | } | |
| 53 | 56 | """Mapping of operator strings and their protobuf equivalents.""" | |
| 54 | 57 | ||
| 55 | - def __init__(self, kind=None, dataset=None): | ||
| 58 | + def __init__(self, kind=None, dataset=None, namespace=None): | ||
| 56 | 59 | self._dataset = dataset | |
| 60 | + self._namespace = namespace | ||
| 57 | 61 | self._pb = datastore_pb.Query() | |
| 58 | 62 | self._cursor = None | |
| 59 | 63 | ||
@@ -66,11 +70,20 @@ def _clone(self): | |||
| 66 | 70 | :rtype: :class:`gcloud.datastore.query.Query` | |
| 67 | 71 | :returns: a copy of 'self'. | |
| 68 | 72 | """ | |
| 69 | - clone = self.__class__(dataset=self._dataset) | ||
| 73 | + clone = self.__class__(dataset=self._dataset, | ||
| 74 | + namespace=self._namespace) | ||
| 70 | 75 | clone._pb.CopyFrom(self._pb) | |
| 71 | 76 | clone._cursor = self._cursor | |
| 72 | 77 | return clone | |
| 73 | 78 | ||
| 79 | + def namespace(self): | ||
| 80 | + """This query's namespace | ||
| 81 | + | ||
| 82 | + :rtype: string or None | ||
| 83 | + :returns: the namespace assigned to this query | ||
| 84 | + """ | ||
| 85 | + return self._namespace | ||
| 86 | + | ||
| 74 | 87 | def to_protobuf(self): | |
| 75 | 88 | """Convert :class:`Query` instance to :class:`.datastore_v1_pb2.Query`. | |
| 76 | 89 | ||
@@ -316,7 +329,10 @@ def fetch(self, limit=None): | |||
| 316 | 329 | clone = self.limit(limit) | |
| 317 | 330 | ||
| 318 | 331 | query_results = self.dataset().connection().run_query( | |
| 319 | - query_pb=clone.to_protobuf(), dataset_id=self.dataset().id()) | ||
| 332 | + query_pb=clone.to_protobuf(), | ||
| 333 | + dataset_id=self.dataset().id(), | ||
| 334 | + namespace=self._namespace, | ||
| 335 | + ) | ||
| 320 | 336 | entity_pbs, end_cursor = query_results[:2] | |
| 321 | 337 | ||
| 322 | 338 | self._cursor = end_cursor | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,39 +8,44 @@ def _getTargetClass(self): | |||
| 8 | 8 | ||
| 9 | 9 | return Query | |
| 10 | 10 | ||
| 11 | - def _makeOne(self, kind=None, dataset=None): | ||
| 12 | - return self._getTargetClass()(kind, dataset) | ||
| 11 | + def _makeOne(self, kind=None, dataset=None, namespace=None): | ||
| 12 | + return self._getTargetClass()(kind, dataset, namespace) | ||
| 13 | 13 | ||
| 14 | 14 | def test_ctor_defaults(self): | |
| 15 | - query = self._makeOne() | ||
| 15 | + query = self._getTargetClass()() | ||
| 16 | 16 | self.assertEqual(query.dataset(), None) | |
| 17 | 17 | self.assertEqual(list(query.kind()), []) | |
| 18 | 18 | self.assertEqual(query.limit(), 0) | |
| 19 | + self.assertEqual(query.namespace(), None) | ||
| 19 | 20 | ||
| 20 | 21 | def test_ctor_explicit(self): | |
| 21 | 22 | from gcloud.datastore.dataset import Dataset | |
| 22 | 23 | ||
| 23 | 24 | _DATASET = 'DATASET' | |
| 24 | 25 | _KIND = 'KIND' | |
| 26 | + _NAMESPACE = 'NAMESPACE' | ||
| 25 | 27 | dataset = Dataset(_DATASET) | |
| 26 | - query = self._makeOne(_KIND, dataset) | ||
| 28 | + query = self._makeOne(_KIND, dataset, _NAMESPACE) | ||
| 27 | 29 | self.assertTrue(query.dataset() is dataset) | |
| 28 | 30 | kq_pb, = list(query.kind()) | |
| 29 | 31 | self.assertEqual(kq_pb.name, _KIND) | |
| 32 | + self.assertEqual(query.namespace(), _NAMESPACE) | ||
| 30 | 33 | ||
| 31 | 34 | def test__clone(self): | |
| 32 | 35 | from gcloud.datastore.dataset import Dataset | |
| 33 | 36 | ||
| 34 | 37 | _DATASET = 'DATASET' | |
| 35 | 38 | _KIND = 'KIND' | |
| 36 | 39 | _CURSOR = 'DEADBEEF' | |
| 40 | + _NAMESPACE = 'NAMESPACE' | ||
| 37 | 41 | dataset = Dataset(_DATASET) | |
| 38 | - query = self._makeOne(_KIND, dataset) | ||
| 42 | + query = self._makeOne(_KIND, dataset, _NAMESPACE) | ||
| 39 | 43 | query._cursor = _CURSOR | |
| 40 | 44 | clone = query._clone() | |
| 41 | 45 | self.assertFalse(clone is query) | |
| 42 | 46 | self.assertTrue(isinstance(clone, self._getTargetClass())) | |
| 43 | 47 | self.assertTrue(clone.dataset() is dataset) | |
| 48 | + self.assertEqual(clone.namespace(), _NAMESPACE) | ||
| 44 | 49 | kq_pb, = list(clone.kind()) | |
| 45 | 50 | self.assertEqual(kq_pb.name, _KIND) | |
| 46 | 51 | self.assertEqual(clone._cursor, _CURSOR) | |
@@ -101,7 +106,7 @@ def test_ancestor_w_non_key_non_list(self): | |||
| 101 | 106 | query = self._makeOne() | |
| 102 | 107 | self.assertRaises(TypeError, query.ancestor, object()) | |
| 103 | 108 | ||
| 104 | - def test_ancester_wo_existing_ancestor_query_w_key_and_propfilter(self): | ||
| 109 | + def test_ancestor_wo_existing_ancestor_query_w_key_and_propfilter(self): | ||
| 105 | 110 | from gcloud.datastore.key import Key | |
| 106 | 111 | _KIND = 'KIND' | |
| 107 | 112 | _ID = 123 | |
@@ -121,7 +126,7 @@ def test_ancester_wo_existing_ancestor_query_w_key_and_propfilter(self): | |||
| 121 | 126 | self.assertEqual(p_pb.property.name, '__key__') | |
| 122 | 127 | self.assertEqual(p_pb.value.key_value, key.to_protobuf()) | |
| 123 | 128 | ||
| 124 | - def test_ancester_wo_existing_ancestor_query_w_key(self): | ||
| 129 | + def test_ancestor_wo_existing_ancestor_query_w_key(self): | ||
| 125 | 130 | from gcloud.datastore.key import Key | |
| 126 | 131 | _KIND = 'KIND' | |
| 127 | 132 | _ID = 123 | |
@@ -137,7 +142,7 @@ def test_ancester_wo_existing_ancestor_query_w_key(self): | |||
| 137 | 142 | self.assertEqual(p_pb.property.name, '__key__') | |
| 138 | 143 | self.assertEqual(p_pb.value.key_value, key.to_protobuf()) | |
| 139 | 144 | ||
| 140 | - def test_ancester_wo_existing_ancestor_query_w_list(self): | ||
| 145 | + def test_ancestor_wo_existing_ancestor_query_w_list(self): | ||
| 141 | 146 | from gcloud.datastore.key import Key | |
| 142 | 147 | _KIND = 'KIND' | |
| 143 | 148 | _ID = 123 | |
@@ -153,7 +158,7 @@ def test_ancester_wo_existing_ancestor_query_w_list(self): | |||
| 153 | 158 | self.assertEqual(p_pb.property.name, '__key__') | |
| 154 | 159 | self.assertEqual(p_pb.value.key_value, key.to_protobuf()) | |
| 155 | 160 | ||
| 156 | - def test_ancester_clears_existing_ancestor_query_w_only(self): | ||
| 161 | + def test_ancestor_clears_existing_ancestor_query_w_only(self): | ||
| 157 | 162 | _KIND = 'KIND' | |
| 158 | 163 | _ID = 123 | |
| 159 | 164 | query = self._makeOne() | |
@@ -164,7 +169,7 @@ def test_ancester_clears_existing_ancestor_query_w_only(self): | |||
| 164 | 169 | q_pb = after.to_protobuf() | |
| 165 | 170 | self.assertEqual(list(q_pb.filter.composite_filter.filter), []) | |
| 166 | 171 | ||
| 167 | - def test_ancester_clears_existing_ancestor_query_w_others(self): | ||
| 172 | + def test_ancestor_clears_existing_ancestor_query_w_others(self): | ||
| 168 | 173 | _KIND = 'KIND' | |
| 169 | 174 | _ID = 123 | |
| 170 | 175 | _NAME = 'NAME' | |
@@ -257,6 +262,7 @@ def test_fetch_default_limit(self): | |||
| 257 | 262 | expected_called_with = { | |
| 258 | 263 | 'dataset_id': _DATASET, | |
| 259 | 264 | 'query_pb': query.to_protobuf(), | |
| 265 | + 'namespace': None, | ||
| 260 | 266 | } | |
| 261 | 267 | self.assertEqual(connection._called_with, expected_called_with) | |
| 262 | 268 | ||
@@ -266,6 +272,7 @@ def test_fetch_explicit_limit(self): | |||
| 266 | 272 | _DATASET = 'DATASET' | |
| 267 | 273 | _KIND = 'KIND' | |
| 268 | 274 | _ID = 123 | |
| 275 | + _NAMESPACE = 'NAMESPACE' | ||
| 269 | 276 | entity_pb = Entity() | |
| 270 | 277 | path_element = entity_pb.key.path_element.add() | |
| 271 | 278 | path_element.kind = _KIND | |
@@ -276,7 +283,7 @@ def test_fetch_explicit_limit(self): | |||
| 276 | 283 | connection = _Connection(entity_pb) | |
| 277 | 284 | connection._cursor = _CURSOR | |
| 278 | 285 | dataset = _Dataset(_DATASET, connection) | |
| 279 | - query = self._makeOne(_KIND, dataset) | ||
| 286 | + query = self._makeOne(_KIND, dataset, _NAMESPACE) | ||
| 280 | 287 | limited = query.limit(13) | |
| 281 | 288 | entities = query.fetch(13) | |
| 282 | 289 | self.assertEqual(query._cursor, _CURSOR) | |
@@ -286,6 +293,7 @@ def test_fetch_explicit_limit(self): | |||
| 286 | 293 | expected_called_with = { | |
| 287 | 294 | 'dataset_id': _DATASET, | |
| 288 | 295 | 'query_pb': limited.to_protobuf(), | |
| 296 | + 'namespace': _NAMESPACE, | ||
| 289 | 297 | } | |
| 290 | 298 | self.assertEqual(connection._called_with, expected_called_with) | |
| 291 | 299 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments