Feat: add test with a template using random

This commit is contained in:
Bertrand Benjamin 2022-05-08 09:45:45 +02:00
parent 78f6ddc813
commit b1353bb6c7
1 changed files with 39 additions and 8 deletions

View File

@ -1,7 +1,6 @@
import os import os
import jinja2 import jinja2
from pathlib import Path from bopytex.worker.generate import generate, tpl2tex
from bopytex.worker.generate import tpl2tex, generate
import pytest import pytest
@ -12,6 +11,12 @@ def test_tpl2tex():
assert fed == "Plop 1" assert fed == "Plop 1"
@pytest.fixture
def jinja2_env(tmp_path):
templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path))
return templateEnv
@pytest.fixture @pytest.fixture
def template_path(tmp_path): def template_path(tmp_path):
template = tmp_path / "template.j2" template = tmp_path / "template.j2"
@ -20,12 +25,6 @@ def template_path(tmp_path):
return template return template
@pytest.fixture
def jinja2_env(tmp_path):
templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path))
return templateEnv
def test_generate(template_path, jinja2_env): def test_generate(template_path, jinja2_env):
tmp_path = template_path.parent tmp_path = template_path.parent
os.chdir(tmp_path) os.chdir(tmp_path)
@ -46,3 +45,35 @@ def test_generate(template_path, jinja2_env):
with open(output, "r") as out: with open(output, "r") as out:
lines = out.readlines() lines = out.readlines()
assert lines == ["Plop 2"] assert lines == ["Plop 2"]
@pytest.fixture
def template_path_with_random(tmp_path):
template = tmp_path / "template.j2"
with open(template, "w") as tpl:
tpl.write("Plop {{ random.randint(0, 10) }}")
return template
def test_generate_with_random(template_path_with_random, jinja2_env):
tmp_path = template_path_with_random.parent
os.chdir(tmp_path)
assert template_path_with_random.exists
template = str(template_path_with_random.name)
output = "output"
import random
message = generate(
args={"random": random, "jinja2": {"environment": jinja2_env}},
deps=[template],
output=output,
)
print(message.err)
assert message.status == 0
with open(output, "r") as out:
lines = out.readlines()
assert int(lines[0][-1]) > 0
assert int(lines[0][-1]) < 10