-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_row_data.py
1450 lines (1017 loc) · 44.1 KB
/
test_row_data.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
# Copyright 2016 Google LLC
#
# 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 os
import mock
import pytest
from ._testing import _make_credentials
TIMESTAMP_MICROS = 18738724000 # Make sure millis granularity
ROW_KEY = b"row-key"
FAMILY_NAME = u"family"
QUALIFIER = b"qualifier"
TIMESTAMP_MICROS = 100
VALUE = b"value"
TABLE_NAME = "table_name"
def _make_cell(*args, **kwargs):
from google.cloud.bigtable.row_data import Cell
return Cell(*args, **kwargs)
def _cell_from_pb_test_helper(labels=None):
import datetime
from google.cloud._helpers import _EPOCH
from google.cloud.bigtable_v2.types import data as data_v2_pb2
from google.cloud.bigtable.row_data import Cell
timestamp = _EPOCH + datetime.timedelta(microseconds=TIMESTAMP_MICROS)
value = b"value-bytes"
if labels is None:
cell_pb = data_v2_pb2.Cell(value=value, timestamp_micros=TIMESTAMP_MICROS)
cell_expected = _make_cell(value, TIMESTAMP_MICROS)
else:
cell_pb = data_v2_pb2.Cell(
value=value, timestamp_micros=TIMESTAMP_MICROS, labels=labels
)
cell_expected = _make_cell(value, TIMESTAMP_MICROS, labels=labels)
result = Cell.from_pb(cell_pb)
assert result == cell_expected
assert result.timestamp == timestamp
def test_cell_from_pb():
_cell_from_pb_test_helper()
def test_cell_from_pb_with_labels():
labels = [u"label1", u"label2"]
_cell_from_pb_test_helper(labels)
def test_cell_constructor():
value = object()
cell = _make_cell(value, TIMESTAMP_MICROS)
assert cell.value == value
def test_cell___eq__():
value = object()
cell1 = _make_cell(value, TIMESTAMP_MICROS)
cell2 = _make_cell(value, TIMESTAMP_MICROS)
assert cell1 == cell2
def test_cell___eq__type_differ():
cell1 = _make_cell(None, None)
cell2 = object()
assert not (cell1 == cell2)
def test_cell___ne__same_value():
value = object()
cell1 = _make_cell(value, TIMESTAMP_MICROS)
cell2 = _make_cell(value, TIMESTAMP_MICROS)
assert not (cell1 != cell2)
def test_cell___ne__():
value1 = "value1"
value2 = "value2"
cell1 = _make_cell(value1, TIMESTAMP_MICROS)
cell2 = _make_cell(value2, TIMESTAMP_MICROS)
assert cell1 != cell2
def _make_partial_row_data(*args, **kwargs):
from google.cloud.bigtable.row_data import PartialRowData
return PartialRowData(*args, **kwargs)
def test_partial_row_data_constructor():
row_key = object()
partial_row_data = _make_partial_row_data(row_key)
assert partial_row_data._row_key is row_key
assert partial_row_data._cells == {}
def test_partial_row_data___eq__():
row_key = object()
partial_row_data1 = _make_partial_row_data(row_key)
partial_row_data2 = _make_partial_row_data(row_key)
assert partial_row_data1 == partial_row_data2
def test_partial_row_data___eq__type_differ():
partial_row_data1 = _make_partial_row_data(None)
partial_row_data2 = object()
assert not (partial_row_data1 == partial_row_data2)
def test_partial_row_data___ne__same_value():
row_key = object()
partial_row_data1 = _make_partial_row_data(row_key)
partial_row_data2 = _make_partial_row_data(row_key)
assert not (partial_row_data1 != partial_row_data2)
def test_partial_row_data___ne__():
row_key1 = object()
partial_row_data1 = _make_partial_row_data(row_key1)
row_key2 = object()
partial_row_data2 = _make_partial_row_data(row_key2)
assert partial_row_data1 != partial_row_data2
def test_partial_row_data___ne__cells():
row_key = object()
partial_row_data1 = _make_partial_row_data(row_key)
partial_row_data1._cells = object()
partial_row_data2 = _make_partial_row_data(row_key)
assert partial_row_data1 != partial_row_data2
def test_partial_row_data_to_dict():
cell1 = object()
cell2 = object()
cell3 = object()
family_name1 = u"name1"
family_name2 = u"name2"
qual1 = b"col1"
qual2 = b"col2"
qual3 = b"col3"
partial_row_data = _make_partial_row_data(None)
partial_row_data._cells = {
family_name1: {qual1: cell1, qual2: cell2},
family_name2: {qual3: cell3},
}
result = partial_row_data.to_dict()
expected_result = {
b"name1:col1": cell1,
b"name1:col2": cell2,
b"name2:col3": cell3,
}
assert result == expected_result
def test_partial_row_data_cell_value():
family_name = u"name1"
qualifier = b"col1"
cell = _make_cell_pb(b"value-bytes")
partial_row_data = _make_partial_row_data(None)
partial_row_data._cells = {family_name: {qualifier: [cell]}}
result = partial_row_data.cell_value(family_name, qualifier)
assert result == cell.value
def test_partial_row_data_cell_value_invalid_index():
family_name = u"name1"
qualifier = b"col1"
cell = _make_cell_pb(b"")
partial_row_data = _make_partial_row_data(None)
partial_row_data._cells = {family_name: {qualifier: [cell]}}
with pytest.raises(IndexError):
partial_row_data.cell_value(family_name, qualifier, index=None)
def test_partial_row_data_cell_value_invalid_column_family_key():
family_name = u"name1"
qualifier = b"col1"
partial_row_data = _make_partial_row_data(None)
with pytest.raises(KeyError):
partial_row_data.cell_value(family_name, qualifier)
def test_partial_row_data_cell_value_invalid_column_key():
family_name = u"name1"
qualifier = b"col1"
partial_row_data = _make_partial_row_data(None)
partial_row_data._cells = {family_name: {}}
with pytest.raises(KeyError):
partial_row_data.cell_value(family_name, qualifier)
def test_partial_row_data_cell_values():
family_name = u"name1"
qualifier = b"col1"
cell = _make_cell_pb(b"value-bytes")
partial_row_data = _make_partial_row_data(None)
partial_row_data._cells = {family_name: {qualifier: [cell]}}
values = []
for value, timestamp_micros in partial_row_data.cell_values(family_name, qualifier):
values.append(value)
assert values[0] == cell.value
def test_partial_row_data_cell_values_with_max_count():
family_name = u"name1"
qualifier = b"col1"
cell_1 = _make_cell_pb(b"value-bytes-1")
cell_2 = _make_cell_pb(b"value-bytes-2")
partial_row_data = _make_partial_row_data(None)
partial_row_data._cells = {family_name: {qualifier: [cell_1, cell_2]}}
values = []
for value, timestamp_micros in partial_row_data.cell_values(
family_name, qualifier, max_count=1
):
values.append(value)
assert 1 == len(values)
assert values[0] == cell_1.value
def test_partial_row_data_cells_property():
partial_row_data = _make_partial_row_data(None)
cells = {1: 2}
partial_row_data._cells = cells
assert partial_row_data.cells == cells
def test_partial_row_data_row_key_getter():
row_key = object()
partial_row_data = _make_partial_row_data(row_key)
assert partial_row_data.row_key is row_key
def _make_grpc_call_error(exception):
from grpc import Call
from grpc import RpcError
class TestingException(Call, RpcError):
def __init__(self, exception):
self.exception = exception
def code(self):
return self.exception.grpc_status_code
def details(self):
return "Testing"
def trailing_metadata(self):
return None
return TestingException(exception)
def test__retry_read_rows_exception_miss():
from google.api_core.exceptions import Conflict
from google.cloud.bigtable.row_data import _retry_read_rows_exception
exception = Conflict("testing")
assert not _retry_read_rows_exception(exception)
def test__retry_read_rows_exception_service_unavailable():
from google.api_core.exceptions import ServiceUnavailable
from google.cloud.bigtable.row_data import _retry_read_rows_exception
exception = ServiceUnavailable("testing")
assert _retry_read_rows_exception(exception)
def test__retry_read_rows_exception_deadline_exceeded():
from google.api_core.exceptions import DeadlineExceeded
from google.cloud.bigtable.row_data import _retry_read_rows_exception
exception = DeadlineExceeded("testing")
assert _retry_read_rows_exception(exception)
def test__retry_read_rows_exception_miss_wrapped_in_grpc():
from google.api_core.exceptions import Conflict
from google.cloud.bigtable.row_data import _retry_read_rows_exception
wrapped = Conflict("testing")
exception = _make_grpc_call_error(wrapped)
assert not _retry_read_rows_exception(exception)
def test__retry_read_rows_exception_service_unavailable_wrapped_in_grpc():
from google.api_core.exceptions import ServiceUnavailable
from google.cloud.bigtable.row_data import _retry_read_rows_exception
wrapped = ServiceUnavailable("testing")
exception = _make_grpc_call_error(wrapped)
assert _retry_read_rows_exception(exception)
def test__retry_read_rows_exception_deadline_exceeded_wrapped_in_grpc():
from google.api_core.exceptions import DeadlineExceeded
from google.cloud.bigtable.row_data import _retry_read_rows_exception
wrapped = DeadlineExceeded("testing")
exception = _make_grpc_call_error(wrapped)
assert _retry_read_rows_exception(exception)
def _make_partial_rows_data(*args, **kwargs):
from google.cloud.bigtable.row_data import PartialRowsData
return PartialRowsData(*args, **kwargs)
def _partial_rows_data_consume_all(yrd):
return [row.row_key for row in yrd]
def _make_client(*args, **kwargs):
from google.cloud.bigtable.client import Client
return Client(*args, **kwargs)
def test_partial_rows_data_constructor():
from google.cloud.bigtable.row_data import DEFAULT_RETRY_READ_ROWS
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
partial_rows_data = _make_partial_rows_data(client._data_stub.ReadRows, request)
assert partial_rows_data.request is request
assert partial_rows_data.rows == {}
assert partial_rows_data.retry == DEFAULT_RETRY_READ_ROWS
def test_partial_rows_data_constructor_with_retry():
from google.cloud.bigtable.row_data import DEFAULT_RETRY_READ_ROWS
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
retry = DEFAULT_RETRY_READ_ROWS
partial_rows_data = _make_partial_rows_data(
client._data_stub.ReadRows, request, retry
)
partial_rows_data.read_method.assert_called_once_with(
request, timeout=DEFAULT_RETRY_READ_ROWS.deadline + 1
)
assert partial_rows_data.request is request
assert partial_rows_data.rows == {}
assert partial_rows_data.retry == retry
def test_partial_rows_data___eq__():
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
partial_rows_data1 = _make_partial_rows_data(client._data_stub.ReadRows, request)
partial_rows_data2 = _make_partial_rows_data(client._data_stub.ReadRows, request)
assert partial_rows_data1.rows == partial_rows_data2.rows
def test_partial_rows_data___eq__type_differ():
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
partial_rows_data1 = _make_partial_rows_data(client._data_stub.ReadRows, request)
partial_rows_data2 = object()
assert not (partial_rows_data1 == partial_rows_data2)
def test_partial_rows_data___ne__same_value():
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
partial_rows_data1 = _make_partial_rows_data(client._data_stub.ReadRows, request)
partial_rows_data2 = _make_partial_rows_data(client._data_stub.ReadRows, request)
assert partial_rows_data1 != partial_rows_data2
def test_partial_rows_data___ne__():
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
partial_rows_data1 = _make_partial_rows_data(client._data_stub.ReadRows, request)
partial_rows_data2 = _make_partial_rows_data(client._data_stub.ReadRows, request)
assert partial_rows_data1 != partial_rows_data2
def test_partial_rows_data_rows_getter():
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
partial_rows_data = _make_partial_rows_data(client._data_stub.ReadRows, request)
partial_rows_data.rows = value = object()
assert partial_rows_data.rows is value
def test_partial_rows_data_state_start():
client = _Client()
iterator = _MockCancellableIterator()
client._data_stub = mock.MagicMock()
client._data_stub.ReadRows.side_effect = [iterator]
request = object()
yrd = _make_partial_rows_data(client._data_stub.ReadRows, request)
assert yrd.state == yrd.NEW_ROW
def test_partial_rows_data_state_new_row_w_row():
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
chunk = _ReadRowsResponseCellChunkPB(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=True,
)
chunks = [chunk]
response = _ReadRowsResponseV2(chunks)
iterator = _MockCancellableIterator(response)
data_api = mock.create_autospec(BigtableClient)
credentials = _make_credentials()
client = _make_client(project="project-id", credentials=credentials, admin=True)
client._table_data_client = data_api
request = object()
yrd = _make_partial_rows_data(client._table_data_client.read_rows, request)
assert yrd.retry._deadline == 60.0
yrd.response_iterator = iterator
rows = [row for row in yrd]
result = rows[0]
assert result.row_key == ROW_KEY
assert yrd._counter == 1
assert yrd.state == yrd.NEW_ROW
def test_partial_rows_data_multiple_chunks():
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
chunk1 = _ReadRowsResponseCellChunkPB(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=False,
)
chunk2 = _ReadRowsResponseCellChunkPB(
qualifier=QUALIFIER + b"1",
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=True,
)
chunks = [chunk1, chunk2]
response = _ReadRowsResponseV2(chunks)
iterator = _MockCancellableIterator(response)
data_api = mock.create_autospec(BigtableClient)
credentials = _make_credentials()
client = _make_client(project="project-id", credentials=credentials, admin=True)
client._table_data_client = data_api
request = object()
yrd = _make_partial_rows_data(data_api.read_rows, request)
yrd.response_iterator = iterator
rows = [row for row in yrd]
result = rows[0]
assert result.row_key == ROW_KEY
assert yrd._counter == 1
assert yrd.state == yrd.NEW_ROW
def test_partial_rows_data_cancel():
client = _Client()
response_iterator = _MockCancellableIterator()
client._data_stub = mock.MagicMock()
client._data_stub.ReadRows.side_effect = [response_iterator]
request = object()
yield_rows_data = _make_partial_rows_data(client._data_stub.ReadRows, request)
assert response_iterator.cancel_calls == 0
yield_rows_data.cancel()
assert response_iterator.cancel_calls == 1
assert list(yield_rows_data) == []
def test_partial_rows_data_cancel_between_chunks():
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
chunk1 = _ReadRowsResponseCellChunkPB(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=True,
)
chunk2 = _ReadRowsResponseCellChunkPB(
qualifier=QUALIFIER + b"1",
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=True,
)
chunks = [chunk1, chunk2]
response = _ReadRowsResponseV2(chunks)
response_iterator = _MockCancellableIterator(response)
client = _Client()
data_api = mock.create_autospec(BigtableClient)
client._table_data_client = data_api
request = object()
yrd = _make_partial_rows_data(data_api.read_rows, request)
yrd.response_iterator = response_iterator
rows = []
for row in yrd:
yrd.cancel()
rows.append(row)
assert response_iterator.cancel_calls == 1
assert list(yrd) == []
# 'consume_next' tested via 'TestPartialRowsData_JSON_acceptance_tests'
def test_partial_rows_data__copy_from_previous_unset():
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
yrd = _make_partial_rows_data(client._data_stub.read_rows, request)
cell = _PartialCellData()
yrd._copy_from_previous(cell)
assert cell.row_key == b""
assert cell.family_name == u""
assert cell.qualifier is None
assert cell.timestamp_micros == 0
assert cell.labels == []
def test_partial_rows_data__copy_from_previous_blank():
ROW_KEY = "RK"
FAMILY_NAME = u"A"
QUALIFIER = b"C"
TIMESTAMP_MICROS = 100
LABELS = ["L1", "L2"]
client = _Client()
client._data_stub = mock.MagicMock()
request = object()
yrd = _make_partial_rows_data(client._data_stub.ReadRows, request)
cell = _PartialCellData(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
labels=LABELS,
)
yrd._previous_cell = _PartialCellData()
yrd._copy_from_previous(cell)
assert cell.row_key == ROW_KEY
assert cell.family_name == FAMILY_NAME
assert cell.qualifier == QUALIFIER
assert cell.timestamp_micros == TIMESTAMP_MICROS
assert cell.labels == LABELS
def test_partial_rows_data__copy_from_previous_filled():
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
ROW_KEY = "RK"
FAMILY_NAME = u"A"
QUALIFIER = b"C"
TIMESTAMP_MICROS = 100
LABELS = ["L1", "L2"]
client = _Client()
data_api = mock.create_autospec(BigtableClient)
client._data_stub = data_api
request = object()
yrd = _make_partial_rows_data(client._data_stub.read_rows, request)
yrd._previous_cell = _PartialCellData(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
labels=LABELS,
)
cell = _PartialCellData()
yrd._copy_from_previous(cell)
assert cell.row_key == ROW_KEY
assert cell.family_name == FAMILY_NAME
assert cell.qualifier == QUALIFIER
assert cell.timestamp_micros == 0
assert cell.labels == []
def test_partial_rows_data_valid_last_scanned_row_key_on_start():
client = _Client()
response = _ReadRowsResponseV2(chunks=(), last_scanned_row_key="2.AFTER")
iterator = _MockCancellableIterator(response)
client._data_stub = mock.MagicMock()
client._data_stub.read_rows.side_effect = [iterator]
request = object()
yrd = _make_partial_rows_data(client._data_stub.read_rows, request)
yrd.last_scanned_row_key = "1.BEFORE"
_partial_rows_data_consume_all(yrd)
assert yrd.last_scanned_row_key == "2.AFTER"
def test_partial_rows_data_invalid_empty_chunk():
from google.cloud.bigtable.row_data import InvalidChunk
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
client = _Client()
chunks = _generate_cell_chunks([""])
response = _ReadRowsResponseV2(chunks)
iterator = _MockCancellableIterator(response)
client._data_stub = mock.create_autospec(BigtableClient)
client._data_stub.read_rows.side_effect = [iterator]
request = object()
yrd = _make_partial_rows_data(client._data_stub.read_rows, request)
with pytest.raises(InvalidChunk):
_partial_rows_data_consume_all(yrd)
def test_partial_rows_data_state_cell_in_progress():
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
LABELS = ["L1", "L2"]
request = object()
client = _Client()
client._data_stub = mock.create_autospec(BigtableClient)
yrd = _make_partial_rows_data(client._data_stub.read_rows, request)
chunk = _ReadRowsResponseCellChunkPB(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
labels=LABELS,
)
yrd._update_cell(chunk)
more_cell_data = _ReadRowsResponseCellChunkPB(value=VALUE)
yrd._update_cell(more_cell_data)
assert yrd._cell.row_key == ROW_KEY
assert yrd._cell.family_name == FAMILY_NAME
assert yrd._cell.qualifier == QUALIFIER
assert yrd._cell.timestamp_micros == TIMESTAMP_MICROS
assert yrd._cell.labels == LABELS
assert yrd._cell.value == VALUE + VALUE
def test_partial_rows_data_yield_rows_data():
from google.cloud.bigtable_v2.services.bigtable import BigtableClient
client = _Client()
chunk = _ReadRowsResponseCellChunkPB(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=True,
)
chunks = [chunk]
response = _ReadRowsResponseV2(chunks)
iterator = _MockCancellableIterator(response)
data_api = mock.create_autospec(BigtableClient)
client._data_stub = data_api
client._data_stub.read_rows.side_effect = [iterator]
request = object()
yrd = _make_partial_rows_data(client._data_stub.read_rows, request)
result = _partial_rows_data_consume_all(yrd)[0]
assert result == ROW_KEY
def test_partial_rows_data_yield_retry_rows_data():
from google.api_core import retry
client = _Client()
retry_read_rows = retry.Retry(predicate=_read_rows_retry_exception)
chunk = _ReadRowsResponseCellChunkPB(
row_key=ROW_KEY,
family_name=FAMILY_NAME,
qualifier=QUALIFIER,
timestamp_micros=TIMESTAMP_MICROS,
value=VALUE,
commit_row=True,
)
chunks = [chunk]
response = _ReadRowsResponseV2(chunks)
failure_iterator = _MockFailureIterator_1()
iterator = _MockCancellableIterator(response)
client._data_stub = mock.MagicMock()
client._data_stub.ReadRows.side_effect = [failure_iterator, iterator]
request = object()
yrd = _make_partial_rows_data(client._data_stub.ReadRows, request, retry_read_rows)
result = _partial_rows_data_consume_all(yrd)[0]
assert result == ROW_KEY
def _make_read_rows_request_manager(*args, **kwargs):
from google.cloud.bigtable.row_data import _ReadRowsRequestManager
return _ReadRowsRequestManager(*args, **kwargs)
@pytest.fixture(scope="session")
def rrrm_data():
from google.cloud.bigtable import row_set
row_range1 = row_set.RowRange(b"row_key21", b"row_key29")
row_range2 = row_set.RowRange(b"row_key31", b"row_key39")
row_range3 = row_set.RowRange(b"row_key41", b"row_key49")
request = _ReadRowsRequestPB(table_name=TABLE_NAME)
request.rows.row_ranges.append(row_range1.get_range_kwargs())
request.rows.row_ranges.append(row_range2.get_range_kwargs())
request.rows.row_ranges.append(row_range3.get_range_kwargs())
yield {
"row_range1": row_range1,
"row_range2": row_range2,
"row_range3": row_range3,
"request": request,
}
def test_RRRM_constructor():
request = mock.Mock()
last_scanned_key = "last_key"
rows_read_so_far = 10
request_manager = _make_read_rows_request_manager(
request, last_scanned_key, rows_read_so_far
)
assert request == request_manager.message
assert last_scanned_key == request_manager.last_scanned_key
assert rows_read_so_far == request_manager.rows_read_so_far
def test_RRRM__filter_row_key():
table_name = "table_name"
request = _ReadRowsRequestPB(table_name=table_name)
request.rows.row_keys.extend([b"row_key1", b"row_key2", b"row_key3", b"row_key4"])
last_scanned_key = b"row_key2"
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
row_keys = request_manager._filter_rows_keys()
expected_row_keys = [b"row_key3", b"row_key4"]
assert expected_row_keys == row_keys
def test_RRRM__filter_row_ranges_all_ranges_added_back(rrrm_data):
from google.cloud.bigtable_v2.types import data as data_v2_pb2
request = rrrm_data["request"]
last_scanned_key = b"row_key14"
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
row_ranges = request_manager._filter_row_ranges()
exp_row_range1 = data_v2_pb2.RowRange(
start_key_closed=b"row_key21", end_key_open=b"row_key29"
)
exp_row_range2 = data_v2_pb2.RowRange(
start_key_closed=b"row_key31", end_key_open=b"row_key39"
)
exp_row_range3 = data_v2_pb2.RowRange(
start_key_closed=b"row_key41", end_key_open=b"row_key49"
)
exp_row_ranges = [exp_row_range1, exp_row_range2, exp_row_range3]
assert exp_row_ranges == row_ranges
def test_RRRM__filter_row_ranges_all_ranges_already_read(rrrm_data):
request = rrrm_data["request"]
last_scanned_key = b"row_key54"
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
row_ranges = request_manager._filter_row_ranges()
assert row_ranges == []
def test_RRRM__filter_row_ranges_all_ranges_already_read_open_closed():
from google.cloud.bigtable import row_set
last_scanned_key = b"row_key54"
row_range1 = row_set.RowRange(b"row_key21", b"row_key29", False, True)
row_range2 = row_set.RowRange(b"row_key31", b"row_key39")
row_range3 = row_set.RowRange(b"row_key41", b"row_key49", False, True)
request = _ReadRowsRequestPB(table_name=TABLE_NAME)
request.rows.row_ranges.append(row_range1.get_range_kwargs())
request.rows.row_ranges.append(row_range2.get_range_kwargs())
request.rows.row_ranges.append(row_range3.get_range_kwargs())
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
request_manager.new_message = _ReadRowsRequestPB(table_name=TABLE_NAME)
row_ranges = request_manager._filter_row_ranges()
assert row_ranges == []
def test_RRRM__filter_row_ranges_some_ranges_already_read(rrrm_data):
from google.cloud.bigtable_v2.types import data as data_v2_pb2
request = rrrm_data["request"]
last_scanned_key = b"row_key22"
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
request_manager.new_message = _ReadRowsRequestPB(table_name=TABLE_NAME)
row_ranges = request_manager._filter_row_ranges()
exp_row_range1 = data_v2_pb2.RowRange(
start_key_open=b"row_key22", end_key_open=b"row_key29"
)
exp_row_range2 = data_v2_pb2.RowRange(
start_key_closed=b"row_key31", end_key_open=b"row_key39"
)
exp_row_range3 = data_v2_pb2.RowRange(
start_key_closed=b"row_key41", end_key_open=b"row_key49"
)
exp_row_ranges = [exp_row_range1, exp_row_range2, exp_row_range3]
assert exp_row_ranges == row_ranges
def test_RRRM_build_updated_request(rrrm_data):
from google.cloud.bigtable.row_filters import RowSampleFilter
from google.cloud.bigtable_v2 import types
row_range1 = rrrm_data["row_range1"]
row_filter = RowSampleFilter(0.33)
last_scanned_key = b"row_key25"
request = _ReadRowsRequestPB(
filter=row_filter.to_pb(),
rows_limit=8,
table_name=TABLE_NAME,
app_profile_id="app-profile-id-1",
)
request.rows.row_ranges.append(row_range1.get_range_kwargs())
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
result = request_manager.build_updated_request()
expected_result = _ReadRowsRequestPB(
table_name=TABLE_NAME,
filter=row_filter.to_pb(),
rows_limit=6,
app_profile_id="app-profile-id-1",
)
row_range1 = types.RowRange(
start_key_open=last_scanned_key, end_key_open=row_range1.end_key
)
expected_result.rows.row_ranges.append(row_range1)
assert expected_result == result
def test_RRRM_build_updated_request_full_table():
from google.cloud.bigtable_v2 import types
last_scanned_key = b"row_key14"
request = _ReadRowsRequestPB(table_name=TABLE_NAME)
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
result = request_manager.build_updated_request()
expected_result = _ReadRowsRequestPB(table_name=TABLE_NAME)
row_range1 = types.RowRange(start_key_open=last_scanned_key)
expected_result.rows.row_ranges.append(row_range1)
assert expected_result == result
def test_RRRM_build_updated_request_no_start_key():
from google.cloud.bigtable.row_filters import RowSampleFilter
from google.cloud.bigtable_v2 import types
row_filter = RowSampleFilter(0.33)
last_scanned_key = b"row_key25"
request = _ReadRowsRequestPB(
filter=row_filter.to_pb(), rows_limit=8, table_name=TABLE_NAME
)
row_range1 = types.RowRange(end_key_open=b"row_key29")
request.rows.row_ranges.append(row_range1)
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
result = request_manager.build_updated_request()
expected_result = _ReadRowsRequestPB(
table_name=TABLE_NAME, filter=row_filter.to_pb(), rows_limit=6
)
row_range2 = types.RowRange(
start_key_open=last_scanned_key, end_key_open=b"row_key29"
)
expected_result.rows.row_ranges.append(row_range2)
assert expected_result == result
def test_RRRM_build_updated_request_no_end_key():
from google.cloud.bigtable.row_filters import RowSampleFilter
from google.cloud.bigtable_v2 import types
row_filter = RowSampleFilter(0.33)
last_scanned_key = b"row_key25"
request = _ReadRowsRequestPB(
filter=row_filter.to_pb(), rows_limit=8, table_name=TABLE_NAME
)
row_range1 = types.RowRange(start_key_closed=b"row_key20")
request.rows.row_ranges.append(row_range1)
request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2)
result = request_manager.build_updated_request()
expected_result = _ReadRowsRequestPB(
table_name=TABLE_NAME, filter=row_filter.to_pb(), rows_limit=6
)
row_range2 = types.RowRange(start_key_open=last_scanned_key)
expected_result.rows.row_ranges.append(row_range2)
assert expected_result == result
def test_RRRM_build_updated_request_rows():
from google.cloud.bigtable.row_filters import RowSampleFilter
row_filter = RowSampleFilter(0.33)
last_scanned_key = b"row_key4"
request = _ReadRowsRequestPB(
filter=row_filter.to_pb(), rows_limit=5, table_name=TABLE_NAME
)
request.rows.row_keys.extend(
[b"row_key1", b"row_key2", b"row_key4", b"row_key5", b"row_key7", b"row_key9"]