Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 96f5375

Browse files
daniel-sancheaxyjo
authored andcommitted
added system tests for AddToCell
1 parent b8c6f24 commit 96f5375

4 files changed

Lines changed: 116 additions & 5 deletions

File tree

tests/system/data/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
TEST_FAMILY = "test-family"
1818
TEST_FAMILY_2 = "test-family-2"
19+
TEST_AGGREGATE_FAMILY = "test-aggregate-family"

tests/system/data/setup_fixtures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import os
2121
import uuid
2222

23-
from . import TEST_FAMILY, TEST_FAMILY_2
23+
from . import TEST_FAMILY, TEST_FAMILY_2, TEST_AGGREGATE_FAMILY
2424

2525
# authorized view subset to allow all qualifiers
2626
ALLOW_ALL = ""
@@ -183,6 +183,7 @@ def authorized_view_id(
183183
"family_subsets": {
184184
TEST_FAMILY: ALL_QUALIFIERS,
185185
TEST_FAMILY_2: ALL_QUALIFIERS,
186+
TEST_AGGREGATE_FAMILY: ALL_QUALIFIERS,
186187
},
187188
},
188189
},

tests/system/data/test_system_async.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from google.cloud.bigtable.data._cross_sync import CrossSync
2929

30-
from . import TEST_FAMILY, TEST_FAMILY_2
30+
from . import TEST_FAMILY, TEST_FAMILY_2, TEST_AGGREGATE_FAMILY
3131

3232

3333
__CROSS_SYNC_OUTPUT__ = "tests.system.data.test_system_autogen"
@@ -76,6 +76,27 @@ async def add_row(
7676
await self.target.client._gapic_client.mutate_row(request)
7777
self.rows.append(row_key)
7878

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+
79100
@CrossSync.convert
80101
async def delete_rows(self):
81102
if self.rows:
@@ -132,7 +153,17 @@ def column_family_config(self):
132153
"""
133154
from google.cloud.bigtable_admin_v2 import types
134155

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+
}
136167

137168
@pytest.fixture(scope="session")
138169
def init_table_id(self):
@@ -281,6 +312,33 @@ async def test_mutation_set_cell(self, target, temp_rows):
281312
# ensure cell is updated
282313
assert (await self._retrieve_cell_value(target, row_key)) == new_value
283314

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, DeleteAllFromFamily
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(row_key, AddToCell(family, qualifier, 1, timestamp=0))
333+
encoded_result = await self._retrieve_cell_value(target, row_key)
334+
int_result = int.from_bytes(encoded_result, byteorder="big")
335+
assert int_result == 1
336+
# update again
337+
await target.mutate_row(row_key, AddToCell(family, qualifier, 9, timestamp=0))
338+
encoded_result = await self._retrieve_cell_value(target, row_key)
339+
int_result = int.from_bytes(encoded_result, byteorder="big")
340+
assert int_result == 10
341+
284342
@pytest.mark.skipif(
285343
bool(os.environ.get(BIGTABLE_EMULATOR)), reason="emulator doesn't use splits"
286344
)

tests/system/data/test_system_autogen.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from google.cloud.environment_vars import BIGTABLE_EMULATOR
2727
from google.type import date_pb2
2828
from google.cloud.bigtable.data._cross_sync import CrossSync
29-
from . import TEST_FAMILY, TEST_FAMILY_2
29+
from . import TEST_FAMILY, TEST_FAMILY_2, TEST_AGGREGATE_FAMILY
3030

3131
TARGETS = ["table"]
3232
if not os.environ.get(BIGTABLE_EMULATOR):
@@ -66,6 +66,26 @@ def add_row(
6666
self.target.client._gapic_client.mutate_row(request)
6767
self.rows.append(row_key)
6868

69+
def add_aggregate_row(
70+
self, row_key, *, family=TEST_AGGREGATE_FAMILY, qualifier=b"q", input=0
71+
):
72+
request = {
73+
"table_name": self.target.table_name,
74+
"row_key": row_key,
75+
"mutations": [
76+
{
77+
"add_to_cell": {
78+
"family_name": family,
79+
"column_qualifier": {"raw_value": qualifier},
80+
"timestamp": {"raw_timestamp_micros": 0},
81+
"input": {"int_value": input},
82+
}
83+
}
84+
],
85+
}
86+
self.target.client._gapic_client.mutate_row(request)
87+
self.rows.append(row_key)
88+
6989
def delete_rows(self):
7090
if self.rows:
7191
request = {
@@ -106,7 +126,17 @@ def column_family_config(self):
106126
"""specify column families to create when creating a new test table"""
107127
from google.cloud.bigtable_admin_v2 import types
108128

109-
return {TEST_FAMILY: types.ColumnFamily(), TEST_FAMILY_2: types.ColumnFamily()}
129+
int_aggregate_type = types.Type.Aggregate(
130+
input_type=types.Type(int64_type={"encoding": {"big_endian_bytes": {}}}),
131+
sum={},
132+
)
133+
return {
134+
TEST_FAMILY: types.ColumnFamily(),
135+
TEST_FAMILY_2: types.ColumnFamily(),
136+
TEST_AGGREGATE_FAMILY: types.ColumnFamily(
137+
value_type=types.Type(aggregate_type=int_aggregate_type)
138+
),
139+
}
110140

111141
@pytest.fixture(scope="session")
112142
def init_table_id(self):
@@ -225,6 +255,27 @@ def test_mutation_set_cell(self, target, temp_rows):
225255
target.mutate_row(row_key, mutation)
226256
assert self._retrieve_cell_value(target, row_key) == new_value
227257

258+
@pytest.mark.usefixtures("target")
259+
@CrossSync._Sync_Impl.Retry(
260+
predicate=retry.if_exception_type(ClientError), initial=1, maximum=5
261+
)
262+
def test_mutation_add_to_cell(self, target, temp_rows):
263+
"""Test add to cell mutation"""
264+
from google.cloud.bigtable.data.mutations import AddToCell
265+
266+
row_key = b"add_to_cell"
267+
family = TEST_AGGREGATE_FAMILY
268+
qualifier = b"test-qualifier"
269+
temp_rows.add_aggregate_row(row_key, family=family, qualifier=qualifier)
270+
target.mutate_row(row_key, AddToCell(family, qualifier, 1, timestamp=0))
271+
encoded_result = self._retrieve_cell_value(target, row_key)
272+
int_result = int.from_bytes(encoded_result, byteorder="big")
273+
assert int_result == 1
274+
target.mutate_row(row_key, AddToCell(family, qualifier, 9, timestamp=0))
275+
encoded_result = self._retrieve_cell_value(target, row_key)
276+
int_result = int.from_bytes(encoded_result, byteorder="big")
277+
assert int_result == 10
278+
228279
@pytest.mark.skipif(
229280
bool(os.environ.get(BIGTABLE_EMULATOR)), reason="emulator doesn't use splits"
230281
)

0 commit comments

Comments
 (0)