|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import unittest2 |
| 17 | + |
| 18 | + |
| 19 | +class TestCell(unittest2.TestCase): |
| 20 | + |
| 21 | + def _getTargetClass(self): |
| 22 | + from gcloud.bigtable.row_data import Cell |
| 23 | + return Cell |
| 24 | + |
| 25 | + def _makeOne(self, *args, **kwargs): |
| 26 | + return self._getTargetClass()(*args, **kwargs) |
| 27 | + |
| 28 | + def _from_pb_test_helper(self, labels=None): |
| 29 | + import datetime |
| 30 | + from gcloud._helpers import _EPOCH |
| 31 | + from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2 |
| 32 | + |
| 33 | + timestamp_micros = 18738724000 # Make sure millis granularity |
| 34 | + timestamp = _EPOCH + datetime.timedelta(microseconds=timestamp_micros) |
| 35 | + value = b'value-bytes' |
| 36 | + |
| 37 | + if labels is None: |
| 38 | + cell_pb = data_pb2.Cell(value=value, |
| 39 | + timestamp_micros=timestamp_micros) |
| 40 | + cell_expected = self._makeOne(value, timestamp) |
| 41 | + else: |
| 42 | + cell_pb = data_pb2.Cell(value=value, |
| 43 | + timestamp_micros=timestamp_micros, |
| 44 | + labels=labels) |
| 45 | + cell_expected = self._makeOne(value, timestamp, labels=labels) |
| 46 | + |
| 47 | + klass = self._getTargetClass() |
| 48 | + result = klass.from_pb(cell_pb) |
| 49 | + self.assertEqual(result, cell_expected) |
| 50 | + |
| 51 | + def test_from_pb(self): |
| 52 | + self._from_pb_test_helper() |
| 53 | + |
| 54 | + def test_from_pb_with_labels(self): |
| 55 | + labels = [u'label1', u'label2'] |
| 56 | + self._from_pb_test_helper(labels) |
| 57 | + |
| 58 | + def test_constructor(self): |
| 59 | + value = object() |
| 60 | + timestamp = object() |
| 61 | + cell = self._makeOne(value, timestamp) |
| 62 | + self.assertEqual(cell.value, value) |
| 63 | + self.assertEqual(cell.timestamp, timestamp) |
| 64 | + |
| 65 | + def test___eq__(self): |
| 66 | + value = object() |
| 67 | + timestamp = object() |
| 68 | + cell1 = self._makeOne(value, timestamp) |
| 69 | + cell2 = self._makeOne(value, timestamp) |
| 70 | + self.assertEqual(cell1, cell2) |
| 71 | + |
| 72 | + def test___eq__type_differ(self): |
| 73 | + cell1 = self._makeOne(None, None) |
| 74 | + cell2 = object() |
| 75 | + self.assertNotEqual(cell1, cell2) |
| 76 | + |
| 77 | + def test___ne__same_value(self): |
| 78 | + value = object() |
| 79 | + timestamp = object() |
| 80 | + cell1 = self._makeOne(value, timestamp) |
| 81 | + cell2 = self._makeOne(value, timestamp) |
| 82 | + comparison_val = (cell1 != cell2) |
| 83 | + self.assertFalse(comparison_val) |
| 84 | + |
| 85 | + def test___ne__(self): |
| 86 | + value1 = 'value1' |
| 87 | + value2 = 'value2' |
| 88 | + timestamp = object() |
| 89 | + cell1 = self._makeOne(value1, timestamp) |
| 90 | + cell2 = self._makeOne(value2, timestamp) |
| 91 | + self.assertNotEqual(cell1, cell2) |
0 commit comments