@@ -292,6 +292,80 @@ def test_temporary_dataset_is_unique(self, patched_time_sleep):
292292 wrapper .create_temporary_dataset ('project-id' , 'location' )
293293 self .assertTrue (client .datasets .Get .called )
294294
295+ def test_get_table_invokes_tables_get_and_caches_result (self ):
296+
297+ from apache_beam .io .gcp .bigquery_tools import BigQueryWrapper
298+
299+ client = mock .Mock ()
300+ client .tables = mock .Mock ()
301+
302+ returned_table = mock .Mock (name = "BigQueryTable" )
303+ client .tables .Get = mock .Mock (return_value = returned_table )
304+
305+ wrapper = BigQueryWrapper (client = client )
306+
307+ project_id = "my-project"
308+ dataset_id = "my_dataset"
309+ table_id = "my_table"
310+
311+ table1 = wrapper .get_table (project_id , dataset_id , table_id )
312+
313+ assert table1 is returned_table
314+ assert client .tables .Get .call_count == 1
315+
316+ (request ,), _ = client .tables .Get .call_args
317+ assert isinstance (request , bigquery .BigqueryTablesGetRequest )
318+ assert request .projectId == project_id
319+ assert request .datasetId == dataset_id
320+ assert request .tableId == table_id
321+
322+ table2 = wrapper .get_table (project_id , dataset_id , table_id )
323+
324+ assert table2 is returned_table
325+ assert client .tables .Get .call_count == 1 # still 1 => cached
326+
327+ def test_get_table_shared_cache_across_wrapper_instances (self ):
328+ from apache_beam .io .gcp .bigquery_tools import BigQueryWrapper
329+
330+ # ensure isolation -> clear the shared cache before the test
331+ BigQueryWrapper ._TABLE_CACHE .clear ()
332+
333+ client = mock .Mock ()
334+ client .tables = mock .Mock ()
335+
336+ returned_table = mock .Mock (name = "BigQueryTable" )
337+ client .tables .Get = mock .Mock (return_value = returned_table )
338+
339+ project_id = "my-project"
340+ dataset_id = "my_dataset"
341+ table_id = "my_table"
342+
343+ w1 = BigQueryWrapper (client = client )
344+ w2 = BigQueryWrapper (client = client )
345+ w3 = BigQueryWrapper (client = client )
346+
347+ # first call -> populate cache
348+ t1 = w1 .get_table (project_id , dataset_id , table_id )
349+ assert t1 is returned_table
350+ assert client .tables .Get .call_count == 1
351+
352+ # verify request shape (from first call)
353+ (request ,), _ = client .tables .Get .call_args
354+ assert isinstance (request , bigquery .BigqueryTablesGetRequest )
355+ assert request .projectId == project_id
356+ assert request .datasetId == dataset_id
357+ assert request .tableId == table_id
358+
359+ # calls from DIFFERENT wrapper instances -> hit the SAME cache entry
360+ t2 = w2 .get_table (project_id , dataset_id , table_id )
361+ t3 = w3 .get_table (project_id , dataset_id , table_id )
362+
363+ assert t2 is returned_table
364+ assert t3 is returned_table
365+
366+ # still 1 -> record cached across instances
367+ assert client .tables .Get .call_count == 1
368+
295369 def test_get_or_create_dataset_created (self ):
296370 client = mock .Mock ()
297371 client .datasets .Get .side_effect = HttpError (
0 commit comments