Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify Template tests & add Loader tests #483

Merged
merged 7 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/invoice2data/extract/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,28 @@ def read_templates(folder=None):
logger.warning("json Loader Failed to load %s template:\n%s", name, error)
tpl["template_name"] = name

# Test if all required fields are in template:
assert "keywords" in tpl.keys(), "Missing keywords field."

# Keywords as list, if only one.
if type(tpl["keywords"]) is not list:
# Test if all required fields are in template
if "keywords" not in tpl.keys():
logger.warning(
"Failed to load template %s Missing mandatory 'keywords' field.",
name,
)
continue

# Convert keywords to list, if only one
if not isinstance(tpl["keywords"], list):
tpl["keywords"] = [tpl["keywords"]]

# Define excluded_keywords as empty list if not provided
# Convert to list if only one provided
# Set excluded_keywords as empty list, if not provided
if "exclude_keywords" not in tpl.keys():
tpl["exclude_keywords"] = []
elif type(tpl["exclude_keywords"]) is not list:

# Convert excluded_keywords to list, if only one
if not isinstance(tpl["exclude_keywords"], list):
tpl["exclude_keywords"] = [tpl["exclude_keywords"]]

if 'priority' not in tpl.keys():
tpl['priority'] = 5
if "priority" not in tpl.keys():
tpl["priority"] = 5

output.append(InvoiceTemplate(tpl))

Expand Down
117 changes: 117 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import shutil
from pathlib import Path

import os
import pytest

from invoice2data.extract.invoice_template import InvoiceTemplate
from invoice2data.extract.loader import read_templates


@pytest.fixture
def templatedirectory() -> Path:
templatedirectory = Path("tests/templatedirectory/")
templatedirectory.mkdir(parents=True)

yield templatedirectory

shutil.rmtree(templatedirectory, ignore_errors=True)


def test_default_templates_are_loaded():
templates = read_templates()

builtin_tpl_folder = "./src/invoice2data/extract/templates"
qty_templ_files = sum(len(files) for _, _, files in os.walk(builtin_tpl_folder))

print("Amount of loaded templates %s" % len(templates))
print("Amount of template files %s" % qty_templ_files)
assert len(templates) == qty_templ_files
assert all(isinstance(template, InvoiceTemplate) for template in templates)


def test_template_with_missing_keywords_is_not_loaded(templatedirectory: Path):
yamlfile = templatedirectory / "template_with_missing_keywords.yml"
yamlfile.write_text(template_with_missing_keywords, encoding="utf-8")

templates = read_templates(str(templatedirectory))
assert templates == []


def test_template_name_is_yaml_filename(templatedirectory: Path):
yamlfile = templatedirectory / "thisnameisimportant.yml"
yamlfile.write_text(template_with_single_special_char, encoding="utf-8")

templates = read_templates(str(templatedirectory))

assert templates[0]["template_name"] == "thisnameisimportant.yml"


def test_template_with_single_specialchar_is_loaded(templatedirectory: Path):
yamlfile = templatedirectory / "specialchartemplate.yml"
yamlfile.write_text(template_with_single_special_char, encoding="utf-8")

templates = read_templates(str(templatedirectory))

assert templates[0]["fields"]["single_specialchar"]["value"] == "ä"


def test_template_with_keyword_is_not_list(templatedirectory: Path):
yamlfile = templatedirectory / "keywordnotlist.yml"
yamlfile.write_text(template_keyword_not_list, encoding="utf-8")

tpl = read_templates(str(templatedirectory))
assert tpl[0]["keywords"] == ['Basic Test']


def test_template_with_exclude_keyword_is_not_list(templatedirectory: Path):
yamlfile = templatedirectory / "excludekeywordnotlist.yml"
yamlfile.write_text(template_exclude_keyword_not_list, encoding="utf-8")

tpl = read_templates(str(templatedirectory))
assert tpl[0]["exclude_keywords"] == ['Exclude_this']


def test_template_bad_yaml_format_not_loaded(templatedirectory: Path):
yamlfile = templatedirectory / "template_bad_yaml.yml"
yamlfile.write_text(template_bad_yaml, encoding="utf-8")

tpl = read_templates(str(templatedirectory))
assert tpl == [], "Bad Yaml Template is loaded!"


template_with_missing_keywords = """
fields:
foo:
parser: static
value: bar
"""


template_with_single_special_char = """
keywords:
- Basic Test
fields:
single_specialchar:
parser: static
value: ä
"""


template_keyword_not_list = """
keywords: Basic Test
"""


template_exclude_keyword_not_list = """
keywords: Basic Test
exclude_keywords: Exclude_this
"""


template_bad_yaml = """
keywords: Basic Test
exclude_keywords Exclude_this
options:
language: EN
"""