https://prefect.io logo
Title
d

Dolor Oculus

09/18/2020, 9:33 PM
for the testing flows example (https://docs.prefect.io/core/idioms/testing-flows.html), is there an equivalent example for using the
with Flow() as flow:
   e = Extract()
   t = Transform(e)
   l = Load(t)
state = flow.run()
syntax? I'm getting key not found results on asserting state.result[e], and wondering if you have to do it in the way given in the testing-flows link above to unit test the flows.
I'm wondering if you have to explicitly build the dag as the example code shows in order to explicitly test things.
n

nicholas

09/19/2020, 12:06 AM
Hi @Dolor Oculus I don’t think there’s any reason you’d need to follow a particular syntax when testing - can you share a minimum reproducible example of what you’re seeing?
d

Dolor Oculus

09/19/2020, 12:55 AM
Hi, I was probably doing something dumb. Here's a repro that works perfectly
def test_simple_flow():
    @task
    def extract():
        return 42

    @task
    def transform(x: int) -> int:
        return int(x / 2 + 2)

    @task
    def load(x: int) -> str:
        return str(x)

    with Flow(__name__, environment=LocalEnvironment()) as flow:
        e = extract()
        t = transform(e)
        l = load(t)

    state = flow.run()
    assert state.is_successful()

    assert state.result[e].is_successful()
    assert state.result[t].is_successful()
    assert state.result[l].is_successful()

    assert state.result[e].result == 42
    assert state.result[t].result == 23
    assert state.result[l].result == "23"
n

nicholas

09/19/2020, 12:57 AM
Happens to the best of us!