Skip to content
Prev Previous commit
Next Next commit
remove dicts from check_and_mutate
  • Loading branch information
daniel-sanche committed Oct 16, 2023
commit 57d65efc0337355d5c13c29beff2d698d532adee
21 changes: 8 additions & 13 deletions google/cloud/bigtable/data/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,29 +1093,24 @@ async def check_and_mutate_row(
operation_timeout = operation_timeout or self.default_operation_timeout
if operation_timeout <= 0:
raise ValueError("operation_timeout must be greater than 0")
row_key = row_key.encode("utf-8") if isinstance(row_key, str) else row_key
if true_case_mutations is not None and not isinstance(
true_case_mutations, list
):
true_case_mutations = [true_case_mutations]
true_case_dict = [m._to_dict() for m in true_case_mutations or []]
true_case_list = [m._to_pb() for m in true_case_mutations or []]
if false_case_mutations is not None and not isinstance(
false_case_mutations, list
):
false_case_mutations = [false_case_mutations]
false_case_dict = [m._to_dict() for m in false_case_mutations or []]
false_case_list = [m._to_pb() for m in false_case_mutations or []]
metadata = _make_metadata(self.table_name, self.app_profile_id)
result = await self.client._gapic_client.check_and_mutate_row(
request={
"predicate_filter": predicate._to_dict()
if predicate is not None
else None,
"true_mutations": true_case_dict,
"false_mutations": false_case_dict,
"table_name": self.table_name,
"row_key": row_key,
"app_profile_id": self.app_profile_id,
},
true_mutations=true_case_list,
false_mutations=false_case_list,
predicate_filter=predicate._to_pb() if predicate is not None else None,
row_key=row_key.encode("utf-8") if isinstance(row_key, str) else row_key,
table_name=self.table_name,
app_profile_id=self.app_profile_id,
metadata=metadata,
timeout=operation_timeout,
)
Expand Down
48 changes: 23 additions & 25 deletions tests/unit/data/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2582,17 +2582,16 @@ async def test_check_and_mutate(self, gapic_result):
)
assert found == gapic_result
kwargs = mock_gapic.call_args[1]
request = kwargs["request"]
assert request["table_name"] == table.table_name
assert request["row_key"] == row_key
assert request["predicate_filter"] == predicate
assert request["true_mutations"] == [
m._to_dict() for m in true_mutations
assert kwargs["table_name"] == table.table_name
assert kwargs["row_key"] == row_key
assert kwargs["predicate_filter"] == predicate
assert kwargs["true_mutations"] == [
m._to_pb() for m in true_mutations
]
assert request["false_mutations"] == [
m._to_dict() for m in false_mutations
assert kwargs["false_mutations"] == [
m._to_pb() for m in false_mutations
]
assert request["app_profile_id"] == app_profile
assert kwargs["app_profile_id"] == app_profile
assert kwargs["timeout"] == operation_timeout

@pytest.mark.asyncio
Expand Down Expand Up @@ -2649,18 +2648,17 @@ async def test_check_and_mutate_single_mutations(self):
false_case_mutations=false_mutation,
)
kwargs = mock_gapic.call_args[1]
request = kwargs["request"]
assert request["true_mutations"] == [true_mutation._to_dict()]
assert request["false_mutations"] == [false_mutation._to_dict()]
assert kwargs["true_mutations"] == [true_mutation._to_pb()]
assert kwargs["false_mutations"] == [false_mutation._to_pb()]

@pytest.mark.asyncio
async def test_check_and_mutate_predicate_object(self):
"""predicate filter should be passed to gapic request"""
from google.cloud.bigtable_v2.types import CheckAndMutateRowResponse

mock_predicate = mock.Mock()
predicate_dict = {"predicate": "dict"}
mock_predicate._to_dict.return_value = predicate_dict
predicate_pb = {"predicate": "dict"}
mock_predicate._to_pb.return_value = predicate_pb
async with self._make_client() as client:
async with client.get_table("instance", "table") as table:
with mock.patch.object(
Expand All @@ -2675,18 +2673,18 @@ async def test_check_and_mutate_predicate_object(self):
false_case_mutations=[mock.Mock()],
)
kwargs = mock_gapic.call_args[1]
assert kwargs["request"]["predicate_filter"] == predicate_dict
assert mock_predicate._to_dict.call_count == 1
assert kwargs["predicate_filter"] == predicate_pb
assert mock_predicate._to_pb.call_count == 1

@pytest.mark.asyncio
async def test_check_and_mutate_mutations_parsing(self):
"""mutations objects should be converted to dicts"""
"""mutations objects should be converted to protos"""
from google.cloud.bigtable_v2.types import CheckAndMutateRowResponse
from google.cloud.bigtable.data.mutations import DeleteAllFromRow

mutations = [mock.Mock() for _ in range(5)]
for idx, mutation in enumerate(mutations):
mutation._to_dict.return_value = {"fake": idx}
mutation._to_pb.return_value = f"fake {idx}"
mutations.append(DeleteAllFromRow())
async with self._make_client() as client:
async with client.get_table("instance", "table") as table:
Expand All @@ -2702,16 +2700,16 @@ async def test_check_and_mutate_mutations_parsing(self):
true_case_mutations=mutations[0:2],
false_case_mutations=mutations[2:],
)
kwargs = mock_gapic.call_args[1]["request"]
assert kwargs["true_mutations"] == [{"fake": 0}, {"fake": 1}]
kwargs = mock_gapic.call_args[1]
assert kwargs["true_mutations"] == ["fake 0", "fake 1"]
assert kwargs["false_mutations"] == [
{"fake": 2},
{"fake": 3},
{"fake": 4},
{"delete_from_row": {}},
"fake 2",
"fake 3",
"fake 4",
DeleteAllFromRow()._to_pb(),
]
assert all(
mutation._to_dict.call_count == 1 for mutation in mutations[:5]
mutation._to_pb.call_count == 1 for mutation in mutations[:5]
)

@pytest.mark.parametrize("include_app_profile", [True, False])
Expand Down