forked from googleapis/python-bigquery-sqlalchemy
/
test_parse_url.py
259 lines (226 loc) · 7.79 KB
/
test_parse_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Copyright (c) 2017 The PyBigQuery Authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import pytest
from sqlalchemy.engine.url import make_url
from google.cloud.bigquery import QueryJobConfig
from google.cloud.bigquery.table import EncryptionConfiguration, TableReference
from google.cloud.bigquery.dataset import DatasetReference
from pybigquery.parse_url import parse_url
@pytest.fixture(scope="session")
def url_with_everything():
# create_disposition
# CREATE_IF_NEEDED
# CREATE_NEVER
# write_disposition
# WRITE_APPEND
# WRITE_TRUNCATE
# WRITE_EMPTY
# priority
# INTERACTIVE
# BATCH
# schema_update_option
# ALLOW_FIELD_ADDITION
# ALLOW_FIELD_RELAXATION
return make_url(
"bigquery://some-project/some-dataset"
"?credentials_path=/some/path/to.json"
"&location=some-location"
"&arraysize=1000"
"&list_tables_page_size=5000"
"&clustering_fields=a,b,c"
"&create_disposition=CREATE_IF_NEEDED"
"&destination=different-project.different-dataset.table"
"&destination_encryption_configuration=some-configuration"
"&dry_run=true"
"&labels=a:b,c:d"
"&maximum_bytes_billed=1000"
"&priority=INTERACTIVE"
"&schema_update_options=ALLOW_FIELD_ADDITION,ALLOW_FIELD_RELAXATION"
"&use_query_cache=true"
"&write_disposition=WRITE_APPEND"
)
def test_basic(url_with_everything):
(
project_id,
location,
dataset_id,
arraysize,
credentials_path,
job_config,
list_tables_page_size,
) = parse_url(url_with_everything)
assert project_id == "some-project"
assert location == "some-location"
assert dataset_id == "some-dataset"
assert arraysize == 1000
assert list_tables_page_size == 5000
assert credentials_path == "/some/path/to.json"
assert isinstance(job_config, QueryJobConfig)
@pytest.mark.parametrize(
"param, value, default",
[
("clustering_fields", ["a", "b", "c"], None),
("create_disposition", "CREATE_IF_NEEDED", None),
(
"destination",
TableReference(
DatasetReference("different-project", "different-dataset"), "table"
),
None,
),
(
"destination_encryption_configuration",
lambda enc: enc.kms_key_name
== EncryptionConfiguration("some-configuration").kms_key_name,
None,
),
("dry_run", True, None),
("labels", {"a": "b", "c": "d"}, {}),
("maximum_bytes_billed", 1000, None),
("priority", "INTERACTIVE", None),
(
"schema_update_options",
["ALLOW_FIELD_ADDITION", "ALLOW_FIELD_RELAXATION"],
None,
),
("use_query_cache", True, None),
("write_disposition", "WRITE_APPEND", None),
],
)
def test_all_values(url_with_everything, param, value, default):
url_with_this_one = make_url("bigquery://some-project/some-dataset")
url_with_this_one.query[param] = url_with_everything.query[param]
for url in url_with_everything, url_with_this_one:
job_config = parse_url(url)[5]
config_value = getattr(job_config, param)
if callable(value):
assert value(config_value)
else:
assert config_value == value
url_with_nothing = make_url("bigquery://some-project/some-dataset")
job_config = parse_url(url_with_nothing)[5]
assert getattr(job_config, param) == default
@pytest.mark.parametrize(
"param, value",
[
("arraysize", "not-int"),
("list_tables_page_size", "not-int"),
("create_disposition", "not-attribute"),
("destination", "not.fully-qualified"),
("dry_run", "not-bool"),
("labels", "not-key-value"),
("maximum_bytes_billed", "not-int"),
("priority", "not-attribute"),
("schema_update_options", "not-attribute"),
("use_query_cache", "not-bool"),
("write_disposition", "not-attribute"),
],
)
def test_bad_values(param, value):
url = make_url("bigquery:///?" + param + "=" + value)
with pytest.raises(ValueError):
parse_url(url)
def test_empty_url():
for value in parse_url(make_url("bigquery://")):
assert value is None
for value in parse_url(make_url("bigquery:///")):
assert value is None
def test_empty_with_non_config():
url = parse_url(
make_url(
"bigquery:///?location=some-location&arraysize=1000&credentials_path=/some/path/to.json"
)
)
(
project_id,
location,
dataset_id,
arraysize,
credentials_path,
job_config,
list_tables_page_size,
) = url
assert project_id is None
assert location == "some-location"
assert dataset_id is None
assert arraysize == 1000
assert credentials_path == "/some/path/to.json"
assert job_config is None
assert list_tables_page_size is None
def test_only_dataset():
url = parse_url(make_url("bigquery:///some-dataset"))
(
project_id,
location,
dataset_id,
arraysize,
credentials_path,
job_config,
list_tables_page_size,
) = url
assert project_id is None
assert location is None
assert dataset_id == "some-dataset"
assert arraysize is None
assert credentials_path is None
assert list_tables_page_size is None
assert isinstance(job_config, QueryJobConfig)
# we can't actually test that the dataset is on the job_config,
# since we take care of that afterwards, when we have a client to fill in the project
@pytest.mark.parametrize(
"disallowed_arg",
[
"use_legacy_sql",
"allow_large_results",
"flatten_results",
"maximum_billing_tier",
"default_dataset",
"dataset_id",
"project_id",
],
)
def test_disallowed(disallowed_arg):
url = make_url(
"bigquery://some-project/some-dataset/?" + disallowed_arg + "=" + "whatever"
)
with pytest.raises(ValueError):
parse_url(url)
@pytest.mark.parametrize(
"not_implemented_arg",
["query_parameters", "table_definitions", "time_partitioning", "udf_resources"],
)
def test_not_implemented(not_implemented_arg):
url = make_url(
"bigquery://some-project/some-dataset/?"
+ not_implemented_arg
+ "="
+ "whatever"
)
with pytest.raises(NotImplementedError):
parse_url(url)
def test_parse_boolean():
from pybigquery.parse_url import parse_boolean
assert parse_boolean("true")
assert parse_boolean("True")
assert parse_boolean("TRUE")
assert not parse_boolean("false")
assert not parse_boolean("False")
assert not parse_boolean("FALSE")
with pytest.raises(ValueError):
parse_boolean("Thursday")