Skip to content

Commit

Permalink
chore(tests): clean up sample test tables
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Oct 25, 2024
1 parent 18650e7 commit 1270f03
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 119 deletions.
15 changes: 5 additions & 10 deletions samples/beam/hello_world_write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,38 +20,34 @@

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)
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:
Expand Down
33 changes: 19 additions & 14 deletions samples/hello/async_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
32 changes: 18 additions & 14 deletions samples/hello/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions samples/hello_happybase/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
10 changes: 4 additions & 6 deletions samples/hello_happybase/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions samples/quickstart/main_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions samples/quickstart/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

import os
import uuid

from google.cloud import bigtable
import pytest
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions samples/quickstart_happybase/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
22 changes: 5 additions & 17 deletions samples/snippets/deletes/deletes_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions samples/snippets/deletes/deletes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import datetime
import os
import time
import uuid

from google.cloud import bigtable
import pytest
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
20 changes: 5 additions & 15 deletions samples/snippets/filters/filter_snippets_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions samples/snippets/filters/filters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 1270f03

Please sign in to comment.