Skip to content

Commit dbdc92b

Browse files
authored
test: add unit testings for core functions of sanity module (#223)
Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent 4829ec5 commit dbdc92b

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
5+
from returns.maybe import Nothing, Some
6+
7+
from t4_devkit.sanity.context import SanityContext
8+
from t4_devkit.schema import SchemaName
9+
10+
11+
def test_sanity_context() -> None:
12+
"""Test the sanity context function."""
13+
data_root = Path(__file__).parent.parent.joinpath("sample/t4dataset")
14+
context = SanityContext.from_path(data_root.as_posix())
15+
16+
assert context.data_root == Some(data_root)
17+
assert context.dataset_id == Some("t4dataset")
18+
assert context.version == Nothing
19+
assert context.annotation_dir == Some(data_root.joinpath("annotation"))
20+
assert context.sensor_data_dir == Some(data_root.joinpath("data"))
21+
assert context.map_dir == Some(data_root.joinpath("map"))
22+
assert context.bag_dir == Some(data_root.joinpath("input_bag"))
23+
assert context.status_json == Some(data_root.joinpath("status.json"))
24+
25+
for schema in SchemaName:
26+
assert context.to_schema_file(schema) == Some(
27+
data_root.joinpath("annotation", schema.filename)
28+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
from t4_devkit.sanity.checker import RuleID
4+
from t4_devkit.sanity.registry import RuleGroup
5+
6+
7+
def test_rule_group_to_group() -> None:
8+
"""Test the RuleGroup.to_group method."""
9+
# valid rule IDs
10+
assert RuleGroup.to_group(RuleID("STR000")) == RuleGroup.STRUCTURE
11+
assert RuleGroup.to_group(RuleID("REC000")) == RuleGroup.RECORD
12+
assert RuleGroup.to_group(RuleID("REF000")) == RuleGroup.REFERENCE
13+
assert RuleGroup.to_group(RuleID("FMT000")) == RuleGroup.FORMAT
14+
assert RuleGroup.to_group(RuleID("TIV000")) == RuleGroup.TIER4
15+
# invalid rule IDs
16+
assert RuleGroup.to_group(RuleID("FOO000")) is None
17+
assert RuleGroup.to_group(RuleID("")) is None

tests/sanity/test_sanity_result.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from t4_devkit.sanity.checker import RuleID, RuleName, Severity
6+
from t4_devkit.sanity.result import Reason, Report, SanityResult, Status, make_report, make_skipped
7+
8+
9+
@pytest.mark.parametrize(
10+
["severity", "reasons"],
11+
[
12+
(Severity.ERROR, None),
13+
(Severity.WARNING, None),
14+
(Severity.ERROR, Reason("Failed reason")),
15+
(Severity.WARNING, Reason("Failed reason")),
16+
],
17+
)
18+
def test_make_report(severity: Severity, reasons: list[Reason] | None) -> None:
19+
"""Test the make_report function."""
20+
report = make_report(
21+
id=RuleID("STR000"),
22+
name=RuleName("custom-checker"),
23+
severity=severity,
24+
description="This is a custom checker.",
25+
reasons=reasons,
26+
)
27+
if reasons:
28+
assert report.status == Status.FAILED, f"Expected FAILED status, got {report.status}"
29+
else:
30+
assert report.status == Status.PASSED, f"Expected PASSED status, got {report.status}"
31+
32+
33+
def test_make_skipped() -> None:
34+
"""Test the make_skipped function."""
35+
report = make_skipped(
36+
id=RuleID("STR000"),
37+
name=RuleName("custom-checker"),
38+
severity=Severity.ERROR,
39+
description="This is a custom checker.",
40+
reason=Reason("Skipped reason"),
41+
)
42+
assert report.status == Status.SKIPPED, f"Expected SKIPPED status, got {report.status}"
43+
44+
45+
@pytest.mark.parametrize("strict", [True, False])
46+
def test_sanity_result(strict: bool) -> None:
47+
"""Test the SanityResult class.
48+
49+
Note:
50+
When strict=True, SanityResult.is_passed(strict) should return True if all reports are passed.
51+
Otherwise, it should return True even if at least one report whose severity is WARNING failed.
52+
"""
53+
reports = [
54+
Report(
55+
id=RuleID("STR000"),
56+
name=RuleName("custom-checker0"),
57+
severity=Severity.ERROR,
58+
description="This is a custom checker0.",
59+
status=Status.PASSED,
60+
reasons=None,
61+
),
62+
Report(
63+
id=RuleID("STR001"),
64+
name=RuleName("custom-checker1"),
65+
severity=Severity.WARNING,
66+
description="This is a custom checker1.",
67+
status=Status.PASSED,
68+
reasons=None,
69+
),
70+
Report(
71+
id=RuleID("STR002"),
72+
name=RuleName("custom-checker2"),
73+
severity=Severity.WARNING,
74+
description="This is a custom checker2.",
75+
status=Status.FAILED,
76+
reasons=[Reason("Failed reason2")],
77+
),
78+
]
79+
result = SanityResult("test-dataset", "0", reports=reports)
80+
if strict:
81+
assert result.is_passed(strict=strict) is False
82+
else:
83+
assert result.is_passed(strict=strict)

tests/sanity/test_sanity_run.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
5+
import pytest
6+
7+
from t4_devkit.sanity.run import sanity_check
8+
9+
10+
@pytest.mark.parametrize("strict", [True, False])
11+
def test_sanity_check(strict: bool) -> None:
12+
"""Test the sanity check function."""
13+
data_root = Path(__file__).parent.parent.joinpath("sample/t4dataset")
14+
result = sanity_check(data_root.as_posix())
15+
16+
if strict:
17+
assert result.is_passed(strict=strict) is False
18+
else:
19+
assert result.is_passed(strict=strict)

0 commit comments

Comments
 (0)