dag = DAG(dag_id="test_latest_runs_1", start_date=DEFAULT_DATE)
        self.create_dag_run(dag, execution_date=timezone.datetime(2015, 1, 1), session=session)
        self.create_dag_run(dag, execution_date=timezone.datetime(2015, 1, 2), session=session)
        dagruns = DagRun.get_latest_runs(session)
        session.close()
        for dagrun in dagruns:
            if dagrun.dag_id == "test_latest_runs_1":
                assert dagrun.execution_date == timezone.datetime(2015, 1, 2)
    def test_removed_task_instances_can_be_restored(self, session):
        def with_all_tasks_removed(dag):
            return DAG(dag_id=dag.dag_id, start_date=dag.start_date)
        dag = DAG("test_task_restoration", start_date=DEFAULT_DATE)
        dag.add_task(EmptyOperator(task_id="flaky_task", owner="test"))
        dagrun = self.create_dag_run(dag, session=session)
        flaky_ti = dagrun.get_task_instances()[0]
        assert "flaky_task" == flaky_ti.task_id
        assert flaky_ti.state is None
        dagrun.dag = with_all_tasks_removed(dag)
        dagrun.verify_integrity()
        flaky_ti.refresh_from_db()
        assert flaky_ti.state is None
        dagrun.dag.add_task(EmptyOperator(task_id="flaky_task", owner="test"))
        dagrun.verify_integrity()
        flaky_ti.refresh_from_db()
        assert flaky_ti.state is None
    def test_already_added_task_instances_can_be_ignored(self, session):