27
27
28
28
from google .cloud .bigtable .data ._cross_sync import CrossSync
29
29
30
- from . import TEST_FAMILY , TEST_FAMILY_2
30
+ from . import TEST_FAMILY , TEST_FAMILY_2 , TEST_AGGREGATE_FAMILY
31
31
32
32
33
33
__CROSS_SYNC_OUTPUT__ = "tests.system.data.test_system_autogen"
@@ -76,6 +76,27 @@ async def add_row(
76
76
await self .target .client ._gapic_client .mutate_row (request )
77
77
self .rows .append (row_key )
78
78
79
+ @CrossSync .convert
80
+ async def add_aggregate_row (
81
+ self , row_key , * , family = TEST_AGGREGATE_FAMILY , qualifier = b"q" , input = 0
82
+ ):
83
+ request = {
84
+ "table_name" : self .target .table_name ,
85
+ "row_key" : row_key ,
86
+ "mutations" : [
87
+ {
88
+ "add_to_cell" : {
89
+ "family_name" : family ,
90
+ "column_qualifier" : {"raw_value" : qualifier },
91
+ "timestamp" : {"raw_timestamp_micros" : 0 },
92
+ "input" : {"int_value" : input },
93
+ }
94
+ }
95
+ ],
96
+ }
97
+ await self .target .client ._gapic_client .mutate_row (request )
98
+ self .rows .append (row_key )
99
+
79
100
@CrossSync .convert
80
101
async def delete_rows (self ):
81
102
if self .rows :
@@ -132,7 +153,17 @@ def column_family_config(self):
132
153
"""
133
154
from google .cloud .bigtable_admin_v2 import types
134
155
135
- return {TEST_FAMILY : types .ColumnFamily (), TEST_FAMILY_2 : types .ColumnFamily ()}
156
+ int_aggregate_type = types .Type .Aggregate (
157
+ input_type = types .Type (int64_type = {"encoding" : {"big_endian_bytes" : {}}}),
158
+ sum = {},
159
+ )
160
+ return {
161
+ TEST_FAMILY : types .ColumnFamily (),
162
+ TEST_FAMILY_2 : types .ColumnFamily (),
163
+ TEST_AGGREGATE_FAMILY : types .ColumnFamily (
164
+ value_type = types .Type (aggregate_type = int_aggregate_type )
165
+ ),
166
+ }
136
167
137
168
@pytest .fixture (scope = "session" )
138
169
def init_table_id (self ):
@@ -281,6 +312,37 @@ async def test_mutation_set_cell(self, target, temp_rows):
281
312
# ensure cell is updated
282
313
assert (await self ._retrieve_cell_value (target , row_key )) == new_value
283
314
315
+ @CrossSync .pytest
316
+ @pytest .mark .usefixtures ("target" )
317
+ @CrossSync .Retry (
318
+ predicate = retry .if_exception_type (ClientError ), initial = 1 , maximum = 5
319
+ )
320
+ async def test_mutation_add_to_cell (self , target , temp_rows ):
321
+ """
322
+ Test add to cell mutation
323
+ """
324
+ from google .cloud .bigtable .data .mutations import AddToCell
325
+
326
+ row_key = b"add_to_cell"
327
+ family = TEST_AGGREGATE_FAMILY
328
+ qualifier = b"test-qualifier"
329
+ # add row to temp_rows, for future deletion
330
+ await temp_rows .add_aggregate_row (row_key , family = family , qualifier = qualifier )
331
+ # set and check cell value
332
+ await target .mutate_row (
333
+ row_key , AddToCell (family , qualifier , 1 , timestamp_micros = 0 )
334
+ )
335
+ encoded_result = await self ._retrieve_cell_value (target , row_key )
336
+ int_result = int .from_bytes (encoded_result , byteorder = "big" )
337
+ assert int_result == 1
338
+ # update again
339
+ await target .mutate_row (
340
+ row_key , AddToCell (family , qualifier , 9 , timestamp_micros = 0 )
341
+ )
342
+ encoded_result = await self ._retrieve_cell_value (target , row_key )
343
+ int_result = int .from_bytes (encoded_result , byteorder = "big" )
344
+ assert int_result == 10
345
+
284
346
@pytest .mark .skipif (
285
347
bool (os .environ .get (BIGTABLE_EMULATOR )), reason = "emulator doesn't use splits"
286
348
)
@@ -1123,7 +1185,7 @@ async def test_execute_query_simple(self, client, table_id, instance_id):
1123
1185
predicate = retry .if_exception_type (ClientError ), initial = 1 , maximum = 5
1124
1186
)
1125
1187
async def test_execute_against_target (
1126
- self , client , instance_id , table_id , temp_rows
1188
+ self , client , instance_id , table_id , temp_rows , column_family_config
1127
1189
):
1128
1190
await temp_rows .add_row (b"row_key_1" )
1129
1191
result = await client .execute_query (
@@ -1138,14 +1200,19 @@ async def test_execute_against_target(
1138
1200
assert family_map [b"q" ] == b"test-value"
1139
1201
assert len (rows [0 ][TEST_FAMILY_2 ]) == 0
1140
1202
md = result .metadata
1141
- assert len (md ) == 3
1203
+ # we expect it to fetch each column family, plus _key
1204
+ # add additional families here if column_family_config changes
1205
+ assert len (md ) == len (column_family_config ) + 1
1142
1206
assert md ["_key" ].column_type == SqlType .Bytes ()
1143
1207
assert md [TEST_FAMILY ].column_type == SqlType .Map (
1144
1208
SqlType .Bytes (), SqlType .Bytes ()
1145
1209
)
1146
1210
assert md [TEST_FAMILY_2 ].column_type == SqlType .Map (
1147
1211
SqlType .Bytes (), SqlType .Bytes ()
1148
1212
)
1213
+ assert md [TEST_AGGREGATE_FAMILY ].column_type == SqlType .Map (
1214
+ SqlType .Bytes (), SqlType .Int64 ()
1215
+ )
1149
1216
1150
1217
@pytest .mark .skipif (
1151
1218
bool (os .environ .get (BIGTABLE_EMULATOR )),
@@ -1248,7 +1315,7 @@ async def test_execute_query_params(self, client, table_id, instance_id):
1248
1315
predicate = retry .if_exception_type (ClientError ), initial = 1 , maximum = 5
1249
1316
)
1250
1317
async def test_execute_metadata_on_empty_response (
1251
- self , client , instance_id , table_id , temp_rows
1318
+ self , client , instance_id , table_id , temp_rows , column_family_config
1252
1319
):
1253
1320
await temp_rows .add_row (b"row_key_1" )
1254
1321
result = await client .execute_query (
@@ -1258,11 +1325,16 @@ async def test_execute_metadata_on_empty_response(
1258
1325
1259
1326
assert len (rows ) == 0
1260
1327
md = result .metadata
1261
- assert len (md ) == 3
1328
+ # we expect it to fetch each column family, plus _key
1329
+ # add additional families here if column_family_config change
1330
+ assert len (md ) == len (column_family_config ) + 1
1262
1331
assert md ["_key" ].column_type == SqlType .Bytes ()
1263
1332
assert md [TEST_FAMILY ].column_type == SqlType .Map (
1264
1333
SqlType .Bytes (), SqlType .Bytes ()
1265
1334
)
1266
1335
assert md [TEST_FAMILY_2 ].column_type == SqlType .Map (
1267
1336
SqlType .Bytes (), SqlType .Bytes ()
1268
1337
)
1338
+ assert md [TEST_AGGREGATE_FAMILY ].column_type == SqlType .Map (
1339
+ SqlType .Bytes (), SqlType .Int64 ()
1340
+ )
0 commit comments