Skip to content

Refactor ti clear next method kwargs tests - #19194

Merged
potiuk merged 11 commits into
apache:mainfrom
ReadytoRocc:refactor_ti_clear_next_method_kwargs_tests
Jan 10, 2022
Merged

Refactor ti clear next method kwargs tests#19194
potiuk merged 11 commits into
apache:mainfrom
ReadytoRocc:refactor_ti_clear_next_method_kwargs_tests

Conversation

@ReadytoRocc

Copy link
Copy Markdown
Contributor

related: #19120

Refactoring tests introduced in #19183.

@uranusjr

Copy link
Copy Markdown
Member

It's probably better to refactor using paramtrize instead, something like

def _failure():
    raise AirflowException

def _skip():
    raise AirflowSkipException

# Others...

@pytest.mark.parametrize(
    "state, func",
    [
        (State.FAILED, _failure),
        (State.SKIPPED, _skip),
        # Others...
    ],
)
def test_task_wipes_next_fields(self, session, dag_maker, state, func):
    with dag_maker("test_deferred_method_clear"):
        task = PythonOperator(
            ...,
            python_callable=func,
        )
    ...

@ReadytoRocc

Copy link
Copy Markdown
Contributor Author

Thanks @uranusjr. We are utilizing @pytest.mark.parametrize on lines L485-L488 for the state var. @jedcunningham suggested refactoring these tests to use a single run() function that takes state as an input.

@jedcunningham

Copy link
Copy Markdown
Member

@ReadytoRocc, I think @uranusjr's suggestion is even better fwiw.

@ReadytoRocc

Copy link
Copy Markdown
Contributor Author

Thanks @jedcunningham and @uranusjr. Refactored as suggested.

Comment thread tests/models/test_taskinstance.py Outdated
@github-actions

Copy link
Copy Markdown
Contributor

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@github-actions github-actions Bot added the full tests needed We need to run full set of tests for this PR to merge label Oct 25, 2021
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Dec 11, 2021
@github-actions github-actions Bot closed this Dec 16, 2021
@jedcunningham jedcunningham reopened this Dec 16, 2021
@jedcunningham

Copy link
Copy Markdown
Member

@ReadytoRocc, geez sorry, didn't realize this never got merged! Can you rebase on main when you get the chance?

@eladkal eladkal removed the stale Stale PRs per the .github/workflows/stale.yml policy file label Dec 16, 2021
@ReadytoRocc
ReadytoRocc force-pushed the refactor_ti_clear_next_method_kwargs_tests branch from 9768d0d to 05d0adc Compare December 23, 2021 21:57
@ReadytoRocc

Copy link
Copy Markdown
Contributor Author

Thanks. @jedcunningham and @uranusjr I needed to slightly modify some of the code due to a mypy check that was not present when this PR was originally opened. Please review again before merging. Thanks!

Comment thread tests/models/test_taskinstance.py Outdated
retries=_retries,
retry_delay=_retry_delay,
python_callable=_raise_af_exception,
op_args=[exception_type],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you remove exception_type as a param in _raise_af_exception you can also remove op_kwargs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dstandish, will do.

Comment thread tests/models/test_taskinstance.py Outdated
"""
Test that ensures that tasks wipe their next_method and next_kwargs
when they go into a state of FAILED, SKIPPED, SUCCESS, UP_FOR_RESCHEDULE, or UP_FOR_RETRY.
Test that ensures that tasks wipe their next_method and next_kwargs for the configured states:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Test that ensures that tasks wipe their next_method and next_kwargs for the configured states:
Test that ensures that tasks wipe their next_method and next_kwargs when the TI enters one of the following states:

@ReadytoRocc

Copy link
Copy Markdown
Contributor Author

@jedcunningham @uranusjr I needed to slightly modify some of the code due to a mypy check that was not present when this PR was originally opened. Please review again and let me know if any changes are needed before merging. Thanks!

Comment thread tests/models/test_taskinstance.py Outdated
_python_callable = failure
_retries = 1
_retry_delay = datetime.timedelta(seconds=2)
def _raise_af_exception(exception_type):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _raise_af_exception(exception_type):
def _raise_if_exception(exception_type):

Probably? Or does af stand for Airflow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

af stands for Airflow. However, _raise_if_exception better describes the function. I will update this, thanks.

Comment thread tests/models/test_taskinstance.py Outdated

ti.task = task
if state in [State.FAILED, State.UP_FOR_RETRY]:
if exception_type == AirflowException:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not correctly cover the AirflowRescheduleException(timezone.utcnow()) case. It’s probably better to change exception_type to exception instead and always instantiate to avoid this subtle issue caused by exception_type may be either a type or an instance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I will rename exception_type to exception. I will edit as follows:

if state in [State.FAILED, State.UP_FOR_RETRY]:
    with pytest.raises(exception):
        ti.run()
else:
    ti.run()

This ensures that the exception is tied to whatever state is being checked.

The reason I did not include AirflowRescheduleException(timezone.utcnow()) is because that exception is caught during ti.run().

…ion directly to _raise_if_exception, and checked state vs. exception when running pytest.raises.
@potiuk
potiuk merged commit 3b5adaf into apache:main Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full tests needed We need to run full set of tests for this PR to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants