|
| 1 | +# Copyright 2018 Google Inc. |
| 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 | +import os |
| 16 | +import uuid |
| 17 | +import pytest |
| 18 | + |
| 19 | +from google.cloud import bigtable |
| 20 | + |
| 21 | +from .write_batch import write_batch |
| 22 | +from .write_conditionally import write_conditional |
| 23 | +from .write_increment import write_increment |
| 24 | +from .write_simple import write_simple |
| 25 | + |
| 26 | +PROJECT = os.environ['GCLOUD_PROJECT'] |
| 27 | +BIGTABLE_INSTANCE = os.environ['BIGTABLE_CLUSTER'] |
| 28 | +TABLE_ID_PREFIX = 'mobile-time-series-{}' |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def bigtable_client(): |
| 33 | + return bigtable.Client(project=PROJECT, admin=True) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def bigtable_instance(bigtable_client): |
| 38 | + return bigtable_client.instance(BIGTABLE_INSTANCE) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def table_id(bigtable_instance): |
| 43 | + table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) |
| 44 | + table = bigtable_instance.table(table_id) |
| 45 | + if table.exists(): |
| 46 | + table.delete() |
| 47 | + |
| 48 | + column_family_id = 'stats_summary' |
| 49 | + column_families = {column_family_id: None} |
| 50 | + table.create(column_families=column_families) |
| 51 | + |
| 52 | + yield table_id |
| 53 | + |
| 54 | + table.delete() |
| 55 | + |
| 56 | + |
| 57 | +def test_writes(capsys, table_id): |
| 58 | + write_simple(PROJECT, BIGTABLE_INSTANCE, table_id) |
| 59 | + |
| 60 | + out, _ = capsys.readouterr() |
| 61 | + assert 'Successfully wrote row' in out |
| 62 | + |
| 63 | + write_increment(PROJECT, BIGTABLE_INSTANCE, table_id) |
| 64 | + |
| 65 | + out, _ = capsys.readouterr() |
| 66 | + assert 'Successfully updated row' in out |
| 67 | + |
| 68 | + write_conditional(PROJECT, BIGTABLE_INSTANCE, table_id) |
| 69 | + |
| 70 | + out, _ = capsys.readouterr() |
| 71 | + assert 'Successfully updated row\'s os_name' in out |
| 72 | + |
| 73 | + write_batch(PROJECT, BIGTABLE_INSTANCE, table_id) |
| 74 | + |
| 75 | + out, _ = capsys.readouterr() |
| 76 | + assert 'Successfully wrote 2 rows' in out |
0 commit comments