Skip to content

Commit

Permalink
fix(pain001): ✅ fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 18, 2024
1 parent a9e0f59 commit 1837c8f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 150 deletions.
129 changes: 66 additions & 63 deletions pain001/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
from pain001.context.context import Context
from pain001.core.core import process_files

# from pain001.xml.validate_via_xsd import validate_via_xsd

from rich.console import Console
from rich.table import Table
from rich import box
Expand Down Expand Up @@ -97,70 +95,75 @@ def main(
xsd_schema_file_path,
data_file_path,
):
# Check that the required arguments are provided
if not xml_message_type:
print("The XML message type is required.")
sys.exit(1)

# Check that xml_template_file_path is not invalid
if not os.path.isfile(xml_template_file_path):
print(
f"The XML template file '{xml_template_file_path}' does not exist."
)
sys.exit(1)

# Check that xsd_schema_file_path is not invalid
if not os.path.isfile(xsd_schema_file_path):
print(
f"The XSD template file '{xsd_schema_file_path}' does not exist."
try:
# Check that the required arguments are provided
if not xml_message_type:
console.print("The XML message type is required.")
sys.exit(1)

if not xml_template_file_path:
console.print("The XML template file path is required.")
sys.exit(1)

if not xsd_schema_file_path:
console.print("The XSD schema file path is required.")
sys.exit(1)

if not data_file_path:
console.print("The data file path is required.")
sys.exit(1)

logger = Context.get_instance().get_logger()

logger.info("Parsing command line arguments.")

# Check that the XML message type is valid
if xml_message_type not in valid_xml_types:
logger.info(
f"Invalid XML message type: {xml_message_type}."
)
console.print(
f"Invalid XML message type: {xml_message_type}."
)
sys.exit(1)

if not os.path.isfile(xml_template_file_path):
logger.info(
f"The XML template file '{xml_template_file_path}' does not exist."
)
console.print(
f"The XML template file '{xml_template_file_path}' does not exist."
)
sys.exit(1)

if not os.path.isfile(xsd_schema_file_path):
logger.info(
f"The XSD template file '{xsd_schema_file_path}' does not exist."
)
console.print(
f"The XSD template file '{xsd_schema_file_path}' does not exist."
)
sys.exit(1)

if not os.path.isfile(data_file_path):
logger.info(
f"The data file '{data_file_path}' does not exist."
)
console.print(
f"The data file '{data_file_path}' does not exist."
)
sys.exit(1)

process_files(
xml_message_type,
xml_template_file_path,
xsd_schema_file_path,
data_file_path,
)
except Exception as e:
console.print(f"An error occurred: {e}")
sys.exit(1)

# Check that data_file_path is not invalid
if not os.path.isfile(data_file_path):
print(f"The data file '{data_file_path}' does not exist.")
sys.exit(1)

logger = Context.get_instance().get_logger()

logger.info("Parsing command line arguments.")

# Check that the XML message type is valid
if xml_message_type not in valid_xml_types:
logger.info(f"Invalid XML message type: {xml_message_type}.")
print(f"Invalid XML message type: {xml_message_type}.")
sys.exit(1)

if not os.path.isfile(xml_template_file_path):
logger.info(
f"The XML template file '{xml_template_file_path}' does not exist."
)
print(
f"The XML template file '{xml_template_file_path}' does not exist."
)
sys.exit(1)

if not os.path.isfile(xsd_schema_file_path):
logger.info(
f"The XSD template file '{xsd_schema_file_path}' does not exist."
)
print(
f"The XSD template file '{xsd_schema_file_path}' does not exist."
)
sys.exit(1)

if not os.path.isfile(data_file_path):
logger.info(f"The data file '{data_file_path}' does not exist.")
print(f"The data file '{data_file_path}' does not exist.")
sys.exit(1)

process_files(
xml_message_type,
xml_template_file_path,
xsd_schema_file_path,
data_file_path,
)


if __name__ == "__main__":
# pylint: disable=no-value-for-parameter
Expand Down
8 changes: 5 additions & 3 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ def test_invalid_csv_data(self):
"""
Test case for processing files with invalid CSV data.
"""
with pytest.raises(SystemExit) as exc_info:
with pytest.raises(ValueError) as exc_info:
process_files(
"pain.001.001.03",
"tests/data/template.xml",
"tests/data/template.xsd",
"tests/data/invalid.csv",
)
assert exc_info.type == SystemExit
assert exc_info.value.code == 1
assert (
str(exc_info.value)
== "The CSV file 'tests/data/invalid.csv' is empty."
)

def test_successful_execution(self):
"""
Expand Down
89 changes: 5 additions & 84 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def test_main_with_valid_files(self):
self.csv_file,
],
)
assert result.exit_code == 0
assert (
"The XML has been validated against `tests/data/template.xsd`\n"
"The XML has been validated against `tests/data/template.xsd`"
in result.output
)
assert result.exit_code == 0

def test_main_with_missing_xml_message_type(self):
result = self.runner.invoke(
Expand All @@ -43,7 +43,7 @@ def test_main_with_missing_xml_message_type(self):
],
)
assert result.exit_code == 1
assert "Error: xml_message_type is required." in result.output
assert "The XML message type is required." in result.output

def test_main_with_missing_xsd_template_file(self):
result = self.runner.invoke(
Expand All @@ -58,9 +58,7 @@ def test_main_with_missing_xsd_template_file(self):
],
)
assert result.exit_code == 1
assert (
"Error: xsd_schema_file_path is required." in result.output
)
assert "The XSD schema file path is required." in result.output

def test_main_with_missing_data_file(self):
result = self.runner.invoke(
Expand All @@ -75,7 +73,7 @@ def test_main_with_missing_data_file(self):
],
)
assert result.exit_code == 1
assert "Error: data_file_path is required." in result.output
assert "The data file path is required." in result.output

def test_main_with_invalid_xml_message_type(self):
result = self.runner.invoke(
Expand Down Expand Up @@ -133,80 +131,3 @@ def test_main_with_invalid_xsd_template_file(self):
"The XSD template file 'invalid' does not exist."
in result.output
)

def test_main_with_invalid_data_file(self):
result = self.runner.invoke(
main,
[
"--xml_message_type",
self.xml_message_type,
"--xml_template_file_path",
self.xml_file,
"--xsd_schema_file_path",
self.xsd_file,
"--data_file_path",
"invalid",
],
)
assert result.exit_code == 1
assert (
"The data file 'invalid' does not exist." in result.output
)

def test_invalid_xml_template_file_path(self):
"""
Test that the `print(click.get_current_context().get_help())` line is
executed when the `xml_template_file_path` argument is set to an
invalid value.
"""

result = self.runner.invoke(
main,
[
"--xml_message_type",
"pain.001.001.03",
"--xml_template_file_path",
"invalid",
"--xsd_schema_file_path",
self.xsd_file,
"--data_file_path",
self.csv_file,
],
)

assert result.exit_code == 1
assert (
"The XML template file 'invalid' does not exist."
in result.output
)
assert (
"The XML template file 'invalid' does not exist."
in result.output
)

def test_non_existent_xml_template_file_path(self):
"""
Test that the `logger.info()` and `print()` lines are executed
when the `xml_template_file_path` argument is set to a non-existent
file path.
"""

result = self.runner.invoke(
main,
[
"--xml_message_type",
"pain.001.001.03",
"--xml_template_file_path",
"non_existent_file.xml",
"--xsd_schema_file_path",
self.xsd_file,
"--data_file_path",
self.csv_file,
],
)

assert result.exit_code == 1
assert (
"The XML template file 'non_existent_file.xml' does not exist."
in result.output
)

0 comments on commit 1837c8f

Please sign in to comment.