/
test_base_collection.py
338 lines (255 loc) · 12.9 KB
/
test_base_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
# 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 unittest
import mock
class TestCollectionReference(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
return BaseCollectionReference
def _make_one(self, *args, **kwargs):
klass = self._get_target_class()
return klass(*args, **kwargs)
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_empty(self):
with self.assertRaises(ValueError):
self._make_one()
def test_constructor_invalid_path_bad_collection_id(self):
with self.assertRaises(ValueError):
self._make_one(99, "doc", "bad-collection-id")
def test_constructor_invalid_path_bad_document_id(self):
with self.assertRaises(ValueError):
self._make_one("bad-document-ID", None, "sub-collection")
def test_constructor_invalid_path_bad_number_args(self):
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.base_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)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_select(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
field_paths = ["a", "b"]
query = collection.select(field_paths)
mock_query.select.assert_called_once_with(field_paths)
self.assertEqual(query, mock_query.select.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_where(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
field_path = "foo"
op_string = "=="
value = 45
query = collection.where(field_path, op_string, value)
mock_query.where.assert_called_once_with(field_path, op_string, value)
self.assertEqual(query, mock_query.where.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_order_by(self, mock_query):
from google.cloud.firestore_v1.base_query import BaseQuery
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
field_path = "foo"
direction = BaseQuery.DESCENDING
query = collection.order_by(field_path, direction=direction)
mock_query.order_by.assert_called_once_with(field_path, direction=direction)
self.assertEqual(query, mock_query.order_by.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_limit(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
limit = 15
query = collection.limit(limit)
mock_query.limit.assert_called_once_with(limit)
self.assertEqual(query, mock_query.limit.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_offset(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
offset = 113
query = collection.offset(offset)
mock_query.offset.assert_called_once_with(offset)
self.assertEqual(query, mock_query.offset.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_start_at(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
doc_fields = {"a": "b"}
query = collection.start_at(doc_fields)
mock_query.start_at.assert_called_once_with(doc_fields)
self.assertEqual(query, mock_query.start_at.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_start_after(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
doc_fields = {"d": "foo", "e": 10}
query = collection.start_after(doc_fields)
mock_query.start_after.assert_called_once_with(doc_fields)
self.assertEqual(query, mock_query.start_after.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_end_before(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
doc_fields = {"bar": 10.5}
query = collection.end_before(doc_fields)
mock_query.end_before.assert_called_once_with(doc_fields)
self.assertEqual(query, mock_query.end_before.return_value)
@mock.patch("google.cloud.firestore_v1.base_query.BaseQuery", autospec=True)
def test_end_at(self, mock_query):
from google.cloud.firestore_v1.base_collection import BaseCollectionReference
with mock.patch.object(BaseCollectionReference, "_query") as _query:
_query.return_value = mock_query
collection = self._make_one("collection")
doc_fields = {"opportunity": True, "reason": 9}
query = collection.end_at(doc_fields)
mock_query.end_at.assert_called_once_with(doc_fields)
self.assertEqual(query, mock_query.end_at.return_value)
class Test__auto_id(unittest.TestCase):
@staticmethod
def _call_fut():
from google.cloud.firestore_v1.base_collection import _auto_id
return _auto_id()
@mock.patch("random.choice")
def test_it(self, mock_rand_choice):
from google.cloud.firestore_v1.base_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)