/
test__helpers.py
2407 lines (1882 loc) · 87.2 KB
/
test__helpers.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
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# 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 sys
import unittest
import mock
class AsyncMock(mock.MagicMock):
async def __call__(self, *args, **kwargs):
return super(AsyncMock, self).__call__(*args, **kwargs)
class AsyncIter:
def __init__(self, items):
self.items = items
async def __aiter__(self, **_):
for i in self.items:
yield i
class TestGeoPoint(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.firestore_v1._helpers import GeoPoint
return GeoPoint
def _make_one(self, *args, **kwargs):
klass = self._get_target_class()
return klass(*args, **kwargs)
def test_constructor(self):
lat = 81.25
lng = 359.984375
geo_pt = self._make_one(lat, lng)
self.assertEqual(geo_pt.latitude, lat)
self.assertEqual(geo_pt.longitude, lng)
def test_to_protobuf(self):
from google.type import latlng_pb2
lat = 0.015625
lng = 20.03125
geo_pt = self._make_one(lat, lng)
result = geo_pt.to_protobuf()
geo_pt_pb = latlng_pb2.LatLng(latitude=lat, longitude=lng)
self.assertEqual(result, geo_pt_pb)
def test___eq__(self):
lat = 0.015625
lng = 20.03125
geo_pt1 = self._make_one(lat, lng)
geo_pt2 = self._make_one(lat, lng)
self.assertEqual(geo_pt1, geo_pt2)
def test___eq__type_differ(self):
lat = 0.015625
lng = 20.03125
geo_pt1 = self._make_one(lat, lng)
geo_pt2 = object()
self.assertNotEqual(geo_pt1, geo_pt2)
self.assertIs(geo_pt1.__eq__(geo_pt2), NotImplemented)
def test___ne__same_value(self):
lat = 0.015625
lng = 20.03125
geo_pt1 = self._make_one(lat, lng)
geo_pt2 = self._make_one(lat, lng)
comparison_val = geo_pt1 != geo_pt2
self.assertFalse(comparison_val)
def test___ne__(self):
geo_pt1 = self._make_one(0.0, 1.0)
geo_pt2 = self._make_one(2.0, 3.0)
self.assertNotEqual(geo_pt1, geo_pt2)
def test___ne__type_differ(self):
lat = 0.015625
lng = 20.03125
geo_pt1 = self._make_one(lat, lng)
geo_pt2 = object()
self.assertNotEqual(geo_pt1, geo_pt2)
self.assertIs(geo_pt1.__ne__(geo_pt2), NotImplemented)
class Test_verify_path(unittest.TestCase):
@staticmethod
def _call_fut(path, is_collection):
from google.cloud.firestore_v1._helpers import verify_path
return verify_path(path, is_collection)
def test_empty(self):
path = ()
with self.assertRaises(ValueError):
self._call_fut(path, True)
with self.assertRaises(ValueError):
self._call_fut(path, False)
def test_wrong_length_collection(self):
path = ("foo", "bar")
with self.assertRaises(ValueError):
self._call_fut(path, True)
def test_wrong_length_document(self):
path = ("Kind",)
with self.assertRaises(ValueError):
self._call_fut(path, False)
def test_wrong_type_collection(self):
path = (99, "ninety-nine", "zap")
with self.assertRaises(ValueError):
self._call_fut(path, True)
def test_wrong_type_document(self):
path = ("Users", "Ada", "Candy", {})
with self.assertRaises(ValueError):
self._call_fut(path, False)
def test_success_collection(self):
path = ("Computer", "Magic", "Win")
ret_val = self._call_fut(path, True)
# NOTE: We are just checking that it didn't fail.
self.assertIsNone(ret_val)
def test_success_document(self):
path = ("Tokenizer", "Seventeen", "Cheese", "Burger")
ret_val = self._call_fut(path, False)
# NOTE: We are just checking that it didn't fail.
self.assertIsNone(ret_val)
class Test_encode_value(unittest.TestCase):
@staticmethod
def _call_fut(value):
from google.cloud.firestore_v1._helpers import encode_value
return encode_value(value)
def test_none(self):
from google.protobuf import struct_pb2
result = self._call_fut(None)
expected = _value_pb(null_value=struct_pb2.NULL_VALUE)
self.assertEqual(result, expected)
def test_boolean(self):
result = self._call_fut(True)
expected = _value_pb(boolean_value=True)
self.assertEqual(result, expected)
def test_integer(self):
value = 425178
result = self._call_fut(value)
expected = _value_pb(integer_value=value)
self.assertEqual(result, expected)
def test_float(self):
value = 123.4453125
result = self._call_fut(value)
expected = _value_pb(double_value=value)
self.assertEqual(result, expected)
def test_datetime_with_nanos(self):
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
from google.protobuf import timestamp_pb2
dt_seconds = 1488768504
dt_nanos = 458816991
timestamp_pb = timestamp_pb2.Timestamp(seconds=dt_seconds, nanos=dt_nanos)
dt_val = DatetimeWithNanoseconds.from_timestamp_pb(timestamp_pb)
result = self._call_fut(dt_val)
expected = _value_pb(timestamp_value=timestamp_pb)
self.assertEqual(result, expected)
def test_datetime_wo_nanos(self):
from google.protobuf import timestamp_pb2
dt_seconds = 1488768504
dt_nanos = 458816000
# Make sure precision is valid in microseconds too.
self.assertEqual(dt_nanos % 1000, 0)
dt_val = datetime.datetime.utcfromtimestamp(dt_seconds + 1e-9 * dt_nanos)
result = self._call_fut(dt_val)
timestamp_pb = timestamp_pb2.Timestamp(seconds=dt_seconds, nanos=dt_nanos)
expected = _value_pb(timestamp_value=timestamp_pb)
self.assertEqual(result, expected)
def test_string(self):
value = u"\u2018left quote, right quote\u2019"
result = self._call_fut(value)
expected = _value_pb(string_value=value)
self.assertEqual(result, expected)
def test_bytes(self):
value = b"\xe3\xf2\xff\x00"
result = self._call_fut(value)
expected = _value_pb(bytes_value=value)
self.assertEqual(result, expected)
def test_reference_value(self):
client = _make_client()
value = client.document("my", "friend")
result = self._call_fut(value)
expected = _value_pb(reference_value=value._document_path)
self.assertEqual(result, expected)
def test_geo_point(self):
from google.cloud.firestore_v1._helpers import GeoPoint
value = GeoPoint(50.5, 88.75)
result = self._call_fut(value)
expected = _value_pb(geo_point_value=value.to_protobuf())
self.assertEqual(result, expected)
def test_array(self):
from google.cloud.firestore_v1.types.document import ArrayValue
result = self._call_fut([99, True, 118.5])
array_pb = ArrayValue(
values=[
_value_pb(integer_value=99),
_value_pb(boolean_value=True),
_value_pb(double_value=118.5),
]
)
expected = _value_pb(array_value=array_pb)
self.assertEqual(result, expected)
def test_map(self):
from google.cloud.firestore_v1.types.document import MapValue
result = self._call_fut({"abc": 285, "def": b"piglatin"})
map_pb = MapValue(
fields={
"abc": _value_pb(integer_value=285),
"def": _value_pb(bytes_value=b"piglatin"),
}
)
expected = _value_pb(map_value=map_pb)
self.assertEqual(result, expected)
def test_bad_type(self):
value = object()
with self.assertRaises(TypeError):
self._call_fut(value)
class Test_encode_dict(unittest.TestCase):
@staticmethod
def _call_fut(values_dict):
from google.cloud.firestore_v1._helpers import encode_dict
return encode_dict(values_dict)
def test_many_types(self):
from google.protobuf import struct_pb2
from google.protobuf import timestamp_pb2
from google.cloud.firestore_v1.types.document import ArrayValue
from google.cloud.firestore_v1.types.document import MapValue
dt_seconds = 1497397225
dt_nanos = 465964000
# Make sure precision is valid in microseconds too.
self.assertEqual(dt_nanos % 1000, 0)
dt_val = datetime.datetime.utcfromtimestamp(dt_seconds + 1e-9 * dt_nanos)
client = _make_client()
document = client.document("most", "adjective", "thing", "here")
values_dict = {
"foo": None,
"bar": True,
"baz": 981,
"quux": 2.875,
"quuz": dt_val,
"corge": u"\N{snowman}",
"grault": b"\xe2\x98\x83",
"wibble": document,
"garply": [u"fork", 4.0],
"waldo": {"fred": u"zap", "thud": False},
}
encoded_dict = self._call_fut(values_dict)
expected_dict = {
"foo": _value_pb(null_value=struct_pb2.NULL_VALUE),
"bar": _value_pb(boolean_value=True),
"baz": _value_pb(integer_value=981),
"quux": _value_pb(double_value=2.875),
"quuz": _value_pb(
timestamp_value=timestamp_pb2.Timestamp(
seconds=dt_seconds, nanos=dt_nanos
)
),
"corge": _value_pb(string_value=u"\N{snowman}"),
"grault": _value_pb(bytes_value=b"\xe2\x98\x83"),
"wibble": _value_pb(reference_value=document._document_path),
"garply": _value_pb(
array_value=ArrayValue(
values=[
_value_pb(string_value=u"fork"),
_value_pb(double_value=4.0),
]
)
),
"waldo": _value_pb(
map_value=MapValue(
fields={
"fred": _value_pb(string_value=u"zap"),
"thud": _value_pb(boolean_value=False),
}
)
),
}
self.assertEqual(encoded_dict, expected_dict)
class Test_reference_value_to_document(unittest.TestCase):
@staticmethod
def _call_fut(reference_value, client):
from google.cloud.firestore_v1._helpers import reference_value_to_document
return reference_value_to_document(reference_value, client)
def test_bad_format(self):
from google.cloud.firestore_v1._helpers import BAD_REFERENCE_ERROR
reference_value = "not/the/right/format"
with self.assertRaises(ValueError) as exc_info:
self._call_fut(reference_value, None)
err_msg = BAD_REFERENCE_ERROR.format(reference_value)
self.assertEqual(exc_info.exception.args, (err_msg,))
def test_same_client(self):
from google.cloud.firestore_v1.document import DocumentReference
client = _make_client()
document = client.document("that", "this")
reference_value = document._document_path
new_document = self._call_fut(reference_value, client)
self.assertIsNot(new_document, document)
self.assertIsInstance(new_document, DocumentReference)
self.assertIs(new_document._client, client)
self.assertEqual(new_document._path, document._path)
def test_different_client(self):
from google.cloud.firestore_v1._helpers import WRONG_APP_REFERENCE
client1 = _make_client(project="kirk")
document = client1.document("tin", "foil")
reference_value = document._document_path
client2 = _make_client(project="spock")
with self.assertRaises(ValueError) as exc_info:
self._call_fut(reference_value, client2)
err_msg = WRONG_APP_REFERENCE.format(reference_value, client2._database_string)
self.assertEqual(exc_info.exception.args, (err_msg,))
class Test_decode_value(unittest.TestCase):
@staticmethod
def _call_fut(value, client=mock.sentinel.client):
from google.cloud.firestore_v1._helpers import decode_value
return decode_value(value, client)
def test_none(self):
from google.protobuf import struct_pb2
value = _value_pb(null_value=struct_pb2.NULL_VALUE)
self.assertIsNone(self._call_fut(value))
def test_bool(self):
value1 = _value_pb(boolean_value=True)
self.assertTrue(self._call_fut(value1))
value2 = _value_pb(boolean_value=False)
self.assertFalse(self._call_fut(value2))
def test_int(self):
int_val = 29871
value = _value_pb(integer_value=int_val)
self.assertEqual(self._call_fut(value), int_val)
def test_float(self):
float_val = 85.9296875
value = _value_pb(double_value=float_val)
self.assertEqual(self._call_fut(value), float_val)
@unittest.skipIf(
(3,) <= sys.version_info < (3, 4, 4), "known datetime bug (bpo-23517) in Python"
)
def test_datetime(self):
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
from google.protobuf import timestamp_pb2
dt_seconds = 552855006
dt_nanos = 766961828
timestamp_pb = timestamp_pb2.Timestamp(seconds=dt_seconds, nanos=dt_nanos)
value = _value_pb(timestamp_value=timestamp_pb)
expected_dt_val = DatetimeWithNanoseconds.from_timestamp_pb(timestamp_pb)
self.assertEqual(self._call_fut(value), expected_dt_val)
def test_unicode(self):
unicode_val = u"zorgon"
value = _value_pb(string_value=unicode_val)
self.assertEqual(self._call_fut(value), unicode_val)
def test_bytes(self):
bytes_val = b"abc\x80"
value = _value_pb(bytes_value=bytes_val)
self.assertEqual(self._call_fut(value), bytes_val)
def test_reference(self):
from google.cloud.firestore_v1.document import DocumentReference
client = _make_client()
path = (u"then", u"there-was-one")
document = client.document(*path)
ref_string = document._document_path
value = _value_pb(reference_value=ref_string)
result = self._call_fut(value, client)
self.assertIsInstance(result, DocumentReference)
self.assertIs(result._client, client)
self.assertEqual(result._path, path)
def test_geo_point(self):
from google.cloud.firestore_v1._helpers import GeoPoint
geo_pt = GeoPoint(latitude=42.5, longitude=99.0625)
value = _value_pb(geo_point_value=geo_pt.to_protobuf())
self.assertEqual(self._call_fut(value), geo_pt)
def test_array(self):
from google.cloud.firestore_v1.types import document
sub_value1 = _value_pb(boolean_value=True)
sub_value2 = _value_pb(double_value=14.1396484375)
sub_value3 = _value_pb(bytes_value=b"\xde\xad\xbe\xef")
array_pb = document.ArrayValue(values=[sub_value1, sub_value2, sub_value3])
value = _value_pb(array_value=array_pb)
expected = [
sub_value1.boolean_value,
sub_value2.double_value,
sub_value3.bytes_value,
]
self.assertEqual(self._call_fut(value), expected)
def test_map(self):
from google.cloud.firestore_v1.types import document
sub_value1 = _value_pb(integer_value=187680)
sub_value2 = _value_pb(string_value=u"how low can you go?")
map_pb = document.MapValue(fields={"first": sub_value1, "second": sub_value2})
value = _value_pb(map_value=map_pb)
expected = {
"first": sub_value1.integer_value,
"second": sub_value2.string_value,
}
self.assertEqual(self._call_fut(value), expected)
def test_nested_map(self):
from google.cloud.firestore_v1.types import document
actual_value1 = 1009876
actual_value2 = u"hey you guys"
actual_value3 = 90.875
map_pb1 = document.MapValue(
fields={
"lowest": _value_pb(integer_value=actual_value1),
"aside": _value_pb(string_value=actual_value2),
}
)
map_pb2 = document.MapValue(
fields={
"middle": _value_pb(map_value=map_pb1),
"aside": _value_pb(boolean_value=True),
}
)
map_pb3 = document.MapValue(
fields={
"highest": _value_pb(map_value=map_pb2),
"aside": _value_pb(double_value=actual_value3),
}
)
value = _value_pb(map_value=map_pb3)
expected = {
"highest": {
"middle": {"lowest": actual_value1, "aside": actual_value2},
"aside": True,
},
"aside": actual_value3,
}
self.assertEqual(self._call_fut(value), expected)
def test_unset_value_type(self):
with self.assertRaises(ValueError):
self._call_fut(_value_pb())
def test_unknown_value_type(self):
value_pb = mock.Mock()
value_pb._pb.WhichOneof.return_value = "zoob_value"
with self.assertRaises(ValueError):
self._call_fut(value_pb)
value_pb._pb.WhichOneof.assert_called_once_with("value_type")
class Test_decode_dict(unittest.TestCase):
@staticmethod
def _call_fut(value_fields, client=mock.sentinel.client):
from google.cloud.firestore_v1._helpers import decode_dict
return decode_dict(value_fields, client)
@unittest.skipIf(
(3,) <= sys.version_info < (3, 4, 4), "known datetime bug (bpo-23517) in Python"
)
def test_many_types(self):
from google.protobuf import struct_pb2
from google.protobuf import timestamp_pb2
from google.cloud.firestore_v1.types.document import ArrayValue
from google.cloud.firestore_v1.types.document import MapValue
from google.cloud._helpers import UTC
from google.cloud.firestore_v1.field_path import FieldPath
dt_seconds = 1394037350
dt_nanos = 667285000
# Make sure precision is valid in microseconds too.
self.assertEqual(dt_nanos % 1000, 0)
dt_val = datetime.datetime.utcfromtimestamp(
dt_seconds + 1e-9 * dt_nanos
).replace(tzinfo=UTC)
value_fields = {
"foo": _value_pb(null_value=struct_pb2.NULL_VALUE),
"bar": _value_pb(boolean_value=True),
"baz": _value_pb(integer_value=981),
"quux": _value_pb(double_value=2.875),
"quuz": _value_pb(
timestamp_value=timestamp_pb2.Timestamp(
seconds=dt_seconds, nanos=dt_nanos
)
),
"corge": _value_pb(string_value=u"\N{snowman}"),
"grault": _value_pb(bytes_value=b"\xe2\x98\x83"),
"garply": _value_pb(
array_value=ArrayValue(
values=[
_value_pb(string_value=u"fork"),
_value_pb(double_value=4.0),
]
)
),
"waldo": _value_pb(
map_value=MapValue(
fields={
"fred": _value_pb(string_value=u"zap"),
"thud": _value_pb(boolean_value=False),
}
)
),
FieldPath("a", "b", "c").to_api_repr(): _value_pb(boolean_value=False),
}
expected = {
"foo": None,
"bar": True,
"baz": 981,
"quux": 2.875,
"quuz": dt_val,
"corge": u"\N{snowman}",
"grault": b"\xe2\x98\x83",
"garply": [u"fork", 4.0],
"waldo": {"fred": u"zap", "thud": False},
"a.b.c": False,
}
self.assertEqual(self._call_fut(value_fields), expected)
class Test_get_doc_id(unittest.TestCase):
@staticmethod
def _call_fut(document_pb, expected_prefix):
from google.cloud.firestore_v1._helpers import get_doc_id
return get_doc_id(document_pb, expected_prefix)
@staticmethod
def _dummy_ref_string(collection_id):
from google.cloud.firestore_v1.client import DEFAULT_DATABASE
project = u"bazzzz"
return u"projects/{}/databases/{}/documents/{}".format(
project, DEFAULT_DATABASE, collection_id
)
def test_success(self):
from google.cloud.firestore_v1.types import document
prefix = self._dummy_ref_string("sub-collection")
actual_id = "this-is-the-one"
name = "{}/{}".format(prefix, actual_id)
document_pb = document.Document(name=name)
document_id = self._call_fut(document_pb, prefix)
self.assertEqual(document_id, actual_id)
def test_failure(self):
from google.cloud.firestore_v1.types import document
actual_prefix = self._dummy_ref_string("the-right-one")
wrong_prefix = self._dummy_ref_string("the-wrong-one")
name = "{}/{}".format(actual_prefix, "sorry-wont-works")
document_pb = document.Document(name=name)
with self.assertRaises(ValueError) as exc_info:
self._call_fut(document_pb, wrong_prefix)
exc_args = exc_info.exception.args
self.assertEqual(len(exc_args), 4)
self.assertEqual(exc_args[1], name)
self.assertEqual(exc_args[3], wrong_prefix)
class Test_extract_fields(unittest.TestCase):
@staticmethod
def _call_fut(document_data, prefix_path, expand_dots=False):
from google.cloud.firestore_v1 import _helpers
return _helpers.extract_fields(
document_data, prefix_path, expand_dots=expand_dots
)
def test_w_empty_document(self):
from google.cloud.firestore_v1._helpers import _EmptyDict
document_data = {}
prefix_path = _make_field_path()
expected = [(_make_field_path(), _EmptyDict)]
iterator = self._call_fut(document_data, prefix_path)
self.assertEqual(list(iterator), expected)
def test_w_invalid_key_and_expand_dots(self):
document_data = {"b": 1, "a~d": 2, "c": 3}
prefix_path = _make_field_path()
with self.assertRaises(ValueError):
list(self._call_fut(document_data, prefix_path, expand_dots=True))
def test_w_shallow_keys(self):
document_data = {"b": 1, "a": 2, "c": 3}
prefix_path = _make_field_path()
expected = [
(_make_field_path("a"), 2),
(_make_field_path("b"), 1),
(_make_field_path("c"), 3),
]
iterator = self._call_fut(document_data, prefix_path)
self.assertEqual(list(iterator), expected)
def test_w_nested(self):
from google.cloud.firestore_v1._helpers import _EmptyDict
document_data = {"b": {"a": {"d": 4, "c": 3, "g": {}}, "e": 7}, "f": 5}
prefix_path = _make_field_path()
expected = [
(_make_field_path("b", "a", "c"), 3),
(_make_field_path("b", "a", "d"), 4),
(_make_field_path("b", "a", "g"), _EmptyDict),
(_make_field_path("b", "e"), 7),
(_make_field_path("f"), 5),
]
iterator = self._call_fut(document_data, prefix_path)
self.assertEqual(list(iterator), expected)
def test_w_expand_dotted(self):
from google.cloud.firestore_v1._helpers import _EmptyDict
document_data = {
"b": {"a": {"d": 4, "c": 3, "g": {}, "k.l.m": 17}, "e": 7},
"f": 5,
"h.i.j": 9,
}
prefix_path = _make_field_path()
expected = [
(_make_field_path("b", "a", "c"), 3),
(_make_field_path("b", "a", "d"), 4),
(_make_field_path("b", "a", "g"), _EmptyDict),
(_make_field_path("b", "a", "k.l.m"), 17),
(_make_field_path("b", "e"), 7),
(_make_field_path("f"), 5),
(_make_field_path("h", "i", "j"), 9),
]
iterator = self._call_fut(document_data, prefix_path, expand_dots=True)
self.assertEqual(list(iterator), expected)
class Test_set_field_value(unittest.TestCase):
@staticmethod
def _call_fut(document_data, field_path, value):
from google.cloud.firestore_v1 import _helpers
return _helpers.set_field_value(document_data, field_path, value)
def test_normal_value_w_shallow(self):
document = {}
field_path = _make_field_path("a")
value = 3
self._call_fut(document, field_path, value)
self.assertEqual(document, {"a": 3})
def test_normal_value_w_nested(self):
document = {}
field_path = _make_field_path("a", "b", "c")
value = 3
self._call_fut(document, field_path, value)
self.assertEqual(document, {"a": {"b": {"c": 3}}})
def test_empty_dict_w_shallow(self):
from google.cloud.firestore_v1._helpers import _EmptyDict
document = {}
field_path = _make_field_path("a")
value = _EmptyDict
self._call_fut(document, field_path, value)
self.assertEqual(document, {"a": {}})
def test_empty_dict_w_nested(self):
from google.cloud.firestore_v1._helpers import _EmptyDict
document = {}
field_path = _make_field_path("a", "b", "c")
value = _EmptyDict
self._call_fut(document, field_path, value)
self.assertEqual(document, {"a": {"b": {"c": {}}}})
class Test_get_field_value(unittest.TestCase):
@staticmethod
def _call_fut(document_data, field_path):
from google.cloud.firestore_v1 import _helpers
return _helpers.get_field_value(document_data, field_path)
def test_w_empty_path(self):
document = {}
with self.assertRaises(ValueError):
self._call_fut(document, _make_field_path())
def test_miss_shallow(self):
document = {}
with self.assertRaises(KeyError):
self._call_fut(document, _make_field_path("nonesuch"))
def test_miss_nested(self):
document = {"a": {"b": {}}}
with self.assertRaises(KeyError):
self._call_fut(document, _make_field_path("a", "b", "c"))
def test_hit_shallow(self):
document = {"a": 1}
self.assertEqual(self._call_fut(document, _make_field_path("a")), 1)
def test_hit_nested(self):
document = {"a": {"b": {"c": 1}}}
self.assertEqual(self._call_fut(document, _make_field_path("a", "b", "c")), 1)
class TestDocumentExtractor(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.firestore_v1 import _helpers
return _helpers.DocumentExtractor
def _make_one(self, document_data):
return self._get_target_class()(document_data)
def test_ctor_w_empty_document(self):
document_data = {}
inst = self._make_one(document_data)
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [])
self.assertEqual(inst.server_timestamps, [])
self.assertEqual(inst.array_removes, {})
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertTrue(inst.empty_document)
self.assertFalse(inst.has_transforms)
self.assertEqual(inst.transform_paths, [])
def test_ctor_w_delete_field_shallow(self):
from google.cloud.firestore_v1.transforms import DELETE_FIELD
document_data = {"a": DELETE_FIELD}
inst = self._make_one(document_data)
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [_make_field_path("a")])
self.assertEqual(inst.server_timestamps, [])
self.assertEqual(inst.array_removes, {})
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertFalse(inst.has_transforms)
self.assertEqual(inst.transform_paths, [])
def test_ctor_w_delete_field_nested(self):
from google.cloud.firestore_v1.transforms import DELETE_FIELD
document_data = {"a": {"b": {"c": DELETE_FIELD}}}
inst = self._make_one(document_data)
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [_make_field_path("a", "b", "c")])
self.assertEqual(inst.server_timestamps, [])
self.assertEqual(inst.array_removes, {})
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertFalse(inst.has_transforms)
self.assertEqual(inst.transform_paths, [])
def test_ctor_w_server_timestamp_shallow(self):
from google.cloud.firestore_v1.transforms import SERVER_TIMESTAMP
document_data = {"a": SERVER_TIMESTAMP}
inst = self._make_one(document_data)
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [])
self.assertEqual(inst.server_timestamps, [_make_field_path("a")])
self.assertEqual(inst.array_removes, {})
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertTrue(inst.has_transforms)
self.assertEqual(inst.transform_paths, [_make_field_path("a")])
def test_ctor_w_server_timestamp_nested(self):
from google.cloud.firestore_v1.transforms import SERVER_TIMESTAMP
document_data = {"a": {"b": {"c": SERVER_TIMESTAMP}}}
inst = self._make_one(document_data)
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [])
self.assertEqual(inst.server_timestamps, [_make_field_path("a", "b", "c")])
self.assertEqual(inst.array_removes, {})
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertTrue(inst.has_transforms)
self.assertEqual(inst.transform_paths, [_make_field_path("a", "b", "c")])
def test_ctor_w_array_remove_shallow(self):
from google.cloud.firestore_v1.transforms import ArrayRemove
values = [1, 3, 5]
document_data = {"a": ArrayRemove(values)}
inst = self._make_one(document_data)
expected_array_removes = {_make_field_path("a"): values}
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [])
self.assertEqual(inst.server_timestamps, [])
self.assertEqual(inst.array_removes, expected_array_removes)
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertTrue(inst.has_transforms)
self.assertEqual(inst.transform_paths, [_make_field_path("a")])
def test_ctor_w_array_remove_nested(self):
from google.cloud.firestore_v1.transforms import ArrayRemove
values = [2, 4, 8]
document_data = {"a": {"b": {"c": ArrayRemove(values)}}}
inst = self._make_one(document_data)
expected_array_removes = {_make_field_path("a", "b", "c"): values}
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [])
self.assertEqual(inst.server_timestamps, [])
self.assertEqual(inst.array_removes, expected_array_removes)
self.assertEqual(inst.array_unions, {})
self.assertEqual(inst.increments, {})
self.assertEqual(inst.maximums, {})
self.assertEqual(inst.minimums, {})
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertTrue(inst.has_transforms)
self.assertEqual(inst.transform_paths, [_make_field_path("a", "b", "c")])
def test_ctor_w_array_union_shallow(self):
from google.cloud.firestore_v1.transforms import ArrayUnion
values = [1, 3, 5]
document_data = {"a": ArrayUnion(values)}
inst = self._make_one(document_data)
expected_array_unions = {_make_field_path("a"): values}
self.assertEqual(inst.document_data, document_data)
self.assertEqual(inst.field_paths, [])
self.assertEqual(inst.deleted_fields, [])
self.assertEqual(inst.server_timestamps, [])
self.assertEqual(inst.array_removes, {})
self.assertEqual(inst.array_unions, expected_array_unions)
self.assertEqual(inst.set_fields, {})
self.assertFalse(inst.empty_document)
self.assertTrue(inst.has_transforms)
self.assertEqual(inst.transform_paths, [_make_field_path("a")])
def test_ctor_w_array_union_nested(self):
from google.cloud.firestore_v1.transforms import ArrayUnion
values = [2, 4, 8]
document_data = {"a": {"b": {"c": ArrayUnion(values)}}}
inst = self._make_one(document_data)