2023-01-19 19:27:39 +00:00
|
|
|
from moto.ses.template import parse_template
|
|
|
|
|
|
|
|
|
|
|
|
def test_template_without_args():
|
2023-08-04 14:14:14 +00:00
|
|
|
assert parse_template("some template", template_data={}) == "some template"
|
2023-01-19 19:27:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_simple_arg():
|
2023-08-04 14:14:14 +00:00
|
|
|
template = "Dear {{name}},"
|
|
|
|
assert parse_template(template, template_data={"name": "John"}) == "Dear John,"
|
2023-01-19 19:27:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_foreach():
|
2023-08-04 14:14:14 +00:00
|
|
|
template = "List:{{#each l}} - {{obj}}{{/each}} and other things"
|
2023-01-19 19:27:39 +00:00
|
|
|
kwargs = {"l": [{"obj": "it1"}, {"obj": "it2"}]}
|
2023-08-04 14:14:14 +00:00
|
|
|
assert parse_template(template, kwargs) == "List: - it1 - it2 and other things"
|
2023-01-19 19:27:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_multiple_foreach():
|
2023-08-04 14:14:14 +00:00
|
|
|
template = (
|
|
|
|
"{{#each l}} - {{obj}}{{/each}} and list 2 {{#each l2}} {{o1}} {{o2}} {{/each}}"
|
|
|
|
)
|
2023-01-19 19:27:39 +00:00
|
|
|
kwargs = {
|
|
|
|
"l": [{"obj": "it1"}, {"obj": "it2"}],
|
|
|
|
"l2": [{"o1": "ob1", "o2": "ob2"}, {"o1": "oc1", "o2": "oc2"}],
|
|
|
|
}
|
2023-08-04 14:14:14 +00:00
|
|
|
assert (
|
|
|
|
parse_template(template, kwargs) == " - it1 - it2 and list 2 ob1 ob2 oc1 oc2 "
|
|
|
|
)
|
2023-04-13 14:43:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_single_curly_brace():
|
2023-08-04 14:14:14 +00:00
|
|
|
template = "Dear {{name}} my {brace} is fine."
|
|
|
|
assert parse_template(template, template_data={"name": "John"}) == (
|
2023-04-13 14:43:09 +00:00
|
|
|
"Dear John my {brace} is fine."
|
|
|
|
)
|
2023-09-30 07:35:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_if():
|
|
|
|
template = "Dear {{ #if name }}John{{ /if }}"
|
|
|
|
assert parse_template(template, template_data={"name": True}) == "Dear John"
|
|
|
|
assert parse_template(template, template_data={"name": False}) == "Dear "
|
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_if_else():
|
|
|
|
template = "Dear {{ #if name }}John{{ else }}English{{ /if }}"
|
|
|
|
assert parse_template(template, template_data={"name": True}) == "Dear John"
|
|
|
|
assert parse_template(template, template_data={"name": False}) == "Dear English"
|
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_nested_foreaches():
|
|
|
|
template = (
|
|
|
|
"{{#each l}} - {{obj}} {{#each l2}} {{o1}} {{/each}} infix-each {{/each}}"
|
|
|
|
)
|
|
|
|
kwargs = {
|
|
|
|
"name": True,
|
|
|
|
"l": [
|
|
|
|
{
|
|
|
|
"obj": "it1",
|
|
|
|
"l2": [{"o1": "ob1", "o2": "ob2"}, {"o1": "oc1", "o2": "oc2"}],
|
|
|
|
},
|
|
|
|
{"obj": "it2", "l2": [{"o1": "ob3"}]},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
assert (
|
|
|
|
parse_template(template, kwargs)
|
|
|
|
== " - it1 ob1 oc1 infix-each - it2 ob3 infix-each "
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_template_with_nested_ifs_and_foreaches():
|
|
|
|
template = "{{#each l}} - {{obj}} {{#if name}} post-if {{#each l2}} {{o1}} {{/each}} pre-if-end {{else}}elsedata{{/if}} post-if {{/each}}"
|
|
|
|
kwargs = {
|
|
|
|
"name": True,
|
|
|
|
"l": [
|
|
|
|
{"obj": "it1", "name": True, "l2": [{"o1": "ob1"}]},
|
|
|
|
{"obj": "it2", "name": False},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
assert (
|
|
|
|
parse_template(template, kwargs)
|
|
|
|
== " - it1 post-if ob1 pre-if-end post-if - it2 elsedata post-if "
|
|
|
|
)
|