def test_RRRM_build_updated_request_no_end_key(): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable_v2 import types row_filter = RowSampleFilter(0.33) last_scanned_key = b"row_key25" request = _ReadRowsRequestPB(filter=row_filter.to_pb(), rows_limit=8, table_name=TABLE_NAME) row_range1 = types.RowRange(start_key_closed=b"row_key20") request.rows.row_ranges.append(row_range1) request_manager = _make_read_rows_request_manager(request, last_scanned_key, 2) result = request_manager.build_updated_request() expected_result = _ReadRowsRequestPB(table_name=TABLE_NAME, filter=row_filter.to_pb(), rows_limit=6) row_range2 = types.RowRange(start_key_open=last_scanned_key) expected_result.rows.row_ranges.append(row_range2) assert expected_result == result
def test_build_updated_request_rows(self): from google.cloud.bigtable.row_filters import RowSampleFilter row_filter = RowSampleFilter(0.33) last_scanned_key = b"row_key4" request = _ReadRowsRequestPB(filter=row_filter.to_pb(), rows_limit=5, table_name=self.table_name) request.rows.row_keys.extend([ b"row_key1", b"row_key2", b"row_key4", b"row_key5", b"row_key7", b"row_key9", ]) request_manager = self._make_one(request, last_scanned_key, 3) result = request_manager.build_updated_request() expected_result = _ReadRowsRequestPB(table_name=self.table_name, filter=row_filter.to_pb(), rows_limit=2) expected_result.rows.row_keys.extend( [b"row_key5", b"row_key7", b"row_key9"]) self.assertEqual(expected_result, result)
def test_to_pb(self): from google.cloud.bigtable.row_filters import CellsRowOffsetFilter from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable.row_filters import ( StripValueTransformerFilter) row_filter1 = StripValueTransformerFilter(True) row_filter1_pb = row_filter1.to_pb() row_filter2 = RowSampleFilter(0.25) row_filter2_pb = row_filter2.to_pb() row_filter3 = CellsRowOffsetFilter(11) row_filter3_pb = row_filter3.to_pb() row_filter4 = self._makeOne(row_filter1, true_filter=row_filter2, false_filter=row_filter3) filter_pb = row_filter4.to_pb() expected_pb = _RowFilterPB(condition=_RowFilterConditionPB( predicate_filter=row_filter1_pb, true_filter=row_filter2_pb, false_filter=row_filter3_pb, ), ) self.assertEqual(filter_pb, expected_pb)
def test_RRRM_build_updated_request_rows(): from google.cloud.bigtable.row_filters import RowSampleFilter row_filter = RowSampleFilter(0.33) last_scanned_key = b"row_key4" request = _ReadRowsRequestPB(filter=row_filter.to_pb(), rows_limit=5, table_name=TABLE_NAME) request.rows.row_keys.extend([ b"row_key1", b"row_key2", b"row_key4", b"row_key5", b"row_key7", b"row_key9" ]) request_manager = _make_read_rows_request_manager(request, last_scanned_key, 3) result = request_manager.build_updated_request() expected_result = _ReadRowsRequestPB(table_name=TABLE_NAME, filter=row_filter.to_pb(), rows_limit=2) expected_result.rows.row_keys.extend( [b"row_key5", b"row_key7", b"row_key9"]) assert expected_result == result
def test_build_updated_request_no_start_key(self): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable_v2.types import RowRange row_filter = RowSampleFilter(0.33) last_scanned_key = b"row_key25" request = _ReadRowsRequestPB(filter=row_filter.to_pb(), rows_limit=8, table_name=self.table_name) row_range1 = RowRange(end_key_open=b"row_key29") request.rows.row_ranges.append(row_range1) request_manager = self._make_one(request, last_scanned_key, 2) result = request_manager.build_updated_request() expected_result = _ReadRowsRequestPB(table_name=self.table_name, filter=row_filter.to_pb(), rows_limit=6) row_range2 = RowRange(start_key_open=last_scanned_key, end_key_open=b"row_key29") expected_result.rows.row_ranges.append(row_range2) self.assertEqual(expected_result, result)
def test_with_filter(self): from google.cloud.bigtable.row_filters import RowSampleFilter table_name = 'table_name' row_filter = RowSampleFilter(0.33) result = self._callFUT(table_name, filter_=row_filter) expected_result = _ReadRowsRequestPB( table_name=table_name, filter=row_filter.to_pb(), ) self.assertEqual(result, expected_result)
def test_commit(self): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable_v2.services.bigtable import BigtableClient project_id = "project-id" row_key = b"row_key" table_name = "projects/more-stuff" app_profile_id = "app_profile_id" column_family_id1 = u"column_family_id1" column_family_id2 = u"column_family_id2" column_family_id3 = u"column_family_id3" column1 = b"column1" column2 = b"column2" api = mock.create_autospec(BigtableClient) credentials = _make_credentials() client = self._make_client(project=project_id, credentials=credentials, admin=True) table = _Table(table_name, client=client, app_profile_id=app_profile_id) row_filter = RowSampleFilter(0.33) row = self._make_one(row_key, table, filter_=row_filter) # Create request_pb value1 = b"bytes-value" # Create response_pb predicate_matched = True response_pb = _CheckAndMutateRowResponsePB( predicate_matched=predicate_matched) # Patch the stub used by the API method. api.check_and_mutate_row.side_effect = [response_pb] client._table_data_client = api # Create expected_result. expected_result = predicate_matched # Perform the method and check the result. row.set_cell(column_family_id1, column1, value1, state=True) row.delete(state=False) row.delete_cell(column_family_id2, column2, state=True) row.delete_cells(column_family_id3, row.ALL_COLUMNS, state=True) result = row.commit() call_args = api.check_and_mutate_row.call_args self.assertEqual(app_profile_id, call_args.app_profile_id[0]) self.assertEqual(result, expected_result) self.assertEqual(row._true_pb_mutations, []) self.assertEqual(row._false_pb_mutations, [])
def test_to_pb_false_only(self): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable.row_filters import StripValueTransformerFilter row_filter1 = StripValueTransformerFilter(True) row_filter1_pb = row_filter1.to_pb() row_filter2 = RowSampleFilter(0.25) row_filter2_pb = row_filter2.to_pb() row_filter3 = self._make_one(row_filter1, false_filter=row_filter2) filter_pb = row_filter3.to_pb() expected_pb = _RowFilterPB(condition=_RowFilterConditionPB( predicate_filter=row_filter1_pb, false_filter=row_filter2_pb)) self.assertEqual(filter_pb, expected_pb)
def test_to_pb(self): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable.row_filters import StripValueTransformerFilter row_filter1 = StripValueTransformerFilter(True) row_filter1_pb = row_filter1.to_pb() row_filter2 = RowSampleFilter(0.25) row_filter2_pb = row_filter2.to_pb() row_filter3 = self._make_one(filters=[row_filter1, row_filter2]) filter_pb = row_filter3.to_pb() expected_pb = _RowFilterPB(interleave=_RowFilterInterleavePB( filters=[row_filter1_pb, row_filter2_pb])) self.assertEqual(filter_pb, expected_pb)
def test_commit(self): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable_v2.gapic import bigtable_client project_id = 'project-id' row_key = b'row_key' table_name = 'projects/more-stuff' column_family_id1 = u'column_family_id1' column_family_id2 = u'column_family_id2' column_family_id3 = u'column_family_id3' column1 = b'column1' column2 = b'column2' api = bigtable_client.BigtableClient(mock.Mock()) credentials = _make_credentials() client = self._make_client(project=project_id, credentials=credentials, admin=True) table = _Table(table_name, client=client) row_filter = RowSampleFilter(0.33) row = self._make_one(row_key, table, filter_=row_filter) # Create request_pb value1 = b'bytes-value' # Create response_pb predicate_matched = True response_pb = _CheckAndMutateRowResponsePB( predicate_matched=predicate_matched) # Patch the stub used by the API method. client._table_data_client = api bigtable_stub = client._table_data_client.bigtable_stub bigtable_stub.CheckAndMutateRow.side_effect = [[response_pb]] # Create expected_result. expected_result = predicate_matched # Perform the method and check the result. row.set_cell(column_family_id1, column1, value1, state=True) row.delete(state=False) row.delete_cell(column_family_id2, column2, state=True) row.delete_cells(column_family_id3, row.ALL_COLUMNS, state=True) result = row.commit() self.assertEqual(result, expected_result) self.assertEqual(row._true_pb_mutations, []) self.assertEqual(row._false_pb_mutations, [])
def test_to_pb(self): from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable.row_filters import ( StripValueTransformerFilter) row_filter1 = StripValueTransformerFilter(True) row_filter1_pb = row_filter1.to_pb() row_filter2 = RowSampleFilter(0.25) row_filter2_pb = row_filter2.to_pb() row_filter3 = self._makeOne(filters=[row_filter1, row_filter2]) filter_pb = row_filter3.to_pb() expected_pb = _RowFilterPB(chain=_RowFilterChainPB( filters=[row_filter1_pb, row_filter2_pb], ), ) self.assertEqual(filter_pb, expected_pb)
def test_build_updated_request(self): from google.cloud.bigtable.row_filters import RowSampleFilter row_filter = RowSampleFilter(0.33) last_scanned_key = b"row_key14" request = _ReadRowsRequestPB(filter=row_filter.to_pb(), rows_limit=8, table_name=self.table_name) request.rows.row_ranges.add(**self.row_range1.get_range_kwargs()) request_manager = self._make_one(request, last_scanned_key, 2) result = request_manager.build_updated_request() expected_result = _ReadRowsRequestPB(table_name=self.table_name, filter=row_filter.to_pb(), rows_limit=6) expected_result.rows.row_ranges.add( **self.row_range1.get_range_kwargs()) self.assertEqual(expected_result, result)
def test_to_pb_nested(self): from google.cloud.bigtable.row_filters import CellsRowLimitFilter from google.cloud.bigtable.row_filters import RowSampleFilter from google.cloud.bigtable.row_filters import StripValueTransformerFilter row_filter1 = StripValueTransformerFilter(True) row_filter2 = RowSampleFilter(0.25) row_filter3 = self._make_one(filters=[row_filter1, row_filter2]) row_filter3_pb = row_filter3.to_pb() row_filter4 = CellsRowLimitFilter(11) row_filter4_pb = row_filter4.to_pb() row_filter5 = self._make_one(filters=[row_filter3, row_filter4]) filter_pb = row_filter5.to_pb() expected_pb = _RowFilterPB(chain=_RowFilterChainPB( filters=[row_filter3_pb, row_filter4_pb])) self.assertEqual(filter_pb, expected_pb)
def test_commit(self): from tests.unit._testing import _FakeStub from google.cloud.bigtable.row_filters import RowSampleFilter row_key = b'row_key' table_name = 'projects/more-stuff' column_family_id1 = u'column_family_id1' column_family_id2 = u'column_family_id2' column_family_id3 = u'column_family_id3' column1 = b'column1' column2 = b'column2' client = _Client() table = _Table(table_name, client=client) row_filter = RowSampleFilter(0.33) row = self._make_one(row_key, table, filter_=row_filter) # Create request_pb value1 = b'bytes-value' mutation1 = _MutationPB( set_cell=_MutationSetCellPB( family_name=column_family_id1, column_qualifier=column1, timestamp_micros=-1, # Default value. value=value1, ), ) mutation2 = _MutationPB(delete_from_row=_MutationDeleteFromRowPB(), ) mutation3 = _MutationPB(delete_from_column=_MutationDeleteFromColumnPB( family_name=column_family_id2, column_qualifier=column2, ), ) mutation4 = _MutationPB(delete_from_family=_MutationDeleteFromFamilyPB( family_name=column_family_id3, ), ) request_pb = _CheckAndMutateRowRequestPB( table_name=table_name, row_key=row_key, predicate_filter=row_filter.to_pb(), true_mutations=[mutation1, mutation3, mutation4], false_mutations=[mutation2], ) # Create response_pb predicate_matched = True response_pb = _CheckAndMutateRowResponsePB( predicate_matched=predicate_matched) # Patch the stub used by the API method. client._data_stub = stub = _FakeStub(response_pb) # Create expected_result. expected_result = predicate_matched # Perform the method and check the result. row.set_cell(column_family_id1, column1, value1, state=True) row.delete(state=False) row.delete_cell(column_family_id2, column2, state=True) row.delete_cells(column_family_id3, row.ALL_COLUMNS, state=True) result = row.commit() self.assertEqual(result, expected_result) self.assertEqual(stub.method_calls, [( 'CheckAndMutateRow', (request_pb, ), {}, )]) self.assertEqual(row._true_pb_mutations, []) self.assertEqual(row._false_pb_mutations, [])