Added i18n tests

This commit is contained in:
Profitroll 2023-08-06 12:51:23 +02:00
parent 00b1058014
commit e5c0f5c1d1
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
5 changed files with 186 additions and 0 deletions

11
tests/data/locale/en.json Normal file
View File

@ -0,0 +1,11 @@
{
"foo": "bar",
"messages": {
"example": "okay"
},
"callbacks": {
"default": {
"nested": "sure"
}
}
}

11
tests/data/locale/uk.json Normal file
View File

@ -0,0 +1,11 @@
{
"foo": "бар",
"messages": {
"example": "окей"
},
"callbacks": {
"default": {
"nested": "авжеж"
}
}
}

53
tests/test_bot_locale.py Normal file
View File

@ -0,0 +1,53 @@
from pathlib import Path
from typing import Any, List, Union
import pytest
from libbot.i18n import BotLocale
bot_locale = BotLocale(Path("tests/data/locale"))
@pytest.mark.parametrize(
"key, args, locale, expected",
[
("foo", [], None, "bar"),
("foo", [], "uk", "бар"),
("example", ["messages"], None, "okay"),
("example", ["messages"], "uk", "окей"),
("nested", ["callbacks", "default"], None, "sure"),
("nested", ["callbacks", "default"], "uk", "авжеж"),
],
)
def test_bot_locale_get(
key: str, args: List[str], locale: Union[str, None], expected: Any
):
assert (
bot_locale._(key, *args, locale=locale)
if locale is not None
else bot_locale._(key, *args)
) == expected
@pytest.mark.parametrize(
"key, args, expected",
[
("foo", [], ["bar", "бар"]),
("example", ["messages"], ["okay", "окей"]),
("nested", ["callbacks", "default"], ["sure", "авжеж"]),
],
)
def test_i18n_in_all_locales(key: str, args: List[str], expected: Any):
assert (bot_locale.in_all_locales(key, *args)) == expected
@pytest.mark.parametrize(
"key, args, expected",
[
("foo", [], {"en": "bar", "uk": "бар"}),
("example", ["messages"], {"en": "okay", "uk": "окей"}),
("nested", ["callbacks", "default"], {"en": "sure", "uk": "авжеж"}),
],
)
def test_i18n_in_every_locale(key: str, args: List[str], expected: Any):
assert (bot_locale.in_every_locale(key, *args)) == expected

58
tests/test_i18n_async.py Normal file
View File

@ -0,0 +1,58 @@
from pathlib import Path
from typing import Any, List, Union
import pytest
from libbot import i18n
@pytest.mark.asyncio
@pytest.mark.parametrize(
"key, args, locale, expected",
[
("foo", [], None, "bar"),
("foo", [], "uk", "бар"),
("example", ["messages"], None, "okay"),
("example", ["messages"], "uk", "окей"),
("nested", ["callbacks", "default"], None, "sure"),
("nested", ["callbacks", "default"], "uk", "авжеж"),
],
)
async def test_i18n_get(
key: str, args: List[str], locale: Union[str, None], expected: Any
):
assert (
await i18n._(key, *args, locale=locale, locales_root=Path("tests/data/locale"))
if locale is not None
else await i18n._(key, *args, locales_root=Path("tests/data/locale"))
) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
"key, args, expected",
[
("foo", [], ["bar", "бар"]),
("example", ["messages"], ["okay", "окей"]),
("nested", ["callbacks", "default"], ["sure", "авжеж"]),
],
)
async def test_i18n_in_all_locales(key: str, args: List[str], expected: Any):
assert (
await i18n.in_all_locales(key, *args, locales_root=Path("tests/data/locale"))
) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(
"key, args, expected",
[
("foo", [], {"en": "bar", "uk": "бар"}),
("example", ["messages"], {"en": "okay", "uk": "окей"}),
("nested", ["callbacks", "default"], {"en": "sure", "uk": "авжеж"}),
],
)
async def test_i18n_in_every_locale(key: str, args: List[str], expected: Any):
assert (
await i18n.in_every_locale(key, *args, locales_root=Path("tests/data/locale"))
) == expected

53
tests/test_i18n_sync.py Normal file
View File

@ -0,0 +1,53 @@
from pathlib import Path
from typing import Any, List, Union
import pytest
from libbot.i18n import sync
@pytest.mark.parametrize(
"key, args, locale, expected",
[
("foo", [], None, "bar"),
("foo", [], "uk", "бар"),
("example", ["messages"], None, "okay"),
("example", ["messages"], "uk", "окей"),
("nested", ["callbacks", "default"], None, "sure"),
("nested", ["callbacks", "default"], "uk", "авжеж"),
],
)
def test_i18n_get(key: str, args: List[str], locale: Union[str, None], expected: Any):
assert (
sync._(key, *args, locale=locale, locales_root=Path("tests/data/locale"))
if locale is not None
else sync._(key, *args, locales_root=Path("tests/data/locale"))
) == expected
@pytest.mark.parametrize(
"key, args, expected",
[
("foo", [], ["bar", "бар"]),
("example", ["messages"], ["okay", "окей"]),
("nested", ["callbacks", "default"], ["sure", "авжеж"]),
],
)
def test_i18n_in_all_locales(key: str, args: List[str], expected: Any):
assert (
sync.in_all_locales(key, *args, locales_root=Path("tests/data/locale"))
) == expected
@pytest.mark.parametrize(
"key, args, expected",
[
("foo", [], {"en": "bar", "uk": "бар"}),
("example", ["messages"], {"en": "okay", "uk": "окей"}),
("nested", ["callbacks", "default"], {"en": "sure", "uk": "авжеж"}),
],
)
def test_i18n_in_every_locale(key: str, args: List[str], expected: Any):
assert (
sync.in_every_locale(key, *args, locales_root=Path("tests/data/locale"))
) == expected