forked from googleapis/python-firestore
/
test_collection.py
578 lines (462 loc) · 21.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# 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 types
import unittest
import mock
import six
class TestCollectionReference(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.firestore_v1.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_v1.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___eq___other_type(self):
client = mock.sentinel.client
collection = self._make_one("name", client=client)
other = object()
self.assertFalse(collection == other)
def test___eq___different_path_same_client(self):
client = mock.sentinel.client
collection = self._make_one("name", client=client)
other = self._make_one("other", client=client)
self.assertFalse(collection == other)
def test___eq___same_path_different_client(self):
client = mock.sentinel.client
other_client = mock.sentinel.other_client
collection = self._make_one("name", client=client)
other = self._make_one("name", client=other_client)
self.assertFalse(collection == other)
def test___eq___same_path_same_client(self):
client = mock.sentinel.client
collection = self._make_one("name", client=client)
other = self._make_one("name", client=client)
self.assertTrue(collection == other)
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_v1.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_v1.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_v1.collection._auto_id",
return_value="zorpzorpthreezorp012",
)
def test_document_factory_auto_id(self, mock_auto_id):
from google.cloud.firestore_v1.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_v1.proto import document_pb2
from google.cloud.firestore_v1.document import DocumentReference
from google.cloud.firestore_v1 import SERVER_TIMESTAMP
from google.cloud.firestore_v1._helpers import pbs_for_create
# Create a minimal fake GAPIC add attach it to a real client.
firestore_api = mock.Mock(spec=["create_document", "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", "commit_time"],
commit_time=mock.sentinel.commit_time,
)
firestore_api.commit.return_value = commit_response
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)
# Actually call add() on our collection; include a transform to make
# sure transforms during adds work.
document_data = {"been": "here", "now": SERVER_TIMESTAMP}
patch = mock.patch("google.cloud.firestore_v1.collection._auto_id")
random_doc_id = "DEADBEEF"
with patch as patched:
patched.return_value = random_doc_id
update_time, document_ref = collection.add(document_data)
# 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)
expected_path = collection._path + (random_doc_id,)
self.assertEqual(document_ref._path, expected_path)
write_pbs = pbs_for_create(document_ref._document_path, document_data)
firestore_api.commit.assert_called_once_with(
client._database_string,
write_pbs,
transaction=None,
metadata=client._rpc_metadata,
)
# Since we generate the ID locally, we don't call 'create_document'.
firestore_api.create_document.assert_not_called()
@staticmethod
def _write_pb_for_create(document_path, document_data):
from google.cloud.firestore_v1.proto import common_pb2
from google.cloud.firestore_v1.proto import document_pb2
from google.cloud.firestore_v1.proto import write_pb2
from google.cloud.firestore_v1 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_v1.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", "commit_time"],
commit_time=mock.sentinel.commit_time,
)
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,
metadata=client._rpc_metadata,
)
def test_select(self):
from google.cloud.firestore_v1.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_v1.proto import query_pb2
from google.cloud.firestore_v1 import _helpers
from google.cloud.firestore_v1.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_v1.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_v1.proto import query_pb2
from google.cloud.firestore_v1.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_v1.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_v1.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_limit_to_last(self):
from google.cloud.firestore_v1.query import Query
LIMIT = 15
collection = self._make_one("collection")
query = collection.limit_to_last(LIMIT)
self.assertIsInstance(query, Query)
self.assertIs(query._parent, collection)
self.assertEqual(query._limit, LIMIT)
self.assertTrue(query._limit_to_last)
def test_offset(self):
from google.cloud.firestore_v1.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_v1.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_v1.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_v1.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_v1.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))
def _list_documents_helper(self, page_size=None):
from google.api_core.page_iterator import Iterator
from google.api_core.page_iterator import Page
from google.cloud.firestore_v1.document import DocumentReference
from google.cloud.firestore_v1.gapic.firestore_client import FirestoreClient
from google.cloud.firestore_v1.proto.document_pb2 import Document
class _Iterator(Iterator):
def __init__(self, pages):
super(_Iterator, self).__init__(client=None)
self._pages = pages
def _next_page(self):
if self._pages:
page, self._pages = self._pages[0], self._pages[1:]
return Page(self, page, self.item_to_value)
client = _make_client()
template = client._database_string + "/documents/{}"
document_ids = ["doc-1", "doc-2"]
documents = [
Document(name=template.format(document_id)) for document_id in document_ids
]
iterator = _Iterator(pages=[documents])
api_client = mock.create_autospec(FirestoreClient)
api_client.list_documents.return_value = iterator
client._firestore_api_internal = api_client
collection = self._make_one("collection", client=client)
if page_size is not None:
documents = list(collection.list_documents(page_size=page_size))
else:
documents = list(collection.list_documents())
# Verify the response and the mocks.
self.assertEqual(len(documents), len(document_ids))
for document, document_id in zip(documents, document_ids):
self.assertIsInstance(document, DocumentReference)
self.assertEqual(document.parent, collection)
self.assertEqual(document.id, document_id)
parent, _ = collection._parent_info()
api_client.list_documents.assert_called_once_with(
parent,
collection.id,
page_size=page_size,
show_missing=True,
metadata=client._rpc_metadata,
)
def test_list_documents_wo_page_size(self):
self._list_documents_helper()
def test_list_documents_w_page_size(self):
self._list_documents_helper(page_size=25)
@mock.patch("google.cloud.firestore_v1.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_v1.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)
@mock.patch("google.cloud.firestore_v1.query.Query", autospec=True)
def test_stream(self, query_class):
collection = self._make_one("collection")
stream_response = collection.stream()
query_class.assert_called_once_with(collection)
query_instance = query_class.return_value
self.assertIs(stream_response, query_instance.stream.return_value)
query_instance.stream.assert_called_once_with(transaction=None)
@mock.patch("google.cloud.firestore_v1.query.Query", autospec=True)
def test_stream_with_transaction(self, query_class):
collection = self._make_one("collection")
transaction = mock.sentinel.txn
stream_response = collection.stream(transaction=transaction)
query_class.assert_called_once_with(collection)
query_instance = query_class.return_value
self.assertIs(stream_response, query_instance.stream.return_value)
query_instance.stream.assert_called_once_with(transaction=transaction)
@mock.patch("google.cloud.firestore_v1.collection.Watch", autospec=True)
def test_on_snapshot(self, watch):
collection = self._make_one("collection")
collection.on_snapshot(None)
watch.for_query.assert_called_once()
class Test__auto_id(unittest.TestCase):
@staticmethod
def _call_fut():
from google.cloud.firestore_v1.collection import _auto_id
return _auto_id()
@mock.patch("random.choice")
def test_it(self, mock_rand_choice):
from google.cloud.firestore_v1.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_v1.client import Client
credentials = _make_credentials()
return Client(project="project-project", credentials=credentials)