Skip to content

Commit 273f314

Browse files
committed
fix: Update tests to work with Airflow 3
1 parent 3028402 commit 273f314

1 file changed

Lines changed: 67 additions & 18 deletions

File tree

tests/dags/test_dbt_dags.py

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
airflow = pytest.importorskip("airflow", minversion="2.2")
1313

1414
from airflow import DAG, settings
15+
from airflow import __version__ as airflow_version
1516
from airflow.models import DagBag, DagRun
1617
from airflow.models.serialized_dag import SerializedDagModel
1718
from airflow.utils.state import DagRunState, TaskInstanceState
@@ -27,6 +28,41 @@
2728

2829
DATA_INTERVAL_START = pendulum.datetime(2022, 1, 1, tz="UTC")
2930
DATA_INTERVAL_END = DATA_INTERVAL_START + dt.timedelta(hours=1)
31+
AIRFLOW_MAJOR_VERSION = int(airflow_version.split(".", 1)[0])
32+
33+
34+
def _create_dagrun(
35+
parent_dag: DAG,
36+
state: DagRunState,
37+
logical_date: dt.datetime,
38+
data_interval: tuple[dt.datetime, dt.datetime],
39+
start_date: dt.datetime,
40+
run_type: DagRunType,
41+
) -> DagRun:
42+
if AIRFLOW_MAJOR_VERSION >= 3:
43+
from airflow.utils.types import DagRunTriggeredByType # type: ignore
44+
45+
return parent_dag.create_dagrun( # type: ignore
46+
run_id=f"{parent_dag.dag_id}-{logical_date.isoformat()}-RUN",
47+
state=state,
48+
logical_date=logical_date,
49+
data_interval=data_interval,
50+
start_date=start_date,
51+
conf={},
52+
backfill_id=None,
53+
creating_job_id=None,
54+
run_type=run_type,
55+
run_after=dt.datetime(1970, 1, 1, 0, 0, 0, tzinfo=dt.UTC),
56+
triggered_by=DagRunTriggeredByType.TIMETABLE,
57+
)
58+
else:
59+
return parent_dag.create_dagrun(
60+
state=state,
61+
execution_date=logical_date, # type: ignore
62+
data_interval=data_interval,
63+
start_date=start_date,
64+
run_type=run_type,
65+
)
3066

3167

3268
@pytest.fixture(scope="session")
@@ -115,9 +151,10 @@ def test_dbt_operators_in_dag(
115151
basic_dag, dbt_project_file, profiles_file, clear_dagruns
116152
):
117153
"""Assert DAG contains correct dbt operators when running."""
118-
dagrun = basic_dag.create_dagrun(
154+
dagrun = _create_dagrun(
155+
basic_dag,
119156
state=DagRunState.RUNNING,
120-
execution_date=DATA_INTERVAL_START,
157+
logical_date=DATA_INTERVAL_START,
121158
data_interval=(DATA_INTERVAL_START, DATA_INTERVAL_END),
122159
start_date=DATA_INTERVAL_END,
123160
run_type=DagRunType.MANUAL,
@@ -216,9 +253,15 @@ def test_dbt_operators_in_taskflow_dag(
216253
taskflow_dag, dbt_project_file, profiles_file, clear_dagruns
217254
):
218255
"""Assert DAG contains correct dbt operators when running."""
219-
dagrun = taskflow_dag.create_dagrun(
256+
if AIRFLOW_MAJOR_VERSION >= 3:
257+
dag = DAG.from_sdk_dag(taskflow_dag)
258+
else:
259+
dag = taskflow_dag
260+
261+
dagrun = _create_dagrun(
262+
dag,
220263
state=DagRunState.RUNNING,
221-
execution_date=DATA_INTERVAL_START,
264+
logical_date=DATA_INTERVAL_START,
222265
data_interval=(DATA_INTERVAL_START, DATA_INTERVAL_END),
223266
start_date=DATA_INTERVAL_END,
224267
run_type=DagRunType.MANUAL,
@@ -232,16 +275,18 @@ def test_dbt_operators_in_taskflow_dag(
232275
"dbt_test_taskflow",
233276
):
234277
ti = dagrun.get_task_instance(task_id=task_id)
235-
ti.task = taskflow_dag.get_task(task_id=task_id)
278+
ti.task = dag.get_task(task_id=task_id)
236279

237280
ti.run(ignore_ti_state=True)
238281

239282
assert ti.state == TaskInstanceState.SUCCESS
240-
assert ti.task.retries == taskflow_dag.default_args["retries"]
241-
assert (
242-
ti.task.on_failure_callback
243-
== taskflow_dag.default_args["on_failure_callback"]
244-
)
283+
assert ti.task.retries == dag.default_args["retries"]
284+
285+
if isinstance(ti.task.on_failure_callback, list):
286+
failure_callback = ti.task.on_failure_callback[0]
287+
else:
288+
failure_callback = ti.task.on_failure_callback
289+
assert failure_callback == dag.default_args["on_failure_callback"]
245290

246291
if isinstance(ti.task, DbtBaseOperator):
247292
assert ti.task.profiles_dir == str(profiles_file.parent)
@@ -349,9 +394,10 @@ def test_dbt_operators_in_connection_dag(
349394
target_connection_dag, dbt_project_file, clear_dagruns
350395
):
351396
"""Assert DAG contains correct dbt operators when running."""
352-
dagrun = target_connection_dag.create_dagrun(
397+
dagrun = _create_dagrun(
398+
target_connection_dag,
353399
state=DagRunState.RUNNING,
354-
execution_date=DATA_INTERVAL_START,
400+
logical_date=DATA_INTERVAL_START,
355401
data_interval=(DATA_INTERVAL_START, DATA_INTERVAL_END),
356402
start_date=DATA_INTERVAL_END,
357403
run_type=DagRunType.MANUAL,
@@ -414,9 +460,10 @@ def test_example_basic_dag(
414460
dbt_run.target = "test"
415461
dbt_run.profile = "default"
416462

417-
dagrun = dag.create_dagrun(
463+
dagrun = _create_dagrun(
464+
dag,
418465
state=DagRunState.RUNNING,
419-
execution_date=dag.start_date,
466+
logical_date=dag.start_date,
420467
data_interval=(dag.start_date, DATA_INTERVAL_END),
421468
start_date=DATA_INTERVAL_END,
422469
run_type=DagRunType.MANUAL,
@@ -460,9 +507,10 @@ def test_example_dbt_project_in_github_dag(dagbag, connection, clear_dagruns):
460507
assert dag is not None
461508
assert len(dag.tasks) == 3
462509

463-
dagrun = dag.create_dagrun(
510+
dagrun = _create_dagrun(
511+
dag,
464512
state=DagRunState.RUNNING,
465-
execution_date=dag.start_date,
513+
logical_date=dag.start_date,
466514
data_interval=(dag.start_date, DATA_INTERVAL_END),
467515
start_date=DATA_INTERVAL_END,
468516
run_type=DagRunType.MANUAL,
@@ -511,9 +559,10 @@ def test_example_complete_dbt_workflow_dag(
511559
assert dag is not None
512560
assert len(dag.tasks) == 5
513561

514-
dagrun = dag.create_dagrun(
562+
dagrun = _create_dagrun(
563+
dag,
515564
state=DagRunState.RUNNING,
516-
execution_date=dag.start_date,
565+
logical_date=dag.start_date,
517566
data_interval=(dag.start_date, DATA_INTERVAL_END),
518567
start_date=DATA_INTERVAL_END,
519568
run_type=DagRunType.MANUAL,

0 commit comments

Comments
 (0)