-
Notifications
You must be signed in to change notification settings - Fork 178
/
test_local.py
391 lines (342 loc) · 13.1 KB
/
test_local.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import logging
import os
import shutil
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from airflow import DAG
from airflow import __version__ as airflow_version
from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.hooks.subprocess import SubprocessResult
from airflow.utils.context import Context
from packaging import version
from pendulum import datetime
from cosmos.config import ProfileConfig
from cosmos.operators.local import (
DbtLocalBaseOperator,
DbtLSLocalOperator,
DbtSnapshotLocalOperator,
DbtRunLocalOperator,
DbtTestLocalOperator,
DbtDocsLocalOperator,
DbtDocsS3LocalOperator,
DbtDocsAzureStorageLocalOperator,
DbtSeedLocalOperator,
DbtRunOperationLocalOperator,
)
from cosmos.profiles import PostgresUserPasswordProfileMapping
from tests.utils import test_dag as run_test_dag
DBT_PROJ_DIR = Path(__file__).parent.parent.parent / "dev/dags/dbt/jaffle_shop"
MINI_DBT_PROJ_DIR = Path(__file__).parent.parent / "sample/mini"
MINI_DBT_PROJ_DIR_FAILING_SCHEMA = MINI_DBT_PROJ_DIR / "schema_failing_test.yml"
MINI_DBT_PROJ_PROFILE = MINI_DBT_PROJ_DIR / "profiles.yml"
profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profile_mapping=MagicMock(),
)
real_profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profile_mapping=PostgresUserPasswordProfileMapping(
conn_id="airflow_db",
profile_args={"schema": "public"},
),
)
mini_profile_config = ProfileConfig(profile_name="mini", target_name="dev", profiles_yml_filepath=MINI_DBT_PROJ_PROFILE)
@pytest.fixture
def failing_test_dbt_project(tmp_path):
tmp_dir = tempfile.TemporaryDirectory()
tmp_dir_path = Path(tmp_dir.name) / "mini"
shutil.copytree(MINI_DBT_PROJ_DIR, tmp_dir_path)
target_schema = tmp_dir_path / "models/schema.yml"
target_schema.exists() and os.remove(target_schema)
shutil.copy(MINI_DBT_PROJ_DIR_FAILING_SCHEMA, target_schema)
yield tmp_dir_path
tmp_dir.cleanup()
def test_dbt_base_operator_add_global_flags() -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
vars={
"start_time": "{{ data_interval_start.strftime('%Y%m%d%H%M%S') }}",
"end_time": "{{ data_interval_end.strftime('%Y%m%d%H%M%S') }}",
},
no_version_check=True,
)
assert dbt_base_operator.add_global_flags() == [
"--vars",
"end_time: '{{ data_interval_end.strftime(''%Y%m%d%H%M%S'') }}'\n"
"start_time: '{{ data_interval_start.strftime(''%Y%m%d%H%M%S'') }}'\n",
"--no-version-check",
]
def test_dbt_base_operator_add_user_supplied_flags() -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
base_cmd=["run"],
dbt_cmd_flags=["--full-refresh"],
)
cmd, _ = dbt_base_operator.build_cmd(
Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
assert cmd[-2] == "run"
assert cmd[-1] == "--full-refresh"
def test_dbt_base_operator_add_user_supplied_global_flags() -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
base_cmd=["run"],
dbt_cmd_global_flags=["--cache-selected-only"],
)
cmd, _ = dbt_base_operator.build_cmd(
Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
assert cmd[-2] == "--cache-selected-only"
assert cmd[-1] == "run"
@pytest.mark.parametrize(
["skip_exception", "exception_code_returned", "expected_exception"],
[
(99, 99, AirflowSkipException),
(80, 99, AirflowException),
(None, 0, None),
],
ids=[
"Exception matches skip exception, airflow skip raised",
"Exception does not match skip exception, airflow exception raised",
"No exception raised",
],
)
def test_dbt_base_operator_exception_handling(skip_exception, exception_code_returned, expected_exception) -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
)
if expected_exception:
with pytest.raises(expected_exception):
dbt_base_operator.exception_handling(SubprocessResult(exception_code_returned, None))
else:
dbt_base_operator.exception_handling(SubprocessResult(exception_code_returned, None))
@patch("cosmos.operators.base.context_to_airflow_vars")
def test_dbt_base_operator_get_env(p_context_to_airflow_vars: MagicMock) -> None:
"""
If an end user passes in a
"""
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
)
dbt_base_operator.env = {
"start_date": "20220101",
"end_date": "20220102",
"some_path": Path(__file__),
"retries": 3,
("tuple", "key"): "some_value",
}
p_context_to_airflow_vars.return_value = {"START_DATE": "2023-02-15 12:30:00"}
env = dbt_base_operator.get_env(
Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
expected_env = {
"start_date": "20220101",
"end_date": "20220102",
"some_path": Path(__file__),
"START_DATE": "2023-02-15 12:30:00",
}
assert env == expected_env
@pytest.mark.skipif(
version.parse(airflow_version) < version.parse("2.4"),
reason="Airflow DAG did not have datasets until the 2.4 release",
)
@pytest.mark.integration
def test_run_operator_dataset_inlets_and_outlets():
from airflow.datasets import Dataset
with DAG("test-id-1", start_date=datetime(2022, 1, 1)) as dag:
run_operator = DbtRunLocalOperator(
profile_config=real_profile_config,
project_dir=DBT_PROJ_DIR,
task_id="run",
dbt_cmd_flags=["--models", "stg_customers"],
install_deps=True,
append_env=True,
)
test_operator = DbtTestLocalOperator(
profile_config=real_profile_config,
project_dir=DBT_PROJ_DIR,
task_id="test",
dbt_cmd_flags=["--models", "stg_customers"],
install_deps=True,
append_env=True,
)
run_operator
run_test_dag(dag)
assert run_operator.inlets == []
assert run_operator.outlets == [Dataset(uri="postgres://0.0.0.0:5432/postgres.public.stg_customers", extra=None)]
assert test_operator.inlets == [Dataset(uri="postgres://0.0.0.0:5432/postgres.public.stg_customers", extra=None)]
assert test_operator.outlets == []
@pytest.mark.integration
def test_run_test_operator_with_callback(failing_test_dbt_project):
on_warning_callback = MagicMock()
with DAG("test-id-2", start_date=datetime(2022, 1, 1)) as dag:
run_operator = DbtSeedLocalOperator(
profile_config=mini_profile_config,
project_dir=failing_test_dbt_project,
task_id="run",
append_env=True,
)
test_operator = DbtTestLocalOperator(
profile_config=mini_profile_config,
project_dir=failing_test_dbt_project,
task_id="test",
append_env=True,
on_warning_callback=on_warning_callback,
)
run_operator >> test_operator
run_test_dag(dag)
assert on_warning_callback.called
@pytest.mark.integration
def test_run_test_operator_without_callback():
on_warning_callback = MagicMock()
with DAG("test-id-3", start_date=datetime(2022, 1, 1)) as dag:
run_operator = DbtSeedLocalOperator(
profile_config=mini_profile_config,
project_dir=MINI_DBT_PROJ_DIR,
task_id="run",
append_env=True,
)
test_operator = DbtTestLocalOperator(
profile_config=mini_profile_config,
project_dir=MINI_DBT_PROJ_DIR,
task_id="test",
append_env=True,
on_warning_callback=on_warning_callback,
)
run_operator >> test_operator
run_test_dag(dag)
assert not on_warning_callback.called
@pytest.mark.integration
def test_run_operator_emits_events():
class MockRun:
facets = {"c": 3}
class MockJob:
facets = {"d": 4}
class MockEvent:
inputs = [1]
outputs = [2]
run = MockRun()
job = MockJob()
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
should_store_compiled_sql=False,
)
dbt_base_operator.openlineage_events_completes = [MockEvent(), MockEvent()]
facets = dbt_base_operator.get_openlineage_facets_on_complete(dbt_base_operator)
assert facets.inputs == [1]
assert facets.outputs == [2]
assert facets.run_facets == {"c": 3}
assert facets.job_facets == {"d": 4}
def test_run_operator_emits_events_without_openlineage_events_completes(caplog):
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
should_store_compiled_sql=False,
)
delattr(dbt_base_operator, "openlineage_events_completes")
facets = dbt_base_operator.get_openlineage_facets_on_complete(dbt_base_operator)
assert facets.inputs == []
assert facets.outputs == []
assert facets.run_facets == {}
assert facets.job_facets == {}
log = "Unable to emit OpenLineage events due to lack of dependencies or data."
assert log in caplog.text
def test_store_compiled_sql() -> None:
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
should_store_compiled_sql=False,
)
# here we just need to call the method to make sure it doesn't raise an exception
dbt_base_operator.store_compiled_sql(
tmp_project_dir="my/dir",
context=Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
should_store_compiled_sql=True,
)
# here we call the method and see if it tries to access the context["ti"]
# it should, and it should raise a KeyError because we didn't pass in a ti
with pytest.raises(KeyError):
dbt_base_operator.store_compiled_sql(
tmp_project_dir="my/dir",
context=Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
@pytest.mark.parametrize(
"operator_class,kwargs,expected_call_kwargs",
[
(DbtSeedLocalOperator, {"full_refresh": True}, {"context": {}, "cmd_flags": ["--full-refresh"]}),
(DbtRunLocalOperator, {"full_refresh": True}, {"context": {}, "cmd_flags": ["--full-refresh"]}),
(
DbtRunOperationLocalOperator,
{"args": {"days": 7, "dry_run": True}, "macro_name": "bla"},
{"context": {}, "cmd_flags": ["--args", "days: 7\ndry_run: true\n"]},
),
],
)
@patch("cosmos.operators.local.DbtLocalBaseOperator.build_and_run_cmd")
def test_operator_execute_with_flags(mock_build_and_run_cmd, operator_class, kwargs, expected_call_kwargs):
task = operator_class(profile_config=profile_config, task_id="my-task", project_dir="my/dir", **kwargs)
task.execute(context={})
mock_build_and_run_cmd.assert_called_once_with(**expected_call_kwargs)
@pytest.mark.parametrize(
"operator_class",
(
DbtLSLocalOperator,
DbtSnapshotLocalOperator,
DbtTestLocalOperator,
DbtDocsLocalOperator,
DbtDocsS3LocalOperator,
DbtDocsAzureStorageLocalOperator,
),
)
@patch("cosmos.operators.local.DbtLocalBaseOperator.build_and_run_cmd")
def test_operator_execute_without_flags(mock_build_and_run_cmd, operator_class):
operator_class_kwargs = {
DbtDocsS3LocalOperator: {"aws_conn_id": "fake-conn", "bucket_name": "fake-bucket"},
DbtDocsAzureStorageLocalOperator: {"azure_conn_id": "fake-conn", "container_name": "fake-container"},
}
task = operator_class(
profile_config=profile_config,
task_id="my-task",
project_dir="my/dir",
**operator_class_kwargs.get(operator_class, {}),
)
task.execute(context={})
mock_build_and_run_cmd.assert_called_once_with(context={})
@patch("cosmos.operators.local.DbtLocalArtifactProcessor")
def test_calculate_openlineage_events_completes_openlineage_errors(mock_processor, caplog):
instance = mock_processor.return_value
instance.parse = MagicMock(side_effect=KeyError)
caplog.set_level(logging.DEBUG)
dbt_base_operator = DbtLocalBaseOperator(
profile_config=profile_config,
task_id="my-task",
project_dir=DBT_PROJ_DIR,
should_store_compiled_sql=False,
)
dbt_base_operator.calculate_openlineage_events_completes(env={}, project_dir=DBT_PROJ_DIR)
assert instance.parse.called
err_msg = "Unable to parse OpenLineage events"
assert err_msg in caplog.text