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

Fix models search mode to catch all sub-elements occurrences #27

Merged
merged 5 commits into from
Aug 21, 2024
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
4 changes: 2 additions & 2 deletions qc_baselib/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ReportModuleType(BaseXmlModel):
application: str = attr(name="application")


class CheckerBundleType(BaseXmlModel):
class CheckerBundleType(BaseXmlModel, search_mode="unordered"):
params: List[ParamType] = element(tag="Param", default=[])
checkers: List[CheckerType] = element(tag="Checker", default=[])
application: str = attr(name="application")
Expand All @@ -40,7 +40,7 @@ def check_at_least_one_element(self) -> Any:
return self


class Config(BaseXmlModel, tag="Config"):
class Config(BaseXmlModel, tag="Config", search_mode="unordered"):
params: List[ParamType] = element(tag="Param", default=[])
reports: List[ReportModuleType] = element(tag="ReportModule", default=[])
checker_bundles: List[CheckerBundleType] = element(tag="CheckerBundle", default=[])
Expand Down
10 changes: 6 additions & 4 deletions qc_baselib/models/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FileLocationType(BaseXmlModel):
row: int = attr(name="row")


class LocationType(BaseXmlModel):
class LocationType(BaseXmlModel, search_mode="unordered"):
file_location: List[FileLocationType] = element(tag="FileLocation", default=[])
xml_location: List[XMLLocationType] = element(tag="XMLLocation", default=[])
inertial_location: List[InertialLocationType] = element(
Expand All @@ -44,7 +44,9 @@ class LocationType(BaseXmlModel):
@model_validator(mode="after")
def check_at_least_one_element(self) -> Any:
if (
len(self.file_location) + len(self.xml_location) + len(self.inertial_location)
len(self.file_location)
+ len(self.xml_location)
+ len(self.inertial_location)
< 1
):
raise ValueError(
Expand Down Expand Up @@ -192,7 +194,7 @@ class StatusType(str, enum.Enum):
SKIPPED = "skipped"


class CheckerType(BaseXmlModel, validate_assignment=True, search_mode="ordered"):
class CheckerType(BaseXmlModel, validate_assignment=True, search_mode="unordered"):
addressed_rule: List[RuleType] = element(tag="AddressedRule", default=[])
issues: List[IssueType] = element(tag="Issue", default=[])
metadata: List[MetadataType] = element(tag="Metadata", default=[])
Expand Down Expand Up @@ -225,7 +227,7 @@ def check_skipped_status_containing_issues(self) -> Any:
return self


class CheckerBundleType(BaseXmlModel):
class CheckerBundleType(BaseXmlModel, search_mode="unordered"):
params: List[ParamType] = element(tag="Param", default=[])
checkers: List[CheckerType] = element(tag="Checker", default=[])
build_date: str = attr(name="build_date")
Expand Down
18 changes: 18 additions & 0 deletions tests/data/ordered_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Config>

<Param name="InputFile" value="../stimuli/xodr_examples/three_connected_roads_with_steps.xodr" />

<ReportModule application="TextReport">
<Param name="strInputFile" value="Result.xqar" />
<Param name="strReportFile" value="Report.txt" />
</ReportModule>

<CheckerBundle application="DemoCheckerBundle">
<Param name="strResultFile" value="DemoCheckerBundle.xqar" />
<Checker checkerId="exampleChecker" maxLevel="1" minLevel="3" />
</CheckerBundle>



</Config>
16 changes: 16 additions & 0 deletions tests/data/unordered_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Config>

<Param name="InputFile" value="../stimuli/xodr_examples/three_connected_roads_with_steps.xodr" />

<CheckerBundle application="DemoCheckerBundle">
<Param name="strResultFile" value="DemoCheckerBundle.xqar" />
<Checker checkerId="exampleChecker" maxLevel="1" minLevel="3" />
</CheckerBundle>

<ReportModule application="TextReport">
<Param name="strInputFile" value="Result.xqar" />
<Param name="strReportFile" value="Report.txt" />
</ReportModule>

</Config>
26 changes: 25 additions & 1 deletion tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from qc_baselib.models import config, result
from qc_baselib import Configuration


TEST_DATA_BASE_PATH = "tests/data"
DEMO_CONFIG_PATH = "tests/data/demo_checker_bundle_config.xml"
EXAMPLE_OUTPUT_CONFIG_PATH = "tests/data/config_test_output.xml"
TEST_CONFIG_OUTPUT_PATH = "tests/config_test_output.xml"
Expand Down Expand Up @@ -172,3 +172,27 @@ def test_config_write() -> None:
assert output_xml_text == example_xml_text

os.remove(TEST_CONFIG_OUTPUT_PATH)


def test_config_file_parse_order_independence() -> None:
config_unordered = Configuration()
config_unordered.load_from_file(
os.path.join(TEST_DATA_BASE_PATH, "unordered_config.xml")
)

config_ordered = Configuration()
config_ordered.load_from_file(
os.path.join(TEST_DATA_BASE_PATH, "ordered_config.xml")
)

assert len(config_ordered._configuration.reports) == len(
config_unordered._configuration.reports
)

assert len(config_ordered._configuration.checker_bundles) == len(
config_unordered._configuration.checker_bundles
)

assert len(config_ordered._configuration.params) == len(
config_unordered._configuration.params
)