From 2478bb864adbc71ef606e2b10b3bdfe3a7d44717 Mon Sep 17 00:00:00 2001 From: Ryan Yuan Date: Wed, 23 Sep 2020 05:10:07 +1000 Subject: [PATCH] feat: add response status to DirectRow.commit() (#128) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #127 🦕 --- google/cloud/bigtable/row.py | 8 +++++++- tests/unit/test_row.py | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/google/cloud/bigtable/row.py b/google/cloud/bigtable/row.py index b28b86aa2..87a268056 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..16a8232ec 100644 --- a/tests/unit/test_row.py +++ b/tests/unit/test_row.py @@ -359,6 +359,29 @@ def test_commit(self): row.commit() self.assertEqual(table.mutated_rows, [row]) + 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" + 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) + result = row.commit() + expected = status_pb2.Status(code=0) + self.assertEqual(result, expected) + class TestConditionalRow(unittest.TestCase): @staticmethod @@ -832,4 +855,7 @@ def __init__(self, name, client=None, app_profile_id=None): self.mutated_rows = [] def mutate_rows(self, rows): + from google.rpc import status_pb2 + self.mutated_rows.extend(rows) + return [status_pb2.Status(code=0)]