-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_async_query.py
More file actions
929 lines (741 loc) · 30.7 KB
/
Copy pathtest_async_query.py
File metadata and controls
929 lines (741 loc) · 30.7 KB
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# Copyright 2020 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 mock
import pytest
from google.cloud.firestore_v1.query_profile import ExplainMetrics, QueryExplainError
from google.cloud.firestore_v1.query_results import QueryResultsList
from tests.unit.v1._test_helpers import (
DEFAULT_TEST_PROJECT,
make_async_client,
make_async_query,
)
from tests.unit.v1.test__helpers import AsyncIter, AsyncMock
from tests.unit.v1.test_base_query import _make_cursor_pb, _make_query_response
def test_asyncquery_constructor():
query = make_async_query(mock.sentinel.parent)
assert query._parent is mock.sentinel.parent
assert query._projection is None
assert query._field_filters == ()
assert query._orders == ()
assert query._limit is None
assert query._offset is None
assert query._start_at is None
assert query._end_at is None
assert not query._all_descendants
async def _get_helper(retry=None, timeout=None, explain_options=None, read_time=None):
from google.cloud.firestore_v1 import _helpers
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dee")
# Add a dummy response to the minimal fake GAPIC.
_, expected_prefix = parent._parent_info()
name = "{}/sleep".format(expected_prefix)
data = {"snooze": 10}
explain_metrics = {"execution_stats": {"results_returned": 1}}
response_pb = _make_query_response(
name=name, data=data, explain_metrics=explain_metrics
)
firestore_api.run_query.return_value = AsyncIter([response_pb])
kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)
# Execute the query and check the response.
query = make_async_query(parent)
returned = await query.get(
**kwargs, explain_options=explain_options, read_time=read_time
)
assert isinstance(returned, QueryResultsList)
assert len(returned) == 1
snapshot = returned[0]
assert snapshot.reference._path == ("dee", "sleep")
assert snapshot.to_dict() == data
if explain_options is None:
with pytest.raises(QueryExplainError, match="explain_options not set"):
returned.get_explain_metrics()
else:
actual_explain_metrics = returned.get_explain_metrics()
assert isinstance(actual_explain_metrics, ExplainMetrics)
assert actual_explain_metrics.execution_stats.results_returned == 1
# Create expected request body.
parent_path, _ = parent._parent_info()
request = {
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
}
if explain_options:
request["explain_options"] = explain_options._to_dict()
if read_time:
request["read_time"] = read_time
# Verify the mock call.
firestore_api.run_query.assert_called_once_with(
request=request,
metadata=client._rpc_metadata,
**kwargs,
)
@pytest.mark.asyncio
async def test_asyncquery_get():
await _get_helper()
@pytest.mark.asyncio
async def test_asyncquery_get_w_retry_timeout():
from google.api_core.retry import Retry
retry = Retry(predicate=object())
timeout = 123.0
await _get_helper(retry=retry, timeout=timeout)
@pytest.mark.asyncio
async def test_asyncquery_get_w_read_time():
read_time = datetime.datetime.now(tz=datetime.timezone.utc)
await _get_helper(read_time=read_time)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"direction, expected_direction",
[("DESCENDING", "ASCENDING"), ("ASCENDING", "DESCENDING")],
)
async def test_asyncquery_get_limit_to_last(direction, expected_direction):
from google.cloud.firestore_v1.base_query import _enum_from_direction
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dee")
# Add a dummy response to the minimal fake GAPIC.
_, expected_prefix = parent._parent_info()
name = "{}/sleep".format(expected_prefix)
data = {"snooze": 10}
data2 = {"snooze": 20}
response_pb = _make_query_response(name=name, data=data)
response_pb2 = _make_query_response(name=name, data=data2)
firestore_api.run_query.return_value = AsyncIter([response_pb2, response_pb])
# Execute the query and check the response.
query = make_async_query(parent)
query = query.order_by("snooze", direction=direction).limit_to_last(2)
returned = await query.get()
assert isinstance(returned, list)
assert query._orders[0].direction == _enum_from_direction(expected_direction)
assert len(returned) == 2
snapshot = returned[0]
assert snapshot.reference._path == ("dee", "sleep")
assert snapshot.to_dict() == data
snapshot2 = returned[1]
assert snapshot2.reference._path == ("dee", "sleep")
assert snapshot2.to_dict() == data2
# Verify the mock call.
parent_path, _ = parent._parent_info()
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_get_w_explain_options():
from google.cloud.firestore_v1.query_profile import ExplainOptions
explain_options = ExplainOptions(analyze=True)
await _get_helper(explain_options=explain_options)
def test_asyncquery_sum():
from google.cloud.firestore_v1.base_aggregation import SumAggregation
from google.cloud.firestore_v1.field_path import FieldPath
client = make_async_client()
parent = client.collection("dee")
field_str = "field_str"
field_path = FieldPath("foo", "bar")
query = make_async_query(parent)
# test with only field populated
sum_query = query.sum(field_str)
sum_agg = sum_query._aggregations[0]
assert isinstance(sum_agg, SumAggregation)
assert sum_agg.field_ref == field_str
assert sum_agg.alias is None
# test with field and alias populated
sum_query = query.sum(field_str, alias="alias")
sum_agg = sum_query._aggregations[0]
assert isinstance(sum_agg, SumAggregation)
assert sum_agg.field_ref == field_str
assert sum_agg.alias == "alias"
# test with field_path
sum_query = query.sum(field_path, alias="alias")
sum_agg = sum_query._aggregations[0]
assert isinstance(sum_agg, SumAggregation)
assert sum_agg.field_ref == "foo.bar"
assert sum_agg.alias == "alias"
def test_asyncquery_avg():
from google.cloud.firestore_v1.base_aggregation import AvgAggregation
from google.cloud.firestore_v1.field_path import FieldPath
client = make_async_client()
parent = client.collection("dee")
field_str = "field_str"
field_path = FieldPath("foo", "bar")
query = make_async_query(parent)
# test with only field populated
avg_query = query.avg(field_str)
avg_agg = avg_query._aggregations[0]
assert isinstance(avg_agg, AvgAggregation)
assert avg_agg.field_ref == field_str
assert avg_agg.alias is None
# test with field and alias populated
avg_query = query.avg(field_str, alias="alias")
avg_agg = avg_query._aggregations[0]
assert isinstance(avg_agg, AvgAggregation)
assert avg_agg.field_ref == field_str
assert avg_agg.alias == "alias"
# test with field_path
avg_query = query.avg(field_path, alias="alias")
avg_agg = avg_query._aggregations[0]
assert isinstance(avg_agg, AvgAggregation)
assert avg_agg.field_ref == "foo.bar"
assert avg_agg.alias == "alias"
@pytest.mark.asyncio
async def test_asyncquery_chunkify_w_empty():
client = make_async_client()
firestore_api = AsyncMock(spec=["run_query"])
firestore_api.run_query.return_value = AsyncIter([])
client._firestore_api_internal = firestore_api
query = client.collection("asdf")._query()
chunks = []
async for chunk in query._chunkify(10):
chunks.append(chunk)
assert chunks == [[]]
@pytest.mark.asyncio
async def test_asyncquery_chunkify_w_chunksize_lt_limit():
from google.cloud.firestore_v1.types import document, firestore
client = make_async_client()
firestore_api = AsyncMock(spec=["run_query"])
doc_ids = [
f"projects/{DEFAULT_TEST_PROJECT}/databases/(default)/documents/asdf/{index}"
for index in range(5)
]
responses1 = [
firestore.RunQueryResponse(
document=document.Document(name=doc_id),
)
for doc_id in doc_ids[:2]
]
responses2 = [
firestore.RunQueryResponse(
document=document.Document(name=doc_id),
)
for doc_id in doc_ids[2:4]
]
responses3 = [
firestore.RunQueryResponse(
document=document.Document(name=doc_id),
)
for doc_id in doc_ids[4:]
]
firestore_api.run_query.side_effect = [
AsyncIter(responses1),
AsyncIter(responses2),
AsyncIter(responses3),
]
client._firestore_api_internal = firestore_api
query = client.collection("asdf")._query()
chunks = []
async for chunk in query._chunkify(2):
chunks.append(chunk)
assert len(chunks) == 3
expected_ids = [str(index) for index in range(5)]
assert [snapshot.id for snapshot in chunks[0]] == expected_ids[:2]
assert [snapshot.id for snapshot in chunks[1]] == expected_ids[2:4]
assert [snapshot.id for snapshot in chunks[2]] == expected_ids[4:]
@pytest.mark.asyncio
async def test_asyncquery_chunkify_w_chunksize_gt_limit():
from google.cloud.firestore_v1.types import document, firestore
client = make_async_client()
firestore_api = AsyncMock(spec=["run_query"])
responses = [
firestore.RunQueryResponse(
document=document.Document(
name=(
f"projects/{DEFAULT_TEST_PROJECT}/databases/(default)/"
f"documents/asdf/{index}"
),
),
)
for index in range(5)
]
firestore_api.run_query.return_value = AsyncIter(responses)
client._firestore_api_internal = firestore_api
query = client.collection("asdf")._query()
chunks = []
async for chunk in query.limit(5)._chunkify(10):
chunks.append(chunk)
assert len(chunks) == 1
expected_ids = [str(index) for index in range(5)]
assert [snapshot.id for snapshot in chunks[0]] == expected_ids
async def _stream_helper(
retry=None, timeout=None, explain_options=None, read_time=None
):
from google.cloud.firestore_v1 import _helpers
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dee")
# Add a dummy response to the minimal fake GAPIC.
_, expected_prefix = parent._parent_info()
name = "{}/sleep".format(expected_prefix)
data = {"snooze": 10}
if explain_options is not None:
explain_metrics = {"execution_stats": {"results_returned": 1}}
else:
explain_metrics = None
response_pb = _make_query_response(
name=name, data=data, explain_metrics=explain_metrics
)
firestore_api.run_query.return_value = AsyncIter([response_pb])
kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)
# Execute the query and check the response.
query = make_async_query(parent)
stream_response = query.stream(
**kwargs, explain_options=explain_options, read_time=read_time
)
assert isinstance(stream_response, AsyncStreamGenerator)
returned = [x async for x in stream_response]
await stream_response.aclose()
assert len(returned) == 1
snapshot = returned[0]
assert snapshot.reference._path == ("dee", "sleep")
assert snapshot.to_dict() == data
# Verify explain_metrics.
if explain_options is None:
with pytest.raises(QueryExplainError, match="explain_options not set"):
await stream_response.get_explain_metrics()
else:
explain_metrics = await stream_response.get_explain_metrics()
assert isinstance(explain_metrics, ExplainMetrics)
assert explain_metrics.execution_stats.results_returned == 1
# Create expected request body.
parent_path, _ = parent._parent_info()
request = {
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
}
if explain_options is not None:
request["explain_options"] = explain_options._to_dict()
if read_time is not None:
request["read_time"] = read_time
# Verify the mock call.
firestore_api.run_query.assert_called_once_with(
request=request,
metadata=client._rpc_metadata,
**kwargs,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_simple():
await _stream_helper()
@pytest.mark.asyncio
async def test_asyncquery_stream_w_retry_timeout():
from google.api_core.retry import Retry
retry = Retry(predicate=object())
timeout = 123.0
await _stream_helper(retry=retry, timeout=timeout)
@pytest.mark.asyncio
async def test_asyncquery_stream_w_read_time():
read_time = datetime.datetime.now(tz=datetime.timezone.utc)
await _stream_helper(read_time=read_time)
@pytest.mark.asyncio
async def test_asyncquery_stream_with_limit_to_last():
# Attach the fake GAPIC to a real client.
client = make_async_client()
# Make a **real** collection reference as parent.
parent = client.collection("dee")
# Execute the query and check the response.
query = make_async_query(parent)
query = query.limit_to_last(2)
stream_response = query.stream()
with pytest.raises(ValueError):
[d async for d in stream_response]
@pytest.mark.asyncio
async def test_asyncquery_stream_with_transaction():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Create a real-ish transaction for this client.
transaction = client.transaction()
txn_id = b"\x00\x00\x01-work-\xf2"
transaction._id = txn_id
# Make a **real** collection reference as parent.
parent = client.collection("declaration")
# Add a dummy response to the minimal fake GAPIC.
parent_path, expected_prefix = parent._parent_info()
name = "{}/burger".format(expected_prefix)
data = {"lettuce": b"\xee\x87"}
response_pb = _make_query_response(name=name, data=data)
firestore_api.run_query.return_value = AsyncIter([response_pb])
# Execute the query and check the response.
query = make_async_query(parent)
get_response = query.stream(transaction=transaction)
assert isinstance(get_response, AsyncStreamGenerator)
returned = [x async for x in get_response]
assert len(returned) == 1
snapshot = returned[0]
assert snapshot.reference._path == ("declaration", "burger")
assert snapshot.to_dict() == data
# Verify the mock call.
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": txn_id,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_with_transaction_and_read_time():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Create a real-ish transaction for this client.
transaction = client.transaction()
txn_id = b"\x00\x00\x01-work-\xf2"
transaction._id = txn_id
# Create a read_time for this client.
read_time = datetime.datetime.now(tz=datetime.timezone.utc)
# Make a **real** collection reference as parent.
parent = client.collection("declaration")
# Add a dummy response to the minimal fake GAPIC.
parent_path, expected_prefix = parent._parent_info()
name = "{}/burger".format(expected_prefix)
data = {"lettuce": b"\xee\x87"}
response_pb = _make_query_response(name=name, data=data)
firestore_api.run_query.return_value = AsyncIter([response_pb])
# Execute the query and check the response.
query = make_async_query(parent)
get_response = query.stream(transaction=transaction, read_time=read_time)
assert isinstance(get_response, AsyncStreamGenerator)
returned = [x async for x in get_response]
assert len(returned) == 1
snapshot = returned[0]
assert snapshot.reference._path == ("declaration", "burger")
assert snapshot.to_dict() == data
# Verify the mock call.
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": txn_id,
"read_time": read_time,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_no_results():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC with a dummy response.
firestore_api = AsyncMock(spec=["run_query"])
empty_response = _make_query_response()
run_query_response = AsyncIter([empty_response])
firestore_api.run_query.return_value = run_query_response
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dah", "dah", "dum")
query = make_async_query(parent)
get_response = query.stream()
assert isinstance(get_response, AsyncStreamGenerator)
assert [x async for x in get_response] == []
# Verify the mock call.
parent_path, _ = parent._parent_info()
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_second_response_in_empty_stream():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC with a dummy response.
firestore_api = AsyncMock(spec=["run_query"])
empty_response1 = _make_query_response()
empty_response2 = _make_query_response()
run_query_response = AsyncIter([empty_response1, empty_response2])
firestore_api.run_query.return_value = run_query_response
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dah", "dah", "dum")
query = make_async_query(parent)
get_response = query.stream()
assert isinstance(get_response, AsyncStreamGenerator)
assert [x async for x in get_response] == []
# Verify the mock call.
parent_path, _ = parent._parent_info()
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_with_skipped_results():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("talk", "and", "chew-gum")
# Add two dummy responses to the minimal fake GAPIC.
_, expected_prefix = parent._parent_info()
response_pb1 = _make_query_response(skipped_results=1)
name = "{}/clock".format(expected_prefix)
data = {"noon": 12, "nested": {"bird": 10.5}}
response_pb2 = _make_query_response(name=name, data=data)
firestore_api.run_query.return_value = AsyncIter([response_pb1, response_pb2])
# Execute the query and check the response.
query = make_async_query(parent)
get_response = query.stream()
assert isinstance(get_response, AsyncStreamGenerator)
returned = [x async for x in get_response]
assert len(returned) == 1
snapshot = returned[0]
assert snapshot.reference._path == ("talk", "and", "chew-gum", "clock")
assert snapshot.to_dict() == data
# Verify the mock call.
parent_path, _ = parent._parent_info()
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_empty_after_first_response():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("charles")
# Add two dummy responses to the minimal fake GAPIC.
_, expected_prefix = parent._parent_info()
name = "{}/bark".format(expected_prefix)
data = {"lee": "hoop"}
response_pb1 = _make_query_response(name=name, data=data)
response_pb2 = _make_query_response()
firestore_api.run_query.return_value = AsyncIter([response_pb1, response_pb2])
# Execute the query and check the response.
query = make_async_query(parent)
get_response = query.stream()
assert isinstance(get_response, AsyncStreamGenerator)
returned = [x async for x in get_response]
assert len(returned) == 1
snapshot = returned[0]
assert snapshot.reference._path == ("charles", "bark")
assert snapshot.to_dict() == data
# Verify the mock call.
parent_path, _ = parent._parent_info()
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_w_collection_group():
from google.cloud.firestore_v1.async_stream_generator import AsyncStreamGenerator
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("charles")
other = client.collection("dora")
# Add two dummy responses to the minimal fake GAPIC.
_, other_prefix = other._parent_info()
name = "{}/bark".format(other_prefix)
data = {"lee": "hoop"}
response_pb1 = _make_query_response(name=name, data=data)
response_pb2 = _make_query_response()
firestore_api.run_query.return_value = AsyncIter([response_pb1, response_pb2])
# Execute the query and check the response.
query = make_async_query(parent)
query._all_descendants = True
get_response = query.stream()
assert isinstance(get_response, AsyncStreamGenerator)
returned = [x async for x in get_response]
assert len(returned) == 1
snapshot = returned[0]
to_match = other.document("bark")
assert snapshot.reference._document_path == to_match._document_path
assert snapshot.to_dict() == data
# Verify the mock call.
parent_path, _ = parent._parent_info()
firestore_api.run_query.assert_called_once_with(
request={
"parent": parent_path,
"structured_query": query._to_protobuf(),
"transaction": None,
},
metadata=client._rpc_metadata,
)
@pytest.mark.asyncio
async def test_asyncquery_stream_w_explain_options():
from google.cloud.firestore_v1.query_profile import ExplainOptions
explain_options = ExplainOptions(analyze=True)
await _stream_helper(explain_options=explain_options)
def _make_async_collection_group(*args, **kwargs):
from google.cloud.firestore_v1.async_query import AsyncCollectionGroup
return AsyncCollectionGroup(*args, **kwargs)
def test_asynccollectiongroup_constructor():
query = _make_async_collection_group(mock.sentinel.parent)
assert query._parent is mock.sentinel.parent
assert query._projection is None
assert query._field_filters == ()
assert query._orders == ()
assert query._limit is None
assert query._offset is None
assert query._start_at is None
assert query._end_at is None
assert query._all_descendants
def test_asynccollectiongroup_constructor_all_descendents_is_false():
with pytest.raises(ValueError):
_make_async_collection_group(mock.sentinel.parent, all_descendants=False)
@pytest.mark.asyncio
async def _get_partitions_helper(retry=None, timeout=None, read_time=None):
from google.cloud.firestore_v1 import _helpers
# Create a minimal fake GAPIC.
firestore_api = AsyncMock(spec=["partition_query"])
# Attach the fake GAPIC to a real client.
client = make_async_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("charles")
# Make two **real** document references to use as cursors
document1 = parent.document("one")
document2 = parent.document("two")
# Add cursor pb's to the minimal fake GAPIC.
cursor_pb1 = _make_cursor_pb(([document1], False))
cursor_pb2 = _make_cursor_pb(([document2], False))
firestore_api.partition_query.return_value = AsyncIter([cursor_pb1, cursor_pb2])
kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout)
# Execute the query and check the response.
query = _make_async_collection_group(parent)
get_response = query.get_partitions(2, read_time=read_time, **kwargs)
assert isinstance(get_response, types.AsyncGeneratorType)
returned = [i async for i in get_response]
assert len(returned) == 3
# Verify the mock call.
parent_path, _ = parent._parent_info()
partition_query = _make_async_collection_group(
parent,
orders=(query._make_order("__name__", query.ASCENDING),),
)
expected_request = {
"parent": parent_path,
"structured_query": partition_query._to_protobuf(),
"partition_count": 2,
}
if read_time is not None:
expected_request["read_time"] = read_time
firestore_api.partition_query.assert_called_once_with(
request=expected_request,
metadata=client._rpc_metadata,
**kwargs,
)
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions():
await _get_partitions_helper()
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions_w_retry_timeout():
from google.api_core.retry import Retry
retry = Retry(predicate=object())
timeout = 123.0
await _get_partitions_helper(retry=retry, timeout=timeout)
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions_w_read_time():
read_time = datetime.datetime.now(tz=datetime.timezone.utc)
await _get_partitions_helper(read_time=read_time)
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions_w_filter():
# Make a **real** collection reference as parent.
client = make_async_client()
parent = client.collection("charles")
# Make a query that fails to partition
query = _make_async_collection_group(parent).where("foo", "==", "bar")
with pytest.raises(ValueError):
[i async for i in query.get_partitions(2)]
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions_w_projection():
# Make a **real** collection reference as parent.
client = make_async_client()
parent = client.collection("charles")
# Make a query that fails to partition
query = _make_async_collection_group(parent).select("foo")
with pytest.raises(ValueError):
[i async for i in query.get_partitions(2)]
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions_w_limit():
# Make a **real** collection reference as parent.
client = make_async_client()
parent = client.collection("charles")
# Make a query that fails to partition
query = _make_async_collection_group(parent).limit(10)
with pytest.raises(ValueError):
[i async for i in query.get_partitions(2)]
@pytest.mark.asyncio
async def test_asynccollectiongroup_get_partitions_w_offset():
# Make a **real** collection reference as parent.
client = make_async_client()
parent = client.collection("charles")
# Make a query that fails to partition
query = _make_async_collection_group(parent).offset(10)
with pytest.raises(ValueError):
[i async for i in query.get_partitions(2)]
def test_asyncquery_collection_pipeline_type():
from google.cloud.firestore_v1.async_pipeline import AsyncPipeline
client = make_async_client()
parent = client.collection("test")
query = parent._query()
ppl = query._build_pipeline(client.pipeline())
assert isinstance(ppl, AsyncPipeline)
def test_asyncquery_collectiongroup_pipeline_type():
from google.cloud.firestore_v1.async_pipeline import AsyncPipeline
client = make_async_client()
query = client.collection_group("test")
ppl = query._build_pipeline(client.pipeline())
assert isinstance(ppl, AsyncPipeline)