2022-07-10 17:56:38 +02:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from cpl_translation import TranslationService, TranslatePipe, TranslationSettings
|
|
|
|
from unittests_cli.constants import TRANSLATION_PATH
|
|
|
|
|
|
|
|
|
|
|
|
class TranslationTestCase(unittest.TestCase):
|
|
|
|
def __init__(self, methodName: str):
|
|
|
|
unittest.TestCase.__init__(self, methodName)
|
|
|
|
self._translation: Optional[TranslationService] = None
|
|
|
|
self._translate: Optional[TranslatePipe] = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
os.chdir(os.path.abspath(TRANSLATION_PATH))
|
|
|
|
self._translation = TranslationService()
|
|
|
|
settings = TranslationSettings()
|
2023-02-20 15:55:20 +01:00
|
|
|
settings.from_dict({"Languages": ["de", "en"], "DefaultLanguage": "en"})
|
2022-07-10 17:56:38 +02:00
|
|
|
self._translation.load_by_settings(settings)
|
2023-02-20 15:55:20 +01:00
|
|
|
self._translation.set_default_lang("de")
|
2022-07-10 17:56:38 +02:00
|
|
|
self._translate = TranslatePipe(self._translation)
|
|
|
|
|
|
|
|
def cleanUp(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_service(self):
|
2023-02-20 15:55:20 +01:00
|
|
|
self.assertEqual("Hallo Welt", self._translation.translate("main.text.hello_world"))
|
|
|
|
self._translation.set_lang("en")
|
|
|
|
self.assertEqual("Hello World", self._translation.translate("main.text.hello_world"))
|
2022-07-10 17:56:38 +02:00
|
|
|
with self.assertRaises(KeyError) as ctx:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._translation.translate("main.text.hallo_welt")
|
2022-07-10 17:56:38 +02:00
|
|
|
|
|
|
|
self.assertTrue(type(ctx.exception) == KeyError)
|
2023-02-20 15:55:20 +01:00
|
|
|
self.assertIn("Translation main.text.hallo_welt not found", str(ctx.exception))
|
2022-07-10 17:56:38 +02:00
|
|
|
|
|
|
|
with self.assertRaises(FileNotFoundError) as ctx:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._translation.load("DE")
|
2022-07-10 17:56:38 +02:00
|
|
|
|
|
|
|
self.assertTrue(type(ctx.exception) == FileNotFoundError)
|
|
|
|
|
|
|
|
with self.assertRaises(KeyError) as ctx:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._translation.set_lang("DE")
|
2022-07-10 17:56:38 +02:00
|
|
|
|
|
|
|
self.assertTrue(type(ctx.exception) == KeyError)
|
|
|
|
|
|
|
|
with self.assertRaises(KeyError) as ctx:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._translation.set_default_lang("DE")
|
2022-07-10 17:56:38 +02:00
|
|
|
|
|
|
|
self.assertTrue(type(ctx.exception) == KeyError)
|
|
|
|
|
|
|
|
def test_pipe(self):
|
2023-02-20 15:55:20 +01:00
|
|
|
self.assertEqual("Hallo Welt", self._translate.transform("main.text.hello_world"))
|
|
|
|
self._translation.set_lang("en")
|
|
|
|
self.assertEqual("Hello World", self._translate.transform("main.text.hello_world"))
|
2022-07-10 17:56:38 +02:00
|
|
|
with self.assertRaises(KeyError) as ctx:
|
2023-02-20 15:55:20 +01:00
|
|
|
self._translation.translate("main.text.hallo_welt")
|
2022-07-10 17:56:38 +02:00
|
|
|
|
|
|
|
self.assertTrue(type(ctx.exception) == KeyError)
|
2023-02-20 15:55:20 +01:00
|
|
|
self.assertIn("Translation main.text.hallo_welt not found", str(ctx.exception))
|