Added translation unittests
This commit is contained in:
@@ -5,6 +5,7 @@ from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from unittests_cli.cli_test_suite import CLITestSuite
|
||||
from unittests_query.query_test_suite import QueryTestSuite
|
||||
from unittests_translation.translation_test_suite import TranslationTestSuite
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
@@ -19,3 +20,4 @@ class Application(ApplicationABC):
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(CLITestSuite())
|
||||
runner.run(QueryTestSuite())
|
||||
runner.run(TranslationTestSuite())
|
||||
|
@@ -3,8 +3,8 @@
|
||||
"Name": "unittests",
|
||||
"Version": {
|
||||
"Major": "2022",
|
||||
"Minor": "8",
|
||||
"Micro": "1.dev7"
|
||||
"Minor": "7",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
@@ -22,7 +22,8 @@
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
"Classifiers": [],
|
||||
"DevDependencies": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "unittest",
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
|
||||
PLAYGROUND_PATH = os.path.abspath(os.path.join(os.getcwd(), '../test_cli_playground'))
|
||||
TRANSLATION_PATH = os.path.abspath(os.path.join(os.getcwd(), '../unittests_translation'))
|
||||
CLI_PATH = os.path.abspath(os.path.join(os.getcwd(), '../../src/cpl_cli/main.py'))
|
||||
|
@@ -3,8 +3,8 @@
|
||||
"Name": "unittest_cli",
|
||||
"Version": {
|
||||
"Major": "2022",
|
||||
"Minor": "8",
|
||||
"Micro": "1.dev7"
|
||||
"Minor": "7",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
@@ -17,13 +17,14 @@
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2022.8.1.dev7",
|
||||
"cpl-cli>=2022.8.1.dev7"
|
||||
"cpl-cli>=2022.7.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
"Classifiers": [],
|
||||
"DevDependencies": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "library",
|
||||
|
1
unittests/unittests_translation/__init__.py
Normal file
1
unittests/unittests_translation/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
7
unittests/unittests_translation/translation/de.json
Normal file
7
unittests/unittests_translation/translation/de.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"main": {
|
||||
"text": {
|
||||
"hello_world": "Hallo Welt"
|
||||
}
|
||||
}
|
||||
}
|
7
unittests/unittests_translation/translation/en.json
Normal file
7
unittests/unittests_translation/translation/en.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"main": {
|
||||
"text": {
|
||||
"hello_world": "Hello World"
|
||||
}
|
||||
}
|
||||
}
|
67
unittests/unittests_translation/translation_test_case.py
Normal file
67
unittests/unittests_translation/translation_test_case.py
Normal file
@@ -0,0 +1,67 @@
|
||||
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()
|
||||
settings.from_dict({
|
||||
"Languages": [
|
||||
"de",
|
||||
"en"
|
||||
],
|
||||
"DefaultLanguage": "en"
|
||||
})
|
||||
self._translation.load_by_settings(settings)
|
||||
self._translation.set_default_lang('de')
|
||||
self._translate = TranslatePipe(self._translation)
|
||||
|
||||
def cleanUp(self):
|
||||
pass
|
||||
|
||||
def test_service(self):
|
||||
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'))
|
||||
with self.assertRaises(KeyError) as ctx:
|
||||
self._translation.translate('main.text.hallo_welt')
|
||||
|
||||
self.assertTrue(type(ctx.exception) == KeyError)
|
||||
self.assertIn('Translation main.text.hallo_welt not found', str(ctx.exception))
|
||||
|
||||
with self.assertRaises(FileNotFoundError) as ctx:
|
||||
self._translation.load('DE')
|
||||
|
||||
self.assertTrue(type(ctx.exception) == FileNotFoundError)
|
||||
|
||||
with self.assertRaises(KeyError) as ctx:
|
||||
self._translation.set_lang('DE')
|
||||
|
||||
self.assertTrue(type(ctx.exception) == KeyError)
|
||||
|
||||
with self.assertRaises(KeyError) as ctx:
|
||||
self._translation.set_default_lang('DE')
|
||||
|
||||
self.assertTrue(type(ctx.exception) == KeyError)
|
||||
|
||||
def test_pipe(self):
|
||||
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'))
|
||||
with self.assertRaises(KeyError) as ctx:
|
||||
self._translation.translate('main.text.hallo_welt')
|
||||
|
||||
self.assertTrue(type(ctx.exception) == KeyError)
|
||||
self.assertIn('Translation main.text.hallo_welt not found', str(ctx.exception))
|
51
unittests/unittests_translation/translation_test_suite.py
Normal file
51
unittests/unittests_translation/translation_test_suite.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import shutil
|
||||
import traceback
|
||||
import unittest
|
||||
from typing import Optional
|
||||
from unittest import TestResult
|
||||
|
||||
from unittests_cli.constants import PLAYGROUND_PATH
|
||||
from unittests_translation.translation_test_case import TranslationTestCase
|
||||
|
||||
|
||||
class TranslationTestSuite(unittest.TestSuite):
|
||||
|
||||
def __init__(self):
|
||||
unittest.TestSuite.__init__(self)
|
||||
|
||||
loader = unittest.TestLoader()
|
||||
self._result: Optional[TestResult] = None
|
||||
self._is_online = True
|
||||
|
||||
active_tests = [
|
||||
TranslationTestCase
|
||||
]
|
||||
|
||||
for test in active_tests:
|
||||
self.addTests(loader.loadTestsFromTestCase(test))
|
||||
|
||||
def _setup(self):
|
||||
try:
|
||||
if os.path.exists(PLAYGROUND_PATH):
|
||||
shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH)))
|
||||
|
||||
os.makedirs(PLAYGROUND_PATH)
|
||||
os.chdir(PLAYGROUND_PATH)
|
||||
except Exception as e:
|
||||
print(f'Setup of {__name__} failed: {traceback.format_exc()}')
|
||||
|
||||
def _cleanup(self):
|
||||
try:
|
||||
if self._result is not None and (len(self._result.errors) > 0 or len(self._result.failures) > 0):
|
||||
return
|
||||
|
||||
if os.path.exists(PLAYGROUND_PATH):
|
||||
shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH)))
|
||||
except Exception as e:
|
||||
print(f'Cleanup of {__name__} failed: {traceback.format_exc()}')
|
||||
|
||||
def run(self, *args):
|
||||
self._setup()
|
||||
self._result = super().run(*args)
|
||||
self._cleanup()
|
47
unittests/unittests_translation/unittests_translation.json
Normal file
47
unittests/unittests_translation/unittests_translation.json
Normal file
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"ProjectSettings": {
|
||||
"Name": "unittests_translation",
|
||||
"Version": {
|
||||
"Major": "2022",
|
||||
"Minor": "7",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"URL": "",
|
||||
"CopyrightDate": "",
|
||||
"CopyrightName": "",
|
||||
"LicenseName": "",
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"cpl-core>=2022.7.0",
|
||||
"cpl-translation>=2022.7.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli>=2022.7.0"
|
||||
],
|
||||
"PythonVersion": ">=3.10.4",
|
||||
"PythonPath": {
|
||||
"linux": ""
|
||||
},
|
||||
"Classifiers": []
|
||||
},
|
||||
"BuildSettings": {
|
||||
"ProjectType": "unittest",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "unittests_translation.main",
|
||||
"EntryPoint": "unittests_translation",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user