Skip to content

Commit 94b0e27

Browse files
authored
LanuguageCode: Add tests to raise coverage to 100% (#104)
1 parent e97a674 commit 94b0e27

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/docbuild/models/language.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"""Language model for representing language codes."""
22

3-
from functools import total_ordering, cached_property
4-
import re
3+
from functools import cached_property, total_ordering
54
from typing import Any, ClassVar
65

7-
from pydantic import BaseModel, Field, computed_field, model_validator, field_validator
6+
from pydantic import BaseModel, Field, computed_field, field_validator, model_validator
87
from pydantic.config import ConfigDict
98

109
from ..constants import ALLOWED_LANGUAGES
@@ -118,7 +117,7 @@ def matches(self, other: 'LanguageCode | str') -> bool:
118117
return (
119118
self.language == '*' or other_value == '*' or self.language == other_value
120119
)
121-
120+
122121
@field_validator('language', mode='before')
123122
@classmethod
124123
def _normalize_language_separator(cls, value: str) -> str:
@@ -153,7 +152,7 @@ def _parts(self) -> tuple[str, str] | tuple[str]:
153152
"""
154153
if self.language == '*':
155154
return ('*',)
156-
155+
157156
# Use split('-') as the separator is already normalized
158157
parts = self.language.split('-')
159158
return (parts[0], parts[1]) if len(parts) > 1 else (parts[0],)
@@ -174,4 +173,4 @@ def lang(self) -> str:
174173
)
175174
def country(self) -> str:
176175
"""Extract the country part of the language code (property)."""
177-
return self._parts[1] if len(self._parts) > 1 else '*'
176+
return self._parts[1] if len(self._parts) > 1 else '*'

tests/models/test_language.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,28 @@ def test_compare_with_unknown_type():
138138

139139
def test_language_matches():
140140
lang1 = LanguageCode(language='de-de')
141-
assert lang1.matches('*')
141+
assert lang1.matches('*')
142+
143+
144+
def test_language_code_init_with_invalid_type_raises_error():
145+
"""Test LanguageCode initialization with a non-string, non-dict value.
146+
147+
This covers the non-string path in the `_normalize_language_separator`
148+
field validator (line 128) and ensures Pydantic's internal validation
149+
raises the expected error.
150+
"""
151+
with pytest.raises(ValueError):
152+
LanguageCode(language=123)
153+
154+
155+
def test_convert_str_to_dict_accepts_string() -> None:
156+
"""LanguageCode should accept a plain string and populate .language."""
157+
# Use Pydantic v2 entrypoint that accepts raw input so the
158+
# `@model_validator(mode='before')` runs and converts the string.
159+
lc = LanguageCode.model_validate("en-us")
160+
assert isinstance(lc, LanguageCode)
161+
assert lc.language == "en-us"
162+
# ensure computed properties work as expected
163+
assert lc.lang == "en"
164+
assert lc.country == "us"
165+

0 commit comments

Comments
 (0)