Skip to content

Commit

Permalink
Test str result from render()
Browse files Browse the repository at this point in the history
  • Loading branch information
maddenp-noaa committed Feb 29, 2024
1 parent 22601fc commit 727ab6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/uwtools/tests/api/test_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pylint: disable=missing-function-docstring,redefined-outer-name

import os
from pathlib import Path
from unittest.mock import patch

from pytest import fixture, raises
Expand Down Expand Up @@ -34,6 +36,13 @@ def test_render_fail(kwargs):
template.render(**kwargs)


def test_render_to_str(kwargs):
del kwargs["output_file"]
with patch.object(template, "render") as render:
template.render_to_str(**kwargs)
render.assert_called_once_with(**{**kwargs, "output_file": Path(os.devnull)})


def test_translate():
kwargs: dict = {
"input_file": "path1",
Expand Down
14 changes: 9 additions & 5 deletions src/uwtools/tests/config/test_jinja2.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def values_file(tmp_path):


def render_helper(input_file, values_file, **kwargs):
jinja2.render(
return jinja2.render(
input_file=input_file,
values=values_file,
**kwargs,
Expand Down Expand Up @@ -163,9 +163,11 @@ def test_register_filters_path_join(key):

def test_render(values_file, template, tmp_path):
outfile = str(tmp_path / "out.txt")
render_helper(input_file=template, values_file=values_file, output_file=outfile)
expected = "roses are red, violets are blue"
result = render_helper(input_file=template, values_file=values_file, output_file=outfile)
assert result == expected
with open(outfile, "r", encoding="utf-8") as f:
assert f.read().strip() == "roses are red, violets are blue"
assert f.read().strip() == expected


def test_render_calls__dry_run(template, tmp_path, values_file):
Expand Down Expand Up @@ -210,10 +212,12 @@ def test_render_calls__write(template, tmp_path, values_file):

def test_render_dry_run(caplog, template, values_file):
log.setLevel(logging.INFO)
render_helper(
expected = "roses are red, violets are blue"
result = render_helper(
input_file=template, values_file=values_file, output_file="/dev/null", dry_run=True
)
assert logged(caplog, "roses are red, violets are blue")
assert result == expected
assert logged(caplog, expected)


@pytest.mark.parametrize("partial", [False, True])
Expand Down

0 comments on commit 727ab6c

Please sign in to comment.