-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_system_autogen.py
More file actions
1139 lines (1046 loc) · 47.1 KB
/
Copy pathtest_system_autogen.py
File metadata and controls
1139 lines (1046 loc) · 47.1 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
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 2024 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.
# This file is automatically generated by CrossSync. Do not edit manually.
import datetime
import os
import uuid
import pytest
from google.api_core import retry
from google.api_core.exceptions import ClientError, PermissionDenied
from google.cloud.environment_vars import BIGTABLE_EMULATOR
from google.type import date_pb2
from google.cloud.bigtable.data._cross_sync import CrossSync
from google.cloud.bigtable.data.execute_query.metadata import SqlType
from google.cloud.bigtable.data.read_modify_write_rules import _MAX_INCREMENT_VALUE
from google.cloud.bigtable_v2.services.bigtable.transports.grpc import (
_LoggingClientInterceptor as GapicInterceptor,
)
from . import TEST_AGGREGATE_FAMILY, TEST_FAMILY, TEST_FAMILY_2, SystemTestRunner
TARGETS = ["table"]
if not os.environ.get(BIGTABLE_EMULATOR):
TARGETS.append("authorized_view")
@CrossSync._Sync_Impl.add_mapping_decorator("TempRowBuilder")
class TempRowBuilder:
"""
Used to add rows to a table for testing purposes.
"""
def __init__(self, target):
self.rows = []
self.target = target
def add_row(
self, row_key, *, family=TEST_FAMILY, qualifier=b"q", value=b"test-value"
):
if isinstance(value, str):
value = value.encode("utf-8")
elif isinstance(value, int):
value = value.to_bytes(8, byteorder="big", signed=True)
request = {
"table_name": self.target.table_name,
"row_key": row_key,
"mutations": [
{
"set_cell": {
"family_name": family,
"column_qualifier": qualifier,
"value": value,
}
}
],
}
self.target.client._gapic_client.mutate_row(request)
self.rows.append(row_key)
def add_aggregate_row(
self, row_key, *, family=TEST_AGGREGATE_FAMILY, qualifier=b"q", input=0
):
request = {
"table_name": self.target.table_name,
"row_key": row_key,
"mutations": [
{
"add_to_cell": {
"family_name": family,
"column_qualifier": {"raw_value": qualifier},
"timestamp": {"raw_timestamp_micros": 0},
"input": {"int_value": input},
}
}
],
}
self.target.client._gapic_client.mutate_row(request)
self.rows.append(row_key)
def delete_rows(self):
if self.rows:
chunk_size = 5000
for i in range(0, len(self.rows), chunk_size):
chunk = self.rows[i : i + chunk_size]
request = {
**self.target._request_path,
"entries": [
{"row_key": row, "mutations": [{"delete_from_row": {}}]}
for row in chunk
],
}
stream = self.target.client._gapic_client.mutate_rows(request)
for response in stream:
pass
def retrieve_cell_value(self, target, row_key):
"""Helper to read an individual row"""
from google.cloud.bigtable.data import ReadRowsQuery
row_list = target.read_rows(ReadRowsQuery(row_keys=row_key))
assert len(row_list) == 1
row = row_list[0]
cell = row.cells[0]
return cell.value
def create_row_and_mutation(
self, table, *, start_value=b"start", new_value=b"new_value"
):
"""Helper to create a new row, and a sample set_cell mutation to change its value"""
from google.cloud.bigtable.data.mutations import SetCell
row_key = uuid.uuid4().hex.encode()
family = TEST_FAMILY
qualifier = b"test-qualifier"
self.add_row(row_key, family=family, qualifier=qualifier, value=start_value)
assert self.retrieve_cell_value(table, row_key) == start_value
mutation = SetCell(family=TEST_FAMILY, qualifier=qualifier, new_value=new_value)
return (row_key, mutation)
class TestSystem(SystemTestRunner):
def _make_client(self):
project = os.getenv("GOOGLE_CLOUD_PROJECT") or None
return CrossSync._Sync_Impl.DataClient(project=project)
@pytest.fixture(scope="session")
def client(self):
with self._make_client() as client:
yield client
@pytest.fixture(scope="session", params=TARGETS)
def target(self, client, table_id, authorized_view_id, instance_id, request):
"""This fixture runs twice: once for a standard table, and once with an authorized view
Note: emulator doesn't support authorized views. Only use target"""
if request.param == "table":
with client.get_table(instance_id, table_id) as table:
yield table
elif request.param == "authorized_view":
with client.get_authorized_view(
instance_id, table_id, authorized_view_id
) as view:
yield view
else:
raise ValueError(f"unknown target type: {request.param}")
@pytest.fixture(scope="function")
def temp_rows(self, target):
builder = CrossSync._Sync_Impl.TempRowBuilder(target)
yield builder
builder.delete_rows()
@pytest.mark.usefixtures("target")
@pytest.mark.usefixtures("client")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=10
)
def test_ping_and_warm_gapic(self, client, target):
"""Simple ping rpc test
This test ensures channels are able to authenticate with backend"""
request = {"name": target.instance_name}
client._gapic_client.ping_and_warm(request)
@pytest.mark.usefixtures("target")
@pytest.mark.usefixtures("client")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_ping_and_warm(self, client, target):
"""Test ping and warm from handwritten client"""
results = client._ping_and_warm_instances()
assert len(results) == 1
assert results[0] is None
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)),
reason="emulator mode doesn't refresh channel",
)
def test_channel_refresh(self, table_id, instance_id, temp_rows):
"""perform requests while swapping out the grpc channel. Requests should continue without error"""
import time
temp_rows.add_row(b"test_row")
with self._make_client() as client:
client._channel_refresh_task.cancel()
channel_wrapper = client.transport.grpc_channel
first_channel = channel_wrapper._channel
client._channel_refresh_task = CrossSync._Sync_Impl.create_task(
client._manage_channel,
refresh_interval_min=0.1,
refresh_interval_max=0.1,
grace_period=1,
sync_executor=client._executor,
)
end_time = time.monotonic() + 3
with client.get_table(instance_id, table_id) as table:
while time.monotonic() < end_time:
rows = table.read_rows({})
assert len(rows) == 1
CrossSync._Sync_Impl.yield_to_event_loop()
updated_channel = channel_wrapper._channel
assert updated_channel is not first_channel
assert isinstance(
client.transport._logged_channel._interceptor, GapicInterceptor
)
assert updated_channel._interceptor == client._metrics_interceptor
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutation_set_cell(self, target, temp_rows):
"""Ensure cells can be set properly"""
row_key = b"bulk_mutate"
new_value = uuid.uuid4().hex.encode()
row_key, mutation = temp_rows.create_row_and_mutation(
target, new_value=new_value
)
target.mutate_row(row_key, mutation)
assert temp_rows.retrieve_cell_value(target, row_key) == new_value
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutation_add_to_cell(self, target, temp_rows):
"""Test add to cell mutation"""
from google.cloud.bigtable.data.mutations import AddToCell
row_key = b"add_to_cell"
family = TEST_AGGREGATE_FAMILY
qualifier = b"test-qualifier"
temp_rows.add_aggregate_row(row_key, family=family, qualifier=qualifier)
target.mutate_row(row_key, AddToCell(family, qualifier, 1, timestamp_micros=0))
encoded_result = temp_rows.retrieve_cell_value(target, row_key)
int_result = int.from_bytes(encoded_result, byteorder="big")
assert int_result == 1
target.mutate_row(row_key, AddToCell(family, qualifier, 9, timestamp_micros=0))
encoded_result = temp_rows.retrieve_cell_value(target, row_key)
int_result = int.from_bytes(encoded_result, byteorder="big")
assert int_result == 10
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)), reason="emulator doesn't use splits"
)
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_sample_row_keys(self, client, target, temp_rows, column_split_config):
"""Sample keys should return a single sample in small test targets"""
temp_rows.add_row(b"row_key_1")
temp_rows.add_row(b"row_key_2")
results = target.sample_row_keys()
assert len(results) == len(column_split_config) + 1
for idx in range(len(column_split_config)):
assert results[idx][0] == column_split_config[idx]
assert isinstance(results[idx][1], int)
assert results[-1][0] == b""
assert isinstance(results[-1][1], int)
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)), reason="emulator doesn't use splits"
)
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_sample_row_keys_w_row_range(self, client, target, column_split_config):
"""Sample keys with row range should return samples within the range,
with the last key matching the end of the range."""
if len(column_split_config) < 4:
pytest.skip("Not enough splits in column_split_config for this test")
from google.cloud.bigtable.data import RowRange
start_key = column_split_config[1]
end_key = column_split_config[3]
row_range = RowRange(start_key=start_key, end_key=end_key)
results = target.sample_row_keys(row_range=row_range)
assert len(results) == 2
assert results[0][0] == column_split_config[2]
assert results[1][0] == column_split_config[3]
for _, offset in results:
assert isinstance(offset, int)
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
def test_bulk_mutations_set_cell(self, client, target, temp_rows):
"""Ensure cells can be set properly"""
from google.cloud.bigtable.data.mutations import RowMutationEntry
new_value = uuid.uuid4().hex.encode()
row_key, mutation = temp_rows.create_row_and_mutation(
target, new_value=new_value
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
target.bulk_mutate_rows([bulk_mutation])
assert temp_rows.retrieve_cell_value(target, row_key) == new_value
def test_bulk_mutations_raise_exception(self, client, target):
"""If an invalid mutation is passed, an exception should be raised"""
from google.cloud.bigtable.data.exceptions import (
FailedMutationEntryError,
MutationsExceptionGroup,
)
from google.cloud.bigtable.data.mutations import RowMutationEntry, SetCell
row_key = uuid.uuid4().hex.encode()
mutation = SetCell(
family="nonexistent", qualifier=b"test-qualifier", new_value=b""
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
with pytest.raises(MutationsExceptionGroup) as exc:
target.bulk_mutate_rows([bulk_mutation])
assert len(exc.value.exceptions) == 1
entry_error = exc.value.exceptions[0]
assert isinstance(entry_error, FailedMutationEntryError)
assert entry_error.index == 0
assert entry_error.entry == bulk_mutation
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutations_batcher_context_manager(self, client, target, temp_rows):
"""test batcher with context manager. Should flush on exit"""
from google.cloud.bigtable.data.mutations import RowMutationEntry
new_value, new_value2 = [uuid.uuid4().hex.encode() for _ in range(2)]
row_key, mutation = temp_rows.create_row_and_mutation(
target, new_value=new_value
)
row_key2, mutation2 = temp_rows.create_row_and_mutation(
target, new_value=new_value2
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
bulk_mutation2 = RowMutationEntry(row_key2, [mutation2])
with target.mutations_batcher() as batcher:
batcher.append(bulk_mutation)
batcher.append(bulk_mutation2)
assert temp_rows.retrieve_cell_value(target, row_key) == new_value
assert len(batcher._staged_entries) == 0
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutations_batcher_timer_flush(self, client, target, temp_rows):
"""batch should occur after flush_interval seconds"""
from google.cloud.bigtable.data.mutations import RowMutationEntry
new_value = uuid.uuid4().hex.encode()
row_key, mutation = temp_rows.create_row_and_mutation(
target, new_value=new_value
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
flush_interval = 0.1
with target.mutations_batcher(flush_interval=flush_interval) as batcher:
batcher.append(bulk_mutation)
CrossSync._Sync_Impl.yield_to_event_loop()
assert len(batcher._staged_entries) == 1
CrossSync._Sync_Impl.sleep(flush_interval + 0.1)
assert len(batcher._staged_entries) == 0
assert temp_rows.retrieve_cell_value(target, row_key) == new_value
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutations_batcher_count_flush(self, client, target, temp_rows):
"""batch should flush after flush_limit_mutation_count mutations"""
from google.cloud.bigtable.data.mutations import RowMutationEntry
new_value, new_value2 = [uuid.uuid4().hex.encode() for _ in range(2)]
row_key, mutation = temp_rows.create_row_and_mutation(
target, new_value=new_value
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
row_key2, mutation2 = temp_rows.create_row_and_mutation(
target, new_value=new_value2
)
bulk_mutation2 = RowMutationEntry(row_key2, [mutation2])
with target.mutations_batcher(flush_limit_mutation_count=2) as batcher:
batcher.append(bulk_mutation)
assert len(batcher._flush_jobs) == 0
assert len(batcher._staged_entries) == 1
batcher.append(bulk_mutation2)
assert len(batcher._flush_jobs) == 1
for future in list(batcher._flush_jobs):
future
future.result()
assert len(batcher._staged_entries) == 0
assert len(batcher._flush_jobs) == 0
assert temp_rows.retrieve_cell_value(target, row_key) == new_value
assert temp_rows.retrieve_cell_value(target, row_key2) == new_value2
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutations_batcher_bytes_flush(self, client, target, temp_rows):
"""batch should flush after flush_limit_bytes bytes"""
from google.cloud.bigtable.data.mutations import RowMutationEntry
new_value, new_value2 = [uuid.uuid4().hex.encode() for _ in range(2)]
row_key, mutation = temp_rows.create_row_and_mutation(
target, new_value=new_value
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
row_key2, mutation2 = temp_rows.create_row_and_mutation(
target, new_value=new_value2
)
bulk_mutation2 = RowMutationEntry(row_key2, [mutation2])
flush_limit = bulk_mutation.size() + bulk_mutation2.size() - 1
with target.mutations_batcher(flush_limit_bytes=flush_limit) as batcher:
batcher.append(bulk_mutation)
assert len(batcher._flush_jobs) == 0
assert len(batcher._staged_entries) == 1
batcher.append(bulk_mutation2)
assert len(batcher._flush_jobs) == 1
assert len(batcher._staged_entries) == 0
for future in list(batcher._flush_jobs):
future
future.result()
assert temp_rows.retrieve_cell_value(target, row_key) == new_value
assert temp_rows.retrieve_cell_value(target, row_key2) == new_value2
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
def test_mutations_batcher_no_flush(self, client, target, temp_rows):
"""test with no flush requirements met"""
from google.cloud.bigtable.data.mutations import RowMutationEntry
new_value = uuid.uuid4().hex.encode()
start_value = b"unchanged"
row_key, mutation = temp_rows.create_row_and_mutation(
target, start_value=start_value, new_value=new_value
)
bulk_mutation = RowMutationEntry(row_key, [mutation])
row_key2, mutation2 = temp_rows.create_row_and_mutation(
target, start_value=start_value, new_value=new_value
)
bulk_mutation2 = RowMutationEntry(row_key2, [mutation2])
size_limit = bulk_mutation.size() + bulk_mutation2.size() + 1
with target.mutations_batcher(
flush_limit_bytes=size_limit, flush_limit_mutation_count=3, flush_interval=1
) as batcher:
batcher.append(bulk_mutation)
assert len(batcher._staged_entries) == 1
batcher.append(bulk_mutation2)
assert len(batcher._flush_jobs) == 0
CrossSync._Sync_Impl.yield_to_event_loop()
assert len(batcher._staged_entries) == 2
assert len(batcher._flush_jobs) == 0
assert temp_rows.retrieve_cell_value(target, row_key) == start_value
assert temp_rows.retrieve_cell_value(target, row_key2) == start_value
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_mutations_batcher_large_batch(self, client, target, temp_rows):
"""test batcher with large batch of mutations"""
from google.cloud.bigtable.data.mutations import RowMutationEntry, SetCell
add_mutation = SetCell(
family=TEST_FAMILY, qualifier=b"test-qualifier", new_value=b"a"
)
row_mutations = []
for i in range(50000):
row_key = uuid.uuid4().hex.encode()
row_mutations.append(RowMutationEntry(row_key, [add_mutation]))
temp_rows.rows.append(row_key)
with target.mutations_batcher() as batcher:
for mutation in row_mutations:
batcher.append(mutation)
assert len(batcher._staged_entries) == 0
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@pytest.mark.parametrize(
"start,increment,expected",
[
(0, 0, 0),
(0, 1, 1),
(0, -1, -1),
(1, 0, 1),
(0, -100, -100),
(0, 3000, 3000),
(10, 4, 14),
(_MAX_INCREMENT_VALUE, -_MAX_INCREMENT_VALUE, 0),
(_MAX_INCREMENT_VALUE, 2, -_MAX_INCREMENT_VALUE),
(-_MAX_INCREMENT_VALUE, -2, _MAX_INCREMENT_VALUE),
],
)
def test_read_modify_write_row_increment(
self, client, target, temp_rows, start, increment, expected
):
"""test read_modify_write_row"""
from google.cloud.bigtable.data.read_modify_write_rules import IncrementRule
row_key = b"test-row-key"
family = TEST_FAMILY
qualifier = b"test-qualifier"
temp_rows.add_row(row_key, value=start, family=family, qualifier=qualifier)
rule = IncrementRule(family, qualifier, increment)
result = target.read_modify_write_row(row_key, rule)
assert result.row_key == row_key
assert len(result) == 1
assert result[0].family == family
assert result[0].qualifier == qualifier
assert int(result[0]) == expected
assert temp_rows.retrieve_cell_value(target, row_key) == result[0].value
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@pytest.mark.parametrize(
"start,append,expected",
[
(b"", b"", b""),
("", "", b""),
(b"abc", b"123", b"abc123"),
(b"abc", "123", b"abc123"),
("", b"1", b"1"),
(b"abc", "", b"abc"),
(b"hello", b"world", b"helloworld"),
],
)
def test_read_modify_write_row_append(
self, client, target, temp_rows, start, append, expected
):
"""test read_modify_write_row"""
from google.cloud.bigtable.data.read_modify_write_rules import AppendValueRule
row_key = b"test-row-key"
family = TEST_FAMILY
qualifier = b"test-qualifier"
temp_rows.add_row(row_key, value=start, family=family, qualifier=qualifier)
rule = AppendValueRule(family, qualifier, append)
result = target.read_modify_write_row(row_key, rule)
assert result.row_key == row_key
assert len(result) == 1
assert result[0].family == family
assert result[0].qualifier == qualifier
assert result[0].value == expected
assert temp_rows.retrieve_cell_value(target, row_key) == result[0].value
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
def test_read_modify_write_row_chained(self, client, target, temp_rows):
"""test read_modify_write_row with multiple rules"""
from google.cloud.bigtable.data.read_modify_write_rules import (
AppendValueRule,
IncrementRule,
)
row_key = b"test-row-key"
family = TEST_FAMILY
qualifier = b"test-qualifier"
start_amount = 1
increment_amount = 10
temp_rows.add_row(
row_key, value=start_amount, family=family, qualifier=qualifier
)
rule = [
IncrementRule(family, qualifier, increment_amount),
AppendValueRule(family, qualifier, "hello"),
AppendValueRule(family, qualifier, "world"),
AppendValueRule(family, qualifier, "!"),
]
result = target.read_modify_write_row(row_key, rule)
assert result.row_key == row_key
assert result[0].family == family
assert result[0].qualifier == qualifier
assert (
result[0].value
== (start_amount + increment_amount).to_bytes(8, "big", signed=True)
+ b"helloworld!"
)
assert temp_rows.retrieve_cell_value(target, row_key) == result[0].value
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
@pytest.mark.parametrize(
"start_val,predicate_range,expected_result",
[(1, (0, 2), True), (-1, (0, 2), False)],
)
def test_check_and_mutate(
self, client, target, temp_rows, start_val, predicate_range, expected_result
):
"""test that check_and_mutate_row works applies the right mutations, and returns the right result"""
from google.cloud.bigtable.data.mutations import SetCell
from google.cloud.bigtable.data.row_filters import ValueRangeFilter
row_key = b"test-row-key"
family = TEST_FAMILY
qualifier = b"test-qualifier"
temp_rows.add_row(row_key, value=start_val, family=family, qualifier=qualifier)
false_mutation_value = b"false-mutation-value"
false_mutation = SetCell(
family=TEST_FAMILY, qualifier=qualifier, new_value=false_mutation_value
)
true_mutation_value = b"true-mutation-value"
true_mutation = SetCell(
family=TEST_FAMILY, qualifier=qualifier, new_value=true_mutation_value
)
predicate = ValueRangeFilter(predicate_range[0], predicate_range[1])
result = target.check_and_mutate_row(
row_key,
predicate,
true_case_mutations=true_mutation,
false_case_mutations=false_mutation,
)
assert result == expected_result
expected_value = (
true_mutation_value if expected_result else false_mutation_value
)
assert temp_rows.retrieve_cell_value(target, row_key) == expected_value
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)),
reason="emulator doesn't raise InvalidArgument",
)
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("target")
def test_check_and_mutate_empty_request(self, client, target):
"""check_and_mutate with no true or fale mutations should raise an error"""
from google.api_core import exceptions
with pytest.raises(exceptions.InvalidArgument) as e:
target.check_and_mutate_row(
b"row_key", None, true_case_mutations=None, false_case_mutations=None
)
assert "No mutations provided" in str(e.value)
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_stream(self, target, temp_rows):
"""Ensure that the read_rows_stream method works"""
temp_rows.add_row(b"row_key_1")
temp_rows.add_row(b"row_key_2")
generator = target.read_rows_stream({})
first_row = generator.__next__()
second_row = generator.__next__()
assert first_row.row_key == b"row_key_1"
assert second_row.row_key == b"row_key_2"
with pytest.raises(CrossSync._Sync_Impl.StopIteration):
generator.__next__()
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows(self, target, temp_rows):
"""Ensure that the read_rows method works"""
temp_rows.add_row(b"row_key_1")
temp_rows.add_row(b"row_key_2")
row_list = target.read_rows({})
assert len(row_list) == 2
assert row_list[0].row_key == b"row_key_1"
assert row_list[1].row_key == b"row_key_2"
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_sharded_simple(self, target, temp_rows):
"""Test read rows sharded with two queries"""
from google.cloud.bigtable.data.read_rows_query import ReadRowsQuery
temp_rows.add_row(b"a")
temp_rows.add_row(b"b")
temp_rows.add_row(b"c")
temp_rows.add_row(b"d")
query1 = ReadRowsQuery(row_keys=[b"a", b"c"])
query2 = ReadRowsQuery(row_keys=[b"b", b"d"])
row_list = target.read_rows_sharded([query1, query2])
assert len(row_list) == 4
assert row_list[0].row_key == b"a"
assert row_list[1].row_key == b"c"
assert row_list[2].row_key == b"b"
assert row_list[3].row_key == b"d"
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_sharded_from_sample(self, target, temp_rows):
"""Test end-to-end sharding"""
from google.cloud.bigtable.data.read_rows_query import ReadRowsQuery, RowRange
temp_rows.add_row(b"a")
temp_rows.add_row(b"b")
temp_rows.add_row(b"c")
temp_rows.add_row(b"d")
table_shard_keys = target.sample_row_keys()
query = ReadRowsQuery(row_ranges=[RowRange(start_key=b"b", end_key=b"z")])
shard_queries = query.shard(table_shard_keys)
row_list = target.read_rows_sharded(shard_queries)
assert len(row_list) == 3
assert row_list[0].row_key == b"b"
assert row_list[1].row_key == b"c"
assert row_list[2].row_key == b"d"
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_sharded_filters_limits(self, target, temp_rows):
"""Test read rows sharded with filters and limits"""
from google.cloud.bigtable.data.read_rows_query import ReadRowsQuery
from google.cloud.bigtable.data.row_filters import ApplyLabelFilter
temp_rows.add_row(b"a")
temp_rows.add_row(b"b")
temp_rows.add_row(b"c")
temp_rows.add_row(b"d")
label_filter1 = ApplyLabelFilter("first")
label_filter2 = ApplyLabelFilter("second")
query1 = ReadRowsQuery(row_keys=[b"a", b"c"], limit=1, row_filter=label_filter1)
query2 = ReadRowsQuery(row_keys=[b"b", b"d"], row_filter=label_filter2)
row_list = target.read_rows_sharded([query1, query2])
assert len(row_list) == 3
assert row_list[0].row_key == b"a"
assert row_list[1].row_key == b"b"
assert row_list[2].row_key == b"d"
assert row_list[0][0].labels == ["first"]
assert row_list[1][0].labels == ["second"]
assert row_list[2][0].labels == ["second"]
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_range_query(self, target, temp_rows):
"""Ensure that the read_rows method works"""
from google.cloud.bigtable.data import ReadRowsQuery, RowRange
temp_rows.add_row(b"a")
temp_rows.add_row(b"b")
temp_rows.add_row(b"c")
temp_rows.add_row(b"d")
query = ReadRowsQuery(row_ranges=RowRange(start_key=b"b", end_key=b"d"))
row_list = target.read_rows(query)
assert len(row_list) == 2
assert row_list[0].row_key == b"b"
assert row_list[1].row_key == b"c"
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_single_key_query(self, target, temp_rows):
"""Ensure that the read_rows method works with specified query"""
from google.cloud.bigtable.data import ReadRowsQuery
temp_rows.add_row(b"a")
temp_rows.add_row(b"b")
temp_rows.add_row(b"c")
temp_rows.add_row(b"d")
query = ReadRowsQuery(row_keys=[b"a", b"c"])
row_list = target.read_rows(query)
assert len(row_list) == 2
assert row_list[0].row_key == b"a"
assert row_list[1].row_key == b"c"
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_read_rows_with_filter(self, target, temp_rows):
"""ensure filters are applied"""
from google.cloud.bigtable.data import ReadRowsQuery
from google.cloud.bigtable.data.row_filters import ApplyLabelFilter
temp_rows.add_row(b"a")
temp_rows.add_row(b"b")
temp_rows.add_row(b"c")
temp_rows.add_row(b"d")
expected_label = "test-label"
row_filter = ApplyLabelFilter(expected_label)
query = ReadRowsQuery(row_filter=row_filter)
row_list = target.read_rows(query)
assert len(row_list) == 4
for row in row_list:
assert row[0].labels == [expected_label]
@pytest.mark.usefixtures("target")
def test_read_rows_stream_close(self, target, temp_rows):
"""Ensure that the read_rows_stream can be closed"""
from google.cloud.bigtable.data import ReadRowsQuery
temp_rows.add_row(b"row_key_1")
temp_rows.add_row(b"row_key_2")
query = ReadRowsQuery()
generator = target.read_rows_stream(query)
first_row = generator.__next__()
assert first_row.row_key == b"row_key_1"
generator.close()
with pytest.raises(CrossSync._Sync_Impl.StopIteration):
generator.__next__()
@pytest.mark.usefixtures("target")
def test_read_row(self, target, temp_rows):
"""Test read_row (single row helper)"""
from google.cloud.bigtable.data import Row
temp_rows.add_row(b"row_key_1", value=b"value")
row = target.read_row(b"row_key_1")
assert isinstance(row, Row)
assert row.row_key == b"row_key_1"
assert row.cells[0].value == b"value"
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)),
reason="emulator doesn't raise InvalidArgument",
)
@pytest.mark.usefixtures("target")
def test_read_row_missing(self, target):
"""Test read_row when row does not exist"""
from google.api_core import exceptions
row_key = "row_key_not_exist"
result = target.read_row(row_key)
assert result is None
with pytest.raises(exceptions.InvalidArgument) as e:
target.read_row("")
assert "Row keys must be non-empty" in str(e)
@pytest.mark.usefixtures("target")
def test_read_row_w_filter(self, target, temp_rows):
"""Test read_row (single row helper)"""
from google.cloud.bigtable.data import Row
from google.cloud.bigtable.data.row_filters import ApplyLabelFilter
temp_rows.add_row(b"row_key_1", value=b"value")
expected_label = "test-label"
label_filter = ApplyLabelFilter(expected_label)
row = target.read_row(b"row_key_1", row_filter=label_filter)
assert isinstance(row, Row)
assert row.row_key == b"row_key_1"
assert row.cells[0].value == b"value"
assert row.cells[0].labels == [expected_label]
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)),
reason="emulator doesn't raise InvalidArgument",
)
@pytest.mark.usefixtures("target")
def test_row_exists(self, target, temp_rows):
from google.api_core import exceptions
"Test row_exists with rows that exist and don't exist"
assert target.row_exists(b"row_key_1") is False
temp_rows.add_row(b"row_key_1")
assert target.row_exists(b"row_key_1") is True
assert target.row_exists("row_key_1") is True
assert target.row_exists(b"row_key_2") is False
assert target.row_exists("row_key_2") is False
assert target.row_exists("3") is False
temp_rows.add_row(b"3")
assert target.row_exists(b"3") is True
with pytest.raises(exceptions.InvalidArgument) as e:
target.row_exists("")
assert "Row keys must be non-empty" in str(e)
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
@pytest.mark.parametrize(
"cell_value,filter_input,expect_match",
[
(b"abc", b"abc", True),
(b"abc", "abc", True),
(b".", ".", True),
(".*", ".*", True),
(".*", b".*", True),
("a", ".*", False),
(b".*", b".*", True),
("\\a", "\\a", True),
(b"\xe2\x98\x83", "☃", True),
("☃", "☃", True),
("\\C☃", "\\C☃", True),
(1, 1, True),
(2, 1, False),
(68, 68, True),
("D", 68, False),
(68, "D", False),
(-1, -1, True),
(2852126720, 2852126720, True),
(-1431655766, -1431655766, True),
(-1431655766, -1, False),
],
)
def test_literal_value_filter(
self, target, temp_rows, cell_value, filter_input, expect_match
):
"""Literal value filter does complex escaping on re2 strings.
Make sure inputs are properly interpreted by the server"""
from google.cloud.bigtable.data import ReadRowsQuery
from google.cloud.bigtable.data.row_filters import LiteralValueFilter
f = LiteralValueFilter(filter_input)
temp_rows.add_row(b"row_key_1", value=cell_value)
query = ReadRowsQuery(row_filter=f)
row_list = target.read_rows(query)
assert len(row_list) == bool(expect_match), (
f"row {type(cell_value)}({cell_value}) not found with {type(filter_input)}({filter_input}) filter"
)
@pytest.mark.usefixtures("target")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
@pytest.mark.parametrize(
"cell_value,mask,expect_match",
[
(b"\x01\x02\x03", b"\x01\x02\x03", True),
(b"\x01\x02\x03", b"\x01\x00\x00", True),
(b"\x00\x02\x03", b"\x01\x00\x00", False),
],
)
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)),
reason="value_bitmask_filter not supported by emulator",
)
def test_value_bitmask_filter(
self, target, temp_rows, cell_value, mask, expect_match
):
"""ValueBitmaskFilter matches cells where (value & mask) == mask.
Make sure inputs are properly interpreted by the server."""
from google.cloud.bigtable.data import ReadRowsQuery
from google.cloud.bigtable.data.row_filters import ValueBitmaskFilter
f = ValueBitmaskFilter(mask)
temp_rows.add_row(b"row_key_1", value=cell_value)
query = ReadRowsQuery(row_keys=[b"row_key_1"], row_filter=f)
row_list = target.read_rows(query)
assert len(row_list) == bool(expect_match), (
f"row {cell_value!r} not matched as {expect_match} with {mask!r} bitmask filter"
)
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)), reason="emulator doesn't support SQL"
)
def test_authorized_view_unauthenticated(
self, client, authorized_view_id, instance_id, table_id
):
"""Requesting family outside authorized family_subset should raise exception"""
from google.cloud.bigtable.data.mutations import SetCell
with client.get_authorized_view(
instance_id, table_id, authorized_view_id
) as view:
mutation = SetCell(family="unauthorized", qualifier="q", new_value="v")
with pytest.raises(PermissionDenied) as e:
view.mutate_row(b"row-key", mutation)
assert "outside the Authorized View" in e.value.message
@pytest.mark.skipif(
bool(os.environ.get(BIGTABLE_EMULATOR)), reason="emulator doesn't support SQL"
)
@pytest.mark.usefixtures("client")
@CrossSync._Sync_Impl.Retry(
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
)
def test_execute_query_simple(self, client, table_id, instance_id):
result = client.execute_query("SELECT 1 AS a, 'foo' AS b", instance_id)
rows = [r for r in result]
assert len(rows) == 1
row = rows[0]
assert row["a"] == 1
assert row["b"] == "foo"
@pytest.mark.skipif(