From b1353bb6c76ed123dced371f98e51066f6077540 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 8 May 2022 09:45:45 +0200 Subject: [PATCH] Feat: add test with a template using random --- test/worker/test_generate.py | 47 ++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/test/worker/test_generate.py b/test/worker/test_generate.py index fbd3baf..015f322 100644 --- a/test/worker/test_generate.py +++ b/test/worker/test_generate.py @@ -1,7 +1,6 @@ import os import jinja2 -from pathlib import Path -from bopytex.worker.generate import tpl2tex, generate +from bopytex.worker.generate import generate, tpl2tex import pytest @@ -12,6 +11,12 @@ def test_tpl2tex(): assert fed == "Plop 1" +@pytest.fixture +def jinja2_env(tmp_path): + templateEnv = jinja2.Environment(loader=jinja2.FileSystemLoader(tmp_path)) + return templateEnv + + @pytest.fixture def template_path(tmp_path): template = tmp_path / "template.j2" @@ -20,12 +25,6 @@ def template_path(tmp_path): 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): tmp_path = template_path.parent os.chdir(tmp_path) @@ -46,3 +45,35 @@ def test_generate(template_path, jinja2_env): with open(output, "r") as out: lines = out.readlines() 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