From e5c0f5c1d1e9205b97f18250e0f57e33e1791c5b Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 6 Aug 2023 12:51:23 +0200 Subject: [PATCH] Added i18n tests --- tests/data/locale/en.json | 11 ++++++++ tests/data/locale/uk.json | 11 ++++++++ tests/test_bot_locale.py | 53 +++++++++++++++++++++++++++++++++++ tests/test_i18n_async.py | 58 +++++++++++++++++++++++++++++++++++++++ tests/test_i18n_sync.py | 53 +++++++++++++++++++++++++++++++++++ 5 files changed, 186 insertions(+) create mode 100644 tests/data/locale/en.json create mode 100644 tests/data/locale/uk.json create mode 100644 tests/test_bot_locale.py create mode 100644 tests/test_i18n_async.py create mode 100644 tests/test_i18n_sync.py diff --git a/tests/data/locale/en.json b/tests/data/locale/en.json new file mode 100644 index 0000000..1b44b7e --- /dev/null +++ b/tests/data/locale/en.json @@ -0,0 +1,11 @@ +{ + "foo": "bar", + "messages": { + "example": "okay" + }, + "callbacks": { + "default": { + "nested": "sure" + } + } +} \ No newline at end of file diff --git a/tests/data/locale/uk.json b/tests/data/locale/uk.json new file mode 100644 index 0000000..34ff1fa --- /dev/null +++ b/tests/data/locale/uk.json @@ -0,0 +1,11 @@ +{ + "foo": "бар", + "messages": { + "example": "окей" + }, + "callbacks": { + "default": { + "nested": "авжеж" + } + } +} \ No newline at end of file diff --git a/tests/test_bot_locale.py b/tests/test_bot_locale.py new file mode 100644 index 0000000..916669d --- /dev/null +++ b/tests/test_bot_locale.py @@ -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 diff --git a/tests/test_i18n_async.py b/tests/test_i18n_async.py new file mode 100644 index 0000000..1c48434 --- /dev/null +++ b/tests/test_i18n_async.py @@ -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 diff --git a/tests/test_i18n_sync.py b/tests/test_i18n_sync.py new file mode 100644 index 0000000..bf634f6 --- /dev/null +++ b/tests/test_i18n_sync.py @@ -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