23
23
24
24
from google .cloud .bigquery .client import Client
25
25
from google .cloud .bigquery import _job_helpers
26
+ from google .cloud .bigquery .job import copy_ as job_copy
27
+ from google .cloud .bigquery .job import extract as job_extract
28
+ from google .cloud .bigquery .job import load as job_load
26
29
from google .cloud .bigquery .job import query as job_query
27
30
from google .cloud .bigquery .query import ConnectionProperty , ScalarQueryParameter
28
31
@@ -57,9 +60,34 @@ def make_query_response(
57
60
@pytest .mark .parametrize (
58
61
("job_config" , "expected" ),
59
62
(
60
- (None , make_query_request ()),
61
- (job_query .QueryJobConfig (), make_query_request ()),
62
- (
63
+ pytest .param (
64
+ None ,
65
+ make_query_request (),
66
+ id = "job_config=None-default-request" ,
67
+ ),
68
+ pytest .param (
69
+ job_query .QueryJobConfig (),
70
+ make_query_request (),
71
+ id = "job_config=QueryJobConfig()-default-request" ,
72
+ ),
73
+ pytest .param (
74
+ job_query .QueryJobConfig .from_api_repr (
75
+ {
76
+ "unknownTopLevelProperty" : "some-test-value" ,
77
+ "query" : {
78
+ "unknownQueryProperty" : "some-other-value" ,
79
+ },
80
+ },
81
+ ),
82
+ make_query_request (
83
+ {
84
+ "unknownTopLevelProperty" : "some-test-value" ,
85
+ "unknownQueryProperty" : "some-other-value" ,
86
+ }
87
+ ),
88
+ id = "job_config-with-unknown-properties-includes-all-properties-in-request" ,
89
+ ),
90
+ pytest .param (
63
91
job_query .QueryJobConfig (default_dataset = "my-project.my_dataset" ),
64
92
make_query_request (
65
93
{
@@ -69,17 +97,24 @@ def make_query_response(
69
97
}
70
98
}
71
99
),
100
+ id = "job_config-with-default_dataset" ,
72
101
),
73
- (job_query .QueryJobConfig (dry_run = True ), make_query_request ({"dryRun" : True })),
74
- (
102
+ pytest .param (
103
+ job_query .QueryJobConfig (dry_run = True ),
104
+ make_query_request ({"dryRun" : True }),
105
+ id = "job_config-with-dry_run" ,
106
+ ),
107
+ pytest .param (
75
108
job_query .QueryJobConfig (use_query_cache = False ),
76
109
make_query_request ({"useQueryCache" : False }),
110
+ id = "job_config-with-use_query_cache" ,
77
111
),
78
- (
112
+ pytest . param (
79
113
job_query .QueryJobConfig (use_legacy_sql = True ),
80
114
make_query_request ({"useLegacySql" : True }),
115
+ id = "job_config-with-use_legacy_sql" ,
81
116
),
82
- (
117
+ pytest . param (
83
118
job_query .QueryJobConfig (
84
119
query_parameters = [
85
120
ScalarQueryParameter ("named_param1" , "STRING" , "param-value" ),
@@ -103,8 +138,9 @@ def make_query_response(
103
138
],
104
139
}
105
140
),
141
+ id = "job_config-with-query_parameters-named" ,
106
142
),
107
- (
143
+ pytest . param (
108
144
job_query .QueryJobConfig (
109
145
query_parameters = [
110
146
ScalarQueryParameter (None , "STRING" , "param-value" ),
@@ -126,8 +162,9 @@ def make_query_response(
126
162
],
127
163
}
128
164
),
165
+ id = "job_config-with-query_parameters-positional" ,
129
166
),
130
- (
167
+ pytest . param (
131
168
job_query .QueryJobConfig (
132
169
connection_properties = [
133
170
ConnectionProperty (key = "time_zone" , value = "America/Chicago" ),
@@ -142,14 +179,17 @@ def make_query_response(
142
179
]
143
180
}
144
181
),
182
+ id = "job_config-with-connection_properties" ,
145
183
),
146
- (
184
+ pytest . param (
147
185
job_query .QueryJobConfig (labels = {"abc" : "def" }),
148
186
make_query_request ({"labels" : {"abc" : "def" }}),
187
+ id = "job_config-with-labels" ,
149
188
),
150
- (
189
+ pytest . param (
151
190
job_query .QueryJobConfig (maximum_bytes_billed = 987654 ),
152
191
make_query_request ({"maximumBytesBilled" : "987654" }),
192
+ id = "job_config-with-maximum_bytes_billed" ,
153
193
),
154
194
),
155
195
)
@@ -159,6 +199,19 @@ def test__to_query_request(job_config, expected):
159
199
assert result == expected
160
200
161
201
202
+ @pytest .mark .parametrize (
203
+ ("job_config" , "invalid_key" ),
204
+ (
205
+ pytest .param (job_copy .CopyJobConfig (), "copy" , id = "copy" ),
206
+ pytest .param (job_extract .ExtractJobConfig (), "extract" , id = "extract" ),
207
+ pytest .param (job_load .LoadJobConfig (), "load" , id = "load" ),
208
+ ),
209
+ )
210
+ def test__to_query_request_raises_for_invalid_config (job_config , invalid_key ):
211
+ with pytest .raises (ValueError , match = f"{ repr (invalid_key )} in job_config" ):
212
+ _job_helpers ._to_query_request (job_config , query = "SELECT 1" )
213
+
214
+
162
215
def test__to_query_job_defaults ():
163
216
mock_client = mock .create_autospec (Client )
164
217
response = make_query_response (
0 commit comments