Skip to content

Commit

Permalink
Add function to check if there is at least one issue from a set of rules
Browse files Browse the repository at this point in the history
Signed-off-by: hoangtungdinh <[email protected]>
  • Loading branch information
hoangtungdinh committed Sep 10, 2024
1 parent a893b7b commit 8b98463
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
11 changes: 10 additions & 1 deletion qc_baselib/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from copy import deepcopy
from dataclasses import dataclass
from typing import Union, List, Dict, Any
from typing import Union, List, Set
from lxml import etree
from .models import IssueSeverity, result

Expand Down Expand Up @@ -544,3 +544,12 @@ def get_issues_by_rule_uid(self, rule_uid: str) -> List[result.IssueType]:
rule_issues.append(issue)

return rule_issues

def has_at_least_one_issue_from_rules(self, rule_uid_set: Set[str]) -> bool:
for bundle in self._report_results.checker_bundles:
for checker in bundle.checkers:
for issue in checker.issues:
if issue.rule_uid in rule_uid_set:
return True

return False
84 changes: 84 additions & 0 deletions tests/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,87 @@ def test_markdown_docs_output():
assert output_md_text == example_md_text

os.remove(TEST_MARKDOWN_DOC_OUTPUT_PATH)


def test_has_at_least_one_issue_from_rules() -> None:
result_report = Result()

result_report.register_checker_bundle(
name="TestBundle",
build_date="2024-05-31",
description="Example checker bundle",
version="0.0.1",
summary="Tested example checkers",
)

result_report.register_checker(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
description="Test checker",
summary="Executed evaluation",
)

rule_uid_1 = result_report.register_rule(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
emanating_entity="test.com",
standard="qc",
definition_setting="1.0.0",
rule_full_name="first.rule",
)

result_report.register_issue(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
description="Issue found at odr",
level=IssueSeverity.INFORMATION,
rule_uid=rule_uid_1,
)

result_report.register_checker(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
description="Test checker",
summary="Executed evaluation",
)

rule_uid_2 = result_report.register_rule(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
emanating_entity="test.com",
standard="qc",
definition_setting="1.0.0",
rule_full_name="second.rule",
)

result_report.register_issue(
checker_bundle_name="TestBundle",
checker_id="TestChecker",
description="Issue found at odr",
level=IssueSeverity.INFORMATION,
rule_uid=rule_uid_2,
)

first_rule_uid = "test.com:qc:1.0.0:first.rule"
second_rule_uid = "test.com:qc:1.0.0:second.rule"

assert (
result_report.has_at_least_one_issue_from_rules(
{"test.com:qc:1.0.0:third.rule", "test.com:qc:1.0.0:fourth.rule"}
)
== False
)

assert (
result_report.has_at_least_one_issue_from_rules(
{"test.com:qc:1.0.0:first.rule", "test.com:qc:1.0.0:fourth.rule"}
)
== True
)

assert (
result_report.has_at_least_one_issue_from_rules(
{"test.com:qc:1.0.0:second.rule", "test.com:qc:1.0.0:fourth.rule"}
)
== True
)

0 comments on commit 8b98463

Please sign in to comment.