Added translation unittests
This commit is contained in:
parent
a604f0e821
commit
2772ea8df2
@ -5,6 +5,7 @@
|
|||||||
"cpl-core": "src/cpl_core/cpl-core.json",
|
"cpl-core": "src/cpl_core/cpl-core.json",
|
||||||
"cpl-cli": "src/cpl_cli/cpl-cli.json",
|
"cpl-cli": "src/cpl_cli/cpl-cli.json",
|
||||||
"cpl-query": "src/cpl_query/cpl-query.json",
|
"cpl-query": "src/cpl_query/cpl-query.json",
|
||||||
|
"cpl-translation": "src/cpl_translation/cpl-translation.json",
|
||||||
"set-version": "tools/set_version/set-version.json",
|
"set-version": "tools/set_version/set-version.json",
|
||||||
"set-pip-urls": "tools/set_pip_urls/set-pip-urls.json",
|
"set-pip-urls": "tools/set_pip_urls/set-pip-urls.json",
|
||||||
"unittests": "unittests/unittests/unittests.json",
|
"unittests": "unittests/unittests/unittests.json",
|
||||||
@ -12,7 +13,7 @@
|
|||||||
"unittests_core": "unittests/unittests_core/unittests_core.json",
|
"unittests_core": "unittests/unittests_core/unittests_core.json",
|
||||||
"unittests_query": "unittests/unittests_query/unittests_query.json",
|
"unittests_query": "unittests/unittests_query/unittests_query.json",
|
||||||
"unittests_shared": "unittests/unittests_shared/unittests_shared.json",
|
"unittests_shared": "unittests/unittests_shared/unittests_shared.json",
|
||||||
"cpl-translation": "src/cpl_translation/cpl-translation.json"
|
"unittests_translation": "unittests/unittests_translation/unittests_translation.json"
|
||||||
},
|
},
|
||||||
"Scripts": {
|
"Scripts": {
|
||||||
"hello-world": "echo 'Hello World'",
|
"hello-world": "echo 'Hello World'",
|
||||||
|
@ -68,6 +68,7 @@ class NewService(CommandABC):
|
|||||||
Types:
|
Types:
|
||||||
console
|
console
|
||||||
library
|
library
|
||||||
|
unittest
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -82,6 +83,7 @@ class NewService(CommandABC):
|
|||||||
schematics = [
|
schematics = [
|
||||||
'console (c|C) <name>',
|
'console (c|C) <name>',
|
||||||
'library (l|L) <name>',
|
'library (l|L) <name>',
|
||||||
|
'unittest (ut|UT) <name>',
|
||||||
]
|
]
|
||||||
Console.write_line('Available Schematics:')
|
Console.write_line('Available Schematics:')
|
||||||
for name in schematics:
|
for name in schematics:
|
||||||
|
@ -20,6 +20,10 @@ __version__ = '2022.8.1.dev7'
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
# imports:
|
# imports:
|
||||||
|
from .translate_pipe import TranslatePipe
|
||||||
|
from .translation_service import TranslationService
|
||||||
|
from .translation_service_abc import TranslationServiceABC
|
||||||
|
from .translation_settings import TranslationSettings
|
||||||
# build-ignore
|
# build-ignore
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ import json
|
|||||||
import os.path
|
import os.path
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
|
||||||
from cpl_core.console import Console
|
|
||||||
from cpl_translation.translation_service_abc import TranslationServiceABC
|
from cpl_translation.translation_service_abc import TranslationServiceABC
|
||||||
from cpl_translation.translation_settings import TranslationSettings
|
from cpl_translation.translation_settings import TranslationSettings
|
||||||
|
|
||||||
@ -18,10 +17,16 @@ class TranslationService(TranslationServiceABC):
|
|||||||
TranslationServiceABC.__init__(self)
|
TranslationServiceABC.__init__(self)
|
||||||
|
|
||||||
def set_default_lang(self, lang: str):
|
def set_default_lang(self, lang: str):
|
||||||
|
if lang not in self._translation:
|
||||||
|
raise KeyError()
|
||||||
|
|
||||||
self._default_language = lang
|
self._default_language = lang
|
||||||
self.set_lang(lang)
|
self.set_lang(lang)
|
||||||
|
|
||||||
def set_lang(self, lang: str):
|
def set_lang(self, lang: str):
|
||||||
|
if lang not in self._translation:
|
||||||
|
raise KeyError()
|
||||||
|
|
||||||
self._language = lang
|
self._language = lang
|
||||||
|
|
||||||
def load(self, lang: str):
|
def load(self, lang: str):
|
||||||
|
@ -5,6 +5,7 @@ from cpl_core.configuration import ConfigurationABC
|
|||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from unittests_cli.cli_test_suite import CLITestSuite
|
from unittests_cli.cli_test_suite import CLITestSuite
|
||||||
from unittests_query.query_test_suite import QueryTestSuite
|
from unittests_query.query_test_suite import QueryTestSuite
|
||||||
|
from unittests_translation.translation_test_suite import TranslationTestSuite
|
||||||
|
|
||||||
|
|
||||||
class Application(ApplicationABC):
|
class Application(ApplicationABC):
|
||||||
@ -19,3 +20,4 @@ class Application(ApplicationABC):
|
|||||||
runner = unittest.TextTestRunner()
|
runner = unittest.TextTestRunner()
|
||||||
runner.run(CLITestSuite())
|
runner.run(CLITestSuite())
|
||||||
runner.run(QueryTestSuite())
|
runner.run(QueryTestSuite())
|
||||||
|
runner.run(TranslationTestSuite())
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
"Name": "unittests",
|
"Name": "unittests",
|
||||||
"Version": {
|
"Version": {
|
||||||
"Major": "2022",
|
"Major": "2022",
|
||||||
"Minor": "8",
|
"Minor": "7",
|
||||||
"Micro": "1.dev7"
|
"Micro": "0"
|
||||||
},
|
},
|
||||||
"Author": "",
|
"Author": "",
|
||||||
"AuthorEmail": "",
|
"AuthorEmail": "",
|
||||||
@ -22,7 +22,8 @@
|
|||||||
"PythonPath": {
|
"PythonPath": {
|
||||||
"linux": ""
|
"linux": ""
|
||||||
},
|
},
|
||||||
"Classifiers": []
|
"Classifiers": [],
|
||||||
|
"DevDependencies": []
|
||||||
},
|
},
|
||||||
"BuildSettings": {
|
"BuildSettings": {
|
||||||
"ProjectType": "unittest",
|
"ProjectType": "unittest",
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
PLAYGROUND_PATH = os.path.abspath(os.path.join(os.getcwd(), '../test_cli_playground'))
|
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'))
|
CLI_PATH = os.path.abspath(os.path.join(os.getcwd(), '../../src/cpl_cli/main.py'))
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
"Name": "unittest_cli",
|
"Name": "unittest_cli",
|
||||||
"Version": {
|
"Version": {
|
||||||
"Major": "2022",
|
"Major": "2022",
|
||||||
"Minor": "8",
|
"Minor": "7",
|
||||||
"Micro": "1.dev7"
|
"Micro": "0"
|
||||||
},
|
},
|
||||||
"Author": "",
|
"Author": "",
|
||||||
"AuthorEmail": "",
|
"AuthorEmail": "",
|
||||||
@ -17,13 +17,14 @@
|
|||||||
"LicenseDescription": "",
|
"LicenseDescription": "",
|
||||||
"Dependencies": [
|
"Dependencies": [
|
||||||
"cpl-core>=2022.8.1.dev7",
|
"cpl-core>=2022.8.1.dev7",
|
||||||
"cpl-cli>=2022.8.1.dev7"
|
"cpl-cli>=2022.7.0"
|
||||||
],
|
],
|
||||||
"PythonVersion": ">=3.10.4",
|
"PythonVersion": ">=3.10.4",
|
||||||
"PythonPath": {
|
"PythonPath": {
|
||||||
"linux": ""
|
"linux": ""
|
||||||
},
|
},
|
||||||
"Classifiers": []
|
"Classifiers": [],
|
||||||
|
"DevDependencies": []
|
||||||
},
|
},
|
||||||
"BuildSettings": {
|
"BuildSettings": {
|
||||||
"ProjectType": "library",
|
"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": []
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user