From 666a9086f138a16608de30be90a13ba322f2844a Mon Sep 17 00:00:00 2001 From: Ryan Yuan Date: Fri, 18 Sep 2020 22:08:18 +1000 Subject: [PATCH 1/2] feat: add response status to DirectRow.commit() --- google/cloud/bigtable/row.py | 8 +++++++- tests/unit/test_row.py | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/google/cloud/bigtable/row.py b/google/cloud/bigtable/row.py index 92f5b818b..eaaa207b3 100644 --- a/google/cloud/bigtable/row.py +++ b/google/cloud/bigtable/row.py @@ -457,12 +457,18 @@ def commit(self): :end-before: [END bigtable_row_commit] :dedent: 4 + :rtype: :class:`~google.rpc.status_pb2.Status` + :returns: A response status (`google.rpc.status_pb2.Status`) + representing success or failure of the row committed. :raises: :exc:`~.table.TooManyMutationsError` if the number of mutations is greater than 100,000. """ - self._table.mutate_rows([self]) + response = self._table.mutate_rows([self]) + self.clear() + return response[0] + def clear(self): """Removes all currently accumulated mutations on the current row. diff --git a/tests/unit/test_row.py b/tests/unit/test_row.py index 47424d910..4bdf59c8d 100644 --- a/tests/unit/test_row.py +++ b/tests/unit/test_row.py @@ -359,6 +359,28 @@ def test_commit(self): row.commit() self.assertEqual(table.mutated_rows, [row]) + @mock.patch("google.cloud.bigtable.table.Table.mutate_rows") + def test_commit_with_exception(self, mock_mutate_rows): + project_id = "project-id" + row_key = b"row_key" + table_name = "projects/more-stuff" + column_family_id = u"column_family_id" + column = b"column" + + credentials = _make_credentials() + client = self._make_client( + project=project_id, credentials=credentials, admin=True + ) + table = _Table(table_name, client=client) + row = self._make_one(row_key, table) + value = b"bytes-value" + + # Perform the method and check the result. + row.set_cell(column_family_id, column, value) + from google.rpc import status_pb2 + result = row.commit() + self.assertIsInstance(result, status_pb2.Status) + class TestConditionalRow(unittest.TestCase): @staticmethod @@ -833,3 +855,5 @@ def __init__(self, name, client=None, app_profile_id=None): def mutate_rows(self, rows): self.mutated_rows.extend(rows) + from google.rpc import status_pb2 + return [status_pb2.Status()] From 1dbfe0eca49c200a93676d64e5d8de8167229551 Mon Sep 17 00:00:00 2001 From: Ryan Yuan Date: Tue, 22 Sep 2020 17:15:57 +1000 Subject: [PATCH 2/2] feat: add response status to DirectRow.commit() --- tests/unit/test_row.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/unit/test_row.py b/tests/unit/test_row.py index 4bdf59c8d..16a8232ec 100644 --- a/tests/unit/test_row.py +++ b/tests/unit/test_row.py @@ -359,8 +359,9 @@ def test_commit(self): row.commit() self.assertEqual(table.mutated_rows, [row]) - @mock.patch("google.cloud.bigtable.table.Table.mutate_rows") - def test_commit_with_exception(self, mock_mutate_rows): + def test_commit_with_exception(self): + from google.rpc import status_pb2 + project_id = "project-id" row_key = b"row_key" table_name = "projects/more-stuff" @@ -377,9 +378,9 @@ def test_commit_with_exception(self, mock_mutate_rows): # Perform the method and check the result. row.set_cell(column_family_id, column, value) - from google.rpc import status_pb2 result = row.commit() - self.assertIsInstance(result, status_pb2.Status) + expected = status_pb2.Status(code=0) + self.assertEqual(result, expected) class TestConditionalRow(unittest.TestCase): @@ -854,6 +855,7 @@ def __init__(self, name, client=None, app_profile_id=None): self.mutated_rows = [] def mutate_rows(self, rows): - self.mutated_rows.extend(rows) from google.rpc import status_pb2 - return [status_pb2.Status()] + + self.mutated_rows.extend(rows) + return [status_pb2.Status(code=0)]