dstandish 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_r291724185
 
 

 ##########
 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:
   OK -- I refactored the setup helper to allow you so set up arbitrary 
scenarios in a given test, doing away with the static constants.  
   
   This is way better.  Thank you for shepherding me through this learning 
process and forcing me to do this better.  Never really written tests before!
   
   Anyway, now a test looks like so:
   ```
           scenario = [State.SUCCESS, State.FAILED, State.SUCCESS]
   
           ti_list = self._test_previous_dates_setup(schedule_interval, 
catchup, scenario)
   
           self.assertIsNone(ti_list[0].previous_ti)
   
           self.assertEqual(
               ti_list[2].previous_ti.execution_date,
               ti_list[1].execution_date
           )
   
           self.assertNotEqual(
               ti_list[2].previous_ti.execution_date,
               ti_list[0].execution_date
           )
   ```
   
   Each test can set up whatever scenario it needs, in order to test desired 
behavior.  The indexes of `ti_list` are the same as the `scenario` list.
   
   So, returning to your concern here, for `previous_ti_success`, I used the 
FSFS scenario, and it tests that `ti_list[1].previous_ti_success` is `None`.
   
   Please take a look when you have a sec and let me know if you'd like 
anything further.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to