From 106975015ea5dc139b9b4b075cb2a062470fabd8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 9 Apr 2023 12:51:57 +0200 Subject: [PATCH] Improved config tests --- src/cpl_core/configuration/configuration.py | 2 +- .../configuration/configuration_abc.py | 2 +- .../configuration/configuration_test_case.py | 33 ++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/cpl_core/configuration/configuration.py b/src/cpl_core/configuration/configuration.py index 7e179274..a8372cac 100644 --- a/src/cpl_core/configuration/configuration.py +++ b/src/cpl_core/configuration/configuration.py @@ -314,7 +314,7 @@ class Configuration(ConfigurationABC): for arg in self._argument_types: call(arg) - def get_configuration(self, search_type: Type[T]) -> Optional[T]: + def get_configuration(self, search_type: T) -> Optional[T]: if type(search_type) is str: if search_type == ConfigurationVariableNameEnum.environment.value: return self._application_environment.environment_name diff --git a/src/cpl_core/configuration/configuration_abc.py b/src/cpl_core/configuration/configuration_abc.py index 2f8d7a4c..815fa361 100644 --- a/src/cpl_core/configuration/configuration_abc.py +++ b/src/cpl_core/configuration/configuration_abc.py @@ -124,7 +124,7 @@ class ConfigurationABC(ABC): pass @abstractmethod - def get_configuration(self, search_type: Type[T]) -> Optional[T]: + def get_configuration(self, search_type: T) -> Optional[T]: r"""Returns value from configuration by given type Parameter: diff --git a/unittests/unittests_core/configuration/configuration_test_case.py b/unittests/unittests_core/configuration/configuration_test_case.py index 5e4f113e..1d59d202 100644 --- a/unittests/unittests_core/configuration/configuration_test_case.py +++ b/unittests/unittests_core/configuration/configuration_test_case.py @@ -1,8 +1,11 @@ import os +import sys import unittest +from unittest.mock import Mock, MagicMock -from cpl_core.configuration import Configuration +from cpl_core.configuration import Configuration, ArgumentTypeEnum from cpl_core.database import DatabaseSettings +from cpl_core.dependency_injection import ServiceProvider, ServiceCollection from cpl_core.mailing import EMailClientSettings @@ -35,3 +38,31 @@ class ConfigurationTestCase(unittest.TestCase): self.assertTrue(db.buffered) self.assertEqual("mysql_native_password", db.auth_plugin) self.assertIsNone(self._config.get_configuration(EMailClientSettings)) + + def test_add_config(self): + self.assertIsNone(self._config.get_configuration("Test")) + self._config.add_configuration("Test", "Hello World") + self.assertIsNotNone(self._config.get_configuration("Test")) + self.assertEqual("Hello World", self._config.get_configuration("Test")) + + def test_console_argument(self): + sc = ServiceCollection(self._config) + self.assertEqual([], sys.argv[1:]) + sys.argv.append("flag") + sys.argv.append("exec") + sys.argv.append("var=test") + self.assertNotEqual([], sys.argv[1:]) + + self._config.create_console_argument(ArgumentTypeEnum.Flag, "", "flag", []) + mocked_exec = Mock() + mocked_exec.run = MagicMock() + sc.add_transient(mocked_exec) + self._config.create_console_argument(ArgumentTypeEnum.Executable, "", "exec", [], Mock) + self._config.create_console_argument(ArgumentTypeEnum.Variable, "", "var", [], "=") + + self.assertIsNone(self._config.get_configuration("var")) + self._config.parse_console_arguments(sc.build_service_provider()) + mocked_exec.run.assert_called() + + self.assertEqual("test", self._config.get_configuration("var")) + self.assertIn("flag", self._config.additional_arguments)