Using the <patch/task_mock functionality> I'm able...
# ask-the-community
c
Using the patch/task_mock functionality I'm able to write unit tests to confirm that a workflow calls tasks correctly. Is there a way to test that a workflow calls subworkflows by mocking them, or do unit tests always need to go down to the task level?
k
you can only mock a task for now. is there any particular reason you want to mock a workflow
c
Thanks, good to know πŸ‘ The main use case is having unit tests to confirm that a sequence of sub-workflows execute as expected in a top-level workflow, particularly as the top-level workflow contains a conditional for whether a sub-workflow might be run. For example, in the below code snippet, it would be handy being able to have a test like the example at the bottom, to protect against unwanted modifications to the workflow steps or conditionals behaving unexpectedly.
Copy code
@task
def final_task():
    return 5

@task
def no_task():
    return 0

@workflow
def my_subworkflow() -> int:
    do_some_complicated_tasks()
    return final_task()

@workflow
def my_workflow(some_condition: bool) -> int:
    result = (
        conditional("my_workflow_switch")
        .if_(some_condition.is_true())
        .then(my_subworkflow())
        .else_()
        .then(no_task())
    )
    return result


@workflow_patch(my_subworkflow)
@task_patch(no_task)
def test_my_workflow_invokes_expected_condition(
    task_mock,
    subworkflow_mock,
):
    my_workflow(True)

    subworkflow_mock.assert_called_once()
    task_mock.assert_not_called()
k
@workflow_patch(my_subworkflow)
I believe it’s doable. mind creating an issue. [flyte-core] contributions are welcome
c
Thanks! Good suggestion, I've added the issue πŸ‘
r
ah, I came here to ask this same question! We have a workflow with some sub-workflows that call remote entities and do some other side-effecty stuff, and I want to just patch the sub-workflows to test the workflow that calls them. For some reason it worked in flyte 1.2.7 (we were stuck on that version for a long time due to some other dependency stuff) but when I just upgraded to python 3.10/Flyte 1.11, the "mocked" sub-workflows now fail if the underlying remote tasks aren't mocked out. @Charlie Moriarty wondering if you came up with any ad-hoc fix that can be used before that ticket gets tackled?
k
@Romaine Martin please +1 the issue, or if you can offer / propose a fix
c
@Romaine Martin Unfortunately we haven't found a workaround as of yet - for the time being we've reduced our test coverage but it would be great to be able to address these missing patches. Here's the link to the issue if you want to keep an eye on it!
k
The core team at union will get on this in sometime. Sorry higher pri items