From 1270f034baf360ec8d653bd90c83cd537e57aadd Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 25 Oct 2024 13:56:29 -0700 Subject: [PATCH] chore(tests): clean up sample test tables --- samples/beam/hello_world_write_test.py | 15 ++---- samples/hello/async_main_test.py | 33 ++++++------ samples/hello/main_test.py | 32 ++++++------ samples/hello_happybase/main.py | 1 + samples/hello_happybase/main_test.py | 10 ++-- samples/quickstart/main_async_test.py | 5 +- samples/quickstart/main_test.py | 5 +- samples/quickstart_happybase/main_test.py | 4 +- .../data_client_snippets_async_test.py | 2 +- .../snippets/deletes/deletes_async_test.py | 22 ++------ samples/snippets/deletes/deletes_test.py | 6 +-- .../filters/filter_snippets_async_test.py | 20 ++------ samples/snippets/filters/filters_test.py | 4 +- samples/snippets/reads/reads_test.py | 5 +- samples/snippets/writes/writes_test.py | 4 +- samples/tableadmin/tableadmin_test.py | 50 ++++++++++--------- 16 files changed, 99 insertions(+), 119 deletions(-) diff --git a/samples/beam/hello_world_write_test.py b/samples/beam/hello_world_write_test.py index 4e9a47c7d..9990ecf29 100644 --- a/samples/beam/hello_world_write_test.py +++ b/samples/beam/hello_world_write_test.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. import os -import uuid from google.cloud import bigtable import pytest @@ -21,7 +20,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-beam" @pytest.fixture(scope="module", autouse=True) @@ -29,30 +28,26 @@ def table_id(): client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() table.create(column_families={"stats_summary": None}) - yield table_id + yield table table.delete() -def test_hello_world_write(table_id): +def test_hello_world_write(table): hello_world_write.run( [ "--bigtable-project=%s" % PROJECT, "--bigtable-instance=%s" % BIGTABLE_INSTANCE, - "--bigtable-table=%s" % table_id, + "--bigtable-table=%s" % TABLE_ID, ] ) - client = bigtable.Client(project=PROJECT, admin=True) - instance = client.instance(BIGTABLE_INSTANCE) - table = instance.table(table_id) - rows = table.read_rows() count = 0 for _ in rows: diff --git a/samples/hello/async_main_test.py b/samples/hello/async_main_test.py index a47ac2d33..c7592c508 100644 --- a/samples/hello/async_main_test.py +++ b/samples/hello/async_main_test.py @@ -13,27 +13,32 @@ # limitations under the License. import os -import random import asyncio +from google.cloud import bigtable from async_main import main PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_NAME_FORMAT = "hello-world-test-{}" -TABLE_NAME_RANGE = 10000 +TABLE_ID = "hello-world-test-async" def test_async_main(capsys): - table_name = TABLE_NAME_FORMAT.format(random.randrange(TABLE_NAME_RANGE)) + try: + asyncio.run(main(PROJECT, BIGTABLE_INSTANCE, TABLE_ID)) - asyncio.run(main(PROJECT, BIGTABLE_INSTANCE, table_name)) - - out, _ = capsys.readouterr() - assert "Creating the {} table.".format(table_name) in out - assert "Writing some greetings to the table." in out - assert "Getting a single greeting by row key." in out - assert "Hello World!" in out - assert "Scanning for all greetings" in out - assert "Hello Cloud Bigtable!" in out - assert "Deleting the {} table.".format(table_name) in out + out, _ = capsys.readouterr() + assert "Creating the {} table.".format(TABLE_ID) in out + assert "Writing some greetings to the table." in out + assert "Getting a single greeting by row key." in out + assert "Hello World!" in out + assert "Scanning for all greetings" in out + assert "Hello Cloud Bigtable!" in out + assert "Deleting the {} table.".format(TABLE_ID) in out + finally: + # delete table + client = bigtable.Client(PROJECT, admin=True) + instance = client.instance(BIGTABLE_INSTANCE) + table = instance.table(TABLE_ID) + if table.exists(): + table.delete() diff --git a/samples/hello/main_test.py b/samples/hello/main_test.py index 641b34d11..d28bfdaf2 100644 --- a/samples/hello/main_test.py +++ b/samples/hello/main_test.py @@ -13,26 +13,30 @@ # limitations under the License. import os -import random from main import main PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_NAME_FORMAT = "hello-world-test-{}" -TABLE_NAME_RANGE = 10000 +TABLE_ID = "hello-world-test" def test_main(capsys): - table_name = TABLE_NAME_FORMAT.format(random.randrange(TABLE_NAME_RANGE)) + try: + main(PROJECT, BIGTABLE_INSTACE, TABLE_ID) - main(PROJECT, BIGTABLE_INSTANCE, table_name) - - out, _ = capsys.readouterr() - assert "Creating the {} table.".format(table_name) in out - assert "Writing some greetings to the table." in out - assert "Getting a single greeting by row key." in out - assert "Hello World!" in out - assert "Scanning for all greetings" in out - assert "Hello Cloud Bigtable!" in out - assert "Deleting the {} table.".format(table_name) in out + out, _ = capsys.readouterr() + assert "Creating the {} table.".format(TABLE_ID) in out + assert "Writing some greetings to the table." in out + assert "Getting a single greeting by row key." in out + assert "Hello World!" in out + assert "Scanning for all greetings" in out + assert "Hello Cloud Bigtable!" in out + assert "Deleting the {} table.".format(TABLE_ID) in out + finally: + # delete table + client = bigtable.Client(PROJECT, admin=True) + instance = client.instance(BIGTABLE_INSTANCE) + table = instance.table(TABLE_ID) + if table.exists(): + table.delete() diff --git a/samples/hello_happybase/main.py b/samples/hello_happybase/main.py index 7999fd006..e85050548 100644 --- a/samples/hello_happybase/main.py +++ b/samples/hello_happybase/main.py @@ -96,6 +96,7 @@ def main(project_id, instance_id, table_name): # [END bigtable_hw_delete_table_happybase] finally: + connection.delete_table(table_name) connection.close() diff --git a/samples/hello_happybase/main_test.py b/samples/hello_happybase/main_test.py index 6a63750da..0225e6ff8 100644 --- a/samples/hello_happybase/main_test.py +++ b/samples/hello_happybase/main_test.py @@ -19,19 +19,17 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_NAME_FORMAT = "hello-world-hb-test-{}" -TABLE_NAME_RANGE = 10000 +TABLE_ID = "hello-world-hb-test" def test_main(capsys): - table_name = TABLE_NAME_FORMAT.format(random.randrange(TABLE_NAME_RANGE)) - main(PROJECT, BIGTABLE_INSTANCE, table_name) + main(PROJECT, BIGTABLE_INSTANCE, TABLE_ID) out, _ = capsys.readouterr() - assert "Creating the {} table.".format(table_name) in out + assert "Creating the {} table.".format(TABLE_ID) in out assert "Writing some greetings to the table." in out assert "Getting a single greeting by row key." in out assert "Hello World!" in out assert "Scanning for all greetings" in out assert "Hello Cloud Bigtable!" in out - assert "Deleting the {} table.".format(table_name) in out + assert "Deleting the {} table.".format(TABLE_ID) in out diff --git a/samples/quickstart/main_async_test.py b/samples/quickstart/main_async_test.py index 841cfc180..fa95d085d 100644 --- a/samples/quickstart/main_async_test.py +++ b/samples/quickstart/main_async_test.py @@ -24,7 +24,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_FORMAT = "quickstart-test-{}" +TABLE_ID = "quickstart-async-test" @pytest_asyncio.fixture @@ -39,12 +39,11 @@ async def table_id() -> AsyncGenerator[str, None]: def _create_table(): from google.cloud import bigtable - import uuid client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() diff --git a/samples/quickstart/main_test.py b/samples/quickstart/main_test.py index 46d578b6b..22cccfde8 100644 --- a/samples/quickstart/main_test.py +++ b/samples/quickstart/main_test.py @@ -13,7 +13,6 @@ # limitations under the License. import os -import uuid from google.cloud import bigtable import pytest @@ -23,12 +22,12 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_FORMAT = "quickstart-test-{}" +TABLE_ID = "quickstart-test" @pytest.fixture() def table(): - table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8]) + table_id = TABLE_ID client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) table = instance.table(table_id) diff --git a/samples/quickstart_happybase/main_test.py b/samples/quickstart_happybase/main_test.py index dc62ebede..c662aa0e1 100644 --- a/samples/quickstart_happybase/main_test.py +++ b/samples/quickstart_happybase/main_test.py @@ -23,12 +23,12 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_FORMAT = "quickstart-hb-test-{}" +TABLE_ID = "quickstart-hb-test" @pytest.fixture() def table(): - table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8]) + table_id = TABLE_ID client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) table = instance.table(table_id) diff --git a/samples/snippets/data_client/data_client_snippets_async_test.py b/samples/snippets/data_client/data_client_snippets_async_test.py index 2e0fb9b81..759f71e45 100644 --- a/samples/snippets/data_client/data_client_snippets_async_test.py +++ b/samples/snippets/data_client/data_client_snippets_async_test.py @@ -31,7 +31,7 @@ def table_id(): client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_STATIC or f"data-client-{str(uuid.uuid4())[:16]}" + table_id = TABLE_ID_STATIC or "data-client" admin_table = instance.table(table_id) if not admin_table.exists(): diff --git a/samples/snippets/deletes/deletes_async_test.py b/samples/snippets/deletes/deletes_async_test.py index b708bd52e..35c6bcf66 100644 --- a/samples/snippets/deletes/deletes_async_test.py +++ b/samples/snippets/deletes/deletes_async_test.py @@ -25,42 +25,30 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-deletes-async" @pytest_asyncio.fixture async def table_id() -> AsyncGenerator[str, None]: - table_id = _create_table() + table, table_id = _create_table() await _populate_table(table_id) yield table_id - _delete_table(table_id) + table.delete() def _create_table(): from google.cloud import bigtable - import uuid client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() table.create(column_families={"stats_summary": None, "cell_plan": None}) - client.close() - return table_id - - -def _delete_table(table_id: str): - from google.cloud import bigtable - - client = bigtable.Client(project=PROJECT, admin=True) - instance = client.instance(BIGTABLE_INSTANCE) - table = instance.table(table_id) - table.delete() - client.close() + return table, table_id async def _populate_table(table_id): diff --git a/samples/snippets/deletes/deletes_test.py b/samples/snippets/deletes/deletes_test.py index bebaabafb..61c975b55 100644 --- a/samples/snippets/deletes/deletes_test.py +++ b/samples/snippets/deletes/deletes_test.py @@ -16,7 +16,6 @@ import datetime import os import time -import uuid from google.cloud import bigtable import pytest @@ -25,7 +24,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-deletes" @pytest.fixture(scope="module", autouse=True) @@ -35,7 +34,7 @@ def table_id(): client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() @@ -93,6 +92,7 @@ def table_id(): fetched = list(table.read_rows(row_set=row_set)) yield table_id + table.delete() def assert_output_match(capsys, expected): diff --git a/samples/snippets/filters/filter_snippets_async_test.py b/samples/snippets/filters/filter_snippets_async_test.py index 76751feaf..6f0bfb13f 100644 --- a/samples/snippets/filters/filter_snippets_async_test.py +++ b/samples/snippets/filters/filter_snippets_async_test.py @@ -29,40 +29,30 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-filters-async" @pytest_asyncio.fixture async def table_id() -> AsyncGenerator[str, None]: - table_id = _create_table() + table, table_id = _create_table() await _populate_table(table_id) yield table_id - _delete_table(table_id) + table.delete() def _create_table(): from google.cloud import bigtable - import uuid client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() table.create(column_families={"stats_summary": None, "cell_plan": None}) - return table_id - - -def _delete_table(table_id: str): - from google.cloud import bigtable - - client = bigtable.Client(project=PROJECT, admin=True) - instance = client.instance(BIGTABLE_INSTANCE) - table = instance.table(table_id) - table.delete() + return table, table_id async def _populate_table(table_id): diff --git a/samples/snippets/filters/filters_test.py b/samples/snippets/filters/filters_test.py index a84932039..913fe720f 100644 --- a/samples/snippets/filters/filters_test.py +++ b/samples/snippets/filters/filters_test.py @@ -26,7 +26,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-filters" @pytest.fixture(scope="module", autouse=True) @@ -36,7 +36,7 @@ def table_id(): client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() diff --git a/samples/snippets/reads/reads_test.py b/samples/snippets/reads/reads_test.py index da826d6fb..e6f9febbe 100644 --- a/samples/snippets/reads/reads_test.py +++ b/samples/snippets/reads/reads_test.py @@ -13,7 +13,6 @@ import datetime import os -import uuid import inspect from google.cloud import bigtable @@ -25,7 +24,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-reads" @pytest.fixture(scope="module", autouse=True) @@ -33,7 +32,7 @@ def table_id(): client = bigtable.Client(project=PROJECT, admin=True) instance = client.instance(BIGTABLE_INSTANCE) - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = instance.table(table_id) if table.exists(): table.delete() diff --git a/samples/snippets/writes/writes_test.py b/samples/snippets/writes/writes_test.py index 77ae883d6..f473780a2 100644 --- a/samples/snippets/writes/writes_test.py +++ b/samples/snippets/writes/writes_test.py @@ -28,7 +28,7 @@ PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"] BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"] -TABLE_ID_PREFIX = "mobile-time-series-{}" +TABLE_ID = "mobile-time-series-writes" @pytest.fixture @@ -43,7 +43,7 @@ def bigtable_instance(bigtable_client): @pytest.fixture def table_id(bigtable_instance): - table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16]) + table_id = TABLE_ID table = bigtable_instance.table(table_id) if table.exists(): table.delete() diff --git a/samples/tableadmin/tableadmin_test.py b/samples/tableadmin/tableadmin_test.py index 3063eee9f..cc630cda9 100755 --- a/samples/tableadmin/tableadmin_test.py +++ b/samples/tableadmin/tableadmin_test.py @@ -31,36 +31,38 @@ def test_run_table_operations(capsys): - table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8]) - - retry_429_503(run_table_operations)(PROJECT, BIGTABLE_INSTANCE, table_id) - out, _ = capsys.readouterr() + try: + table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8]) - assert "Creating the " + table_id + " table." in out - assert "Listing tables in current project." in out - assert "Creating column family cf1 with with MaxAge GC Rule" in out - assert "Created column family cf1 with MaxAge GC Rule." in out - assert "Created column family cf2 with Max Versions GC Rule." in out - assert "Created column family cf3 with Union GC rule" in out - assert "Created column family cf4 with Intersection GC rule." in out - assert "Created column family cf5 with a Nested GC rule." in out - assert "Printing Column Family and GC Rule for all column families." in out - assert "Updating column family cf1 GC rule..." in out - assert "Updated column family cf1 GC rule" in out - assert "Print column family cf1 GC rule after update..." in out - assert "Column Family: cf1" in out - assert "max_num_versions: 1" in out - assert "Delete a column family cf2..." in out - assert "Column family cf2 deleted successfully." in out + retry_429_503(run_table_operations)(PROJECT, BIGTABLE_INSTANCE, table_id) + out, _ = capsys.readouterr() - retry_429_503(delete_table)(PROJECT, BIGTABLE_INSTANCE, table_id) + assert "Creating the " + table_id + " table." in out + assert "Listing tables in current project." in out + assert "Creating column family cf1 with with MaxAge GC Rule" in out + assert "Created column family cf1 with MaxAge GC Rule." in out + assert "Created column family cf2 with Max Versions GC Rule." in out + assert "Created column family cf3 with Union GC rule" in out + assert "Created column family cf4 with Intersection GC rule." in out + assert "Created column family cf5 with a Nested GC rule." in out + assert "Printing Column Family and GC Rule for all column families." in out + assert "Updating column family cf1 GC rule..." in out + assert "Updated column family cf1 GC rule" in out + assert "Print column family cf1 GC rule after update..." in out + assert "Column Family: cf1" in out + assert "max_num_versions: 1" in out + assert "Delete a column family cf2..." in out + assert "Column family cf2 deleted successfully." in out + finally: + retry_429_503(delete_table)(PROJECT, BIGTABLE_INSTANCE, table_id) def test_delete_table(capsys): table_id = TABLE_ID_FORMAT.format(uuid.uuid4().hex[:8]) - retry_429_503(create_table)(PROJECT, BIGTABLE_INSTANCE, table_id) - - retry_429_503(delete_table)(PROJECT, BIGTABLE_INSTANCE, table_id) + try: + retry_429_503(create_table)(PROJECT, BIGTABLE_INSTANCE, table_id) + finally: + retry_429_503(delete_table)(PROJECT, BIGTABLE_INSTANCE, table_id) out, _ = capsys.readouterr() assert "Table " + table_id + " exists." in out