generated from populationgenomics/cpg-python-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(utils.py): add dependency handling wrapper #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ba523d4
fix(utils.py): add dependency handling wrapper
MattWellie a670906
fix(test_utils.py): added dependency test cases
MattWellie c98ac05
fix(test_utils.py): pytest.parametrize
MattWellie 0c9e474
fix(test_utils.py): less deepcopy
MattWellie ebc3fc9
fix(test_utils.py): extra test case
MattWellie 628acae
fix(test_utils.py): remove commented out tests
MattWellie 324d6a6
fix(stage.py): add implementation
MattWellie ed3ab86
fix(test_utils.py): add error handling
MattWellie e8b43b5
fix(utils.py): strange logging error
MattWellie a4becea
fix(pyproject.toml): python version
MattWellie 51ff494
fix(utils.py): remove duplicated operation
MattWellie 2694aff
fix(test_utils.py): add a depends-on-last
MattWellie 2fb89b7
fix(test_utils.py): split logic to please sonar
MattWellie fd50d09
fix(test_utils.py): changes following review
MattWellie fd167dd
fix(test_utils.py): linting following method deletion
MattWellie cd87c5f
fix(test_utils.py): remove Iterable import
MattWellie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import logging | ||
| from collections.abc import Iterable | ||
| from copy import deepcopy | ||
| from itertools import product | ||
|
|
||
| import pytest | ||
|
|
||
| from cpg_flow.utils import dependency_handler | ||
|
|
||
|
|
||
| class MockJob: | ||
| """ | ||
| A mock class to simulate hailtop.batch.job.Job | ||
| """ | ||
|
|
||
| def __init__(self, name: str): | ||
| self.name = name | ||
| self._dependencies: set[MockJob] = set() | ||
|
|
||
| def depends_on(self, *jobs): | ||
| """ | ||
| Simulate depends_on by adding jobs to a set | ||
| """ | ||
| for job in jobs: | ||
| self._dependencies.add(job) | ||
|
|
||
| def __repr__(self): | ||
| return f'MockJob({self.name})' | ||
|
|
||
| def __eq__(self, other) -> bool: | ||
| return self.name == other.name | ||
|
|
||
| def __hash__(self) -> int: | ||
| return int(self.name[-1]) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ['new_dep', 'old_dep', 'error'], | ||
| [ | ||
| pytest.param(None, None, 'No Target(s), cannot set depends_on relationships'), | ||
| pytest.param(MockJob('job1'), None, 'No Tail, cannot set depends_on relationships or append'), | ||
| pytest.param(None, MockJob('job1'), 'No Target(s), cannot set depends_on relationships'), | ||
| pytest.param(None, [MockJob('job1')], 'No Target(s), cannot set depends_on relationships'), | ||
| ], | ||
| ) | ||
| def test_all_dependency_handlers_null(new_dep, old_dep, error: str, caplog): | ||
| caplog.set_level(logging.DEBUG) | ||
|
|
||
| dependency_handler(target=new_dep, tail=old_dep) | ||
| assert error in caplog.text | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ['new_dep', 'old_dep', 'append_arg', 'expect_append'], | ||
| [ | ||
| pytest.param(MockJob('job1'), [MockJob('job2'), MockJob('job3')], True, True), # set and append | ||
| pytest.param([MockJob('job1'), MockJob('job2')], [MockJob('job3')], True, True), # set and append | ||
| pytest.param([MockJob('job1'), MockJob('job2')], MockJob('job3'), False, False), # set and don't append | ||
| pytest.param([MockJob('job1'), MockJob('job2')], MockJob('job3'), True, True), # set and fail to append | ||
| pytest.param([MockJob('job1')], [], True, True), # set and fail to append | ||
| ], | ||
| ) | ||
| def test_all_dependency_handlers_real(new_dep, old_dep, append_arg, expect_append: bool, caplog): | ||
| og_tail_list = deepcopy(old_dep if isinstance(old_dep, Iterable) else [old_dep]) | ||
|
|
||
| dependency_handler(target=new_dep, tail=old_dep, append_to_tail=append_arg) | ||
|
|
||
| new_dep_list = new_dep if isinstance(new_dep, Iterable) else [new_dep] | ||
| new_tail_list = old_dep if isinstance(old_dep, Iterable) else [old_dep] | ||
|
|
||
| # dependency setting, we expect all the original tail list to be in the current target dependencies | ||
| for each_new, each_old in product(new_dep_list, og_tail_list): | ||
| assert each_old in each_new._dependencies | ||
|
|
||
| # appending, we expect all the original targets to be in the new tail | ||
| if expect_append and isinstance(old_dep, list): | ||
| for each_new in new_dep_list: | ||
| assert each_new in new_tail_list | ||
| else: | ||
| for each_new in new_dep_list: | ||
| assert each_new not in new_tail_list | ||
|
|
||
|
|
||
| def test_depends_on_none(caplog): | ||
| caplog.set_level(logging.WARNING) | ||
| with pytest.raises(AttributeError): | ||
| dependency_handler( | ||
| target=[MockJob('job1'), None], tail=[MockJob('job2'), MockJob('job3')], append_to_tail=False | ||
| ) | ||
|
|
||
| assert 'Failure to set dependencies between target ' in caplog.text | ||
|
|
||
|
|
||
| def test_depends_on_only_last(): | ||
| # new behaviour - what if we only want to dependency set on the last in a job series | ||
| target = MockJob('job1') | ||
| tail = [MockJob('job2'), MockJob('job3'), MockJob('job4')] | ||
| dependency_handler(target=target, tail=tail, only_last=True) | ||
|
|
||
| assert target._dependencies == {MockJob('job4')} | ||
| assert len(tail) == 4 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.