jean-web/tests/test_context.py

89 lines
3.3 KiB
Python

from pathlib import Path
from pytest import mark
from jweb.context import Context
def test_render(datadir: Path) -> None:
context = Context(datadir)
context.render("test-render.html", "output.html", animal="Otters")
with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters"
def test_jinja_localization(datadir: Path) -> None:
context = Context(datadir)
context.load_translations("tests", datadir / "locale", "fr")
context.render("test-jinja-localization.html", "output.html")
with open(datadir / "build" / "en" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters"
with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Loutres"
@mark.parametrize("path", ["content.txt", Path("content.txt")])
def test_load(datadir: Path, path: str | Path) -> None:
context = Context(datadir)
context.render("test-load.html", "output.html", path=path)
with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters\n"
def test_load_localized(datadir: Path) -> None:
context = Context(datadir)
context.load_translations("tests", datadir / "locale", "fr")
context.render("test-load-localized.html", "output.html")
with open(datadir / "build" / "en" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters\nCaimans\n"
with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Loutres\nCaimans\n"
def test_write(datadir: Path) -> None:
context = Context(datadir)
context.render("test-write.html", "output.html")
with open(datadir / "build" / "content.txt", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters\n"
with open(datadir / "build/subdir/content.txt", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Weasel\n"
with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "/content.txt\n/subdir/content.txt"
def test_glob(datadir: Path) -> None:
context = Context(datadir)
context.render("test-glob.html", "output.html")
with open(datadir / "build" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters\nCaiman\n"
def test_glob_localized(datadir: Path) -> None:
context = Context(datadir)
context.load_translations("tests", datadir / "locale", "fr")
context.render("test-glob.html", "output.html")
with open(datadir / "build" / "en" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Otters\nCaimans\n"
with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Loutres\n"
def test_glob_localized_include_base_language(datadir: Path) -> None:
context = Context(datadir)
context.load_translations("tests", datadir / "locale", "fr")
context.render("test-glob-include-base-language.html", "output.html")
with open(datadir / "build" / "fr" / "output.html", encoding="utf-8") as ouput_file:
assert ouput_file.read() == "Caimans\nLoutres\n"