公開前の確認用サイト
日常訳NICHIJOYAKUEnglish ↗

OPEN SOURCE

software/tests/test_validation.py

このサイトで読み、必要なファイルを保存できます。

日常訳はAIを用いて作成された翻訳です。人間の聖書学・原語の専門家による全訳の検証は確認されていません。既存の翻訳や原語資料と照らし合わせてお読みください。

← オープンソースの案内へ

software/tests/test_validation.py265 行このファイルを保存 ↓

ZIPに含まれるものと同じ公開ソースです。行番号から、その行へのリンクを保存できます。

ファイルの確認情報SHA-256 2c4d14dda7a0fe949a008e99db3e70b515b95fc92ba69240320c6fa8b315bd7f
1"""Tests for validation checks."""23import pytest45from src.context.glossary import GlossaryManager6from src.models.verse import GlossaryTerm, TranslationOutput, VerseReference7from src.validation.checks import TranslationValidator, ValidationReport, ValidationResult8910@pytest.fixture11def glossary_terms() -> list[GlossaryTerm]:12    """Create test glossary terms."""13    return [14        GlossaryTerm(15            original="λόγος",16            transliteration="logos",17            japanese="言葉",18            english="word",19        ),20    ]212223class TestValidationResult:24    """Tests for ValidationResult."""2526    def test_create_result(self) -> None:27        """Test creating a validation result."""28        result = ValidationResult(29            passed=True,30            check_name="test_check",31            message="All good",32        )33        assert result.passed is True34        assert result.check_name == "test_check"3536    def test_result_with_details(self) -> None:37        """Test result with details."""38        result = ValidationResult(39            passed=False,40            check_name="length",41            message="Too short",42            details={"length": 5, "min": 10},43        )44        assert result.details["length"] == 5454647class TestValidationReport:48    """Tests for ValidationReport."""4950    def test_empty_report(self) -> None:51        """Test empty report."""52        report = ValidationReport()53        assert report.all_passed is True54        assert report.failed_checks == []5556    def test_add_result(self) -> None:57        """Test adding results."""58        report = ValidationReport()59        report.add_result(60            ValidationResult(passed=True, check_name="check1", message="OK")61        )62        assert len(report.results) == 16364    def test_all_passed_true(self) -> None:65        """Test all_passed returns True when all pass."""66        report = ValidationReport()67        report.add_result(68            ValidationResult(passed=True, check_name="check1", message="OK")69        )70        report.add_result(71            ValidationResult(passed=True, check_name="check2", message="OK")72        )73        assert report.all_passed is True7475    def test_all_passed_false(self) -> None:76        """Test all_passed returns False when any fail."""77        report = ValidationReport()78        report.add_result(79            ValidationResult(passed=True, check_name="check1", message="OK")80        )81        report.add_result(82            ValidationResult(passed=False, check_name="check2", message="Failed")83        )84        assert report.all_passed is False8586    def test_failed_checks(self) -> None:87        """Test getting failed checks."""88        report = ValidationReport()89        report.add_result(90            ValidationResult(passed=True, check_name="check1", message="OK")91        )92        report.add_result(93            ValidationResult(passed=False, check_name="check2", message="Failed")94        )95        failed = report.failed_checks96        assert len(failed) == 197        assert failed[0].check_name == "check2"9899100class TestTranslationValidator:101    """Tests for TranslationValidator."""102103    def test_init_defaults(self) -> None:104        """Test default initialization."""105        validator = TranslationValidator()106        assert validator.min_length == 5107        assert validator.max_length == 2000108109    def test_init_custom_params(self) -> None:110        """Test custom parameters."""111        validator = TranslationValidator(min_length=10, max_length=100)112        assert validator.min_length == 10113        assert validator.max_length == 100114115    def test_validate_valid_translation(self) -> None:116        """Test validating a valid translation."""117        validator = TranslationValidator()118        ref = VerseReference(book="john", chapter=1, verse=1)119        output = TranslationOutput(120            reference=ref,121            text="初めに言葉があった。言葉は神と共にあった。",122            model="test",123        )124        report = validator.validate(output)125        assert report.all_passed is True126127    def test_check_not_empty_fails(self) -> None:128        """Test not_empty check fails for empty text."""129        validator = TranslationValidator()130        ref = VerseReference(book="john", chapter=1, verse=1)131        output = TranslationOutput(reference=ref, text="", model="test")132        report = validator.validate(output)133        assert not report.all_passed134        failed_names = [r.check_name for r in report.failed_checks]135        assert "not_empty" in failed_names136137    def test_check_not_empty_whitespace_only(self) -> None:138        """Test not_empty check fails for whitespace-only text."""139        validator = TranslationValidator()140        ref = VerseReference(book="john", chapter=1, verse=1)141        output = TranslationOutput(reference=ref, text="   \n\t  ", model="test")142        report = validator.validate(output)143        failed_names = [r.check_name for r in report.failed_checks]144        assert "not_empty" in failed_names145146    def test_check_length_too_short(self) -> None:147        """Test length check fails for short text."""148        validator = TranslationValidator(min_length=10)149        ref = VerseReference(book="john", chapter=1, verse=1)150        output = TranslationOutput(reference=ref, text="短い", model="test")151        report = validator.validate(output)152        failed_names = [r.check_name for r in report.failed_checks]153        assert "length" in failed_names154155    def test_check_length_too_long(self) -> None:156        """Test length check fails for long text."""157        validator = TranslationValidator(max_length=10)158        ref = VerseReference(book="john", chapter=1, verse=1)159        output = TranslationOutput(160            reference=ref, text="これはとても長い文章です。", model="test"161        )162        report = validator.validate(output)163        failed_names = [r.check_name for r in report.failed_checks]164        assert "length" in failed_names165166    def test_check_no_english_only_fails(self) -> None:167        """Test no_english_only check fails for English-only text."""168        validator = TranslationValidator()169        ref = VerseReference(book="john", chapter=1, verse=1)170        output = TranslationOutput(171            reference=ref, text="In the beginning was the Word", model="test"172        )173        report = validator.validate(output)174        failed_names = [r.check_name for r in report.failed_checks]175        assert "no_english_only" in failed_names176177    def test_check_has_japanese_low_ratio(self) -> None:178        """Test has_japanese check fails for low Japanese ratio."""179        validator = TranslationValidator()180        ref = VerseReference(book="john", chapter=1, verse=1)181        # Mostly English with just a few Japanese182        output = TranslationOutput(183            reference=ref, text="This is English text あ", model="test"184        )185        report = validator.validate(output)186        failed_names = [r.check_name for r in report.failed_checks]187        assert "has_japanese" in failed_names188189    def test_check_glossary_consistency_no_glossary(self) -> None:190        """Test glossary check passes without glossary."""191        validator = TranslationValidator()192        ref = VerseReference(book="john", chapter=1, verse=1)193        output = TranslationOutput(194            reference=ref,195            text="初めに言葉があった。",196            model="test",197            glossary_terms_applied=["λόγος"],198        )199        report = validator.validate(output)200        # Should pass since no glossary to check against201        glossary_results = [202            r for r in report.results if r.check_name == "glossary_consistency"203        ]204        assert len(glossary_results) == 0  # No glossary configured205206    def test_check_glossary_consistency_with_glossary(207        self, glossary_terms: list[GlossaryTerm]208    ) -> None:209        """Test glossary check with glossary."""210        glossary = GlossaryManager(glossary_terms)211        validator = TranslationValidator(glossary=glossary)212        ref = VerseReference(book="john", chapter=1, verse=1)213        output = TranslationOutput(214            reference=ref,215            text="初めに言葉があった。",  # Contains 言葉216            model="test",217            glossary_terms_applied=["λόγος"],218        )219        report = validator.validate(output)220        glossary_results = [221            r for r in report.results if r.check_name == "glossary_consistency"222        ]223        assert len(glossary_results) == 1224        assert glossary_results[0].passed is True225226    def test_check_glossary_consistency_missing_term(227        self, glossary_terms: list[GlossaryTerm]228    ) -> None:229        """Test glossary check fails for missing term."""230        glossary = GlossaryManager(glossary_terms)231        validator = TranslationValidator(glossary=glossary)232        ref = VerseReference(book="john", chapter=1, verse=1)233        output = TranslationOutput(234            reference=ref,235            text="初めにワードがあった。",  # Missing 言葉, using ワード instead236            model="test",237            glossary_terms_applied=["λόγος"],238        )239        report = validator.validate(output)240        glossary_results = [241            r for r in report.results if r.check_name == "glossary_consistency"242        ]243        assert len(glossary_results) == 1244        assert glossary_results[0].passed is False245246    def test_check_glossary_consistency_no_terms_applied(247        self, glossary_terms: list[GlossaryTerm]248    ) -> None:249        """Test glossary check passes when no terms expected."""250        glossary = GlossaryManager(glossary_terms)251        validator = TranslationValidator(glossary=glossary)252        ref = VerseReference(book="john", chapter=1, verse=1)253        output = TranslationOutput(254            reference=ref,255            text="初めにあった。",256            model="test",257            glossary_terms_applied=[],  # No terms expected258        )259        report = validator.validate(output)260        glossary_results = [261            r for r in report.results if r.check_name == "glossary_consistency"262        ]263        assert len(glossary_results) == 1264        assert glossary_results[0].passed is True265