2222
2323import google .auth .credentials
2424from google .api_core import exceptions
25+ from google .cloud import bigquery
2526import google .cloud .bigquery .client
2627from google .cloud .bigquery import _job_helpers
2728
@@ -40,10 +41,15 @@ def make_response(body, *, status_code: int = 200):
4041@pytest .fixture
4142def client ():
4243 """A real client object with mocked API requests."""
43- credentials = mock .create_autospec (google .auth .credentials .Credentials , instance = True )
44+ credentials = mock .create_autospec (
45+ google .auth .credentials .Credentials , instance = True
46+ )
4447 http_session = mock .Mock ()
4548 return google .cloud .bigquery .client .Client (
46- project = PROJECT , credentials = credentials , _http = http_session , location = LOCATION ,
49+ project = PROJECT ,
50+ credentials = credentials ,
51+ _http = http_session ,
52+ location = LOCATION ,
4753 )
4854
4955
@@ -66,39 +72,89 @@ def test_query_and_wait_bigframes_callback(client):
6672 callback .assert_has_calls (
6773 [
6874 mock .call (
69- _job_helpers .QuerySentEvent (
70- query = "SELECT 1" ,
71- billing_project = PROJECT ,
72- location = LOCATION ,
73- # No job ID, because a basic query is elegible for jobs.query.
74- job_id = None ,
75- request_id = mock .ANY ,
76- )
75+ _job_helpers .QuerySentEvent (
76+ query = "SELECT 1" ,
77+ billing_project = PROJECT ,
78+ location = LOCATION ,
79+ # No job ID, because a basic query is elegible for jobs.query.
80+ job_id = None ,
81+ request_id = mock .ANY ,
82+ )
83+ ),
84+ mock .call (
85+ _job_helpers .QueryCompleteEvent (
86+ billing_project = PROJECT ,
87+ location = LOCATION ,
88+ query_id = "abcdefg" ,
89+ total_rows = 100 ,
90+ total_bytes_processed = 123 ,
91+ slot_millis = 987 ,
92+ # No job ID or destination, because a basic query is elegible for jobs.query.
93+ job_id = None ,
94+ destination = None ,
95+ ),
96+ ),
97+ ]
98+ )
99+
100+
101+ def test_query_and_wait_bigframes_with_job_callback (client ):
102+ client ._http .request .side_effect = [
103+ make_response (
104+ {
105+ # https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert
106+ # https://cloud.google.com/bigquery/docs/reference/rest/v2/Job
107+ "jobReference" : {},
108+ }
109+ ),
110+ make_response (
111+ {
112+ # https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/insert
113+ # https://cloud.google.com/bigquery/docs/reference/rest/v2/Job
114+ "jobReference" : {},
115+ "status" : {"state" : "DONE" },
116+ }
117+ ),
118+ ]
119+ callback = mock .Mock ()
120+ config = bigquery .QueryJobConfig ()
121+ config .destination = "proj.dset.table"
122+ client ._query_and_wait_bigframes (
123+ query = "SELECT 1" , job_config = config , callback = callback
124+ )
125+ callback .assert_has_calls (
126+ [
127+ mock .call (
128+ _job_helpers .QuerySentEvent (
129+ query = "SELECT 1" ,
130+ billing_project = PROJECT ,
131+ location = LOCATION ,
132+ # No job ID, because a basic query is elegible for jobs.query.
133+ job_id = None ,
134+ request_id = mock .ANY ,
135+ )
77136 ),
78137 mock .call (
79- _job_helpers .QueryCompleteEvent (
138+ _job_helpers .QueryCompleteEvent (
80139 billing_project = PROJECT ,
81140 location = LOCATION ,
82141 query_id = "abcdefg" ,
83142 total_rows = 100 ,
84143 total_bytes_processed = 123 ,
85144 slot_millis = 987 ,
86145 # No job ID or destination, because a basic query is elegible for jobs.query.
87- job_id = None ,
146+ job_id = None ,
88147 destination = None ,
89- ),
148+ ),
90149 ),
91150 ]
92151 )
93152
94153
95- def test_query_and_wait_bigframes_with_job_retry_callbacks (client ):
154+ def test_query_and_wait_bigframes_with_query_retry_callbacks (client ):
96155 client ._http .request .side_effect = [
97156 exceptions .InternalServerError (
98- "first try" ,
99- errors = (
100- {"reason" : "jobInternalError" },
101- )
157+ "first try" , errors = ({"reason" : "jobInternalError" },)
102158 ),
103159 make_response (
104160 {
@@ -117,37 +173,37 @@ def test_query_and_wait_bigframes_with_job_retry_callbacks(client):
117173 callback .assert_has_calls (
118174 [
119175 mock .call (
120- _job_helpers .QuerySentEvent (
176+ _job_helpers .QuerySentEvent (
121177 query = "SELECT 1" ,
122178 billing_project = PROJECT ,
123179 location = LOCATION ,
124180 # No job ID, because a basic query is elegible for jobs.query.
125- job_id = None ,
181+ job_id = None ,
126182 request_id = mock .ANY ,
127- )
183+ )
128184 ),
129185 mock .call (
130- _job_helpers .QueryRetryEvent (
186+ _job_helpers .QueryRetryEvent (
131187 query = "SELECT 1" ,
132188 billing_project = PROJECT ,
133189 location = LOCATION ,
134190 # No job ID, because a basic query is elegible for jobs.query.
135- job_id = None ,
191+ job_id = None ,
136192 request_id = mock .ANY ,
137- )
193+ )
138194 ),
139195 mock .call (
140- _job_helpers .QueryCompleteEvent (
196+ _job_helpers .QueryCompleteEvent (
141197 billing_project = PROJECT ,
142198 location = LOCATION ,
143199 query_id = mock .ANY ,
144200 total_rows = 100 ,
145201 total_bytes_processed = 123 ,
146202 slot_millis = 987 ,
147203 # No job ID or destination, because a basic query is elegible for jobs.query.
148- job_id = None ,
204+ job_id = None ,
149205 destination = None ,
150- ),
206+ ),
151207 ),
152208 ]
153209 )
0 commit comments