adding latest tests

This commit is contained in:
2021-10-30 02:12:36 +01:00
parent be55e20e4d
commit 1f3fa4100d
5 changed files with 147 additions and 131 deletions

View File

@@ -0,0 +1,126 @@
import pathlib
import pytest
import jinja2
from tembo.journal.pages import PageCreator, ScopedPageCreator
def test_page_creator_convert_to_path_missing_base_path(caplog):
# arrange
base_path = "/some/nonexistent/path"
page_path = "some_page"
filename = "some_filename"
extension = "ex"
# act
with pytest.raises(SystemExit) as system_exit:
PageCreator._convert_to_path(
base_path=base_path,
page_path=page_path,
filename=filename,
extension=extension,
)
# assert
assert system_exit.value.code == 1
assert caplog.records[0].levelname == "CRITICAL"
assert (
caplog.records[0].message
== "Tembo base path of /some/nonexistent/path does not exist - exiting"
)
@pytest.mark.parametrize(
"page_path,filename,extension",
[
("some_pagepath", "some_filename", "ex"),
("some pagepath", "some filename", "ex"),
],
)
def test_page_creator_convert_to_path_full_path_to_file(
page_path, filename, extension, tmpdir
):
# arrange
path_to_file = (
pathlib.Path(tmpdir)
/ pathlib.Path(page_path)
/ pathlib.Path(filename).with_suffix(f".{extension}")
)
base_path = tmpdir
# act
converted_path = PageCreator._convert_to_path(
base_path, page_path, filename, extension
)
# assert
assert str(path_to_file).replace(" ", "_") == str(converted_path)
def test_page_creator_convert_to_path_full_path_no_file(tmpdir):
# arrange
full_path = pathlib.Path("/some/path")
base_path = ""
page_path = "/some/path"
filename = ""
extension = ""
# act
converted_path = PageCreator._convert_to_path(
base_path, page_path, filename, extension
)
# assert
assert str(full_path).replace(" ", "_") == str(converted_path)
def test_page_creator_load_template_with_base_path_success(datadir):
# arrange
# default template_path would be datadir/.templates
base_path = str(datadir)
template_filename = "some_template.md.tpl"
# act
template_contents = ScopedPageCreator()._load_template(
base_path, template_filename, None
)
# assert
assert template_contents == "template contents"
def test_page_creator_load_template_overriden_template_path_success(datadir):
# arrange
base_path = str(datadir)
template_filename = "some_template.md.tpl"
template_path = str(datadir / ".templates")
# act
# we explicitly pass in the template_path to override the default
template_contents = ScopedPageCreator()._load_template(
base_path, template_filename, template_path
)
# assert
assert template_contents == "template contents"
def test_page_creator_load_template_missing_template_file(datadir, caplog):
# arrange
base_path = str(datadir)
template_filename = "some_nonexistent_template.md.tpl"
template_path = str(datadir / ".templates")
# act
with pytest.raises(SystemExit) as system_exit:
template_contents = ScopedPageCreator()._load_template(
base_path, template_filename, template_path
)
# assert
assert system_exit.value.code == 1
assert caplog.records[0].message == (
f"Template file {template_path}/some_nonexistent_template.md.tpl not found "
"- exiting"
)