22
22
23
23
import google .auth .credentials
24
24
from google .api_core import exceptions
25
+ from google .cloud import bigquery
25
26
import google .cloud .bigquery .client
26
27
from google .cloud .bigquery import _job_helpers
27
28
@@ -40,10 +41,15 @@ def make_response(body, *, status_code: int = 200):
40
41
@pytest .fixture
41
42
def client ():
42
43
"""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
+ )
44
47
http_session = mock .Mock ()
45
48
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 ,
47
53
)
48
54
49
55
@@ -66,39 +72,89 @@ def test_query_and_wait_bigframes_callback(client):
66
72
callback .assert_has_calls (
67
73
[
68
74
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
+ )
77
136
),
78
137
mock .call (
79
- _job_helpers .QueryCompleteEvent (
138
+ _job_helpers .QueryCompleteEvent (
80
139
billing_project = PROJECT ,
81
140
location = LOCATION ,
82
141
query_id = "abcdefg" ,
83
142
total_rows = 100 ,
84
143
total_bytes_processed = 123 ,
85
144
slot_millis = 987 ,
86
145
# No job ID or destination, because a basic query is elegible for jobs.query.
87
- job_id = None ,
146
+ job_id = None ,
88
147
destination = None ,
89
- ),
148
+ ),
90
149
),
91
150
]
92
151
)
93
152
94
153
95
- def test_query_and_wait_bigframes_with_job_retry_callbacks (client ):
154
+ def test_query_and_wait_bigframes_with_query_retry_callbacks (client ):
96
155
client ._http .request .side_effect = [
97
156
exceptions .InternalServerError (
98
- "first try" ,
99
- errors = (
100
- {"reason" : "jobInternalError" },
101
- )
157
+ "first try" , errors = ({"reason" : "jobInternalError" },)
102
158
),
103
159
make_response (
104
160
{
@@ -117,37 +173,37 @@ def test_query_and_wait_bigframes_with_job_retry_callbacks(client):
117
173
callback .assert_has_calls (
118
174
[
119
175
mock .call (
120
- _job_helpers .QuerySentEvent (
176
+ _job_helpers .QuerySentEvent (
121
177
query = "SELECT 1" ,
122
178
billing_project = PROJECT ,
123
179
location = LOCATION ,
124
180
# No job ID, because a basic query is elegible for jobs.query.
125
- job_id = None ,
181
+ job_id = None ,
126
182
request_id = mock .ANY ,
127
- )
183
+ )
128
184
),
129
185
mock .call (
130
- _job_helpers .QueryRetryEvent (
186
+ _job_helpers .QueryRetryEvent (
131
187
query = "SELECT 1" ,
132
188
billing_project = PROJECT ,
133
189
location = LOCATION ,
134
190
# No job ID, because a basic query is elegible for jobs.query.
135
- job_id = None ,
191
+ job_id = None ,
136
192
request_id = mock .ANY ,
137
- )
193
+ )
138
194
),
139
195
mock .call (
140
- _job_helpers .QueryCompleteEvent (
196
+ _job_helpers .QueryCompleteEvent (
141
197
billing_project = PROJECT ,
142
198
location = LOCATION ,
143
199
query_id = mock .ANY ,
144
200
total_rows = 100 ,
145
201
total_bytes_processed = 123 ,
146
202
slot_millis = 987 ,
147
203
# No job ID or destination, because a basic query is elegible for jobs.query.
148
- job_id = None ,
204
+ job_id = None ,
149
205
destination = None ,
150
- ),
206
+ ),
151
207
),
152
208
]
153
209
)
0 commit comments