def _write_to_row(self, row1=None, row2=None, row3=None, row4=None): timestamp1 = datetime.datetime.utcnow().replace(tzinfo=UTC) timestamp1_micros = _microseconds_from_datetime(timestamp1) # Truncate to millisecond granularity. timestamp1_micros -= (timestamp1_micros % 1000) timestamp1 = _datetime_from_microseconds(timestamp1_micros) # 1000 microseconds is a millisecond timestamp2 = timestamp1 + datetime.timedelta(microseconds=1000) timestamp3 = timestamp1 + datetime.timedelta(microseconds=2000) timestamp4 = timestamp1 + datetime.timedelta(microseconds=3000) if row1 is not None: row1.set_cell(COLUMN_FAMILY_ID1, COL_NAME1, CELL_VAL1, timestamp=timestamp1) if row2 is not None: row2.set_cell(COLUMN_FAMILY_ID1, COL_NAME1, CELL_VAL2, timestamp=timestamp2) if row3 is not None: row3.set_cell(COLUMN_FAMILY_ID1, COL_NAME2, CELL_VAL3, timestamp=timestamp3) if row4 is not None: row4.set_cell(COLUMN_FAMILY_ID2, COL_NAME3, CELL_VAL4, timestamp=timestamp4) # Create the cells we will check. cell1 = Cell(CELL_VAL1, timestamp1) cell2 = Cell(CELL_VAL2, timestamp2) cell3 = Cell(CELL_VAL3, timestamp3) cell4 = Cell(CELL_VAL4, timestamp4) return cell1, cell2, cell3, cell4
def test__handle_row_contents(self): from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2 from gcloud.bigtable._generated import ( bigtable_service_messages_pb2 as messages_pb2) from gcloud.bigtable.row_data import Cell partial_row_data = self._makeOne(None) cell1_pb = data_pb2.Cell(timestamp_micros=1, value=b'val1') cell2_pb = data_pb2.Cell(timestamp_micros=200, value=b'val2') cell3_pb = data_pb2.Cell(timestamp_micros=300000, value=b'val3') col1 = b'col1' col2 = b'col2' columns = [ data_pb2.Column(qualifier=col1, cells=[cell1_pb, cell2_pb]), data_pb2.Column(qualifier=col2, cells=[cell3_pb]), ] family_name = u'name' row_contents = data_pb2.Family(name=family_name, columns=columns) chunk = messages_pb2.ReadRowsResponse.Chunk(row_contents=row_contents) self.assertEqual(partial_row_data.cells, {}) partial_row_data._handle_row_contents(chunk) expected_cells = { family_name: { col1: [Cell.from_pb(cell1_pb), Cell.from_pb(cell2_pb)], col2: [Cell.from_pb(cell3_pb)], } } self.assertEqual(partial_row_data.cells, expected_cells)
def test_read_row_complete(self): from gcloud.bigtable.row_data import Cell from gcloud.bigtable.row_data import PartialRowData chunk = _ReadRowsResponseCellChunkPB( row_key=self.ROW_KEY, family_name=self.FAMILY_NAME, qualifier=self.QUALIFIER, timestamp_micros=self.TIMESTAMP_MICROS, value=self.VALUE, commit_row=True, ) chunks = [chunk] expected_result = PartialRowData(row_key=self.ROW_KEY) family = expected_result._cells.setdefault(self.FAMILY_NAME, {}) column = family.setdefault(self.QUALIFIER, []) column.append(Cell.from_pb(chunk)) self._read_row_helper(chunks, expected_result)