|
| 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 TestBatch(unittest2.TestCase): |
| 20 | + |
| 21 | + def _getTargetClass(self): |
| 22 | + from gcloud.bigtable.happybase.batch import Batch |
| 23 | + return Batch |
| 24 | + |
| 25 | + def _makeOne(self, *args, **kwargs): |
| 26 | + return self._getTargetClass()(*args, **kwargs) |
| 27 | + |
| 28 | + def test_constructor_defaults(self): |
| 29 | + table = object() |
| 30 | + batch = self._makeOne(table) |
| 31 | + self.assertEqual(batch._table, table) |
| 32 | + self.assertEqual(batch._batch_size, None) |
| 33 | + self.assertEqual(batch._timestamp, None) |
| 34 | + self.assertEqual(batch._delete_range, None) |
| 35 | + self.assertEqual(batch._transaction, False) |
| 36 | + self.assertEqual(batch._row_map, {}) |
| 37 | + self.assertEqual(batch._mutation_count, 0) |
| 38 | + |
| 39 | + def test_constructor_explicit(self): |
| 40 | + from gcloud._helpers import _datetime_from_microseconds |
| 41 | + from gcloud.bigtable.row import TimestampRange |
| 42 | + |
| 43 | + table = object() |
| 44 | + timestamp = 144185290431 |
| 45 | + batch_size = 42 |
| 46 | + transaction = False # Must be False when batch_size is non-null |
| 47 | + |
| 48 | + batch = self._makeOne(table, timestamp=timestamp, |
| 49 | + batch_size=batch_size, transaction=transaction) |
| 50 | + self.assertEqual(batch._table, table) |
| 51 | + self.assertEqual(batch._batch_size, batch_size) |
| 52 | + self.assertEqual(batch._timestamp, |
| 53 | + _datetime_from_microseconds(1000 * timestamp)) |
| 54 | + |
| 55 | + next_timestamp = _datetime_from_microseconds(1000 * (timestamp + 1)) |
| 56 | + time_range = TimestampRange(end=next_timestamp) |
| 57 | + self.assertEqual(batch._delete_range, time_range) |
| 58 | + self.assertEqual(batch._transaction, transaction) |
| 59 | + self.assertEqual(batch._row_map, {}) |
| 60 | + self.assertEqual(batch._mutation_count, 0) |
| 61 | + |
| 62 | + def test_constructor_with_non_default_wal(self): |
| 63 | + from gcloud._testing import _Monkey |
| 64 | + from gcloud.bigtable.happybase import batch as MUT |
| 65 | + |
| 66 | + warned = [] |
| 67 | + |
| 68 | + def mock_warn(msg): |
| 69 | + warned.append(msg) |
| 70 | + |
| 71 | + table = object() |
| 72 | + wal = object() |
| 73 | + with _Monkey(MUT, _WARN=mock_warn): |
| 74 | + self._makeOne(table, wal=wal) |
| 75 | + |
| 76 | + self.assertEqual(warned, [MUT._WAL_WARNING]) |
| 77 | + |
| 78 | + def test_constructor_with_non_positive_batch_size(self): |
| 79 | + table = object() |
| 80 | + batch_size = -10 |
| 81 | + with self.assertRaises(ValueError): |
| 82 | + self._makeOne(table, batch_size=batch_size) |
| 83 | + batch_size = 0 |
| 84 | + with self.assertRaises(ValueError): |
| 85 | + self._makeOne(table, batch_size=batch_size) |
| 86 | + |
| 87 | + def test_constructor_with_batch_size_and_transactional(self): |
| 88 | + table = object() |
| 89 | + batch_size = 1 |
| 90 | + transaction = True |
| 91 | + with self.assertRaises(TypeError): |
| 92 | + self._makeOne(table, batch_size=batch_size, |
| 93 | + transaction=transaction) |
0 commit comments