Skip to content

Commit f811ac3

Browse files
authored
Fix tests/models/test_taskinstance.py for Database Isolation Tests (#41344)
1 parent 2e009c6 commit f811ac3

4 files changed

Lines changed: 184 additions & 19 deletions

File tree

airflow/models/taskinstance.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,25 @@ def _get_previous_execution_date(
14041404
return pendulum.instance(prev_ti.execution_date) if prev_ti and prev_ti.execution_date else None
14051405

14061406

1407+
def _get_previous_start_date(
1408+
*,
1409+
task_instance: TaskInstance | TaskInstancePydantic,
1410+
state: DagRunState | None,
1411+
session: Session,
1412+
) -> pendulum.DateTime | None:
1413+
"""
1414+
Return the start date from property previous_ti_success.
1415+
1416+
:param task_instance: the task instance
1417+
:param state: If passed, it only take into account instances of a specific state.
1418+
:param session: SQLAlchemy ORM Session
1419+
"""
1420+
log.debug("previous_start_date was called")
1421+
prev_ti = task_instance.get_previous_ti(state=state, session=session)
1422+
# prev_ti may not exist and prev_ti.start_date may be None.
1423+
return pendulum.instance(prev_ti.start_date) if prev_ti and prev_ti.start_date else None
1424+
1425+
14071426
def _email_alert(
14081427
*, task_instance: TaskInstance | TaskInstancePydantic, exception, task: BaseOperator
14091428
) -> None:
@@ -2533,10 +2552,7 @@ def get_previous_start_date(
25332552
:param state: If passed, it only take into account instances of a specific state.
25342553
:param session: SQLAlchemy ORM Session
25352554
"""
2536-
self.log.debug("previous_start_date was called")
2537-
prev_ti = self.get_previous_ti(state=state, session=session)
2538-
# prev_ti may not exist and prev_ti.start_date may be None.
2539-
return pendulum.instance(prev_ti.start_date) if prev_ti and prev_ti.start_date else None
2555+
return _get_previous_start_date(task_instance=self, state=state, session=session)
25402556

25412557
@property
25422558
def previous_start_date_success(self) -> pendulum.DateTime | None:

airflow/serialization/pydantic/taskinstance.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@ def get_previous_execution_date(
381381

382382
return _get_previous_execution_date(task_instance=self, state=state, session=session)
383383

384+
def get_previous_start_date(
385+
self,
386+
state: DagRunState | None = None,
387+
session: Session | None = None,
388+
) -> pendulum.DateTime | None:
389+
"""
390+
Return the execution date from property previous_ti_success.
391+
392+
:param state: If passed, it only take into account instances of a specific state.
393+
:param session: SQLAlchemy ORM Session
394+
"""
395+
from airflow.models.taskinstance import _get_previous_start_date
396+
397+
return _get_previous_start_date(task_instance=self, state=state, session=session)
398+
384399
def email_alert(self, exception, task: BaseOperator) -> None:
385400
"""
386401
Send alert email with exception information.

tests/conftest.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ def create_task_instance(dag_maker, create_dummy_dag):
10821082
10831083
Uses ``create_dummy_dag`` to create the dag structure.
10841084
"""
1085+
from airflow.operators.empty import EmptyOperator
10851086

10861087
def maker(
10871088
execution_date=None,
@@ -1091,14 +1092,46 @@ def maker(
10911092
run_type=None,
10921093
data_interval=None,
10931094
external_executor_id=None,
1095+
dag_id="dag",
1096+
task_id="op1",
1097+
task_display_name=None,
1098+
max_active_tis_per_dag=16,
1099+
max_active_tis_per_dagrun=None,
1100+
pool="default_pool",
1101+
executor_config=None,
1102+
trigger_rule="all_done",
1103+
on_success_callback=None,
1104+
on_execute_callback=None,
1105+
on_failure_callback=None,
1106+
on_retry_callback=None,
1107+
email=None,
10941108
map_index=-1,
10951109
**kwargs,
10961110
) -> TaskInstance:
10971111
if execution_date is None:
10981112
from airflow.utils import timezone
10991113

11001114
execution_date = timezone.utcnow()
1101-
_, task = create_dummy_dag(with_dagrun_type=None, **kwargs)
1115+
with dag_maker(dag_id, **kwargs):
1116+
op_kwargs = {}
1117+
from tests.test_utils.compat import AIRFLOW_V_2_9_PLUS
1118+
1119+
if AIRFLOW_V_2_9_PLUS:
1120+
op_kwargs["task_display_name"] = task_display_name
1121+
task = EmptyOperator(
1122+
task_id=task_id,
1123+
max_active_tis_per_dag=max_active_tis_per_dag,
1124+
max_active_tis_per_dagrun=max_active_tis_per_dagrun,
1125+
executor_config=executor_config or {},
1126+
on_success_callback=on_success_callback,
1127+
on_execute_callback=on_execute_callback,
1128+
on_failure_callback=on_failure_callback,
1129+
on_retry_callback=on_retry_callback,
1130+
email=email,
1131+
pool=pool,
1132+
trigger_rule=trigger_rule,
1133+
**op_kwargs,
1134+
)
11021135

11031136
dagrun_kwargs = {"execution_date": execution_date, "state": dagrun_state}
11041137
if run_id is not None:

0 commit comments

Comments
 (0)