76 lines
2.9 KiB
Python
76 lines
2.9 KiB
Python
# #region Test.DictionaryValidation [C:2] [TYPE Module] [SEMANTICS test, translate, validation, bcp47]
|
|
# @BRIEF Tests for dictionary_validation.py — _validate_bcp47.
|
|
# @RELATION BINDS_TO -> [_validate_bcp47]
|
|
# @TEST_CONTRACT: _validate_bcp47 -> None | raises ValueError on invalid tag
|
|
# @TEST_EDGE: empty_tag -> ValueError
|
|
# @TEST_EDGE: invalid_format -> ValueError
|
|
# @TEST_EDGE: valid_format -> None
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src"))
|
|
|
|
import os
|
|
os.environ.setdefault("SECRET_KEY", "test-secret-key-for-testing")
|
|
os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:")
|
|
os.environ.setdefault("AUTH_DATABASE_URL", "sqlite:///:memory:")
|
|
|
|
import pytest
|
|
|
|
from src.plugins.translate.dictionary_validation import _validate_bcp47
|
|
|
|
|
|
class TestValidateBcp47:
|
|
"""_validate_bcp47 — BCP-47 language tag validation."""
|
|
|
|
def test_valid_basic_tag(self):
|
|
"""Simple language code like 'en' passes."""
|
|
_validate_bcp47("en", "language") # no error
|
|
|
|
def test_valid_region_tag(self):
|
|
"""Tag with region like 'zh-CN' passes."""
|
|
_validate_bcp47("zh-CN", "language")
|
|
|
|
def test_valid_script_tag(self):
|
|
"""Tag with script like 'zh-Hans' passes."""
|
|
_validate_bcp47("zh-Hans", "language")
|
|
|
|
def test_valid_multi_subtag(self):
|
|
"""Complex tag like 'sr-Latn-RS' passes."""
|
|
_validate_bcp47("sr-Latn-RS", "language")
|
|
|
|
def test_empty_string_raises(self):
|
|
"""Empty tag raises ValueError."""
|
|
with pytest.raises(ValueError, match="must be a non-empty"):
|
|
_validate_bcp47("", "language")
|
|
|
|
def test_whitespace_only_raises(self):
|
|
"""Whitespace-only tag raises ValueError."""
|
|
with pytest.raises(ValueError, match="must be a non-empty"):
|
|
_validate_bcp47(" ", "language")
|
|
|
|
def test_none_raises(self):
|
|
"""None tag raises ValueError."""
|
|
with pytest.raises(ValueError, match="must be a non-empty"):
|
|
_validate_bcp47(None, "language") # type: ignore[arg-type]
|
|
|
|
def test_invalid_format_raises(self):
|
|
"""Tag with invalid characters raises ValueError."""
|
|
with pytest.raises(ValueError, match="not a valid BCP-47"):
|
|
_validate_bcp47("en@utf8", "language")
|
|
|
|
def test_invalid_numeric_start_raises(self):
|
|
"""Tag starting with a number raises ValueError."""
|
|
with pytest.raises(ValueError, match="not a valid BCP-47"):
|
|
_validate_bcp47("123", "language")
|
|
|
|
def test_error_message_includes_field_name(self):
|
|
"""Error message includes the field_name parameter."""
|
|
with pytest.raises(ValueError, match="source_language"):
|
|
_validate_bcp47("", "source_language")
|
|
|
|
def test_strips_whitespace(self):
|
|
"""Whitespace around tag is stripped."""
|
|
_validate_bcp47(" en ", "language") # no error after strip
|
|
# #endregion Test.DictionaryValidation
|