-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
test_databricks_sql.py
312 lines (298 loc) · 11.6 KB
/
test_databricks_sql.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import os
from unittest.mock import patch
import pytest
from airflow.providers.common.sql.hooks.sql import fetch_all_handler
from airflow.providers.databricks.operators.databricks_sql import DatabricksSqlOperator, Row
from airflow.serialization.serde import serialize
DATE = "2017-04-20"
TASK_ID = "databricks-sql-operator"
DEFAULT_CONN_ID = "databricks_default"
@pytest.mark.parametrize(
"sql, return_last, split_statement, hook_results, hook_descriptions, expected_results",
[
pytest.param(
"select * from dummy",
True,
True,
[Row(id=1, value="value1"), Row(id=2, value="value2")],
[[("id",), ("value",)]],
([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")]),
id="Scalar: Single SQL statement, return_last, split statement",
),
pytest.param(
"select * from dummy;select * from dummy2",
True,
True,
[Row(id=1, value="value1"), Row(id=2, value="value2")],
[[("id",), ("value",)]],
([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")]),
id="Scalar: Multiple SQL statements, return_last, split statement",
),
pytest.param(
"select * from dummy",
False,
False,
[Row(id=1, value="value1"), Row(id=2, value="value2")],
[[("id",), ("value",)]],
([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")]),
id="Scalar: Single SQL statements, no return_last (doesn't matter), no split statement",
),
pytest.param(
"select * from dummy",
True,
False,
[Row(id=1, value="value1"), Row(id=2, value="value2")],
[[("id",), ("value",)]],
([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")]),
id="Scalar: Single SQL statements, return_last (doesn't matter), no split statement",
),
pytest.param(
["select * from dummy"],
False,
False,
[[Row(id=1, value="value1"), Row(id=2, value="value2")]],
[[("id",), ("value",)]],
[([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")])],
id="Non-Scalar: Single SQL statements in list, no return_last, no split statement",
),
pytest.param(
["select * from dummy", "select * from dummy2"],
False,
False,
[
[Row(id=1, value="value1"), Row(id=2, value="value2")],
[Row(id2=1, value2="value1"), Row(id2=2, value2="value2")],
],
[[("id",), ("value",)], [("id2",), ("value2",)]],
[
([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")]),
([("id2",), ("value2",)], [Row(id2=1, value2="value1"), Row(id2=2, value2="value2")]),
],
id="Non-Scalar: Multiple SQL statements in list, no return_last (no matter), no split statement",
),
pytest.param(
["select * from dummy", "select * from dummy2"],
True,
False,
[
[Row(id=1, value="value1"), Row(id=2, value="value2")],
[Row(id2=1, value2="value1"), Row(id2=2, value2="value2")],
],
[[("id",), ("value",)], [("id2",), ("value2",)]],
[
([("id",), ("value",)], [Row(id=1, value="value1"), Row(id=2, value="value2")]),
([("id2",), ("value2",)], [Row(id2=1, value2="value1"), Row(id2=2, value2="value2")]),
],
id="Non-Scalar: Multiple SQL statements in list, return_last (no matter), no split statement",
),
],
)
def test_exec_success(sql, return_last, split_statement, hook_results, hook_descriptions, expected_results):
"""
Test the execute function in case where SQL query was successful.
"""
with patch("airflow.providers.databricks.operators.databricks_sql.DatabricksSqlHook") as db_mock_class:
op = DatabricksSqlOperator(
task_id=TASK_ID,
sql=sql,
do_xcom_push=True,
return_last=return_last,
split_statements=split_statement,
)
db_mock = db_mock_class.return_value
db_mock.run.return_value = hook_results
db_mock.descriptions = hook_descriptions
execute_results = op.execute(None)
assert execute_results == expected_results
db_mock_class.assert_called_once_with(
DEFAULT_CONN_ID,
http_path=None,
session_configuration=None,
sql_endpoint_name=None,
http_headers=None,
catalog=None,
schema=None,
caller="DatabricksSqlOperator",
)
db_mock.run.assert_called_once_with(
sql=sql,
parameters=None,
handler=fetch_all_handler,
autocommit=False,
return_last=return_last,
split_statements=split_statement,
)
def test_return_value_serialization():
hook_descriptions = [[("id",), ("value",)]]
hook_results = [Row(id=1, value="value1"), Row(id=2, value="value2")]
with patch("airflow.providers.databricks.operators.databricks_sql.DatabricksSqlHook") as db_mock_class:
op = DatabricksSqlOperator(
task_id=TASK_ID,
sql="select * from dummy2",
do_xcom_push=True,
return_last=True,
)
db_mock = db_mock_class.return_value
db_mock.run.return_value = hook_results
db_mock.descriptions = hook_descriptions
result = op.execute({})
serialized_result = serialize(result)
assert serialized_result == serialize(([("id",), ("value",)], [(1, "value1"), (2, "value2")]))
@pytest.mark.parametrize(
"return_last, split_statements, sql, descriptions, hook_results, do_xcom_push",
[
pytest.param(
True,
False,
"select * from dummy",
[[("id",), ("value",)]],
[Row(id=1, value="value1"), Row(id=2, value="value2")],
True,
id="Scalar: return_last True and split_statement False",
),
pytest.param(
False,
True,
"select * from dummy",
[[("id",), ("value",)]],
[[Row(id=1, value="value1"), Row(id=2, value="value2")]],
True,
id="Non-Scalar: return_last False and split_statement True",
),
pytest.param(
True,
True,
"select * from dummy",
[[("id",), ("value",)]],
[Row(id=1, value="value1"), Row(id=2, value="value2")],
True,
id="Scalar: return_last True and no split_statement True",
),
pytest.param(
False,
False,
"select * from dummy",
[[("id",), ("value",)]],
[Row(id=1, value="value1"), Row(id=2, value="value2")],
True,
id="Scalar: return_last False and split_statement is False",
),
pytest.param(
False,
True,
"select * from dummy2; select * from dummy",
[[("id2",), ("value2",)], [("id",), ("value",)]],
[
[Row(id2=1, value2="value1"), Row(id2=2, value2="value2")],
[Row(id=1, value="value1"), Row(id=2, value="value2")],
],
True,
id="Non-Scalar: return_last False and split_statement is True",
),
pytest.param(
True,
True,
"select * from dummy2; select * from dummy",
[[("id2",), ("value2",)], [("id",), ("value",)]],
[Row(id=1, value="value1"), Row(id=2, value="value2")],
True,
id="Scalar: return_last True and split_statement is True",
),
pytest.param(
True,
True,
"select * from dummy2; select * from dummy",
[[("id2",), ("value2",)], [("id",), ("value",)]],
[Row(id=1, value="value1"), Row(id=2, value="value2")],
True,
id="Scalar: return_last True and split_statement is True",
),
pytest.param(
True,
True,
["select * from dummy2", "select * from dummy"],
[[("id2",), ("value2",)], [("id",), ("value",)]],
[[Row(id=1, value="value1"), Row(id=2, value="value2")]],
True,
id="Non-Scalar: sql is list and return_last is True",
),
pytest.param(
False,
True,
["select * from dummy2", "select * from dummy"],
[[("id2",), ("value2",)], [("id",), ("value",)]],
[[Row(id=1, value="value1"), Row(id=2, value="value2")]],
True,
id="Non-Scalar: sql is list and return_last is False",
),
pytest.param(
False,
True,
["select * from dummy2", "select * from dummy"],
[[("id2",), ("value2",)], [("id",), ("value",)]],
[[Row(id=1, value="value1"), Row(id=2, value="value2")]],
False,
id="Write output when do_xcom_push is False",
),
],
)
def test_exec_write_file(
return_last, split_statements, sql, descriptions, hook_results, do_xcom_push, tmp_path
):
"""
Test the execute function in case where SQL query was successful and data is written as CSV
"""
with patch("airflow.providers.databricks.operators.databricks_sql.DatabricksSqlHook") as db_mock_class:
path = tmp_path / "testfile"
op = DatabricksSqlOperator(
task_id=TASK_ID,
sql=sql,
output_path=os.fspath(path),
return_last=return_last,
do_xcom_push=do_xcom_push,
split_statements=split_statements,
)
db_mock = db_mock_class.return_value
mock_results = hook_results
db_mock.run.return_value = mock_results
db_mock.descriptions = descriptions
op.execute(None)
results = path.read_text().splitlines()
# In all cases only result of last query i output as file
assert results == ["id,value", "1,value1", "2,value2"]
db_mock_class.assert_called_once_with(
DEFAULT_CONN_ID,
http_path=None,
session_configuration=None,
sql_endpoint_name=None,
http_headers=None,
catalog=None,
schema=None,
caller="DatabricksSqlOperator",
)
db_mock.run.assert_called_once_with(
sql=sql,
parameters=None,
handler=fetch_all_handler,
autocommit=False,
return_last=return_last,
split_statements=split_statements,
)