|
15 | 15 |
|
16 | 16 | import unittest |
17 | 17 |
|
| 18 | +import mock |
| 19 | + |
| 20 | + |
| 21 | +class Test___mutate_rows_request(unittest.TestCase): |
| 22 | + |
| 23 | + def _call_fut(self, table_name, rows): |
| 24 | + from google.cloud.bigtable.table import _mutate_rows_request |
| 25 | + |
| 26 | + return _mutate_rows_request(table_name, rows) |
| 27 | + |
| 28 | + @mock.patch('google.cloud.bigtable.table._MAX_BULK_MUTATIONS', new=3) |
| 29 | + def test__mutate_rows_too_many_mutations(self): |
| 30 | + from google.cloud.bigtable.row import DirectRow |
| 31 | + from google.cloud.bigtable.table import TooManyMutationsError |
| 32 | + |
| 33 | + table = mock.Mock(name='table', spec=['name']) |
| 34 | + table.name = 'table' |
| 35 | + rows = [DirectRow(row_key=b'row_key', table=table), |
| 36 | + DirectRow(row_key=b'row_key_2', table=table)] |
| 37 | + rows[0].set_cell('cf1', b'c1', 1) |
| 38 | + rows[0].set_cell('cf1', b'c1', 2) |
| 39 | + rows[1].set_cell('cf1', b'c1', 3) |
| 40 | + rows[1].set_cell('cf1', b'c1', 4) |
| 41 | + with self.assertRaises(TooManyMutationsError): |
| 42 | + self._call_fut('table', rows) |
| 43 | + |
| 44 | + def test__mutate_rows_request(self): |
| 45 | + from google.cloud.bigtable.row import DirectRow |
| 46 | + |
| 47 | + table = mock.Mock(name='table', spec=['name']) |
| 48 | + table.name = 'table' |
| 49 | + rows = [DirectRow(row_key=b'row_key', table=table), |
| 50 | + DirectRow(row_key=b'row_key_2', table=table)] |
| 51 | + rows[0].set_cell('cf1', b'c1', b'1') |
| 52 | + rows[1].set_cell('cf1', b'c1', b'2') |
| 53 | + result = self._call_fut('table', rows) |
| 54 | + |
| 55 | + expected_result = _mutate_rows_request_pb(table_name='table') |
| 56 | + entry1 = expected_result.entries.add() |
| 57 | + entry1.row_key = b'row_key' |
| 58 | + mutations1 = entry1.mutations.add() |
| 59 | + mutations1.set_cell.family_name = 'cf1' |
| 60 | + mutations1.set_cell.column_qualifier = b'c1' |
| 61 | + mutations1.set_cell.timestamp_micros = -1 |
| 62 | + mutations1.set_cell.value = b'1' |
| 63 | + entry2 = expected_result.entries.add() |
| 64 | + entry2.row_key = b'row_key_2' |
| 65 | + mutations2 = entry2.mutations.add() |
| 66 | + mutations2.set_cell.family_name = 'cf1' |
| 67 | + mutations2.set_cell.column_qualifier = b'c1' |
| 68 | + mutations2.set_cell.timestamp_micros = -1 |
| 69 | + mutations2.set_cell.value = b'2' |
| 70 | + |
| 71 | + self.assertEqual(result, expected_result) |
| 72 | + |
| 73 | + |
| 74 | +class Test__check_row_table_name(unittest.TestCase): |
| 75 | + |
| 76 | + def _call_fut(self, table_name, row): |
| 77 | + from google.cloud.bigtable.table import _check_row_table_name |
| 78 | + |
| 79 | + return _check_row_table_name(table_name, row) |
| 80 | + |
| 81 | + def test_wrong_table_name(self): |
| 82 | + from google.cloud.bigtable.table import TableMismatchError |
| 83 | + from google.cloud.bigtable.row import DirectRow |
| 84 | + |
| 85 | + table = mock.Mock(name='table', spec=['name']) |
| 86 | + table.name = 'table' |
| 87 | + row = DirectRow(row_key=b'row_key', table=table) |
| 88 | + with self.assertRaises(TableMismatchError): |
| 89 | + self._call_fut('other_table', row) |
| 90 | + |
| 91 | + def test_right_table_name(self): |
| 92 | + from google.cloud.bigtable.row import DirectRow |
| 93 | + |
| 94 | + table = mock.Mock(name='table', spec=['name']) |
| 95 | + table.name = 'table' |
| 96 | + row = DirectRow(row_key=b'row_key', table=table) |
| 97 | + result = self._call_fut('table', row) |
| 98 | + self.assertFalse(result) |
| 99 | + |
| 100 | + |
| 101 | +class Test__check_row_type(unittest.TestCase): |
| 102 | + def _call_fut(self, row): |
| 103 | + from google.cloud.bigtable.table import _check_row_type |
| 104 | + |
| 105 | + return _check_row_type(row) |
| 106 | + |
| 107 | + def test_test_wrong_row_type(self): |
| 108 | + from google.cloud.bigtable.row import ConditionalRow |
| 109 | + |
| 110 | + row = ConditionalRow(row_key=b'row_key', table='table', filter_=None) |
| 111 | + with self.assertRaises(TypeError): |
| 112 | + self._call_fut(row) |
| 113 | + |
| 114 | + def test_right_row_type(self): |
| 115 | + from google.cloud.bigtable.row import DirectRow |
| 116 | + |
| 117 | + row = DirectRow(row_key=b'row_key', table='table') |
| 118 | + result = self._call_fut(row) |
| 119 | + self.assertFalse(result) |
| 120 | + |
18 | 121 |
|
19 | 122 | class TestTable(unittest.TestCase): |
20 | 123 |
|
@@ -348,6 +451,44 @@ def test_read_row_still_partial(self): |
348 | 451 | with self.assertRaises(ValueError): |
349 | 452 | self._read_row_helper(chunks, None) |
350 | 453 |
|
| 454 | + def test_mutate_rows(self): |
| 455 | + from google.cloud.bigtable._generated.bigtable_pb2 import ( |
| 456 | + MutateRowsResponse) |
| 457 | + from google.cloud.bigtable.row import DirectRow |
| 458 | + from google.rpc.status_pb2 import Status |
| 459 | + from tests.unit._testing import _FakeStub |
| 460 | + |
| 461 | + client = _Client() |
| 462 | + instance = _Instance(self.INSTANCE_NAME, client=client) |
| 463 | + table = self._make_one(self.TABLE_ID, instance) |
| 464 | + |
| 465 | + row_1 = DirectRow(row_key=b'row_key', table=table) |
| 466 | + row_1.set_cell('cf', b'col', b'value1') |
| 467 | + row_2 = DirectRow(row_key=b'row_key_2', table=table) |
| 468 | + row_2.set_cell('cf', b'col', b'value2') |
| 469 | + |
| 470 | + response = MutateRowsResponse( |
| 471 | + entries=[ |
| 472 | + MutateRowsResponse.Entry( |
| 473 | + index=0, |
| 474 | + status=Status(code=0), |
| 475 | + ), |
| 476 | + MutateRowsResponse.Entry( |
| 477 | + index=1, |
| 478 | + status=Status(code=1), |
| 479 | + ), |
| 480 | + ], |
| 481 | + ) |
| 482 | + |
| 483 | + # Patch the stub used by the API method. |
| 484 | + client._data_stub = _FakeStub([response]) |
| 485 | + statuses = table.mutate_rows([row_1, row_2]) |
| 486 | + result = [status.code for status in statuses] |
| 487 | + expected_result = [0, 1] |
| 488 | + |
| 489 | + self.assertEqual(result, expected_result) |
| 490 | + |
| 491 | + |
351 | 492 | def test_read_rows(self): |
352 | 493 | from google.cloud._testing import _Monkey |
353 | 494 | from tests.unit._testing import _FakeStub |
@@ -570,6 +711,13 @@ def _SampleRowKeysRequestPB(*args, **kw): |
570 | 711 | return messages_v2_pb2.SampleRowKeysRequest(*args, **kw) |
571 | 712 |
|
572 | 713 |
|
| 714 | +def _mutate_rows_request_pb(*args, **kw): |
| 715 | + from google.cloud.bigtable._generated import ( |
| 716 | + bigtable_pb2 as data_messages_v2_pb2) |
| 717 | + |
| 718 | + return data_messages_v2_pb2.MutateRowsRequest(*args, **kw) |
| 719 | + |
| 720 | + |
573 | 721 | def _TablePB(*args, **kw): |
574 | 722 | from google.cloud.bigtable._generated import ( |
575 | 723 | table_pb2 as table_v2_pb2) |
|
0 commit comments