Skip to content

Commit

Permalink
added tests for new _to_pb methods
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Oct 17, 2023
1 parent fcfcc1d commit d98cf46
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
84 changes: 84 additions & 0 deletions tests/unit/data/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,42 @@ def test__to_dict_server_timestamp(self):
assert got_inner_dict["value"] == expected_value
assert len(got_inner_dict.keys()) == 4

def test__to_pb(self):
"""ensure proto representation is as expected"""
import google.cloud.bigtable_v2.types.data as data_pb

expected_family = "test-family"
expected_qualifier = b"test-qualifier"
expected_value = b"test-value"
expected_timestamp = 123456789
instance = self._make_one(
expected_family, expected_qualifier, expected_value, expected_timestamp
)
got_pb = instance._to_pb()
assert isinstance(got_pb, data_pb.Mutation)
assert got_pb.set_cell.family_name == expected_family
assert got_pb.set_cell.column_qualifier == expected_qualifier
assert got_pb.set_cell.timestamp_micros == expected_timestamp
assert got_pb.set_cell.value == expected_value

def test__to_pb_server_timestamp(self):
"""test with server side timestamp -1 value"""
import google.cloud.bigtable_v2.types.data as data_pb

expected_family = "test-family"
expected_qualifier = b"test-qualifier"
expected_value = b"test-value"
expected_timestamp = -1
instance = self._make_one(
expected_family, expected_qualifier, expected_value, expected_timestamp
)
got_pb = instance._to_pb()
assert isinstance(got_pb, data_pb.Mutation)
assert got_pb.set_cell.family_name == expected_family
assert got_pb.set_cell.column_qualifier == expected_qualifier
assert got_pb.set_cell.timestamp_micros == expected_timestamp
assert got_pb.set_cell.value == expected_value

@pytest.mark.parametrize(
"timestamp,expected_value",
[
Expand Down Expand Up @@ -406,6 +442,18 @@ def test__to_dict(self, start, end):
if end is not None:
assert time_range_dict["end_timestamp_micros"] == end

def test__to_pb(self):
"""ensure proto representation is as expected"""
import google.cloud.bigtable_v2.types.data as data_pb

expected_family = "test-family"
expected_qualifier = b"test-qualifier"
instance = self._make_one(expected_family, expected_qualifier)
got_pb = instance._to_pb()
assert isinstance(got_pb, data_pb.Mutation)
assert got_pb.delete_from_column.family_name == expected_family
assert got_pb.delete_from_column.column_qualifier == expected_qualifier

def test_is_idempotent(self):
"""is_idempotent is always true"""
instance = self._make_one(
Expand Down Expand Up @@ -445,6 +493,16 @@ def test__to_dict(self):
assert len(got_inner_dict.keys()) == 1
assert got_inner_dict["family_name"] == expected_family

def test__to_pb(self):
"""ensure proto representation is as expected"""
import google.cloud.bigtable_v2.types.data as data_pb

expected_family = "test-family"
instance = self._make_one(expected_family)
got_pb = instance._to_pb()
assert isinstance(got_pb, data_pb.Mutation)
assert got_pb.delete_from_family.family_name == expected_family

def test_is_idempotent(self):
"""is_idempotent is always true"""
instance = self._make_one("test-family")
Expand Down Expand Up @@ -477,6 +535,15 @@ def test__to_dict(self):
assert list(got_dict.keys()) == ["delete_from_row"]
assert len(got_dict["delete_from_row"].keys()) == 0

def test__to_pb(self):
"""ensure proto representation is as expected"""
import google.cloud.bigtable_v2.types.data as data_pb

instance = self._make_one()
got_pb = instance._to_pb()
assert isinstance(got_pb, data_pb.Mutation)
assert "delete_from_row" in str(got_pb)

def test_is_idempotent(self):
"""is_idempotent is always true"""
instance = self._make_one()
Expand Down Expand Up @@ -550,6 +617,23 @@ def test__to_dict(self):
assert instance._to_dict() == expected_result
assert mutation_mock._to_dict.call_count == n_mutations

def test__to_pb(self):
from google.cloud.bigtable_v2.types.bigtable import MutateRowsRequest
from google.cloud.bigtable_v2.types.data import Mutation

expected_key = "row_key"
mutation_mock = mock.Mock()
n_mutations = 3
expected_mutations = [mutation_mock for i in range(n_mutations)]
for mock_mutations in expected_mutations:
mock_mutations._to_pb.return_value = Mutation()
instance = self._make_one(expected_key, expected_mutations)
pb_result = instance._to_pb()
assert isinstance(pb_result, MutateRowsRequest.Entry)
assert pb_result.row_key == b"row_key"
assert pb_result.mutations == [Mutation()] * n_mutations
assert mutation_mock._to_pb.call_count == n_mutations

@pytest.mark.parametrize(
"mutations,result",
[
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/data/test_read_modify_write_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def test_abstract(self):
self._target_class()(family="foo", qualifier=b"bar")

def test__to_dict(self):
"""
to_dict not implemented in base class
"""
with pytest.raises(NotImplementedError):
self._target_class()._to_dict(mock.Mock())

Expand Down Expand Up @@ -97,6 +100,27 @@ def test__to_dict(self, args, expected):
}
assert instance._to_dict() == expected

@pytest.mark.parametrize(
"args,expected",
[
(("fam", b"qual", 1), ("fam", b"qual", 1)),
(("fam", b"qual", -12), ("fam", b"qual", -12)),
(("fam", "qual", 1), ("fam", b"qual", 1)),
(("fam", "qual", 0), ("fam", b"qual", 0)),
(("", "", 0), ("", b"", 0)),
(("f", b"q"), ("f", b"q", 1)),
],
)
def test__to_pb(self, args, expected):
import google.cloud.bigtable_v2.types.data as data_pb

instance = self._target_class()(*args)
pb_result = instance._to_pb()
assert isinstance(pb_result, data_pb.ReadModifyWriteRule)
assert pb_result.family_name == expected[0]
assert pb_result.column_qualifier == expected[1]
assert pb_result.increment_amount == expected[2]


class TestAppendValueRule:
def _target_class(self):
Expand Down Expand Up @@ -142,3 +166,21 @@ def test__to_dict(self, args, expected):
"append_value": expected[2],
}
assert instance._to_dict() == expected

@pytest.mark.parametrize(
"args,expected",
[
(("fam", b"qual", b"val"), ("fam", b"qual", b"val")),
(("fam", "qual", b"val"), ("fam", b"qual", b"val")),
(("", "", b""), ("", b"", b"")),
],
)
def test__to_pb(self, args, expected):
import google.cloud.bigtable_v2.types.data as data_pb

instance = self._target_class()(*args)
pb_result = instance._to_pb()
assert isinstance(pb_result, data_pb.ReadModifyWriteRule)
assert pb_result.family_name == expected[0]
assert pb_result.column_qualifier == expected[1]
assert pb_result.append_value == expected[2]

0 comments on commit d98cf46

Please sign in to comment.