Skip to content

Commit

Permalink
Add additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tomschr committed Jun 24, 2024
1 parent a902366 commit 1df2894
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ def check_namespace(tree: etree._ElementTree, config: dict[t.Any, t.Any]):
tag = etree.QName(tree.getroot().tag)
if tag.namespace != DOCBOOK_NS:
raise InvalidValueError(f"Root element {tag.localname!r} doesn't belong to DocBook 5.")

15 changes: 4 additions & 11 deletions python-scripts/metadatavalidator/src/metadatavalidator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,11 @@ def validate_valid_meta_architecture(config: dict) -> list[str]:
:param config: the configuration object
:return: a list of valid meta architecture
"""
try:
# architectures = re.split(r"[;,]", config.get("metadata", {}).get("valid_meta_architecture", []))
return [x.strip() for x in re.split(r"[;,]",
return [x.strip() for x in re.split(r"[;,]",
config.get("metadata", {}).get("valid_meta_architecture", "")
)
if x
]
except TypeError:
raise MissingKeyError("metadata.valid_meta_architecture")
]


def validate_valid_meta_category(config: dict) -> list[str]:
Expand All @@ -172,14 +168,11 @@ def validate_valid_meta_category(config: dict) -> list[str]:
:param config: the configuration object
:return: a list of valid meta category
"""
try:
return [x.strip() for x in re.split(r"[;,]",
return [x.strip() for x in re.split(r"[;,]",
config.get("metadata", {}).get("valid_meta_category", "")
)
if x
]
except TypeError:
raise MissingKeyError("metadata.valid_meta_category")
]


def validate_and_convert_config(config: configparser.ConfigParser) -> dict[t.Any, t.Any]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from lxml import etree

from metadatavalidator.checks import (
check_info, check_info_revhistory,
check_info,
check_info_revhistory,
check_info_revhistory_revision,
check_info_revhistory_revision_date,
check_info_revhistory_revision_order,
Expand Down Expand Up @@ -49,7 +50,7 @@ def test_check_info_revhistory_missing(xmlparser):

with pytest.raises(InvalidValueError,
match="Couldn't find a revhistory element"):
check_info_revhistory(tree, {})
check_info_revhistory(tree, {"metadata": {"require_revhistory": True}})


def test_check_info_revhistory(xmlparser):
Expand All @@ -76,7 +77,6 @@ def test_check_info_revhistory_without_info(xmlparser):
)

assert check_info_revhistory(tree, {}) is None
assert check_info_revhistory(tree, {}) is None


def test_check_info_revhistory_xmlid(xmlparser):
Expand All @@ -94,11 +94,11 @@ def test_check_info_revhistory_xmlid(xmlparser):
assert check_info_revhistory(tree, {}) is None


def test_check_info_revhistory_missing_xmlid(xmlparser):
def test_info_revhistory_missing_xmlid(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
<revhistory></revhistory>
<revhistory/>
</info>
<para/>
</article>"""
Expand Down Expand Up @@ -169,20 +169,6 @@ def test_check_info_revhistory_revision_missing_xmlid(xmlparser):
{"metadata": {"require_xmlid_on_revision": True}})


def test_check_info_revhistory_missing(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
</info>
<para/>
</article>"""
tree = etree.ElementTree(
etree.fromstring(xmlcontent, parser=xmlparser)
)

check_info_revhistory_revision(tree, {}) is None


def test_check_info_revhistory_revision_missing(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
Expand Down Expand Up @@ -334,3 +320,31 @@ def test_check_info_revhistory_revision_order_one_invalid_date(xmlparser):
match=".*Couldn't convert all dates.*see position dates=1.*"
):
check_info_revhistory_revision_order(tree, {})


def test_check_info_revhistory_revision_wrong_order(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
<revhistory xml:id="rh">
<revision>
<date>2024-12</date>
</revision>
<revision>
<date>2023-12-12</date>
</revision>
<revision>
<date>2026-04</date>
</revision>
</revhistory>
</info>
<para/>
</article>"""
tree = etree.ElementTree(
etree.fromstring(xmlcontent, parser=xmlparser)
)

with pytest.raises(InvalidValueError,
match=".*Dates in revhistory/revision are not in descending order.*"
):
check_info_revhistory_revision_order(tree, {})
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,70 @@ def test_meta_category(xmlparser):
tree = etree.ElementTree(etree.fromstring(xmlcontent, parser=xmlparser))
config = dict(metadata=dict(require_meta_category=True,
valid_meta_category=["Systems Management"]))
assert check_meta_category(tree, config) is None
assert check_meta_category(tree, config) is None


def test_missing_optional_meta_category(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
</info>
<para/>
</article>"""
tree = etree.ElementTree(etree.fromstring(xmlcontent, parser=xmlparser))
config = dict(metadata=dict(require_meta_category=True))
with pytest.raises(InvalidValueError,
match=r".*Couldn't find required meta.*"):
check_meta_category(tree, config)


def test_missing_child_meta_category(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
<meta name="category"/>
</info>
<para/>
</article>"""
tree = etree.ElementTree(etree.fromstring(xmlcontent, parser=xmlparser))
config = dict(metadata=dict(require_meta_category=True))
with pytest.raises(InvalidValueError,
match=r".*Couldn't find any child elements in meta.*"):
check_meta_category(tree, config)


def test_duplicate_child_meta_category(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
<meta name="category">
<phrase>Systems Management</phrase>
<phrase>Systems Management</phrase>
</meta>
</info>
<para/>
</article>"""
tree = etree.ElementTree(etree.fromstring(xmlcontent, parser=xmlparser))
config = dict(metadata=dict(require_meta_category=True))
with pytest.raises(InvalidValueError,
match=r".*Duplicate categories found in meta.*"):
check_meta_category(tree, config)


def test_unknown_category_meta_category(xmlparser):
xmlcontent = """<article xmlns="http://docbook.org/ns/docbook" version="5.2">
<info>
<title>Test</title>
<meta name="category">
<phrase>Systems Management</phrase>
<phrase>Foo</phrase>
</meta>
</info>
<para/>
</article>"""
tree = etree.ElementTree(etree.fromstring(xmlcontent, parser=xmlparser))
config = dict(metadata=dict(require_meta_category=True,
valid_meta_category=["Systems Management"]))
with pytest.raises(InvalidValueError,
match=r".*Unknown category.*"):
check_meta_category(tree, config)
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def test_missing_validator_section():
validate_and_convert_config(config)


def test_missing_metadata_section():
config = ConfigParser()
config.add_section("validator")
with pytest.raises(MissingSectionError, match=".*metadata.*"):
validate_and_convert_config(config)


def test_missing_key_check_root_elements(config):
config.remove_option("validator", "check_root_elements")
with pytest.raises(MissingKeyError, match=".*validator.check_root_elements.*"):
Expand Down

0 comments on commit 1df2894

Please sign in to comment.