/
test_async_query.py
374 lines (309 loc) · 13.9 KB
/
test_async_query.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
# 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 pytest
import types
import aiounittest
import mock
from tests.unit.v1.test_base_query import _make_credentials, _make_query_response
class MockAsyncIter:
def __init__(self, count=3):
# count is arbitrary value
self.count = count
async def __aiter__(self, **_):
for i in range(self.count):
yield i
class TestAsyncQuery(aiounittest.AsyncTestCase):
@staticmethod
def _get_target_class():
from google.cloud.firestore_v1.async_query import AsyncQuery
return AsyncQuery
def _make_one(self, *args, **kwargs):
klass = self._get_target_class()
return klass(*args, **kwargs)
def test_constructor_defaults(self):
query = self._make_one(mock.sentinel.parent)
self.assertIs(query._parent, mock.sentinel.parent)
self.assertIsNone(query._projection)
self.assertEqual(query._field_filters, ())
self.assertEqual(query._orders, ())
self.assertIsNone(query._limit)
self.assertIsNone(query._offset)
self.assertIsNone(query._start_at)
self.assertIsNone(query._end_at)
self.assertFalse(query._all_descendants)
@pytest.mark.asyncio
async def test_get(self):
import warnings
with mock.patch.object(self._get_target_class(), "stream") as stream_mock:
stream_mock.return_value = MockAsyncIter(3)
# Create a minimal fake GAPIC.
firestore_api = mock.Mock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = _make_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dee")
# Execute the query and check the response.
query = self._make_one(parent)
with warnings.catch_warnings(record=True) as warned:
get_response = query.get()
returned = [x async for x in get_response]
# Verify that `get` merely wraps `stream`.
stream_mock.assert_called_once()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
self.assertEqual(returned, list(range(stream_mock.return_value.count)))
# Verify the deprecation.
self.assertEqual(len(warned), 1)
self.assertIs(warned[0].category, DeprecationWarning)
@pytest.mark.asyncio
async def test_stream_simple(self):
# Create a minimal fake GAPIC.
firestore_api = mock.Mock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = _make_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}
response_pb = _make_query_response(name=name, data=data)
firestore_api.run_query.return_value = iter([response_pb])
# Execute the query and check the response.
query = self._make_one(parent)
get_response = query.stream()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
returned = [x async for x in get_response]
self.assertEqual(len(returned), 1)
snapshot = returned[0]
self.assertEqual(snapshot.reference._path, ("dee", "sleep"))
self.assertEqual(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_stream_with_transaction(self):
# Create a minimal fake GAPIC.
firestore_api = mock.Mock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = _make_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 = iter([response_pb])
# Execute the query and check the response.
query = self._make_one(parent)
get_response = query.stream(transaction=transaction)
self.assertIsInstance(get_response, types.AsyncGeneratorType)
returned = [x async for x in get_response]
self.assertEqual(len(returned), 1)
snapshot = returned[0]
self.assertEqual(snapshot.reference._path, ("declaration", "burger"))
self.assertEqual(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_stream_no_results(self):
# Create a minimal fake GAPIC with a dummy response.
firestore_api = mock.Mock(spec=["run_query"])
empty_response = _make_query_response()
run_query_response = iter([empty_response])
firestore_api.run_query.return_value = run_query_response
# Attach the fake GAPIC to a real client.
client = _make_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dah", "dah", "dum")
query = self._make_one(parent)
get_response = query.stream()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
self.assertEqual([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_stream_second_response_in_empty_stream(self):
# Create a minimal fake GAPIC with a dummy response.
firestore_api = mock.Mock(spec=["run_query"])
empty_response1 = _make_query_response()
empty_response2 = _make_query_response()
run_query_response = iter([empty_response1, empty_response2])
firestore_api.run_query.return_value = run_query_response
# Attach the fake GAPIC to a real client.
client = _make_client()
client._firestore_api_internal = firestore_api
# Make a **real** collection reference as parent.
parent = client.collection("dah", "dah", "dum")
query = self._make_one(parent)
get_response = query.stream()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
self.assertEqual([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_stream_with_skipped_results(self):
# Create a minimal fake GAPIC.
firestore_api = mock.Mock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = _make_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 = iter([response_pb1, response_pb2])
# Execute the query and check the response.
query = self._make_one(parent)
get_response = query.stream()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
returned = [x async for x in get_response]
self.assertEqual(len(returned), 1)
snapshot = returned[0]
self.assertEqual(snapshot.reference._path, ("talk", "and", "chew-gum", "clock"))
self.assertEqual(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_stream_empty_after_first_response(self):
# Create a minimal fake GAPIC.
firestore_api = mock.Mock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = _make_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 = iter([response_pb1, response_pb2])
# Execute the query and check the response.
query = self._make_one(parent)
get_response = query.stream()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
returned = [x async for x in get_response]
self.assertEqual(len(returned), 1)
snapshot = returned[0]
self.assertEqual(snapshot.reference._path, ("charles", "bark"))
self.assertEqual(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_stream_w_collection_group(self):
# Create a minimal fake GAPIC.
firestore_api = mock.Mock(spec=["run_query"])
# Attach the fake GAPIC to a real client.
client = _make_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 = iter([response_pb1, response_pb2])
# Execute the query and check the response.
query = self._make_one(parent)
query._all_descendants = True
get_response = query.stream()
self.assertIsInstance(get_response, types.AsyncGeneratorType)
returned = [x async for x in get_response]
self.assertEqual(len(returned), 1)
snapshot = returned[0]
to_match = other.document("bark")
self.assertEqual(snapshot.reference._document_path, to_match._document_path)
self.assertEqual(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,
)
@mock.patch("google.cloud.firestore_v1.async_query.Watch", autospec=True)
def test_on_snapshot(self, watch):
query = self._make_one(mock.sentinel.parent)
query.on_snapshot(None)
watch.for_query.assert_called_once()
def _make_client(project="project-project"):
from google.cloud.firestore_v1.async_client import AsyncClient
credentials = _make_credentials()
return AsyncClient(project=project, credentials=credentials)