pytest templater

pull/6/head
Yax 4 years ago
parent 8b38769cfc
commit 6397e547d8

@ -13,12 +13,15 @@ env = Environment(loader=FileSystemLoader(template_path))
TEMPLATE_DROP_COMMENT = "drop_comment" TEMPLATE_DROP_COMMENT = "drop_comment"
TEMPLATE_APPROVE_COMMENT = "approve_comment" TEMPLATE_APPROVE_COMMENT = "approve_comment"
TEMPLATE_NEW_COMMENT = "new_comment" TEMPLATE_NEW_COMMENT = "new_comment"
TEMPLATE_NOTIFY_MESSAGE = "notify_message"
TEMPLATE_RSS_TITLE_MESSAGE = "rss_title_message"
def get_template(lang, name): def get_template(lang, name):
return env.get_template(lang + "/" + name + ".tpl") return env.get_template(lang + "/" + name + ".tpl")
class Templater:
class Templater:
def __init__(self, lang, template_path): def __init__(self, lang, template_path):
self._env = Environment(loader=FileSystemLoader(template_path)) self._env = Environment(loader=FileSystemLoader(template_path))
self._lang = lang self._lang = lang
@ -26,4 +29,3 @@ class Templater:
def get_template(self, name): def get_template(self, name):
return self._env.get_template(self._lang + "/" + name + ".tpl") return self._env.get_template(self._lang + "/" + name + ".tpl")

@ -4,17 +4,61 @@
import os import os
import pytest import pytest
from stacosys.core.templater import Templater, TEMPLATE_APPROVE_COMMENT from stacosys.core.templater import (
Templater,
TEMPLATE_APPROVE_COMMENT,
TEMPLATE_DROP_COMMENT,
TEMPLATE_NEW_COMMENT,
TEMPLATE_NOTIFY_MESSAGE,
TEMPLATE_RSS_TITLE_MESSAGE,
)
@pytest.fixture
def templater_fr(): def get_template_content(lang, template_name, **kwargs):
current_path = os.path.dirname(__file__) current_path = os.path.dirname(__file__)
template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates")) template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates"))
return Templater("fr", template_path) templater = Templater(lang, template_path)
template = templater.get_template(template_name)
def test_approve_comment_fr(templater_fr):
template = templater_fr.get_template(TEMPLATE_APPROVE_COMMENT)
assert template assert template
content = template.render(original="texte original") return template.render(kwargs)
def test_approve_comment():
content = get_template_content("fr", TEMPLATE_APPROVE_COMMENT, original="[texte]")
assert content.startswith("Bonjour,\n\nLe commentaire sera bientôt publié.") assert content.startswith("Bonjour,\n\nLe commentaire sera bientôt publié.")
assert content.endswith("texte original") assert content.endswith("[texte]")
content = get_template_content("en", TEMPLATE_APPROVE_COMMENT, original="[texte]")
assert content.startswith("Hi,\n\nThe comment should be published soon.")
assert content.endswith("[texte]")
def test_drop_comment():
content = get_template_content("fr", TEMPLATE_DROP_COMMENT, original="[texte]")
assert content.startswith("Bonjour,\n\nLe commentaire ne sera pas publié.")
assert content.endswith("[texte]")
content = get_template_content("en", TEMPLATE_DROP_COMMENT, original="[texte]")
assert content.startswith("Hi,\n\nThe comment will not be published.")
assert content.endswith("[texte]")
def test_new_comment():
content = get_template_content("fr", TEMPLATE_NEW_COMMENT, comment="[comment]")
assert content.startswith("Bonjour,\n\nUn nouveau commentaire a été posté")
assert content.endswith("[comment]\n\n--\nStacosys")
content = get_template_content("en", TEMPLATE_NEW_COMMENT, comment="[comment]")
assert content.startswith("Hi,\n\nA new comment has been submitted")
assert content.endswith("[comment]\n\n--\nStacosys")
def test_notify_message():
content = get_template_content("fr", TEMPLATE_NOTIFY_MESSAGE)
assert content == "Nouveau commentaire"
content = get_template_content("en", TEMPLATE_NOTIFY_MESSAGE)
assert content == "New comment"
def test_rss_title():
content = get_template_content("fr", TEMPLATE_RSS_TITLE_MESSAGE, site="[site]")
assert content == "[site] : commentaires"
content = get_template_content("en", TEMPLATE_RSS_TITLE_MESSAGE, site="[site]")
assert content == "[site] : comments"

Loading…
Cancel
Save