def test_execute_success_opt_in(dag, mock_hook):
    testing_component_id = 42

    mock_conn = mock_hook.return_value.get_conn()
    mock_conn.get_or_create.return_value = testing_component_id

    success_op = PythonOperator(task_id="test", python_callable=lambda: True, dag=dag)
    _ = register_status(
        success_op, "Test Success", "Testing operational status", on_success=True
    )

    # run each of the operators in order
    for operator in dag.topological_sort():
        operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)

    # get the dag state
    dagrun = dag.create_dagrun(
        run_id="manual__",
        start_date=datetime.utcnow(),
        execution_date=DEFAULT_DATE,
        state=State.RUNNING,
    )

    expected = {
        "test": State.SUCCESS,
        "test_register": State.SUCCESS,
        "test_success": State.SUCCESS,
        "test_failure": State.NONE,
    }

    assert all([expected[ti.task_id] == ti.state for ti in dagrun.get_task_instances()])
    mock_conn.update.assert_called_once_with(testing_component_id, "operational")
def test_execute_success_opt_in(dag, mock_hook):
    testing_component_id = 42

    mock_conn = mock_hook.return_value.get_conn()
    mock_conn.get_or_create.return_value = testing_component_id

    success_op = PythonOperator(task_id="test", python_callable=lambda: True, dag=dag)
    _ = register_status(
        success_op, "Test Success", "Testing operational status", on_success=True
    )

    # run each of the operators in order
    for operator in dag.topological_sort():
        operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)

    # get the dag state
    dagrun = dag.create_dagrun(
        run_id="manual__",
        start_date=tz.localize(datetime.utcnow()),
        execution_date=DEFAULT_DATE,
        state=State.RUNNING,
    )

    expected = {
        "test": State.SUCCESS,
        "test_register": State.SUCCESS,
        "test_success": State.SUCCESS,
        "test_failure": State.NONE,
    }

    assert all([expected[ti.task_id] == ti.state for ti in dagrun.get_task_instances()])
    mock_conn.update.assert_called_once_with(testing_component_id, "operational")
def test_execute_failure(dag, mock_hook):
    testing_component_id = 42

    mock_conn = mock_hook.return_value.get_conn()
    mock_conn.get_or_create.return_value = testing_component_id

    def exception():
        raise Exception()

    failure_op = PythonOperator(task_id="test",
                                python_callable=exception,
                                dag=dag)
    _ = register_status(failure_op, "Test Failure",
                        "Testing partial_outage status")

    # run each of the operators in order
    for operator in dag.topological_sort():
        try:
            operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
        except Exception:
            continue

    # get the dag state
    dagrun = dag.create_dagrun(
        run_id="manual__",
        start_date=tz.localize(datetime.utcnow()),
        execution_date=DEFAULT_DATE,
        state=State.RUNNING,
    )

    expected = {
        "test": State.FAILED,
        # we would otherwise register to be successful, but the job is still registered under failure conditions
        "test_register": State.NONE,
        "test_success": State.NONE,
        "test_failure": State.SUCCESS,
    }

    assert all([
        expected[ti.task_id] == ti.state for ti in dagrun.get_task_instances()
    ])
    mock_conn.update.assert_called_once_with(testing_component_id,
                                             "partial_outage")
def test_execute_failure(dag, mock_hook):
    testing_component_id = 42

    mock_conn = mock_hook.return_value.get_conn()
    mock_conn.get_or_create.return_value = testing_component_id

    def exception():
        raise Exception()

    failure_op = PythonOperator(task_id="test", python_callable=exception, dag=dag)
    _ = register_status(failure_op, "Test Failure", "Testing partial_outage status")

    # run each of the operators in order
    for operator in dag.topological_sort():
        try:
            operator.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE)
        except Exception:
            continue

    # get the dag state
    dagrun = dag.create_dagrun(
        run_id="manual__",
        start_date=tz.localize(datetime.utcnow()),
        execution_date=DEFAULT_DATE,
        state=State.RUNNING,
    )

    expected = {
        "test": State.FAILED,
        # we would otherwise register to be successful, but the job is still registered under failure conditions
        "test_register": State.NONE,
        "test_success": State.NONE,
        "test_failure": State.SUCCESS,
    }

    assert all([expected[ti.task_id] == ti.state for ti in dagrun.get_task_instances()])
    mock_conn.update.assert_called_once_with(testing_component_id, "partial_outage")