Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_containment():
s = Memory()
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
def test_two_dependent_tasks():
"""
x1 -> x2
Two identical tasks in a row
"""
f = Flow(name="test")
f.add_edge(get_task("x1"), get_task("x2"))
steps = f.generate_local_task_ids(_debug_steps=True)
# step 1 isn't enough to differentiate the tasks
assert count_unique_ids(steps[1]) == 1
# step 2 is able to differentiate them
assert count_unique_ids(steps[2]) == 2
# no further processing
assert steps[2] == steps[3] == steps[4] == steps[5]
def test_deserialize_flow(self):
f = Flow()
f.add_task(Task())
f.add_task(Parameter("x"))
env = LocalEnvironment()
serialized = env.serialize_flow_to_bytes(f)
deserialized = env.deserialize_flow_from_bytes(serialized)
assert isinstance(deserialized, Flow)
assert len(deserialized.tasks) == 2
assert {p.name for p in deserialized.parameters()} == {"x"}
def test_multiple_flows_in_storage():
s = Bytes()
f = Flow("test")
g = Flow("other")
z = Flow("not")
s.add_flow(f)
s.add_flow(g)
assert "test" in s
assert "other" in s
assert "not" not in s
assert s.get_flow("test") == f
assert s.get_flow("other") == g
assert isinstance(s.flows["test"], bytes)
assert isinstance(s.flows["other"], bytes)
def test_flow_runner_loads_parameters_from_cloud(monkeypatch):
flow = prefect.Flow(name="test")
get_flow_run_info = MagicMock(return_value=MagicMock(parameters={"a": 1}))
set_flow_run_state = MagicMock()
client = MagicMock(
get_flow_run_info=get_flow_run_info, set_flow_run_state=set_flow_run_state
)
monkeypatch.setattr(
"prefect.engine.cloud.flow_runner.Client", MagicMock(return_value=client)
)
res = CloudFlowRunner(flow=flow).initialize_run(
state=Pending(), task_states={}, context={}, task_contexts={}, parameters={}
)
assert res.context["parameters"]["a"] == 1
def test_no_tasks_returns_empty_dict():
assert Flow(name="test").generate_local_task_ids() == {}
def test_upload_multiple_flows_to_azure_blob_name(monkeypatch):
client = MagicMock(upload_blob=MagicMock())
service = MagicMock(get_blob_client=MagicMock(return_value=client))
monkeypatch.setattr(
"prefect.environments.storage.Azure._azure_block_blob_service", service
)
storage = Azure(container="container", blob_name="name")
f1 = Flow("test1")
f2 = Flow("test2")
assert storage.add_flow(f1)
assert storage.add_flow(f2)
assert storage.build()
assert service.get_blob_client.call_args[1]["container"] == "container"
def test_environment_execute_calls_callbacks():
start_func = MagicMock()
exit_func = MagicMock()
with tempfile.TemporaryDirectory() as directory:
@prefect.task
def add_to_dict():
with open(path.join(directory, "output"), "w") as tmp:
tmp.write("success")
with open(path.join(directory, "flow_env.prefect"), "w+") as env:
flow = prefect.Flow("test", tasks=[add_to_dict])
flow_path = path.join(directory, "flow_env.prefect")
with open(flow_path, "wb") as f:
cloudpickle.dump(flow, f)
environment = RemoteEnvironment(on_start=start_func, on_exit=exit_func)
storage = Docker(registry_url="test")
environment.execute(storage, flow_path)
with open(path.join(directory, "output"), "r") as file:
assert file.read() == "success"
assert start_func.called
assert exit_func.called
def test_sqlite_error_results_in_failed_state(self, database):
with Flow(name="test") as f:
task = SQLiteQuery(db=database, query="SELECT * FROM FOOBAR")()
out = f.run()
assert out.is_failed()
assert "no such table: FOOBAR" in str(out.result[task].result)
def test_multiple_flows_in_storage():
s = Memory()
f = Flow("test")
g = Flow("other")
z = Flow("not")
s.add_flow(f)
s.add_flow(g)
assert "test" in s
assert "other" in s
assert "not" not in s
assert s.get_flow("test") is f
assert s.get_flow("other") is g
assert s.flows["test"] is f
assert s.flows["other"] is g