77import pathlib
88
99import pytest
10+ import google .api_core .exceptions
1011from google .cloud import bigquery
1112
1213from typing import List
@@ -21,9 +22,6 @@ def load_sample_data(
2122 bigquery_schema : List [bigquery .SchemaField ],
2223 filename : str = "sample.json" ,
2324):
24- # Delete the table first. Even though we can use WRITE_TRUNCATE, the load
25- # job fails if properties such as table description do not match.
26- bigquery_client .delete_table (full_table_id , not_found_ok = True )
2725 sample_config = bigquery .LoadJobConfig ()
2826 sample_config .destination_table_description = (
2927 "A sample table containing most data types."
@@ -58,27 +56,42 @@ def bigquery_dataset(
5856 dataset_id = "test_pybigquery"
5957 dataset = bigquery .Dataset (f"{ project_id } .{ dataset_id } " )
6058 dataset = bigquery_client .create_dataset (dataset , exists_ok = True )
61- empty_table = bigquery .Table (
62- f"{ project_id } .{ dataset_id } .sample_dml" , schema = bigquery_schema
63- )
59+ sample_table_id = f"{ project_id } .{ dataset_id } .sample"
60+ try :
61+ # Since the data changes rarely and the tests are mostly read-only,
62+ # only create the tables if they don't already exist.
63+ # TODO: Create shared sample data tables in bigquery-public-data that
64+ # include test values for all data types.
65+ bigquery_client .get_table (sample_table_id )
66+ except google .api_core .exceptions .NotFound :
67+ job1 = load_sample_data (sample_table_id , bigquery_client , bigquery_schema )
68+ job1 .result ()
69+ one_row_table_id = f"{ project_id } .{ dataset_id } .sample_one_row"
70+ try :
71+ bigquery_client .get_table (one_row_table_id )
72+ except google .api_core .exceptions .NotFound :
73+ job2 = load_sample_data (
74+ one_row_table_id ,
75+ bigquery_client ,
76+ bigquery_schema ,
77+ filename = "sample_one_row.json" ,
78+ )
79+ job2 .result ()
6480 view = bigquery .Table (f"{ project_id } .{ dataset_id } .sample_view" ,)
6581 view .view_query = f"SELECT string FROM `{ dataset_id } .sample`"
66- job1 = load_sample_data (
67- f"{ project_id } .{ dataset_id } .sample" , bigquery_client , bigquery_schema
68- )
69- job2 = load_sample_data (
70- f"{ project_id } .{ dataset_id } .sample_one_row" ,
71- bigquery_client ,
72- bigquery_schema ,
73- filename = "sample_one_row.json" ,
74- )
75- bigquery_client .create_table (empty_table , exists_ok = True )
76- job1 .result ()
77- job2 .result ()
7882 bigquery_client .create_table (view , exists_ok = True )
7983 return dataset_id
8084
8185
86+ @pytest .fixture (scope = "session" , autouse = True )
87+ def bigquery_empty_table (bigquery_dataset , bigquery_client , bigquery_schema ):
88+ project_id = bigquery_client .project
89+ dataset_id = bigquery_dataset
90+ table_id = f"{ project_id } .{ dataset_id } .sample_dml"
91+ empty_table = bigquery .Table (table_id , schema = bigquery_schema )
92+ bigquery_client .create_table (empty_table , exists_ok = True )
93+
94+
8295@pytest .fixture (scope = "session" , autouse = True )
8396def bigquery_alt_dataset (
8497 bigquery_client : bigquery .Client , bigquery_schema : List [bigquery .SchemaField ]
@@ -87,10 +100,12 @@ def bigquery_alt_dataset(
87100 dataset_id = "test_pybigquery_alt"
88101 dataset = bigquery .Dataset (f"{ project_id } .{ dataset_id } " )
89102 dataset = bigquery_client .create_dataset (dataset , exists_ok = True )
90- job = load_sample_data (
91- f"{ project_id } .{ dataset_id } .sample_alt" , bigquery_client , bigquery_schema
92- )
93- job .result ()
103+ sample_table_id = f"{ project_id } .{ dataset_id } .sample_alt"
104+ try :
105+ bigquery_client .get_table (sample_table_id )
106+ except google .api_core .exceptions .NotFound :
107+ job = load_sample_data (sample_table_id , bigquery_client , bigquery_schema )
108+ job .result ()
94109 return dataset_id
95110
96111
@@ -101,11 +116,15 @@ def bigquery_regional_dataset(bigquery_client, bigquery_schema):
101116 dataset = bigquery .Dataset (f"{ project_id } .{ dataset_id } " )
102117 dataset .location = "asia-northeast1"
103118 dataset = bigquery_client .create_dataset (dataset , exists_ok = True )
104- job = load_sample_data (
105- f"{ project_id } .{ dataset_id } .sample_one_row" ,
106- bigquery_client ,
107- bigquery_schema ,
108- filename = "sample_one_row.json" ,
109- )
110- job .result ()
119+ sample_table_id = f"{ project_id } .{ dataset_id } .sample_one_row"
120+ try :
121+ bigquery_client .get_table (sample_table_id )
122+ except google .api_core .exceptions .NotFound :
123+ job = load_sample_data (
124+ sample_table_id ,
125+ bigquery_client ,
126+ bigquery_schema ,
127+ filename = "sample_one_row.json" ,
128+ )
129+ job .result ()
111130 return dataset_id
0 commit comments