Skip to content

Commit d354349

Browse files
chore(tests): clean up sample test tables (#1030)
1 parent 2057c20 commit d354349

30 files changed

+605
-713
lines changed

samples/__init__.py

Whitespace-only changes.

samples/beam/__init__.py

Whitespace-only changes.

samples/beam/hello_world_write_test.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,33 @@
1414
import os
1515
import uuid
1616

17-
from google.cloud import bigtable
1817
import pytest
1918

20-
import hello_world_write
19+
from . import hello_world_write
20+
from ..utils import create_table_cm
2121

2222
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2323
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
24-
TABLE_ID_PREFIX = "mobile-time-series-{}"
24+
TABLE_ID = f"mobile-time-series-beam-{str(uuid.uuid4())[:16]}"
2525

2626

2727
@pytest.fixture(scope="module", autouse=True)
28-
def table_id():
29-
client = bigtable.Client(project=PROJECT, admin=True)
30-
instance = client.instance(BIGTABLE_INSTANCE)
28+
def table():
29+
with create_table_cm(
30+
PROJECT, BIGTABLE_INSTANCE, TABLE_ID, {"stats_summary": None}
31+
) as table:
32+
yield table
3133

32-
table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
33-
table = instance.table(table_id)
34-
if table.exists():
35-
table.delete()
3634

37-
table.create(column_families={"stats_summary": None})
38-
yield table_id
39-
40-
table.delete()
41-
42-
43-
def test_hello_world_write(table_id):
35+
def test_hello_world_write(table):
4436
hello_world_write.run(
4537
[
4638
"--bigtable-project=%s" % PROJECT,
4739
"--bigtable-instance=%s" % BIGTABLE_INSTANCE,
48-
"--bigtable-table=%s" % table_id,
40+
"--bigtable-table=%s" % TABLE_ID,
4941
]
5042
)
5143

52-
client = bigtable.Client(project=PROJECT, admin=True)
53-
instance = client.instance(BIGTABLE_INSTANCE)
54-
table = instance.table(table_id)
55-
5644
rows = table.read_rows()
5745
count = 0
5846
for _ in rows:

samples/hello/__init__.py

Whitespace-only changes.

samples/hello/async_main.py

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626

2727
import argparse
2828
import asyncio
29+
from ..utils import wait_for_table
2930

3031
# [START bigtable_async_hw_imports]
3132
from google.cloud import bigtable
3233
from google.cloud.bigtable.data import row_filters
3334
from google.cloud.bigtable.data import RowMutationEntry
3435
from google.cloud.bigtable.data import SetCell
3536
from google.cloud.bigtable.data import ReadRowsQuery
36-
3737
# [END bigtable_async_hw_imports]
3838

3939

@@ -65,63 +65,66 @@ async def main(project_id, instance_id, table_id):
6565
print("Table {} already exists.".format(table_id))
6666
# [END bigtable_async_hw_create_table]
6767

68-
# [START bigtable_async_hw_write_rows]
69-
print("Writing some greetings to the table.")
70-
greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"]
71-
mutations = []
72-
column = "greeting"
73-
for i, value in enumerate(greetings):
74-
# Note: This example uses sequential numeric IDs for simplicity,
75-
# but this can result in poor performance in a production
76-
# application. Since rows are stored in sorted order by key,
77-
# sequential keys can result in poor distribution of operations
78-
# across nodes.
79-
#
80-
# For more information about how to design a Bigtable schema for
81-
# the best performance, see the documentation:
82-
#
83-
# https://cloud.google.com/bigtable/docs/schema-design
84-
row_key = "greeting{}".format(i).encode()
85-
row_mutation = RowMutationEntry(
86-
row_key, SetCell(column_family_id, column, value)
87-
)
88-
mutations.append(row_mutation)
89-
await table.bulk_mutate_rows(mutations)
90-
# [END bigtable_async_hw_write_rows]
91-
92-
# [START bigtable_async_hw_create_filter]
93-
# Create a filter to only retrieve the most recent version of the cell
94-
# for each column across entire row.
95-
row_filter = row_filters.CellsColumnLimitFilter(1)
96-
# [END bigtable_async_hw_create_filter]
97-
98-
# [START bigtable_async_hw_get_with_filter]
99-
# [START bigtable_async_hw_get_by_key]
100-
print("Getting a single greeting by row key.")
101-
key = "greeting0".encode()
102-
103-
row = await table.read_row(key, row_filter=row_filter)
104-
cell = row.cells[0]
105-
print(cell.value.decode("utf-8"))
106-
# [END bigtable_async_hw_get_by_key]
107-
# [END bigtable_async_hw_get_with_filter]
108-
109-
# [START bigtable_async_hw_scan_with_filter]
110-
# [START bigtable_async_hw_scan_all]
111-
print("Scanning for all greetings:")
112-
query = ReadRowsQuery(row_filter=row_filter)
113-
async for row in await table.read_rows_stream(query):
68+
try:
69+
# let table creation complete
70+
wait_for_table(admin_table)
71+
# [START bigtable_async_hw_write_rows]
72+
print("Writing some greetings to the table.")
73+
greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"]
74+
mutations = []
75+
column = "greeting"
76+
for i, value in enumerate(greetings):
77+
# Note: This example uses sequential numeric IDs for simplicity,
78+
# but this can result in poor performance in a production
79+
# application. Since rows are stored in sorted order by key,
80+
# sequential keys can result in poor distribution of operations
81+
# across nodes.
82+
#
83+
# For more information about how to design a Bigtable schema for
84+
# the best performance, see the documentation:
85+
#
86+
# https://cloud.google.com/bigtable/docs/schema-design
87+
row_key = "greeting{}".format(i).encode()
88+
row_mutation = RowMutationEntry(
89+
row_key, SetCell(column_family_id, column, value)
90+
)
91+
mutations.append(row_mutation)
92+
await table.bulk_mutate_rows(mutations)
93+
# [END bigtable_async_hw_write_rows]
94+
95+
# [START bigtable_async_hw_create_filter]
96+
# Create a filter to only retrieve the most recent version of the cell
97+
# for each column across entire row.
98+
row_filter = row_filters.CellsColumnLimitFilter(1)
99+
# [END bigtable_async_hw_create_filter]
100+
101+
# [START bigtable_async_hw_get_with_filter]
102+
# [START bigtable_async_hw_get_by_key]
103+
print("Getting a single greeting by row key.")
104+
key = "greeting0".encode()
105+
106+
row = await table.read_row(key, row_filter=row_filter)
114107
cell = row.cells[0]
115108
print(cell.value.decode("utf-8"))
116-
# [END bigtable_async_hw_scan_all]
117-
# [END bigtable_async_hw_scan_with_filter]
118-
119-
# [START bigtable_async_hw_delete_table]
120-
# the async client only supports the data API. Table deletion as an admin operation
121-
# use admin client to create the table
122-
print("Deleting the {} table.".format(table_id))
123-
admin_table.delete()
124-
# [END bigtable_async_hw_delete_table]
109+
# [END bigtable_async_hw_get_by_key]
110+
# [END bigtable_async_hw_get_with_filter]
111+
112+
# [START bigtable_async_hw_scan_with_filter]
113+
# [START bigtable_async_hw_scan_all]
114+
print("Scanning for all greetings:")
115+
query = ReadRowsQuery(row_filter=row_filter)
116+
async for row in await table.read_rows_stream(query):
117+
cell = row.cells[0]
118+
print(cell.value.decode("utf-8"))
119+
# [END bigtable_async_hw_scan_all]
120+
# [END bigtable_async_hw_scan_with_filter]
121+
finally:
122+
# [START bigtable_async_hw_delete_table]
123+
# the async client only supports the data API. Table deletion as an admin operation
124+
# use admin client to create the table
125+
print("Deleting the {} table.".format(table_id))
126+
admin_table.delete()
127+
# [END bigtable_async_hw_delete_table]
125128

126129

127130
if __name__ == "__main__":

samples/hello/async_main_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,24 @@
1313
# limitations under the License.
1414

1515
import os
16-
import random
1716
import asyncio
17+
import uuid
1818

19-
from async_main import main
19+
from .async_main import main
2020

2121
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2222
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
23-
TABLE_NAME_FORMAT = "hello-world-test-{}"
24-
TABLE_NAME_RANGE = 10000
23+
TABLE_ID = f"hello-world-test-async-{str(uuid.uuid4())[:16]}"
2524

2625

2726
def test_async_main(capsys):
28-
table_name = TABLE_NAME_FORMAT.format(random.randrange(TABLE_NAME_RANGE))
29-
30-
asyncio.run(main(PROJECT, BIGTABLE_INSTANCE, table_name))
27+
asyncio.run(main(PROJECT, BIGTABLE_INSTANCE, TABLE_ID))
3128

3229
out, _ = capsys.readouterr()
33-
assert "Creating the {} table.".format(table_name) in out
30+
assert "Creating the {} table.".format(TABLE_ID) in out
3431
assert "Writing some greetings to the table." in out
3532
assert "Getting a single greeting by row key." in out
3633
assert "Hello World!" in out
3734
assert "Scanning for all greetings" in out
3835
assert "Hello Cloud Bigtable!" in out
39-
assert "Deleting the {} table.".format(table_name) in out
36+
assert "Deleting the {} table.".format(TABLE_ID) in out

samples/hello/main.py

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"""
2626

2727
import argparse
28+
from ..utils import wait_for_table
2829

2930
# [START bigtable_hw_imports]
3031
import datetime
@@ -60,63 +61,68 @@ def main(project_id, instance_id, table_id):
6061
print("Table {} already exists.".format(table_id))
6162
# [END bigtable_hw_create_table]
6263

63-
# [START bigtable_hw_write_rows]
64-
print("Writing some greetings to the table.")
65-
greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"]
66-
rows = []
67-
column = "greeting".encode()
68-
for i, value in enumerate(greetings):
69-
# Note: This example uses sequential numeric IDs for simplicity,
70-
# but this can result in poor performance in a production
71-
# application. Since rows are stored in sorted order by key,
72-
# sequential keys can result in poor distribution of operations
73-
# across nodes.
74-
#
75-
# For more information about how to design a Bigtable schema for
76-
# the best performance, see the documentation:
77-
#
78-
# https://cloud.google.com/bigtable/docs/schema-design
79-
row_key = "greeting{}".format(i).encode()
80-
row = table.direct_row(row_key)
81-
row.set_cell(
82-
column_family_id, column, value, timestamp=datetime.datetime.utcnow()
83-
)
84-
rows.append(row)
85-
table.mutate_rows(rows)
86-
# [END bigtable_hw_write_rows]
87-
88-
# [START bigtable_hw_create_filter]
89-
# Create a filter to only retrieve the most recent version of the cell
90-
# for each column across entire row.
91-
row_filter = row_filters.CellsColumnLimitFilter(1)
92-
# [END bigtable_hw_create_filter]
93-
94-
# [START bigtable_hw_get_with_filter]
95-
# [START bigtable_hw_get_by_key]
96-
print("Getting a single greeting by row key.")
97-
key = "greeting0".encode()
98-
99-
row = table.read_row(key, row_filter)
100-
cell = row.cells[column_family_id][column][0]
101-
print(cell.value.decode("utf-8"))
102-
# [END bigtable_hw_get_by_key]
103-
# [END bigtable_hw_get_with_filter]
104-
105-
# [START bigtable_hw_scan_with_filter]
106-
# [START bigtable_hw_scan_all]
107-
print("Scanning for all greetings:")
108-
partial_rows = table.read_rows(filter_=row_filter)
109-
110-
for row in partial_rows:
64+
try:
65+
# let table creation complete
66+
wait_for_table(table)
67+
68+
# [START bigtable_hw_write_rows]
69+
print("Writing some greetings to the table.")
70+
greetings = ["Hello World!", "Hello Cloud Bigtable!", "Hello Python!"]
71+
rows = []
72+
column = "greeting".encode()
73+
for i, value in enumerate(greetings):
74+
# Note: This example uses sequential numeric IDs for simplicity,
75+
# but this can result in poor performance in a production
76+
# application. Since rows are stored in sorted order by key,
77+
# sequential keys can result in poor distribution of operations
78+
# across nodes.
79+
#
80+
# For more information about how to design a Bigtable schema for
81+
# the best performance, see the documentation:
82+
#
83+
# https://cloud.google.com/bigtable/docs/schema-design
84+
row_key = "greeting{}".format(i).encode()
85+
row = table.direct_row(row_key)
86+
row.set_cell(
87+
column_family_id, column, value, timestamp=datetime.datetime.utcnow()
88+
)
89+
rows.append(row)
90+
table.mutate_rows(rows)
91+
# [END bigtable_hw_write_rows]
92+
93+
# [START bigtable_hw_create_filter]
94+
# Create a filter to only retrieve the most recent version of the cell
95+
# for each column across entire row.
96+
row_filter = row_filters.CellsColumnLimitFilter(1)
97+
# [END bigtable_hw_create_filter]
98+
99+
# [START bigtable_hw_get_with_filter]
100+
# [START bigtable_hw_get_by_key]
101+
print("Getting a single greeting by row key.")
102+
key = "greeting0".encode()
103+
104+
row = table.read_row(key, row_filter)
111105
cell = row.cells[column_family_id][column][0]
112106
print(cell.value.decode("utf-8"))
113-
# [END bigtable_hw_scan_all]
114-
# [END bigtable_hw_scan_with_filter]
115-
116-
# [START bigtable_hw_delete_table]
117-
print("Deleting the {} table.".format(table_id))
118-
table.delete()
119-
# [END bigtable_hw_delete_table]
107+
# [END bigtable_hw_get_by_key]
108+
# [END bigtable_hw_get_with_filter]
109+
110+
# [START bigtable_hw_scan_with_filter]
111+
# [START bigtable_hw_scan_all]
112+
print("Scanning for all greetings:")
113+
partial_rows = table.read_rows(filter_=row_filter)
114+
115+
for row in partial_rows:
116+
cell = row.cells[column_family_id][column][0]
117+
print(cell.value.decode("utf-8"))
118+
# [END bigtable_hw_scan_all]
119+
# [END bigtable_hw_scan_with_filter]
120+
121+
finally:
122+
# [START bigtable_hw_delete_table]
123+
print("Deleting the {} table.".format(table_id))
124+
table.delete()
125+
# [END bigtable_hw_delete_table]
120126

121127

122128
if __name__ == "__main__":

samples/hello/main_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,23 @@
1313
# limitations under the License.
1414

1515
import os
16-
import random
16+
import uuid
1717

18-
from main import main
18+
from .main import main
1919

2020
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2121
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
22-
TABLE_NAME_FORMAT = "hello-world-test-{}"
23-
TABLE_NAME_RANGE = 10000
22+
TABLE_ID = f"hello-world-test-{str(uuid.uuid4())[:16]}"
2423

2524

2625
def test_main(capsys):
27-
table_name = TABLE_NAME_FORMAT.format(random.randrange(TABLE_NAME_RANGE))
28-
29-
main(PROJECT, BIGTABLE_INSTANCE, table_name)
26+
main(PROJECT, BIGTABLE_INSTANCE, TABLE_ID)
3027

3128
out, _ = capsys.readouterr()
32-
assert "Creating the {} table.".format(table_name) in out
29+
assert "Creating the {} table.".format(TABLE_ID) in out
3330
assert "Writing some greetings to the table." in out
3431
assert "Getting a single greeting by row key." in out
3532
assert "Hello World!" in out
3633
assert "Scanning for all greetings" in out
3734
assert "Hello Cloud Bigtable!" in out
38-
assert "Deleting the {} table.".format(table_name) in out
35+
assert "Deleting the {} table.".format(TABLE_ID) in out

samples/hello_happybase/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)