|
| 1 | +# Copyright 2014 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest2 |
| 16 | + |
| 17 | + |
| 18 | +class TestDataset(unittest2.TestCase): |
| 19 | + |
| 20 | + DATASET_ID = 'DATASET' |
| 21 | + |
| 22 | + def _getTargetClass(self): |
| 23 | + from gcloud.datastore.dataset import Dataset |
| 24 | + return Dataset |
| 25 | + |
| 26 | + def _makeOne(self, dataset_id=DATASET_ID, connection=None): |
| 27 | + return self._getTargetClass()(dataset_id, connection) |
| 28 | + |
| 29 | + def test_ctor_w_dataset_id_None(self): |
| 30 | + self.assertRaises(ValueError, self._makeOne, None) |
| 31 | + |
| 32 | + def test_ctor_w_dataset_id_no_connection(self): |
| 33 | + dataset = self._makeOne() |
| 34 | + self.assertEqual(dataset.dataset_id, self.DATASET_ID) |
| 35 | + |
| 36 | + def test_ctor_w_dataset_id_w_connection(self): |
| 37 | + conn = object() |
| 38 | + dataset = self._makeOne(connection=conn) |
| 39 | + self.assertEqual(dataset.dataset_id, self.DATASET_ID) |
| 40 | + self.assertTrue(dataset.connection is conn) |
| 41 | + |
| 42 | + def test_get_defaults(self): |
| 43 | + from gcloud.datastore import dataset as MUT |
| 44 | + from gcloud._testing import _Monkey |
| 45 | + |
| 46 | + _called_with = [] |
| 47 | + |
| 48 | + def _get(*args, **kw): |
| 49 | + _called_with.append((args, kw)) |
| 50 | + |
| 51 | + dataset = self._makeOne() |
| 52 | + key = object() |
| 53 | + |
| 54 | + with _Monkey(MUT, get=_get): |
| 55 | + dataset.get([key]) |
| 56 | + |
| 57 | + self.assertEqual(_called_with[0][0], ([key],)) |
| 58 | + self.assertTrue(_called_with[0][1]['missing'] is None) |
| 59 | + self.assertTrue(_called_with[0][1]['deferred'] is None) |
| 60 | + self.assertTrue(_called_with[0][1]['connection'] is None) |
| 61 | + self.assertEqual(_called_with[0][1]['dataset_id'], self.DATASET_ID) |
| 62 | + |
| 63 | + def test_get_explicit(self): |
| 64 | + from gcloud.datastore import dataset as MUT |
| 65 | + from gcloud._testing import _Monkey |
| 66 | + |
| 67 | + _called_with = [] |
| 68 | + |
| 69 | + def _get(*args, **kw): |
| 70 | + _called_with.append((args, kw)) |
| 71 | + |
| 72 | + conn = object() |
| 73 | + dataset = self._makeOne(connection=conn) |
| 74 | + key, missing, deferred = object(), [], [] |
| 75 | + |
| 76 | + with _Monkey(MUT, get=_get): |
| 77 | + dataset.get([key], missing, deferred) |
| 78 | + |
| 79 | + self.assertEqual(_called_with[0][0], ([key],)) |
| 80 | + self.assertTrue(_called_with[0][1]['missing'] is missing) |
| 81 | + self.assertTrue(_called_with[0][1]['deferred'] is deferred) |
| 82 | + self.assertTrue(_called_with[0][1]['connection'] is conn) |
| 83 | + self.assertEqual(_called_with[0][1]['dataset_id'], self.DATASET_ID) |
| 84 | + |
| 85 | + def test_put_wo_connection(self): |
| 86 | + from gcloud.datastore import dataset as MUT |
| 87 | + from gcloud._testing import _Monkey |
| 88 | + |
| 89 | + _called_with = [] |
| 90 | + |
| 91 | + def _put(*args, **kw): |
| 92 | + _called_with.append((args, kw)) |
| 93 | + |
| 94 | + dataset = self._makeOne() |
| 95 | + entity = object() |
| 96 | + |
| 97 | + with _Monkey(MUT, put=_put): |
| 98 | + dataset.put([entity]) |
| 99 | + |
| 100 | + self.assertEqual(_called_with[0][0], ([entity],)) |
| 101 | + self.assertTrue(_called_with[0][1]['connection'] is None) |
| 102 | + self.assertEqual(_called_with[0][1]['dataset_id'], self.DATASET_ID) |
| 103 | + |
| 104 | + def test_put_w_connection(self): |
| 105 | + from gcloud.datastore import dataset as MUT |
| 106 | + from gcloud._testing import _Monkey |
| 107 | + |
| 108 | + _called_with = [] |
| 109 | + |
| 110 | + def _put(*args, **kw): |
| 111 | + _called_with.append((args, kw)) |
| 112 | + |
| 113 | + entity, conn = object(), object() |
| 114 | + dataset = self._makeOne(connection=conn) |
| 115 | + |
| 116 | + with _Monkey(MUT, put=_put): |
| 117 | + dataset.put([entity]) |
| 118 | + |
| 119 | + self.assertEqual(_called_with[0][0], ([entity],)) |
| 120 | + self.assertTrue(_called_with[0][1]['connection'] is conn) |
| 121 | + self.assertEqual(_called_with[0][1]['dataset_id'], self.DATASET_ID) |
| 122 | + |
| 123 | + def test_delete_wo_connection(self): |
| 124 | + from gcloud.datastore import dataset as MUT |
| 125 | + from gcloud._testing import _Monkey |
| 126 | + |
| 127 | + _called_with = [] |
| 128 | + |
| 129 | + def _delete(*args, **kw): |
| 130 | + _called_with.append((args, kw)) |
| 131 | + |
| 132 | + dataset = self._makeOne() |
| 133 | + key = object() |
| 134 | + |
| 135 | + with _Monkey(MUT, delete=_delete): |
| 136 | + dataset.delete([key]) |
| 137 | + |
| 138 | + self.assertEqual(_called_with[0][0], ([key],)) |
| 139 | + self.assertTrue(_called_with[0][1]['connection'] is None) |
| 140 | + self.assertEqual(_called_with[0][1]['dataset_id'], self.DATASET_ID) |
| 141 | + |
| 142 | + def test_delete_w_connection(self): |
| 143 | + from gcloud.datastore import dataset as MUT |
| 144 | + from gcloud._testing import _Monkey |
| 145 | + |
| 146 | + _called_with = [] |
| 147 | + |
| 148 | + def _delete(*args, **kw): |
| 149 | + _called_with.append((args, kw)) |
| 150 | + |
| 151 | + key, conn = object(), object() |
| 152 | + dataset = self._makeOne(connection=conn) |
| 153 | + with _Monkey(MUT, delete=_delete): |
| 154 | + dataset.delete([key]) |
| 155 | + |
| 156 | + self.assertEqual(_called_with[0][0], ([key],)) |
| 157 | + self.assertTrue(_called_with[0][1]['connection'] is conn) |
| 158 | + self.assertEqual(_called_with[0][1]['dataset_id'], self.DATASET_ID) |
| 159 | + |
| 160 | + def test_key_w_dataset_id(self): |
| 161 | + KIND = 'KIND' |
| 162 | + ID = 1234 |
| 163 | + dataset = self._makeOne() |
| 164 | + self.assertRaises(TypeError, |
| 165 | + dataset.key, KIND, ID, dataset_id=self.DATASET_ID) |
| 166 | + |
| 167 | + def test_key_wo_dataset_id(self): |
| 168 | + from gcloud.datastore import dataset as MUT |
| 169 | + from gcloud._testing import _Monkey |
| 170 | + KIND = 'KIND' |
| 171 | + ID = 1234 |
| 172 | + dataset = self._makeOne() |
| 173 | + |
| 174 | + with _Monkey(MUT, Key=_Dummy): |
| 175 | + key = dataset.key(KIND, ID) |
| 176 | + |
| 177 | + self.assertTrue(isinstance(key, _Dummy)) |
| 178 | + self.assertEqual(key.args, (KIND, ID)) |
| 179 | + self.assertEqual(key.kwargs, {'dataset_id': self.DATASET_ID}) |
| 180 | + |
| 181 | + def test_batch_wo_connection(self): |
| 182 | + from gcloud.datastore import dataset as MUT |
| 183 | + from gcloud._testing import _Monkey |
| 184 | + dataset = self._makeOne() |
| 185 | + |
| 186 | + with _Monkey(MUT, Batch=_Dummy): |
| 187 | + batch = dataset.batch() |
| 188 | + |
| 189 | + self.assertTrue(isinstance(batch, _Dummy)) |
| 190 | + self.assertEqual(batch.args, ()) |
| 191 | + self.assertEqual(batch.kwargs, |
| 192 | + {'dataset_id': self.DATASET_ID, 'connection': None}) |
| 193 | + |
| 194 | + def test_batch_w_connection(self): |
| 195 | + from gcloud.datastore import dataset as MUT |
| 196 | + from gcloud._testing import _Monkey |
| 197 | + conn = object() |
| 198 | + dataset = self._makeOne(connection=conn) |
| 199 | + |
| 200 | + with _Monkey(MUT, Batch=_Dummy): |
| 201 | + batch = dataset.batch() |
| 202 | + |
| 203 | + self.assertTrue(isinstance(batch, _Dummy)) |
| 204 | + self.assertEqual(batch.args, ()) |
| 205 | + self.assertEqual(batch.kwargs, |
| 206 | + {'dataset_id': self.DATASET_ID, 'connection': conn}) |
| 207 | + |
| 208 | + def test_transaction_wo_connection(self): |
| 209 | + from gcloud.datastore import dataset as MUT |
| 210 | + from gcloud._testing import _Monkey |
| 211 | + dataset = self._makeOne() |
| 212 | + |
| 213 | + with _Monkey(MUT, Transaction=_Dummy): |
| 214 | + xact = dataset.transaction() |
| 215 | + |
| 216 | + self.assertTrue(isinstance(xact, _Dummy)) |
| 217 | + self.assertEqual(xact.args, ()) |
| 218 | + self.assertEqual(xact.kwargs, |
| 219 | + {'dataset_id': self.DATASET_ID, 'connection': None}) |
| 220 | + |
| 221 | + def test_transaction_w_connection(self): |
| 222 | + from gcloud.datastore import dataset as MUT |
| 223 | + from gcloud._testing import _Monkey |
| 224 | + conn = object() |
| 225 | + dataset = self._makeOne(connection=conn) |
| 226 | + |
| 227 | + with _Monkey(MUT, Transaction=_Dummy): |
| 228 | + xact = dataset.transaction() |
| 229 | + |
| 230 | + self.assertTrue(isinstance(xact, _Dummy)) |
| 231 | + self.assertEqual(xact.args, ()) |
| 232 | + self.assertEqual(xact.kwargs, |
| 233 | + {'dataset_id': self.DATASET_ID, 'connection': conn}) |
| 234 | + |
| 235 | + def test_query_w_dataset_id(self): |
| 236 | + KIND = 'KIND' |
| 237 | + dataset = self._makeOne() |
| 238 | + self.assertRaises(TypeError, |
| 239 | + dataset.query, kind=KIND, dataset_id=self.DATASET_ID) |
| 240 | + |
| 241 | + def test_query_w_defaults(self): |
| 242 | + from gcloud.datastore import dataset as MUT |
| 243 | + from gcloud._testing import _Monkey |
| 244 | + dataset = self._makeOne() |
| 245 | + |
| 246 | + with _Monkey(MUT, Query=_Dummy): |
| 247 | + query = dataset.query() |
| 248 | + |
| 249 | + self.assertTrue(isinstance(query, _Dummy)) |
| 250 | + self.assertEqual(query.args, ()) |
| 251 | + self.assertEqual(query.kwargs, {'dataset_id': self.DATASET_ID}) |
| 252 | + |
| 253 | + def test_query_explicit(self): |
| 254 | + from gcloud.datastore import dataset as MUT |
| 255 | + from gcloud._testing import _Monkey |
| 256 | + KIND = 'KIND' |
| 257 | + NAMESPACE = 'NAMESPACE' |
| 258 | + ANCESTOR = object() |
| 259 | + FILTERS = [('PROPERTY', '==', 'VALUE')] |
| 260 | + PROJECTION = ['__key__'] |
| 261 | + ORDER = ['PROPERTY'] |
| 262 | + GROUP_BY = ['GROUPBY'] |
| 263 | + dataset = self._makeOne() |
| 264 | + |
| 265 | + with _Monkey(MUT, Query=_Dummy): |
| 266 | + query = dataset.query( |
| 267 | + kind=KIND, |
| 268 | + namespace=NAMESPACE, |
| 269 | + ancestor=ANCESTOR, |
| 270 | + filters=FILTERS, |
| 271 | + projection=PROJECTION, |
| 272 | + order=ORDER, |
| 273 | + group_by=GROUP_BY, |
| 274 | + ) |
| 275 | + |
| 276 | + self.assertTrue(isinstance(query, _Dummy)) |
| 277 | + kwargs = { |
| 278 | + 'dataset_id': self.DATASET_ID, |
| 279 | + 'kind': KIND, |
| 280 | + 'namespace': NAMESPACE, |
| 281 | + 'ancestor': ANCESTOR, |
| 282 | + 'filters': FILTERS, |
| 283 | + 'projection': PROJECTION, |
| 284 | + 'order': ORDER, |
| 285 | + 'group_by': GROUP_BY, |
| 286 | + } |
| 287 | + self.assertEqual(query.args, ()) |
| 288 | + self.assertEqual(query.kwargs, kwargs) |
| 289 | + |
| 290 | + |
| 291 | +class _Dummy(object): |
| 292 | + |
| 293 | + def __init__(self, *args, **kwargs): |
| 294 | + self.args = args |
| 295 | + self.kwargs = kwargs |
0 commit comments