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

refactor: reorganize excel2xml unit tests #636

Merged
merged 1 commit into from
Nov 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file.
87 changes: 87 additions & 0 deletions test/unittests/commands/excel2xml/test_excel2xml_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# pylint: disable=missing-class-docstring,missing-function-docstring

import os
import unittest

import regex

from dsp_tools.commands.excel2xml import excel2xml_cli
from dsp_tools.models.exceptions import BaseError


class TestExcel2xmlCli(unittest.TestCase):
def test_excel2xml_cli(self) -> None:
# test the valid files, 3 times identical, but in the three formats XLSX, XLS, and CSV
with open("testdata/excel2xml/excel2xml-expected-output.xml", encoding="utf-8") as f:
expected = f.read()
for ext in ["xlsx", "xls", "csv"]:
excel2xml_cli.excel2xml(f"testdata/excel2xml/excel2xml-testdata.{ext}", "1234", "excel2xml-output")
with open("excel2xml-output-data.xml", encoding="utf-8") as f:
returned = f.read()
self.assertEqual(returned, expected, msg=f"Failed with extension {ext}")
if os.path.isfile("excel2xml-output-data.xml"):
os.remove("excel2xml-output-data.xml")

# test the invalid files
invalid_prefix = "testdata/invalid-testdata/excel2xml"
warning_cases = [
(
f"{invalid_prefix}/boolean-prop-two-values.xlsx",
"A <boolean-prop> can only have a single value",
),
(
f"{invalid_prefix}/empty-property.xlsx",
"At least one value per property is required",
),
(
f"{invalid_prefix}/missing-prop-permissions.xlsx",
"Missing permissions in column '2_permissions' of property ':hasName'",
),
(
f"{invalid_prefix}/missing-resource-label.xlsx",
"Missing label for resource",
),
(
f"{invalid_prefix}/missing-resource-permissions.xlsx",
"Missing permissions for resource",
),
(
f"{invalid_prefix}/missing-restype.xlsx",
"Missing restype",
),
(
f"{invalid_prefix}/no-bitstream-permissions.xlsx",
"Missing file permissions",
),
(
f"{invalid_prefix}/single-invalid-value-for-property.xlsx",
"row 3 has an entry in column.+ '1_encoding', '1_permissions', but not",
),
]
for file, _regex in warning_cases:
_, catched_warnings = excel2xml_cli.excel2xml(file, "1234", "excel2xml-invalid")
self.assertTrue(len(catched_warnings) > 0)
messages = [str(w.message) for w in catched_warnings]
self.assertTrue(any(regex.search(_regex, msg) for msg in messages), msg=f"Failed with file '{file}'")

error_cases = [
(
f"{invalid_prefix}/id-propname-both.xlsx",
"Exactly 1 of the 2 columns 'id' and 'prop name' must be filled",
),
(
f"{invalid_prefix}/id-propname-none.xlsx",
"Exactly 1 of the 2 columns 'id' and 'prop name' must be filled",
),
(
f"{invalid_prefix}/nonexisting-proptype.xlsx",
"Invalid prop type",
),
(
f"{invalid_prefix}/start-with-property-row.xlsx",
"The first row must define a resource, not a property",
),
]
for file, _regex in error_cases:
with self.assertRaisesRegex(BaseError, _regex, msg=f"Failed with file '{file}'"):
excel2xml_cli.excel2xml(file, "1234", "excel2xml-invalid")
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# pylint: disable=missing-class-docstring,missing-function-docstring,too-many-public-methods

import os
import unittest
from pathlib import Path
from typing import Any, Callable, Optional, Sequence, Union
Expand All @@ -12,7 +11,6 @@
from lxml import etree

from dsp_tools import excel2xml
from dsp_tools.commands.excel2xml import excel2xml_cli
from dsp_tools.models.exceptions import BaseError


Expand Down Expand Up @@ -149,7 +147,7 @@ def run_test(
method(**kwargs_invalid_value)


class TestExcel2xml(unittest.TestCase):
class TestExcel2xmlLib(unittest.TestCase):
def tearDown(self) -> None:
Path("excel2xml-invalid-data.xml").unlink(missing_ok=True)

Expand Down Expand Up @@ -718,82 +716,6 @@ def test_create_json_list_mapping(self) -> None:
}
self.assertDictEqual(testlist_mapping_returned, testlist_mapping_expected)

def test_excel2xml(self) -> None:
# test the valid files, 3 times identical, but in the three formats XLSX, XLS, and CSV
with open("testdata/excel2xml/excel2xml-expected-output.xml", encoding="utf-8") as f:
expected = f.read()
for ext in ["xlsx", "xls", "csv"]:
excel2xml_cli.excel2xml(f"testdata/excel2xml/excel2xml-testdata.{ext}", "1234", "excel2xml-output")
with open("excel2xml-output-data.xml", encoding="utf-8") as f:
returned = f.read()
self.assertEqual(returned, expected, msg=f"Failed with extension {ext}")
if os.path.isfile("excel2xml-output-data.xml"):
os.remove("excel2xml-output-data.xml")

# test the invalid files
invalid_prefix = "testdata/invalid-testdata/excel2xml"
warning_cases = [
(
f"{invalid_prefix}/boolean-prop-two-values.xlsx",
"A <boolean-prop> can only have a single value",
),
(
f"{invalid_prefix}/empty-property.xlsx",
"At least one value per property is required",
),
(
f"{invalid_prefix}/missing-prop-permissions.xlsx",
"Missing permissions in column '2_permissions' of property ':hasName'",
),
(
f"{invalid_prefix}/missing-resource-label.xlsx",
"Missing label for resource",
),
(
f"{invalid_prefix}/missing-resource-permissions.xlsx",
"Missing permissions for resource",
),
(
f"{invalid_prefix}/missing-restype.xlsx",
"Missing restype",
),
(
f"{invalid_prefix}/no-bitstream-permissions.xlsx",
"Missing file permissions",
),
(
f"{invalid_prefix}/single-invalid-value-for-property.xlsx",
"row 3 has an entry in column.+ '1_encoding', '1_permissions', but not",
),
]
for file, _regex in warning_cases:
_, catched_warnings = excel2xml_cli.excel2xml(file, "1234", "excel2xml-invalid")
self.assertTrue(len(catched_warnings) > 0)
messages = [str(w.message) for w in catched_warnings]
self.assertTrue(any(regex.search(_regex, msg) for msg in messages), msg=f"Failed with file '{file}'")

error_cases = [
(
f"{invalid_prefix}/id-propname-both.xlsx",
"Exactly 1 of the 2 columns 'id' and 'prop name' must be filled",
),
(
f"{invalid_prefix}/id-propname-none.xlsx",
"Exactly 1 of the 2 columns 'id' and 'prop name' must be filled",
),
(
f"{invalid_prefix}/nonexisting-proptype.xlsx",
"Invalid prop type",
),
(
f"{invalid_prefix}/start-with-property-row.xlsx",
"The first row must define a resource, not a property",
),
]
for file, _regex in error_cases:
with self.assertRaisesRegex(BaseError, _regex, msg=f"Failed with file '{file}'"):
excel2xml_cli.excel2xml(file, "1234", "excel2xml-invalid")


if __name__ == "__main__":
pytest.main([__file__])