def test_jinja_template_partially_formats(): task = JinjaTemplate(template="{{ name }} is from {{ place }}") with Flow(name="test") as f: ans = task(name="Ford") res = f.run() assert res.is_successful() assert res.result[ans].result == "Ford is from "
def test_jinja_template_can_execute_python_code(): date = pendulum.parse("1986-09-20") task = JinjaTemplate(template='{{ date.strftime("%Y-%d") }} is a date.') f = Flow(name="test", tasks=[task]) res = f.run(context={"date": date}) assert res.is_successful() assert res.result[task].result == "1986-20 is a date."
def test_jinja_template_can_be_provided_template_at_runtime(): task = JinjaTemplate() with Flow(name="test") as f: ans = task(template="{{ name }} is from {{ place }}", name="Ford", place="Betelgeuse") res = f.run() assert res.is_successful() assert res.result[ans].result == "Ford is from Betelgeuse"
def test_jinja_task_is_pickleable(): task = JinjaTemplate(template="string") new = cloudpickle.loads(cloudpickle.dumps(task)) assert isinstance(new, JinjaTemplate) assert new.template == "string"
def test_jinja_template_formats_from_context(): task = JinjaTemplate(template="I am {{ task_name }}", name="foo") f = Flow(name="test", tasks=[task]) res = f.run() assert res.is_successful() assert res.result[task].result == "I am foo"
def add_7(): date = prefect.context.get("scheduled_start_time", datetime.utcnow()) return date + timedelta(days=7) ## templated command; template vars will be read from both prefect.context as well as ## any passed kwargs to the task command = """ {% for i in range(5) %} echo "{{ scheduled_start_time }}" echo "{{ scheduled_start_time_7 }}" echo "{{ my_param }}" {% endfor %} """ templated_command = JinjaTemplate( template=command, max_retries=1, retry_delay=retry_delay ) ## create schedule for the Flow schedule = IntervalSchedule(start_date=datetime(2015, 6, 1), interval=timedelta(days=1)) ## create Flow and specify dependencies using functional API ## we don't actually attach the schedule to this Flow so it only runs once with Flow("tutorial") as flow: my_param = Parameter("my_param") t2(upstream_tasks=[t1]) t3 = templated_command( scheduled_start_time_7=add_7, my_param=my_param, upstream_tasks=[t1] )