Coverage for src/tests/test_i18n.py: 100%
53 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-12-06 23:17 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-12-06 23:17 +0000
1import pytest
3from couchers.i18n.i18n import MissingTranslationError, get_raw_translation_string, get_translations
4from tests.test_fixtures import testconfig # noqa
7@pytest.fixture(autouse=True)
8def _(testconfig):
9 pass
12def test_translations_loaded():
13 """Test that translations are loaded from JSON files"""
14 # Should have at least English errors loaded
15 assert "en" in get_translations()
16 assert "errors" in get_translations()["en"]
17 assert len(get_translations()["en"]["errors"]) > 0
20def test_get_string_simple():
21 """Test simple string retrieval without substitutions"""
22 result = get_raw_translation_string("en", "errors", "cant_write_reference_indicated_didnt_meetup")
23 expected = "You can't write a reference for that host request because you indicated that you didn't meet up."
24 assert result == expected
27def test_get_string_with_substitution():
28 """Test string retrieval with variable substitutions"""
29 result = get_raw_translation_string("en", "errors", "chat_initiation_rate_limit", substitutions={"hours": 24})
30 expected = "You have messaged a lot of users in the past 24 hours. To avoid spam, you can't contact any more users for now."
31 assert result == expected
34def test_get_string_fallback_to_english():
35 """Test that non-existent languages fall back to English"""
36 # Request in pt-BR which doesn't exist, should fall back to pt, then to en
37 result = get_raw_translation_string("pt-BR", "errors", "cant_write_reference_indicated_didnt_meetup")
38 expected = "You can't write a reference for that host request because you indicated that you didn't meet up."
39 assert result == expected
42def test_get_string_fallback_chain():
43 """Test language fallback chain works correctly"""
44 # Test pt-PT -> pt-BR -> en fallback chain
45 result = get_raw_translation_string("pt-PT", "errors", "user_not_found")
46 expected = "Couldn't find that user."
47 assert result == expected
50def test_get_string_missing():
51 """Test that missing strings raise MissingTranslationError"""
52 with pytest.raises(MissingTranslationError) as exc_info:
53 get_raw_translation_string("en", "errors", "nonexistent_string_key_12345")
55 assert exc_info.value.lang == "en"
56 assert exc_info.value.component == "errors"
57 assert exc_info.value.string_name == "nonexistent_string_key_12345"
58 assert "en.errors.nonexistent_string_key_12345" in str(exc_info.value)
61def test_get_string_missing_component():
62 """Test that missing components raise MissingTranslationError"""
63 with pytest.raises(MissingTranslationError) as exc_info:
64 get_raw_translation_string("en", "nonexistent_component", "some_string")
66 assert exc_info.value.lang == "en"
67 assert exc_info.value.component == "nonexistent_component"
68 assert exc_info.value.string_name == "some_string"
69 assert "en.nonexistent_component.some_string" in str(exc_info.value)
72def test_get_string_multiple_substitutions():
73 """Test string with multiple variable substitutions"""
74 # Using a string that exists, even though this particular one has only one substitution
75 result = get_raw_translation_string("en", "errors", "chat_initiation_rate_limit", substitutions={"hours": 48})
76 assert "48 hours" in result
77 assert "messaged a lot of users" in result
80def test_fallbacks():
81 """Test that en_CORP uses its custom translation"""
82 result = get_raw_translation_string("en_CORP", "errors", "account_not_found")
83 expected = "The requested account could not be located."
84 assert result == expected
86 # Verify it's different from the English version
87 result_en = get_raw_translation_string("en", "errors", "account_not_found")
88 assert result != result_en
90 # user_not_found is not translated in en_CORP, so it should fall back to en
91 result = get_raw_translation_string("en_CORP", "errors", "user_not_found")
92 expected = "Couldn't find that user."
93 assert result == expected