Example #1
0
def test_build_returns_self(tmpdir):
    s = Local(directory=str(tmpdir))
    assert s.build() is s

    f = Flow("test")
    s.add_flow(f)
    assert s.build() is s
Example #2
0
def test_add_flow_raises_if_name_conflict(tmpdir):
    storage = Local(directory=str(tmpdir))
    f = Flow("test")
    storage.add_flow(f)
    g = Flow("test")
    with pytest.raises(ValueError, match='name "test"'):
        storage.add_flow(g)
Example #3
0
def test_build_returns_self():
    with tempfile.TemporaryDirectory() as tmpdir:
        s = Local(directory=tmpdir)
        assert s.build() is s

        f = Flow("test")
        s.add_flow(f)
        assert s.build() is s
Example #4
0
def test_add_flow_raises_if_name_conflict():
    with tempfile.TemporaryDirectory() as tmpdir:
        storage = Local(directory=tmpdir)
        f = Flow("test")
        res = storage.add_flow(f)
        g = Flow("test")
        with pytest.raises(ValueError, match='name "test"'):
            storage.add_flow(g)
Example #5
0
def test_containment(tmpdir):
    s = Local(directory=str(tmpdir))
    f = Flow("test")
    s.add_flow(f)

    assert True not in s
    assert f not in s
    assert "test" in s
    assert Flow("other") not in s
    assert "other" not in s
Example #6
0
def test_containment():
    with tempfile.TemporaryDirectory() as tmpdir:
        s = Local(directory=tmpdir)
        f = Flow("test")
        s.add_flow(f)

        assert True not in s
        assert f not in s
        assert "test" in s
        assert Flow("other") not in s
        assert "other" not in s
Example #7
0
def test_get_flow_from_file_returns_flow(tmpdir):
    contents = """from prefect import Flow\nf=Flow('test-flow')"""

    full_path = os.path.join(tmpdir, "flow.py")

    with open(full_path, "w") as f:
        f.write(contents)

    f = Flow("test-flow")
    storage = Local(stored_as_script=True, path=full_path)
    storage.add_flow(f)

    flow = storage.get_flow(full_path)
    assert flow.run()
Example #8
0
def test_get_flow_returns_flow():
    with tempfile.TemporaryDirectory() as tmpdir:
        storage = Local(directory=tmpdir)
        f = Flow("test")
        loc = storage.add_flow(f)
        runner = storage.get_flow(loc)
        assert runner == f
Example #9
0
def test_add_flow_with_weird_name_is_cleaned(tmpdir):
    s = Local(directory=str(tmpdir))
    flow = Flow("WELL what do you know?!~? looks like a test!!!!")
    loc = s.add_flow(flow)
    assert "?" not in loc
    assert "!" not in loc
    assert " " not in loc
    assert "~" not in loc
Example #10
0
def test_add_flow_with_weird_name_is_cleaned():
    with tempfile.TemporaryDirectory() as tmpdir:
        s = Local(directory=tmpdir)
        flow = Flow("WELL what do you know?!~? looks like a test!!!!")
        loc = s.add_flow(flow)
        assert "?" not in loc
        assert "!" not in loc
        assert " " not in loc
        assert "~" not in loc
Example #11
0
def test_add_flow_file_to_storage(tmpdir):
    contents = """from prefect import Flow\nf=Flow('test-flow')"""

    full_path = os.path.join(tmpdir, "flow.py")

    with open(full_path, "w") as f:
        f.write(contents)

    f = Flow("test-flow")
    storage = Local(stored_as_script=True)

    with pytest.raises(ValueError):
        storage.add_flow(f)

    storage = Local(stored_as_script=True, path=full_path)

    loc = storage.add_flow(f)
    assert loc == full_path
    assert f.name in storage
Example #12
0
def test_load_and_run_flow(monkeypatch, tmpdir):
    myflow = Flow("test-flow")

    # This is gross. Since the flow is pickled/unpickled, there's no easy way
    # to access the same object to set a flag. Resort to setting an environment
    # variable as a global flag that won't get copied eagerly through
    # cloudpickle.
    monkeypatch.setenv("TEST_RUN_CALLED", "FALSE")

    class MyEnvironment(Environment):
        def run(self, flow):
            assert flow is myflow
            os.environ["TEST_RUN_CALLED"] = "TRUE"

    myflow.environment = MyEnvironment()

    storage = Local(str(tmpdir))
    myflow.storage = storage
    storage.add_flow(myflow)

    gql_return = MagicMock(return_value=MagicMock(data=MagicMock(flow_run=[
        GraphQLResult({
            "flow":
            GraphQLResult({
                "name": myflow.name,
                "storage": storage.serialize()
            })
        })
    ], )))
    client = MagicMock()
    client.return_value.graphql = gql_return
    monkeypatch.setattr("prefect.environments.execution.base.Client", client)

    with set_temporary_config({"cloud.auth_token":
                               "test"}), prefect.context({"flow_run_id":
                                                          "id"}):
        load_and_run_flow()
    assert os.environ["TEST_RUN_CALLED"] == "TRUE"
Example #13
0
def test_get_flow_from_file_returns_flow(tmpdir):
    contents = textwrap.dedent("""
        from prefect import Flow
        f1 = Flow('flow-1')
        f2 = Flow('flow-2')
        """)

    full_path = os.path.join(tmpdir, "flow.py")

    with open(full_path, "w") as f:
        f.write(contents)

    f1 = Flow("flow-1")
    f2 = Flow("flow-2")
    storage = Local(stored_as_script=True, path=full_path)
    storage.add_flow(f1)
    storage.add_flow(f2)

    f1_2 = storage.get_flow(f1.name)
    f2_2 = storage.get_flow(f2.name)
    assert f1_2.name == f1.name
    assert f2_2.name == f2.name
    f1_2.run()
    f2_2.run()
Example #14
0
def test_add_flow_to_storage():
    with tempfile.TemporaryDirectory() as tmpdir:
        storage = Local(directory=tmpdir)
        f = Flow("test")
        assert f.name not in storage

        res = storage.add_flow(f)
        assert res.endswith("test.prefect")
        assert f.name in storage

        with open(os.path.join(tmpdir, "test.prefect"), "rb") as f:
            wat = f.read()

    assert isinstance(wat, bytes)
    assert cloudpickle.loads(wat).name == "test"
Example #15
0
def test_add_flow_to_storage(tmpdir):
    storage = Local(directory=str(tmpdir))
    f = Flow("test")
    assert f.name not in storage

    res = storage.add_flow(f)

    flow_dir = os.path.join(tmpdir, "test")
    assert os.path.exists(flow_dir)
    assert len(os.listdir(flow_dir)) == 1
    assert res.startswith(flow_dir)

    assert f.name in storage

    f2 = storage.get_flow(f.name)
    assert f2.name == "test"
Example #16
0
def test_multiple_flows_in_storage(tmpdir):
    s = Local(directory=str(tmpdir))
    f = Flow("test")
    g = Flow("other")
    f_loc = s.add_flow(f)
    g_loc = s.add_flow(g)

    assert "test" in s
    assert "other" in s
    assert "not" not in s

    assert s.get_flow(f.name) == f
    assert s.get_flow(g.name) == g

    assert s.flows["test"] == f_loc
    assert s.flows["other"] == g_loc
Example #17
0
def test_multiple_flows_in_storage():
    with tempfile.TemporaryDirectory() as tmpdir:
        s = Local(directory=tmpdir)
        f = Flow("test")
        g = Flow("other")
        z = Flow("not")
        f_loc = s.add_flow(f)
        g_loc = s.add_flow(g)

        assert "test" in s
        assert "other" in s
        assert "not" not in s

        assert s.get_flow(f_loc) == f
        assert s.get_flow(g_loc) == g

        assert s.flows["test"] == f_loc
        assert s.flows["other"] == g_loc
Example #18
0
def test_get_flow_returns_flow(tmpdir):
    storage = Local(directory=str(tmpdir))
    f = Flow("test")
    storage.add_flow(f)
    f2 = storage.get_flow(f.name)
    assert f2 == f
Example #19
0
def test_build_healthchecks(tmpdir):
    s = Local(directory=str(tmpdir))
    flow = Flow("TestFlow")
    s.add_flow(flow)
    assert s.build()
Example #20
0
def test_build_healthchecks():
    with tempfile.TemporaryDirectory() as tmpdir:
        s = Local(directory=tmpdir)
        flow = Flow("TestFlow")
        s.add_flow(flow)
        assert s.build()
Example #21
0
def test_get_flow_returns_flow(tmpdir):
    storage = Local(directory=str(tmpdir))
    f = Flow("test")
    loc = storage.add_flow(f)
    runner = storage.get_flow(loc)
    assert runner == f