|
| 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) |
0 commit comments