2022.7 - cpl-translation #90

Merged
edraft merged 7 commits from 2022.7 into master 2022-07-10 18:10:31 +02:00
14 changed files with 206 additions and 9 deletions
Showing only changes of commit 2772ea8df2 - Show all commits

View File

@ -5,6 +5,7 @@
"cpl-core": "src/cpl_core/cpl-core.json",
"cpl-cli": "src/cpl_cli/cpl-cli.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-pip-urls": "tools/set_pip_urls/set-pip-urls.json",
"unittests": "unittests/unittests/unittests.json",
@ -12,7 +13,7 @@
"unittests_core": "unittests/unittests_core/unittests_core.json",
"unittests_query": "unittests/unittests_query/unittests_query.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": {
"hello-world": "echo 'Hello World'",

View File

@ -68,6 +68,7 @@ class NewService(CommandABC):
Types:
console
library
unittest
""")
@staticmethod
@ -82,6 +83,7 @@ class NewService(CommandABC):
schematics = [
'console (c|C) <name>',
'library (l|L) <name>',
'unittest (ut|UT) <name>',
]
Console.write_line('Available Schematics:')
for name in schematics:

View File

@ -20,6 +20,10 @@ __version__ = '2022.8.1.dev7'
from collections import namedtuple
# imports:
from .translate_pipe import TranslatePipe
from .translation_service import TranslationService
from .translation_service_abc import TranslationServiceABC
from .translation_settings import TranslationSettings
# build-ignore

View File

@ -2,7 +2,6 @@ import json
import os.path
from functools import reduce
from cpl_core.console import Console
from cpl_translation.translation_service_abc import TranslationServiceABC
from cpl_translation.translation_settings import TranslationSettings
@ -18,10 +17,16 @@ class TranslationService(TranslationServiceABC):
TranslationServiceABC.__init__(self)
def set_default_lang(self, lang: str):
if lang not in self._translation:
raise KeyError()
self._default_language = lang
self.set_lang(lang)
def set_lang(self, lang: str):
if lang not in self._translation:
raise KeyError()
self._language = lang
def load(self, lang: str):

View File

@ -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())

View File

@ -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",

View File

@ -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'))

View File

@ -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",

View File

@ -0,0 +1 @@
# imports:

View File

@ -0,0 +1,7 @@
{
"main": {
"text": {
"hello_world": "Hallo Welt"
}
}
}

View File

@ -0,0 +1,7 @@
{
"main": {
"text": {
"hello_world": "Hello World"
}
}
}

View 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))

View 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()

View 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": []
}
}