ashb commented on a change in pull request #5372: [AIRFLOW-3057] add prev_*_date_success to template context URL: https://github.com/apache/airflow/pull/5372#discussion_r291536811
########## File path: tests/models/test_taskinstance.py ########## @@ -992,3 +994,116 @@ def success_handler(self, context): self.assertEqual(cw.task_state_in_callback, State.RUNNING) ti.refresh_from_db() self.assertEqual(ti.state, State.SUCCESS) + + @staticmethod + def _test_previous_dates_setup(schedule_interval: Union[str, datetime.timedelta, None], + catchup: bool) -> dict: + dag_id = 'test_previous_dates' + dag = models.DAG(dag_id=dag_id, schedule_interval=schedule_interval, catchup=catchup) + task = DummyOperator(task_id='task', dag=dag, start_date=DEFAULT_DATE) + + def get_test_ti(session, execution_date: str, state: str) -> TI: + dag.create_dagrun( + run_id='scheduled__{}'.format(execution_date), + state=state, + execution_date=pendulum.parse(execution_date), + start_date=pendulum.utcnow(), + session=session + ) + ti = TI(task=task, execution_date=pendulum.parse(execution_date)) + ti.set_state(state=State.SUCCESS, session=session) + return ti + + with create_session() as session: # type: Session + + d1_s = '2019-01-01T00:00:00+00:00' + t1_s = get_test_ti(session, d1_s, State.SUCCESS) + + d2_f = '2019-01-02T00:00:00+00:00' + t2_f = get_test_ti(session, d2_f, State.FAILED) + + d3_s = '2019-01-03T00:00:00+00:00' + t3_s = get_test_ti(session, d3_s, State.SUCCESS) + + return { + 'ed1_s': pendulum.parse(d1_s), + 'ed2_f': pendulum.parse(d2_f), + 'ed3_s': pendulum.parse(d3_s), + 'ti1_s': t1_s, + 'ti2_f': t2_f, + 'ti3_s': t3_s, + } + + @parameterized.expand(( + ('cron/catchup', '0 0 * * * ', True), + ('cron/no-catchup', '0 0 * * *', False), + ('no-sched/catchup', None, True), + ('no-sched/no-catchup', None, False), + ('timedelta/catchup', datetime.timedelta(days=1), True), + ('timedelta/no-catchup', datetime.timedelta(days=1), False), + )) + def test_previous_ti(self, _, schedule_interval, catchup) -> None: + + setup_dict = self._test_previous_dates_setup(schedule_interval, catchup) + + self.assertIsNone(setup_dict['ti1_s'].previous_ti) + + self.assertEqual( + setup_dict['ti3_s'].previous_ti.execution_date, + setup_dict['ed2_f'] + ) + + @parameterized.expand(( + ('cron/catchup', '0 0 * * * ', True), + ('cron/no-catchup', '0 0 * * *', False), + ('no-sched/catchup', None, True), + ('no-sched/no-catchup', None, False), + ('timedelta/catchup', datetime.timedelta(days=1), True), + ('timedelta/no-catchup', datetime.timedelta(days=1), False), + )) + def test_previous_ti_success(self, _, schedule_interval, catchup) -> None: Review comment: Overall these tests are much nicer! However there is a case missing - we don't check the behaviour when there is no successful previous TI at all. Using the `param` fn from Parameterized we could do something like this: ```python @parameterized.expand(( ('cron/catchup', '0 0 * * * ', True), ('cron/no-catchup', '0 0 * * *', False), ('no-sched/catchup', None, True), ('no-sched/no-catchup', None, False), ('timedelta/catchup', datetime.timedelta(days=1), True), ('timedelta/no-catchup', datetime.timedelta(days=1), False), param('something_previous_failed', datetime.timedelta(days=1), False, all_previous_failed=True), )) ``` ```suggestion def test_previous_ti_success(self, _, schedule_interval, catchup, all_previous_failed=False) -> None: ``` (and then use all_previous_failed in `_test_previous_dates_setup` and to change the assertions we run etc.) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services