3131 NUM_ROWS ,
3232 INITIAL_CELL_VALUE ,
3333 NEW_CELL_VALUE ,
34+ generate_unique_suffix ,
3435)
3536
3637from datetime import datetime , timedelta
3738
3839import asyncio
3940import pytest
40- import time
4141
4242
4343if CrossSync .is_async :
@@ -61,56 +61,59 @@ def event_loop():
6161
6262@CrossSync .convert
6363@CrossSync .pytest_fixture (scope = "session" )
64- async def data_client ():
65- client = CrossSync .DataClient ()
66- yield client
67- await client .close ()
64+ async def data_client (admin_overlay_project_id ):
65+ async with CrossSync .DataClient (project = admin_overlay_project_id ) as client :
66+ yield client
6867
6968
7069@CrossSync .convert (
7170 replace_symbols = {"BigtableTableAdminAsyncClient" : "BigtableTableAdminClient" }
7271)
7372@CrossSync .pytest_fixture (scope = "session" )
74- async def table_admin_client ():
75- client = admin_v2 .BigtableTableAdminAsyncClient ()
76- yield client
77- await client .transport .close ()
73+ async def table_admin_client (admin_overlay_project_id ):
74+ async with admin_v2 .BigtableTableAdminAsyncClient (
75+ client_options = {
76+ "quota_project_id" : admin_overlay_project_id ,
77+ }
78+ ) as client :
79+ yield client
7880
7981
8082@CrossSync .convert (
8183 replace_symbols = {"BigtableInstanceAdminAsyncClient" : "BigtableInstanceAdminClient" }
8284)
8385@CrossSync .pytest_fixture (scope = "session" )
84- async def instance_admin_client ():
85- client = admin_v2 .BigtableInstanceAdminAsyncClient ()
86- yield client
87- await client .transport .close ()
86+ async def instance_admin_client (admin_overlay_project_id ):
87+ async with admin_v2 .BigtableInstanceAdminAsyncClient (
88+ client_options = {
89+ "quota_project_id" : admin_overlay_project_id ,
90+ }
91+ ) as client :
92+ yield client
8893
8994
9095@CrossSync .convert
9196@CrossSync .pytest_fixture (scope = "session" )
9297async def instances_to_delete (instance_admin_client ):
9398 instances = []
9499
95- yield instances
96-
97- for instance in instances :
98- await instance_admin_client .delete_instance (name = instance .name )
100+ try :
101+ yield instances
102+ finally :
103+ for instance in instances :
104+ await instance_admin_client .delete_instance (name = instance .name )
99105
100106
101107@CrossSync .convert
102108@CrossSync .pytest_fixture (scope = "session" )
103109async def backups_to_delete (table_admin_client ):
104110 backups = []
105111
106- yield backups
107-
108- for backup in backups :
109- await table_admin_client .delete_backup (name = backup .name )
110-
111-
112- def ctime () -> int :
113- return int (time .time ())
112+ try :
113+ yield backups
114+ finally :
115+ for backup in backups :
116+ await table_admin_client .delete_backup (name = backup .name )
114117
115118
116119@CrossSync .convert
@@ -123,10 +126,16 @@ async def create_instance(
123126 storage_type = admin_v2 .StorageType .HDD ,
124127 cluster_locations = DEFAULT_CLUSTER_LOCATIONS ,
125128) -> Tuple [admin_v2 .Instance , admin_v2 .Table ]:
129+ """
130+ Creates a new Bigtable instance with the specified project_id, storage type, and cluster locations.
131+
132+ After creating the Bigtable instance, it will create a test table and populate it with dummy data.
133+ This is not defined as a fixture because the different system tests need different kinds of instances.
134+ """
126135 # Create the instance
127136 clusters = {}
128137
129- instance_id = f" { INSTANCE_PREFIX } - { ctime () } "
138+ instance_id = generate_unique_suffix ( INSTANCE_PREFIX )
130139
131140 for idx , location in enumerate (cluster_locations ):
132141 clusters [location ] = admin_v2 .Cluster (
@@ -175,6 +184,12 @@ async def create_instance(
175184
176185@CrossSync .convert
177186async def populate_table (table_admin_client , data_client , instance , table , cell_value ):
187+ """
188+ Populates all the test cells in the given table with the given cell value.
189+
190+ This is used to populate test data when creating an instance, and for testing the
191+ wait_for_consistency call.
192+ """
178193 data_client_table = data_client .get_table (
179194 table_admin_client .parse_instance_path (instance .name )["instance" ],
180195 table_admin_client .parse_table_path (table .name )["table" ],
@@ -202,13 +217,19 @@ async def populate_table(table_admin_client, data_client, instance, table, cell_
202217async def create_backup (
203218 instance_admin_client , table_admin_client , instance , table , backups_to_delete
204219) -> admin_v2 .Backup :
220+ """
221+ Creates a backup of the given table under the given instance.
222+
223+ This will be restored to a different instance later on, to test
224+ optimize_restored_table.
225+ """
205226 # Get a cluster in the instance for the backup
206227 list_clusters_response = await instance_admin_client .list_clusters (
207228 parent = instance .name
208229 )
209230 cluster_name = list_clusters_response .clusters [0 ].name
210231
211- backup_id = f" { BACKUP_PREFIX } - { ctime () } "
232+ backup_id = generate_unique_suffix ( BACKUP_PREFIX )
212233
213234 # Create the backup
214235 operation = await table_admin_client .create_backup (
@@ -232,6 +253,9 @@ async def create_backup(
232253async def assert_table_cell_value_equal_to (
233254 table_admin_client , data_client , instance , table , value
234255):
256+ """
257+ Asserts that all cells in the given table have the given value.
258+ """
235259 data_client_table = data_client .get_table (
236260 table_admin_client .parse_instance_path (instance .name )["instance" ],
237261 table_admin_client .parse_table_path (table .name )["table" ],
@@ -330,7 +354,6 @@ async def test_optimize_restored_table(
330354 )
331355
332356
333- @CrossSync .convert
334357@CrossSync .pytest
335358async def test_wait_for_consistency (
336359 instance_admin_client ,
0 commit comments