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) timestamp2_micros = _microseconds_from_datetime(timestamp2) timestamp3 = timestamp1 + datetime.timedelta(microseconds=2000) timestamp3_micros = _microseconds_from_datetime(timestamp3) timestamp4 = timestamp1 + datetime.timedelta(microseconds=3000) timestamp4_micros = _microseconds_from_datetime(timestamp4) 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_micros) cell2 = Cell(CELL_VAL2, timestamp2_micros) cell3 = Cell(CELL_VAL3, timestamp3_micros) cell4 = Cell(CELL_VAL4, timestamp4_micros) return cell1, cell2, cell3, cell4
def test_without_timestamp(self): from google.cloud.bigtable.row_data import Cell value1 = "foo" cell1 = Cell(value=value1, timestamp_micros=None) value2 = "bar" cell2 = Cell(value=value2, timestamp_micros=None) result = self._call_fut([cell1, cell2]) self.assertEqual(result, [value1, value2])
def test_without_timestamp(self): from google.cloud.bigtable.row_data import Cell from google.cloud.bigtable.row_data import PartialRowData row_data = PartialRowData(b"row-key") val1 = b"hi-im-bytes" val2 = b"bi-im-hytes" row_data._cells[u"fam1"] = { b"col1": [Cell(val1, None)], b"col2": [Cell(val2, None)], } result = self._call_fut(row_data) expected_result = {b"fam1:col1": val1, b"fam1:col2": val2} self.assertEqual(result, expected_result)
def test_with_timestamp(self): from google.cloud.bigtable.row_data import Cell value1 = "foo" ts1_millis = 1221934570148 ts1 = ts1_millis * 1000 cell1 = Cell(value=value1, timestamp_micros=ts1) value2 = "bar" ts2_millis = 1221955575548 ts2 = ts2_millis * 1000 cell2 = Cell(value=value2, timestamp_micros=ts2) result = self._call_fut([cell1, cell2], include_timestamp=True) self.assertEqual(result, [(value1, ts1_millis), (value2, ts2_millis)])
def _write_to_row(row1, row2, row3, row4): from google.cloud._helpers import _datetime_from_microseconds from google.cloud._helpers import _microseconds_from_datetime from google.cloud._helpers import UTC from google.cloud.bigtable.row_data import Cell 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) timestamp2_micros = _microseconds_from_datetime(timestamp2) timestamp3 = timestamp1 + datetime.timedelta(microseconds=2000) timestamp3_micros = _microseconds_from_datetime(timestamp3) timestamp4 = timestamp1 + datetime.timedelta(microseconds=3000) timestamp4_micros = _microseconds_from_datetime(timestamp4) 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_micros) cell2 = Cell(CELL_VAL2, timestamp2_micros) cell3 = Cell(CELL_VAL3, timestamp3_micros) cell4 = Cell(CELL_VAL4, timestamp4_micros) return cell1, cell2, cell3, cell4
def test_with_timestamp(self): from google.cloud.bigtable.row_data import Cell from google.cloud.bigtable.row_data import PartialRowData row_data = PartialRowData(b"row-key") val1 = b"hi-im-bytes" ts1_millis = 1221934570148 ts1 = ts1_millis * 1000 val2 = b"bi-im-hytes" ts2_millis = 1331934880000 ts2 = ts2_millis * 1000 row_data._cells[u"fam1"] = { b"col1": [Cell(val1, ts1)], b"col2": [Cell(val2, ts2)], } result = self._call_fut(row_data, include_timestamp=True) expected_result = { b"fam1:col1": (val1, ts1_millis), b"fam1:col2": (val2, ts2_millis), } self.assertEqual(result, expected_result)
def test_read_row_complete(self): from google.cloud.bigtable.row_data import Cell from google.cloud.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)
def _cell_from_pb_test_helper(labels=None): import datetime from google.cloud._helpers import _EPOCH from google.cloud.bigtable_v2.types import data as data_v2_pb2 from google.cloud.bigtable.row_data import Cell timestamp = _EPOCH + datetime.timedelta(microseconds=TIMESTAMP_MICROS) value = b"value-bytes" if labels is None: cell_pb = data_v2_pb2.Cell(value=value, timestamp_micros=TIMESTAMP_MICROS) cell_expected = _make_cell(value, TIMESTAMP_MICROS) else: cell_pb = data_v2_pb2.Cell(value=value, timestamp_micros=TIMESTAMP_MICROS, labels=labels) cell_expected = _make_cell(value, TIMESTAMP_MICROS, labels=labels) result = Cell.from_pb(cell_pb) assert result == cell_expected assert result.timestamp == timestamp
def _make_cell(*args, **kwargs): from google.cloud.bigtable.row_data import Cell return Cell(*args, **kwargs)