From 2772ea8df2a4337f8ab43a6fda6add4405ce9641 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 10 Jul 2022 17:56:38 +0200 Subject: [PATCH] Added translation unittests --- cpl-workspace.json | 3 +- src/cpl_cli/command/new_service.py | 2 + src/cpl_translation/__init__.py | 4 ++ src/cpl_translation/translation_service.py | 7 +- unittests/unittests/application.py | 2 + unittests/unittests/unittests.json | 7 +- unittests/unittests_cli/constants.py | 1 + unittests/unittests_cli/unittests_cli.json | 9 +-- unittests/unittests_translation/__init__.py | 1 + .../unittests_translation/translation/de.json | 7 ++ .../unittests_translation/translation/en.json | 7 ++ .../translation_test_case.py | 67 +++++++++++++++++++ .../translation_test_suite.py | 51 ++++++++++++++ .../unittests_translation.json | 47 +++++++++++++ 14 files changed, 206 insertions(+), 9 deletions(-) create mode 100644 unittests/unittests_translation/__init__.py create mode 100644 unittests/unittests_translation/translation/de.json create mode 100644 unittests/unittests_translation/translation/en.json create mode 100644 unittests/unittests_translation/translation_test_case.py create mode 100644 unittests/unittests_translation/translation_test_suite.py create mode 100644 unittests/unittests_translation/unittests_translation.json diff --git a/cpl-workspace.json b/cpl-workspace.json index 55f564a3..e0435866 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -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'", diff --git a/src/cpl_cli/command/new_service.py b/src/cpl_cli/command/new_service.py index 387cf8ce..f7f843dd 100644 --- a/src/cpl_cli/command/new_service.py +++ b/src/cpl_cli/command/new_service.py @@ -68,6 +68,7 @@ class NewService(CommandABC): Types: console library + unittest """) @staticmethod @@ -82,6 +83,7 @@ class NewService(CommandABC): schematics = [ 'console (c|C) ', 'library (l|L) ', + 'unittest (ut|UT) ', ] Console.write_line('Available Schematics:') for name in schematics: diff --git a/src/cpl_translation/__init__.py b/src/cpl_translation/__init__.py index bcc2c35b..98c00d66 100644 --- a/src/cpl_translation/__init__.py +++ b/src/cpl_translation/__init__.py @@ -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 diff --git a/src/cpl_translation/translation_service.py b/src/cpl_translation/translation_service.py index 3fd83b7c..4c75eaf4 100644 --- a/src/cpl_translation/translation_service.py +++ b/src/cpl_translation/translation_service.py @@ -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): diff --git a/unittests/unittests/application.py b/unittests/unittests/application.py index 9ec49cc6..0ba34799 100644 --- a/unittests/unittests/application.py +++ b/unittests/unittests/application.py @@ -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()) diff --git a/unittests/unittests/unittests.json b/unittests/unittests/unittests.json index 3504c2a8..ff83d9a0 100644 --- a/unittests/unittests/unittests.json +++ b/unittests/unittests/unittests.json @@ -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", diff --git a/unittests/unittests_cli/constants.py b/unittests/unittests_cli/constants.py index 766d72c0..f585a27f 100644 --- a/unittests/unittests_cli/constants.py +++ b/unittests/unittests_cli/constants.py @@ -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')) diff --git a/unittests/unittests_cli/unittests_cli.json b/unittests/unittests_cli/unittests_cli.json index cd728928..b7120f2e 100644 --- a/unittests/unittests_cli/unittests_cli.json +++ b/unittests/unittests_cli/unittests_cli.json @@ -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", diff --git a/unittests/unittests_translation/__init__.py b/unittests/unittests_translation/__init__.py new file mode 100644 index 00000000..ad5eca30 --- /dev/null +++ b/unittests/unittests_translation/__init__.py @@ -0,0 +1 @@ +# imports: diff --git a/unittests/unittests_translation/translation/de.json b/unittests/unittests_translation/translation/de.json new file mode 100644 index 00000000..afd34db5 --- /dev/null +++ b/unittests/unittests_translation/translation/de.json @@ -0,0 +1,7 @@ +{ + "main": { + "text": { + "hello_world": "Hallo Welt" + } + } +} \ No newline at end of file diff --git a/unittests/unittests_translation/translation/en.json b/unittests/unittests_translation/translation/en.json new file mode 100644 index 00000000..77c63c59 --- /dev/null +++ b/unittests/unittests_translation/translation/en.json @@ -0,0 +1,7 @@ +{ + "main": { + "text": { + "hello_world": "Hello World" + } + } +} \ No newline at end of file diff --git a/unittests/unittests_translation/translation_test_case.py b/unittests/unittests_translation/translation_test_case.py new file mode 100644 index 00000000..f7546ea6 --- /dev/null +++ b/unittests/unittests_translation/translation_test_case.py @@ -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)) diff --git a/unittests/unittests_translation/translation_test_suite.py b/unittests/unittests_translation/translation_test_suite.py new file mode 100644 index 00000000..f1e15132 --- /dev/null +++ b/unittests/unittests_translation/translation_test_suite.py @@ -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() diff --git a/unittests/unittests_translation/unittests_translation.json b/unittests/unittests_translation/unittests_translation.json new file mode 100644 index 00000000..05cb6ca4 --- /dev/null +++ b/unittests/unittests_translation/unittests_translation.json @@ -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": [] + } +} \ No newline at end of file