/
test_collection.py
449 lines (354 loc) · 16.6 KB
/
test_collection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# Copyright 2017 Google LLC All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import types
import unittest
import mock
import six
class TestCollectionReference(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.firestore_v1beta1.collection import CollectionReference
return CollectionReference
def _make_one(self, *args, **kwargs):
klass = self._get_target_class()
return klass(*args, **kwargs)
@staticmethod
def _get_public_methods(klass):
return set(
name
for name, value in six.iteritems(klass.__dict__)
if (not name.startswith('_') and
isinstance(value, types.FunctionType)))
def test_query_method_matching(self):
from google.cloud.firestore_v1beta1.query import Query
query_methods = self._get_public_methods(Query)
klass = self._get_target_class()
collection_methods = self._get_public_methods(klass)
# Make sure every query method is present on
# ``CollectionReference``.
self.assertLessEqual(query_methods, collection_methods)
def test_constructor(self):
collection_id1 = 'rooms'
document_id = 'roomA'
collection_id2 = 'messages'
client = mock.sentinel.client
collection = self._make_one(
collection_id1, document_id, collection_id2, client=client)
self.assertIs(collection._client, client)
expected_path = (collection_id1, document_id, collection_id2)
self.assertEqual(collection._path, expected_path)
def test_constructor_invalid_path(self):
with self.assertRaises(ValueError):
self._make_one()
with self.assertRaises(ValueError):
self._make_one(99, 'doc', 'bad-collection-id')
with self.assertRaises(ValueError):
self._make_one('bad-document-ID', None, 'sub-collection')
with self.assertRaises(ValueError):
self._make_one('Just', 'A-Document')
def test_constructor_invalid_kwarg(self):
with self.assertRaises(TypeError):
self._make_one('Coh-lek-shun', donut=True)
def test_id_property(self):
collection_id = 'hi-bob'
collection = self._make_one(collection_id)
self.assertEqual(collection.id, collection_id)
def test_parent_property(self):
from google.cloud.firestore_v1beta1.document import DocumentReference
collection_id1 = 'grocery-store'
document_id = 'market'
collection_id2 = 'darth'
client = _make_client()
collection = self._make_one(
collection_id1, document_id, collection_id2, client=client)
parent = collection.parent
self.assertIsInstance(parent, DocumentReference)
self.assertIs(parent._client, client)
self.assertEqual(parent._path, (collection_id1, document_id))
def test_parent_property_top_level(self):
collection = self._make_one('tahp-leh-vull')
self.assertIsNone(collection.parent)
def test_document_factory_explicit_id(self):
from google.cloud.firestore_v1beta1.document import DocumentReference
collection_id = 'grocery-store'
document_id = 'market'
client = _make_client()
collection = self._make_one(collection_id, client=client)
child = collection.document(document_id)
self.assertIsInstance(child, DocumentReference)
self.assertIs(child._client, client)
self.assertEqual(
child._path, (collection_id, document_id))
@mock.patch('google.cloud.firestore_v1beta1.collection._auto_id',
return_value='zorpzorpthreezorp012')
def test_document_factory_auto_id(self, mock_auto_id):
from google.cloud.firestore_v1beta1.document import DocumentReference
collection_name = 'space-town'
client = _make_client()
collection = self._make_one(collection_name, client=client)
child = collection.document()
self.assertIsInstance(child, DocumentReference)
self.assertIs(child._client, client)
self.assertEqual(
child._path, (collection_name, mock_auto_id.return_value))
mock_auto_id.assert_called_once_with()
def test__parent_info_top_level(self):
client = _make_client()
collection_id = 'soap'
collection = self._make_one(collection_id, client=client)
parent_path, expected_prefix = collection._parent_info()
expected_path = 'projects/{}/databases/{}/documents'.format(
client.project, client._database)
self.assertEqual(parent_path, expected_path)
prefix = '{}/{}'.format(expected_path, collection_id)
self.assertEqual(expected_prefix, prefix)
def test__parent_info_nested(self):
collection_id1 = 'bar'
document_id = 'baz'
collection_id2 = 'chunk'
client = _make_client()
collection = self._make_one(
collection_id1, document_id, collection_id2, client=client)
parent_path, expected_prefix = collection._parent_info()
expected_path = 'projects/{}/databases/{}/documents/{}/{}'.format(
client.project, client._database, collection_id1, document_id)
self.assertEqual(parent_path, expected_path)
prefix = '{}/{}'.format(expected_path, collection_id2)
self.assertEqual(expected_prefix, prefix)
def test_add_auto_assigned(self):
from google.cloud.firestore_v1beta1.proto import document_pb2
from google.cloud.firestore_v1beta1 import _helpers
from google.cloud.firestore_v1beta1.document import DocumentReference
# Create a minimal fake GAPIC add attach it to a real client.
firestore_api = mock.Mock(spec=['create_document'])
create_doc_response = document_pb2.Document()
firestore_api.create_document.return_value = create_doc_response
client = _make_client()
client._firestore_api_internal = firestore_api
# Actually make a collection.
collection = self._make_one(
'grand-parent', 'parent', 'child', client=client)
# Add a dummy response for the fake GAPIC.
parent_path = collection.parent._document_path
auto_assigned_id = 'cheezburger'
name = '{}/{}/{}'.format(
parent_path, collection.id, auto_assigned_id)
create_doc_response = document_pb2.Document(name=name)
create_doc_response.update_time.FromDatetime(
datetime.datetime.utcnow())
firestore_api.create_document.return_value = create_doc_response
# Actually call add() on our collection.
document_data = {'been': 'here'}
update_time, document_ref = collection.add(document_data)
# Verify the response and the mocks.
self.assertIs(update_time, create_doc_response.update_time)
self.assertIsInstance(document_ref, DocumentReference)
self.assertIs(document_ref._client, client)
expected_path = collection._path + (auto_assigned_id,)
self.assertEqual(document_ref._path, expected_path)
expected_document_pb = document_pb2.Document(
fields=_helpers.encode_dict(document_data))
firestore_api.create_document.assert_called_once_with(
parent_path, collection_id=collection.id, document_id=None,
document=expected_document_pb, mask=None,
options=client._call_options)
@staticmethod
def _write_pb_for_create(document_path, document_data):
from google.cloud.firestore_v1beta1.proto import common_pb2
from google.cloud.firestore_v1beta1.proto import document_pb2
from google.cloud.firestore_v1beta1.proto import write_pb2
from google.cloud.firestore_v1beta1 import _helpers
return write_pb2.Write(
update=document_pb2.Document(
name=document_path,
fields=_helpers.encode_dict(document_data),
),
current_document=common_pb2.Precondition(exists=False),
)
def test_add_explicit_id(self):
from google.cloud.firestore_v1beta1.document import DocumentReference
# Create a minimal fake GAPIC with a dummy response.
firestore_api = mock.Mock(spec=['commit'])
write_result = mock.Mock(
update_time=mock.sentinel.update_time, spec=['update_time'])
commit_response = mock.Mock(
write_results=[write_result], spec=['write_results'])
firestore_api.commit.return_value = commit_response
# Attach the fake GAPIC to a real client.
client = _make_client()
client._firestore_api_internal = firestore_api
# Actually make a collection and call add().
collection = self._make_one('parent', client=client)
document_data = {
'zorp': 208.75,
'i-did-not': b'know that',
}
doc_id = 'child'
update_time, document_ref = collection.add(
document_data, document_id=doc_id)
# Verify the response and the mocks.
self.assertIs(update_time, mock.sentinel.update_time)
self.assertIsInstance(document_ref, DocumentReference)
self.assertIs(document_ref._client, client)
self.assertEqual(document_ref._path, (collection.id, doc_id))
write_pb = self._write_pb_for_create(
document_ref._document_path, document_data)
firestore_api.commit.assert_called_once_with(
client._database_string, [write_pb], transaction=None,
options=client._call_options)
def test_select(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
field_paths = ['a', 'b']
query = collection.select(field_paths)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
projection_paths = [field_ref.field_path
for field_ref in query._projection.fields]
self.assertEqual(projection_paths, field_paths)
@staticmethod
def _make_field_filter_pb(field_path, op_string, value):
from google.cloud.firestore_v1beta1.proto import query_pb2
from google.cloud.firestore_v1beta1 import _helpers
from google.cloud.firestore_v1beta1.query import _enum_from_op_string
return query_pb2.StructuredQuery.FieldFilter(
field=query_pb2.StructuredQuery.FieldReference(
field_path=field_path,
),
op=_enum_from_op_string(op_string),
value=_helpers.encode_value(value),
)
def test_where(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
field_path = 'foo'
op_string = '=='
value = 45
query = collection.where(field_path, op_string, value)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(len(query._field_filters), 1)
field_filter_pb = query._field_filters[0]
self.assertEqual(
field_filter_pb,
self._make_field_filter_pb(field_path, op_string, value))
@staticmethod
def _make_order_pb(field_path, direction):
from google.cloud.firestore_v1beta1.proto import query_pb2
from google.cloud.firestore_v1beta1.query import _enum_from_direction
return query_pb2.StructuredQuery.Order(
field=query_pb2.StructuredQuery.FieldReference(
field_path=field_path,
),
direction=_enum_from_direction(direction),
)
def test_order_by(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
field_path = 'foo'
direction = Query.DESCENDING
query = collection.order_by(field_path, direction=direction)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(len(query._orders), 1)
order_pb = query._orders[0]
self.assertEqual(
order_pb, self._make_order_pb(field_path, direction))
def test_limit(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
limit = 15
query = collection.limit(limit)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._limit, limit)
def test_offset(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
offset = 113
query = collection.offset(offset)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._offset, offset)
def test_start_at(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
doc_fields = {'a': 'b'}
query = collection.start_at(doc_fields)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._start_at, (doc_fields, True))
def test_start_after(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
doc_fields = {'d': 'foo', 'e': 10}
query = collection.start_after(doc_fields)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._start_at, (doc_fields, False))
def test_end_before(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
doc_fields = {'bar': 10.5}
query = collection.end_before(doc_fields)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._end_at, (doc_fields, True))
def test_end_at(self):
from google.cloud.firestore_v1beta1.query import Query
collection = self._make_one('collection')
doc_fields = {'opportunity': True, 'reason': 9}
query = collection.end_at(doc_fields)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._end_at, (doc_fields, False))
@mock.patch('google.cloud.firestore_v1beta1.query.Query', autospec=True)
def test_get(self, query_class):
collection = self._make_one('collection')
get_response = collection.get()
query_class.assert_called_once_with(collection)
query_instance = query_class.return_value
self.assertIs(get_response, query_instance.get.return_value)
query_instance.get.assert_called_once_with(transaction=None)
@mock.patch('google.cloud.firestore_v1beta1.query.Query', autospec=True)
def test_get_with_transaction(self, query_class):
collection = self._make_one('collection')
transaction = mock.sentinel.txn
get_response = collection.get(transaction=transaction)
query_class.assert_called_once_with(collection)
query_instance = query_class.return_value
self.assertIs(get_response, query_instance.get.return_value)
query_instance.get.assert_called_once_with(transaction=transaction)
class Test__auto_id(unittest.TestCase):
@staticmethod
def _call_fut():
from google.cloud.firestore_v1beta1.collection import _auto_id
return _auto_id()
@mock.patch('random.choice')
def test_it(self, mock_rand_choice):
from google.cloud.firestore_v1beta1.collection import _AUTO_ID_CHARS
mock_result = '0123456789abcdefghij'
mock_rand_choice.side_effect = list(mock_result)
result = self._call_fut()
self.assertEqual(result, mock_result)
mock_calls = [mock.call(_AUTO_ID_CHARS)] * 20
self.assertEqual(mock_rand_choice.mock_calls, mock_calls)
def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)
def _make_client():
from google.cloud.firestore_v1beta1.client import Client
credentials = _make_credentials()
return Client(project='project-project', credentials=credentials)