commit
c94700495b
@ -1,6 +1,7 @@
|
||||
import textwrap
|
||||
|
||||
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
|
||||
from cpl_core.utils import String
|
||||
|
||||
|
||||
class ApplicationExtension(GenerateSchematicABC):
|
||||
@ -22,7 +23,7 @@ class ApplicationExtension(GenerateSchematicABC):
|
||||
def run(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
pass
|
||||
"""
|
||||
x = self.build_code_str(code, Name=self._class_name)
|
||||
x = self.build_code_str(code, Name=String.convert_to_camel_case(self._class_name))
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
|
@ -1,6 +1,7 @@
|
||||
import textwrap
|
||||
|
||||
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
|
||||
from cpl_core.utils import String
|
||||
|
||||
|
||||
class StartupExtension(GenerateSchematicABC):
|
||||
@ -26,7 +27,7 @@ class StartupExtension(GenerateSchematicABC):
|
||||
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
|
||||
pass
|
||||
"""
|
||||
x = self.build_code_str(code, Name=self._class_name)
|
||||
x = self.build_code_str(code, Name=String.convert_to_camel_case(self._class_name))
|
||||
return x
|
||||
|
||||
@classmethod
|
||||
|
@ -1,6 +1,7 @@
|
||||
import textwrap
|
||||
|
||||
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
|
||||
from cpl_core.utils import String
|
||||
|
||||
|
||||
class TestCase(GenerateSchematicABC):
|
||||
@ -20,7 +21,7 @@ class TestCase(GenerateSchematicABC):
|
||||
def test_equal(self):
|
||||
pass
|
||||
"""
|
||||
return self.build_code_str(code, Name=self._class_name)
|
||||
return self.build_code_str(code, Name=String.convert_to_camel_case(self._class_name))
|
||||
|
||||
@classmethod
|
||||
def register(cls):
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -12,11 +12,11 @@ class DatabaseSettings(ConfigurationModelABC):
|
||||
port: int = None,
|
||||
user: str = None,
|
||||
password: str = None,
|
||||
databse: str = None,
|
||||
database: str = None,
|
||||
charset: str = None,
|
||||
use_unicode: bool = None,
|
||||
buffered: bool = None,
|
||||
auth_plugin: bool = None,
|
||||
auth_plugin: str = None,
|
||||
):
|
||||
ConfigurationModelABC.__init__(self)
|
||||
|
||||
@ -24,7 +24,7 @@ class DatabaseSettings(ConfigurationModelABC):
|
||||
self._port: Optional[int] = port
|
||||
self._user: Optional[str] = user
|
||||
self._password: Optional[str] = password
|
||||
self._databse: Optional[str] = databse
|
||||
self._databse: Optional[str] = database
|
||||
self._charset: Optional[str] = charset
|
||||
self._use_unicode: Optional[bool] = use_unicode
|
||||
self._buffered: Optional[bool] = buffered
|
||||
|
@ -19,15 +19,10 @@ __version__ = "2023.4.0"
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports:
|
||||
from .bool_pipe import BoolPipe
|
||||
from .first_char_to_lower_pipe import FirstCharToLowerPipe
|
||||
from .first_to_upper_pipe import FirstToUpperPipe
|
||||
from .ip_address_pipe import IPAddressPipe
|
||||
from .pipe_abc import PipeABC
|
||||
from .to_camel_case_pipe import ToCamelCasePipe
|
||||
from .to_snake_case_pipe import ToSnakeCasePipe
|
||||
|
||||
VersionInfo = namedtuple("VersionInfo", "major minor micro")
|
||||
version_info = VersionInfo(major="2023", minor="4", micro="0")
|
||||
|
@ -1,18 +0,0 @@
|
||||
from cpl_core.pipes.pipe_abc import PipeABC
|
||||
|
||||
|
||||
class FirstCharToLowerPipe(PipeABC):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def transform(self, value: any, *args):
|
||||
r"""Converts first char to lower
|
||||
|
||||
Parameter:
|
||||
value: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns:
|
||||
String with first char as lower
|
||||
"""
|
||||
return f"{value[0].lower()}{value[1:]}"
|
@ -1,18 +0,0 @@
|
||||
from cpl_core.pipes.pipe_abc import PipeABC
|
||||
|
||||
|
||||
class FirstToUpperPipe(PipeABC):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def transform(self, value: str, *args):
|
||||
r"""Converts first char to upper
|
||||
|
||||
Parameter:
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns:
|
||||
String with first char as upper
|
||||
"""
|
||||
return f"{value[0].upper()}{value[1:]}"
|
@ -13,7 +13,7 @@ class IPAddressPipe(PipeABC):
|
||||
|
||||
for i in range(0, len(value)):
|
||||
byte = value[i]
|
||||
if byte > 255:
|
||||
if byte > 255 or byte < 0:
|
||||
raise Exception("Invalid IP")
|
||||
|
||||
if i == len(value) - 1:
|
||||
|
@ -1,26 +0,0 @@
|
||||
import string
|
||||
|
||||
from cpl_core.pipes import PipeABC
|
||||
|
||||
|
||||
class ToCamelCasePipe(PipeABC):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def transform(self, value: str, *args) -> str:
|
||||
r"""Converts string to camel case
|
||||
|
||||
Parameter:
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns:
|
||||
String converted to CamelCase
|
||||
"""
|
||||
converted_name = value
|
||||
char_set = string.punctuation + " "
|
||||
for char in char_set:
|
||||
if char in converted_name:
|
||||
converted_name = "".join(word.title() for word in converted_name.split(char))
|
||||
|
||||
return converted_name
|
@ -1,27 +0,0 @@
|
||||
import re
|
||||
|
||||
from cpl_core.pipes import PipeABC
|
||||
|
||||
|
||||
class ToSnakeCasePipe(PipeABC):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def transform(self, value: str, *args) -> str:
|
||||
r"""Converts string to snake case
|
||||
|
||||
Parameter:
|
||||
chars: :class:`str`
|
||||
String to convert
|
||||
|
||||
Returns:
|
||||
String converted to snake_case
|
||||
"""
|
||||
# convert to train-case to CamelCase
|
||||
if "-" in value:
|
||||
value = "".join(word.title() for word in value.split("-"))
|
||||
|
||||
pattern1 = re.compile(r"(.)([A-Z][a-z]+)")
|
||||
pattern2 = re.compile(r"([a-z0-9])([A-Z])")
|
||||
file_name = re.sub(pattern1, r"\1_\2", value)
|
||||
return re.sub(pattern2, r"\1_\2", file_name).lower()
|
@ -30,6 +30,9 @@ class JSONProcessor:
|
||||
if issubclass(parameter.annotation, enum.Enum):
|
||||
value = parameter.annotation[value]
|
||||
|
||||
if type(value) != parameter.annotation:
|
||||
value = parameter.annotation(value)
|
||||
|
||||
args.append(value)
|
||||
|
||||
elif parameter.default != Parameter.empty:
|
||||
|
@ -37,9 +37,19 @@ class String:
|
||||
String converted to snake_case
|
||||
"""
|
||||
# convert to train-case to CamelCase
|
||||
if "_" in chars:
|
||||
chars = chars.replace("_", "-")
|
||||
|
||||
if "-" in chars:
|
||||
chars = "".join(word.title() for word in chars.split("-"))
|
||||
|
||||
if " " in chars:
|
||||
new_chars = ""
|
||||
for word in chars.split(" "):
|
||||
new_chars += String.first_to_upper(word)
|
||||
|
||||
chars = new_chars
|
||||
|
||||
pattern1 = re.compile(r"(.)([A-Z][a-z]+)")
|
||||
pattern2 = re.compile(r"([a-z0-9])([A-Z])")
|
||||
file_name = re.sub(pattern1, r"\1_\2", chars)
|
||||
|
@ -4,6 +4,7 @@ from cpl_core.application import ApplicationABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from unittests_cli.cli_test_suite import CLITestSuite
|
||||
from unittests_core.core_test_suite import CoreTestSuite
|
||||
from unittests_query.query_test_suite import QueryTestSuite
|
||||
from unittests_translation.translation_test_suite import TranslationTestSuite
|
||||
|
||||
@ -17,6 +18,7 @@ class Application(ApplicationABC):
|
||||
|
||||
def main(self):
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(CoreTestSuite())
|
||||
runner.run(CLITestSuite())
|
||||
runner.run(QueryTestSuite())
|
||||
runner.run(TranslationTestSuite())
|
||||
|
@ -8,6 +8,7 @@ from unittests_cli.constants import PLAYGROUND_PATH
|
||||
|
||||
class CommandTestCase(unittest.TestCase):
|
||||
_skip_tear_down = False
|
||||
_cwd = os.getcwd()
|
||||
|
||||
def __init__(self, method_name: str):
|
||||
unittest.TestCase.__init__(self, method_name)
|
||||
@ -32,6 +33,7 @@ class CommandTestCase(unittest.TestCase):
|
||||
if cls._skip_tear_down:
|
||||
return
|
||||
try:
|
||||
os.chdir(cls._cwd)
|
||||
if os.path.exists(PLAYGROUND_PATH):
|
||||
shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH)))
|
||||
except Exception as e:
|
||||
|
0
unittests/unittests_core/configuration/__init__.py
Normal file
0
unittests/unittests_core/configuration/__init__.py
Normal file
@ -0,0 +1,68 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import Mock, MagicMock
|
||||
|
||||
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
|
||||
|
||||
|
||||
class ConfigurationTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._config = Configuration()
|
||||
|
||||
def test_env_vars(self):
|
||||
os.environ["CPLT_TESTVAR"] = "Hello World"
|
||||
os.environ["CPL_NOT_EXISTING"] = "Hello World"
|
||||
|
||||
self._config.add_environment_variables("CPLT_")
|
||||
|
||||
self.assertEqual(self._config.get_configuration("TESTVAR"), "Hello World")
|
||||
self.assertEqual(self._config.get_configuration("TESTVAR"), "Hello World")
|
||||
self.assertEqual(self._config.get_configuration("NOT_EXISTING"), None)
|
||||
|
||||
def test_add_json_file(self):
|
||||
self._config.add_json_file("unittests_core/configuration/test-settings.json")
|
||||
db = self._config.get_configuration(DatabaseSettings)
|
||||
self.assertIsNotNone(db)
|
||||
self.assertEqual("localhost", db.host)
|
||||
self.assertEqual("local", db.user)
|
||||
self.assertEqual("bG9jYWw=", db.password)
|
||||
self.assertEqual("local", db.database)
|
||||
self.assertEqual(int, type(db.port))
|
||||
self.assertEqual(3306, db.port)
|
||||
self.assertEqual("utf8mb4", db.charset)
|
||||
self.assertTrue(db.use_unicode)
|
||||
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)
|
@ -0,0 +1,75 @@
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import Mock, MagicMock
|
||||
|
||||
from cpl_core.configuration import Configuration, ArgumentTypeEnum
|
||||
from cpl_core.dependency_injection import ServiceCollection
|
||||
|
||||
|
||||
class ConsoleArgumentsTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._config = Configuration()
|
||||
|
||||
self._config.create_console_argument(ArgumentTypeEnum.Flag, "", "flag", [])
|
||||
self._config.create_console_argument(ArgumentTypeEnum.Variable, "", "var", [], "=")
|
||||
|
||||
self._config.create_console_argument(ArgumentTypeEnum.Executable, "", "exec", [], Mock).add_console_argument(
|
||||
ArgumentTypeEnum.Flag, "--", "dev", ["d", "D"]
|
||||
).add_console_argument(ArgumentTypeEnum.Flag, "--", "virtual", ["v", "V"]).add_console_argument(
|
||||
ArgumentTypeEnum.Variable, "", "var1", [], "="
|
||||
)
|
||||
|
||||
self._config.for_each_argument(
|
||||
lambda a: a.add_console_argument(ArgumentTypeEnum.Flag, "--", "help", ["h", "H"])
|
||||
)
|
||||
|
||||
self._sc = ServiceCollection(self._config)
|
||||
self._mocked_exec = Mock()
|
||||
self._mocked_exec.run = MagicMock()
|
||||
self._sc.add_transient(self._mocked_exec)
|
||||
|
||||
def test_flag(self):
|
||||
sys.argv.append("flag")
|
||||
|
||||
self._config.parse_console_arguments(self._sc.build_service_provider())
|
||||
self.assertIn("flag", self._config.additional_arguments)
|
||||
|
||||
def test_var(self):
|
||||
sys.argv.append("var=1")
|
||||
sys.argv.append("var2=1")
|
||||
|
||||
self._config.parse_console_arguments(self._sc.build_service_provider())
|
||||
self.assertEqual("1", self._config.get_configuration("var"))
|
||||
self.assertIsNone(self._config.get_configuration("var1"))
|
||||
|
||||
def test_exec(self):
|
||||
sys.argv.append("exec")
|
||||
|
||||
self._config.parse_console_arguments(self._sc.build_service_provider())
|
||||
self._mocked_exec.run.assert_called()
|
||||
|
||||
def test_exec_with_one_flag(self):
|
||||
sys.argv.append("exec")
|
||||
sys.argv.append("--dev")
|
||||
|
||||
self._config.parse_console_arguments(self._sc.build_service_provider())
|
||||
self._mocked_exec.run.assert_called()
|
||||
self.assertIn("dev", self._config.additional_arguments)
|
||||
|
||||
def test_exec_with_one_flag_alias(self):
|
||||
sys.argv.append("exec")
|
||||
sys.argv.append("--d")
|
||||
|
||||
self._config.parse_console_arguments(self._sc.build_service_provider())
|
||||
self._mocked_exec.run.assert_called()
|
||||
self.assertIn("dev", self._config.additional_arguments)
|
||||
|
||||
def test_exec_with_two_flags(self):
|
||||
sys.argv.append("exec")
|
||||
sys.argv.append("--dev")
|
||||
sys.argv.append("--virtual")
|
||||
|
||||
self._config.parse_console_arguments(self._sc.build_service_provider())
|
||||
self._mocked_exec.run.assert_called()
|
||||
self.assertIn("dev", self._config.additional_arguments)
|
||||
self.assertIn("virtual", self._config.additional_arguments)
|
@ -0,0 +1,52 @@
|
||||
import os
|
||||
import unittest
|
||||
from _socket import gethostname
|
||||
|
||||
from cpl_core.configuration import Configuration
|
||||
from cpl_core.environment import ApplicationEnvironment, ApplicationEnvironmentABC
|
||||
from cpl_core.environment import application_environment
|
||||
|
||||
|
||||
class EnvironmentTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._config = Configuration()
|
||||
self._env = self._config.environment
|
||||
|
||||
def test_app_env_created(self):
|
||||
self.assertTrue(isinstance(self._env, ApplicationEnvironment))
|
||||
self.assertTrue(issubclass(type(self._env), ApplicationEnvironmentABC))
|
||||
|
||||
def test_app_env_values_correct_when_default(self):
|
||||
self.assertEqual(self._env.environment_name, "production")
|
||||
self.assertEqual(self._env.application_name, "")
|
||||
self.assertEqual(self._env.customer, "")
|
||||
self.assertEqual(self._env.host_name, gethostname())
|
||||
self.assertEqual(self._env.working_directory, os.getcwd())
|
||||
self.assertEqual(
|
||||
self._env.runtime_directory,
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(application_environment.__file__))),
|
||||
)
|
||||
|
||||
def test_app_env_values_correct_when_read_from_env(self):
|
||||
os.environ["CPLT_ENVIRONMENT"] = "development"
|
||||
os.environ["CPLT_NAME"] = "Core Tests"
|
||||
os.environ["CPLT_CUSTOMER"] = "sh-edraft.de"
|
||||
|
||||
self._config.add_environment_variables("CPLT_")
|
||||
|
||||
self.assertEqual(self._env.environment_name, "development")
|
||||
self.assertEqual(self._env.application_name, "Core Tests")
|
||||
self.assertEqual(self._env.customer, "sh-edraft.de")
|
||||
self.assertEqual(self._env.host_name, gethostname())
|
||||
self.assertEqual(self._env.working_directory, os.getcwd())
|
||||
self.assertEqual(
|
||||
self._env.runtime_directory,
|
||||
os.path.dirname(os.path.dirname(os.path.abspath(application_environment.__file__))),
|
||||
)
|
||||
|
||||
def test_app_env_set_dirs(self):
|
||||
new_cwd = os.path.join(os.getcwd(), "../")
|
||||
self._env.set_working_directory(new_cwd)
|
||||
self.assertEqual(self._env.working_directory, new_cwd)
|
||||
self._env.set_runtime_directory(new_cwd)
|
||||
self.assertEqual(self._env.runtime_directory, new_cwd)
|
25
unittests/unittests_core/configuration/test-settings.json
Normal file
25
unittests/unittests_core/configuration/test-settings.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"TimeFormatSettings": {
|
||||
"DateFormat": "%Y-%m-%d",
|
||||
"TimeFormat": "%H:%M:%S",
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/$date_now/",
|
||||
"Filename": "bot.log",
|
||||
"ConsoleLogLevel": "TRACE",
|
||||
"FileLogLevel": "TRACE"
|
||||
},
|
||||
"DatabaseSettings": {
|
||||
"Host": "localhost",
|
||||
"User": "local",
|
||||
"Password": "bG9jYWw=",
|
||||
"Database": "local",
|
||||
"Port": "3306",
|
||||
"Charset": "utf8mb4",
|
||||
"UseUnicode": "true",
|
||||
"Buffered": "true",
|
||||
"AuthPlugin": "mysql_native_password"
|
||||
}
|
||||
}
|
48
unittests/unittests_core/core_test_suite.py
Normal file
48
unittests/unittests_core/core_test_suite.py
Normal file
@ -0,0 +1,48 @@
|
||||
import unittest
|
||||
|
||||
from unittests_core.configuration.console_arguments_test_case import ConsoleArgumentsTestCase
|
||||
from unittests_core.configuration.configuration_test_case import ConfigurationTestCase
|
||||
from unittests_core.configuration.environment_test_case import EnvironmentTestCase
|
||||
from unittests_core.di.service_collection_test_case import ServiceCollectionTestCase
|
||||
from unittests_core.di.service_provider_test_case import ServiceProviderTestCase
|
||||
from unittests_core.pipes.bool_pipe_test_case import BoolPipeTestCase
|
||||
from unittests_core.pipes.ip_address_pipe_test_case import IPAddressTestCase
|
||||
from unittests_core.pipes.version_pipe_test_case import VersionPipeTestCase
|
||||
from unittests_core.utils.credential_manager_test_case import CredentialManagerTestCase
|
||||
from unittests_core.utils.json_processor_test_case import JSONProcessorTestCase
|
||||
from unittests_core.utils.string_test_case import StringTestCase
|
||||
|
||||
|
||||
class CoreTestSuite(unittest.TestSuite):
|
||||
def __init__(self):
|
||||
unittest.TestSuite.__init__(self)
|
||||
|
||||
loader = unittest.TestLoader()
|
||||
tests = [
|
||||
# config
|
||||
ConfigurationTestCase,
|
||||
ConsoleArgumentsTestCase,
|
||||
EnvironmentTestCase,
|
||||
# di
|
||||
ServiceCollectionTestCase,
|
||||
ServiceProviderTestCase,
|
||||
# pipes
|
||||
BoolPipeTestCase,
|
||||
IPAddressTestCase,
|
||||
VersionPipeTestCase,
|
||||
# utils
|
||||
CredentialManagerTestCase,
|
||||
JSONProcessorTestCase,
|
||||
StringTestCase,
|
||||
]
|
||||
|
||||
for test in tests:
|
||||
self.addTests(loader.loadTestsFromTestCase(test))
|
||||
|
||||
def run(self, *args):
|
||||
super().run(*args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(CoreTestSuite())
|
0
unittests/unittests_core/di/__init__.py
Normal file
0
unittests/unittests_core/di/__init__.py
Normal file
56
unittests/unittests_core/di/service_collection_test_case.py
Normal file
56
unittests/unittests_core/di/service_collection_test_case.py
Normal file
@ -0,0 +1,56 @@
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
|
||||
from cpl_core.configuration import Configuration
|
||||
from cpl_core.dependency_injection import ServiceCollection, ServiceLifetimeEnum, ServiceProviderABC
|
||||
|
||||
|
||||
class ServiceCollectionTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._sc = ServiceCollection(Configuration())
|
||||
|
||||
def test_add_singleton_type(self):
|
||||
self._sc.add_singleton(Mock)
|
||||
|
||||
service = self._sc._service_descriptors[0]
|
||||
self.assertEqual(ServiceLifetimeEnum.singleton, service.lifetime)
|
||||
self.assertEqual(Mock, service.service_type)
|
||||
self.assertEqual(Mock, service.base_type)
|
||||
self.assertIsNone(service.implementation)
|
||||
|
||||
def test_add_singleton_instance(self):
|
||||
mock = Mock()
|
||||
self._sc.add_singleton(mock)
|
||||
|
||||
service = self._sc._service_descriptors[0]
|
||||
self.assertEqual(ServiceLifetimeEnum.singleton, service.lifetime)
|
||||
self.assertEqual(type(mock), service.service_type)
|
||||
self.assertEqual(type(mock), service.base_type)
|
||||
self.assertIsNotNone(service.implementation)
|
||||
|
||||
def test_add_transient_type(self):
|
||||
self._sc.add_transient(Mock)
|
||||
|
||||
service = self._sc._service_descriptors[0]
|
||||
self.assertEqual(ServiceLifetimeEnum.transient, service.lifetime)
|
||||
self.assertEqual(Mock, service.service_type)
|
||||
self.assertEqual(Mock, service.base_type)
|
||||
self.assertIsNone(service.implementation)
|
||||
|
||||
def test_add_scoped_type(self):
|
||||
self._sc.add_scoped(Mock)
|
||||
|
||||
service = self._sc._service_descriptors[0]
|
||||
self.assertEqual(ServiceLifetimeEnum.scoped, service.lifetime)
|
||||
self.assertEqual(Mock, service.service_type)
|
||||
self.assertEqual(Mock, service.base_type)
|
||||
self.assertIsNone(service.implementation)
|
||||
|
||||
def test_build_service_provider(self):
|
||||
self._sc.add_singleton(Mock)
|
||||
service = self._sc._service_descriptors[0]
|
||||
self.assertIsNone(service.implementation)
|
||||
sp = self._sc.build_service_provider()
|
||||
self.assertTrue(isinstance(sp, ServiceProviderABC))
|
||||
self.assertTrue(isinstance(sp.get_service(Mock), Mock))
|
||||
self.assertIsNotNone(service.implementation)
|
98
unittests/unittests_core/di/service_provider_test_case.py
Normal file
98
unittests/unittests_core/di/service_provider_test_case.py
Normal file
@ -0,0 +1,98 @@
|
||||
import unittest
|
||||
|
||||
from cpl_core.configuration import Configuration
|
||||
from cpl_core.dependency_injection import ServiceCollection, ServiceProviderABC
|
||||
|
||||
|
||||
class ServiceCount:
|
||||
def __init__(self):
|
||||
self.count = 0
|
||||
|
||||
|
||||
class TestService:
|
||||
def __init__(self, sp: ServiceProviderABC, count: ServiceCount):
|
||||
count.count += 1
|
||||
self.sp = sp
|
||||
self.id = count.count
|
||||
|
||||
|
||||
class DifferentService:
|
||||
def __init__(self, sp: ServiceProviderABC, count: ServiceCount):
|
||||
count.count += 1
|
||||
self.sp = sp
|
||||
self.id = count.count
|
||||
|
||||
|
||||
class MoreDifferentService:
|
||||
def __init__(self, sp: ServiceProviderABC, count: ServiceCount):
|
||||
count.count += 1
|
||||
self.sp = sp
|
||||
self.id = count.count
|
||||
|
||||
|
||||
class ServiceProviderTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._services = (
|
||||
ServiceCollection(Configuration())
|
||||
.add_singleton(ServiceCount)
|
||||
.add_singleton(TestService)
|
||||
.add_singleton(TestService)
|
||||
.add_transient(DifferentService)
|
||||
.add_scoped(MoreDifferentService)
|
||||
.build_service_provider()
|
||||
)
|
||||
|
||||
count = self._services.get_service(ServiceCount)
|
||||
|
||||
def test_get_singleton(self):
|
||||
x = self._services.get_service(TestService)
|
||||
self.assertIsNotNone(x)
|
||||
self.assertEqual(1, x.id)
|
||||
self.assertEqual(x, self._services.get_service(TestService))
|
||||
self.assertEqual(x, self._services.get_service(TestService))
|
||||
self.assertEqual(x, self._services.get_service(TestService))
|
||||
|
||||
def test_get_singletons(self):
|
||||
x = self._services.get_services(list[TestService])
|
||||
self.assertEqual(2, len(x))
|
||||
self.assertEqual(1, x[0].id)
|
||||
self.assertEqual(2, x[1].id)
|
||||
self.assertNotEqual(x[0], x[1])
|
||||
|
||||
def test_get_transient(self):
|
||||
x = self._services.get_service(DifferentService)
|
||||
self.assertIsNotNone(x)
|
||||
self.assertEqual(1, x.id)
|
||||
self.assertNotEqual(x, self._services.get_service(DifferentService))
|
||||
self.assertNotEqual(x, self._services.get_service(DifferentService))
|
||||
self.assertNotEqual(x, self._services.get_service(DifferentService))
|
||||
|
||||
def test_scoped(self):
|
||||
scoped_id = 0
|
||||
singleton = self._services.get_service(TestService)
|
||||
transient = self._services.get_service(DifferentService)
|
||||
with self._services.create_scope() as scope:
|
||||
sp: ServiceProviderABC = scope.service_provider
|
||||
self.assertNotEqual(sp, self._services)
|
||||
y = sp.get_service(DifferentService)
|
||||
self.assertIsNotNone(y)
|
||||
self.assertEqual(3, y.id)
|
||||
x = sp.get_service(MoreDifferentService)
|
||||
self.assertIsNotNone(x)
|
||||
self.assertEqual(4, x.id)
|
||||
scoped_id = 4
|
||||
self.assertEqual(singleton.sp, self._services)
|
||||
self.assertEqual(transient.sp, self._services)
|
||||
self.assertEqual(x.sp, sp)
|
||||
self.assertNotEqual(x.sp, singleton.sp)
|
||||
transient_in_scope = sp.get_service(DifferentService)
|
||||
self.assertEqual(transient_in_scope.sp, sp)
|
||||
self.assertNotEqual(transient.sp, transient_in_scope.sp)
|
||||
|
||||
self.assertEqual(x.id, sp.get_service(MoreDifferentService).id)
|
||||
self.assertEqual(x.id, sp.get_service(MoreDifferentService).id)
|
||||
self.assertNotEqual(x, self._services.get_service(MoreDifferentService))
|
||||
self.assertEqual(singleton, self._services.get_service(TestService))
|
||||
|
||||
self.assertIsNone(scope.service_provider)
|
||||
self.assertNotEqual(scoped_id, self._services.get_service(MoreDifferentService).id)
|
1
unittests/unittests_core/pipes/__init__.py
Normal file
1
unittests/unittests_core/pipes/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# imports
|
14
unittests/unittests_core/pipes/bool_pipe_test_case.py
Normal file
14
unittests/unittests_core/pipes/bool_pipe_test_case.py
Normal file
@ -0,0 +1,14 @@
|
||||
import unittest
|
||||
|
||||
from cpl_core.pipes import BoolPipe
|
||||
|
||||
|
||||
class BoolPipeTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_transform(self):
|
||||
pipe = BoolPipe()
|
||||
|
||||
self.assertEqual("True", pipe.transform(True))
|
||||
self.assertEqual("False", pipe.transform(False))
|
20
unittests/unittests_core/pipes/ip_address_pipe_test_case.py
Normal file
20
unittests/unittests_core/pipes/ip_address_pipe_test_case.py
Normal file
@ -0,0 +1,20 @@
|
||||
import unittest
|
||||
|
||||
from cpl_core.pipes import IPAddressPipe
|
||||
|
||||
|
||||
class IPAddressTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_transform(self):
|
||||
pipe = IPAddressPipe()
|
||||
|
||||
self.assertEqual("192.168.178.1", pipe.transform([192, 168, 178, 1]))
|
||||
self.assertEqual("255.255.255.255", pipe.transform([255, 255, 255, 255]))
|
||||
self.assertEqual("0.0.0.0", pipe.transform([0, 0, 0, 0]))
|
||||
|
||||
self.assertRaises(Exception, lambda: pipe.transform([-192, 168, 178, 1]))
|
||||
self.assertRaises(Exception, lambda: pipe.transform([256, 168, 178, 1]))
|
||||
self.assertRaises(Exception, lambda: pipe.transform([256, 168, 178]))
|
||||
self.assertRaises(Exception, lambda: pipe.transform([256, 168, 178, 1, 1]))
|
16
unittests/unittests_core/pipes/version_pipe_test_case.py
Normal file
16
unittests/unittests_core/pipes/version_pipe_test_case.py
Normal file
@ -0,0 +1,16 @@
|
||||
import unittest
|
||||
|
||||
from cpl_core.pipes.version_pipe import VersionPipe
|
||||
|
||||
|
||||
class VersionPipeTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_transform(self):
|
||||
pipe = VersionPipe()
|
||||
|
||||
self.assertEqual("1.1.1", pipe.transform({"Major": 1, "Minor": 1, "Micro": 1}))
|
||||
self.assertEqual("0.1.1", pipe.transform({"Major": 0, "Minor": 1, "Micro": 1}))
|
||||
self.assertEqual("0.0.1", pipe.transform({"Major": 0, "Minor": 0, "Micro": 1}))
|
||||
self.assertEqual("0.0.0", pipe.transform({"Major": 0, "Minor": 0, "Micro": 0}))
|
0
unittests/unittests_core/utils/__init__.py
Normal file
0
unittests/unittests_core/utils/__init__.py
Normal file
@ -0,0 +1,40 @@
|
||||
import unittest
|
||||
|
||||
from cpl_core.utils import CredentialManager
|
||||
|
||||
|
||||
class CredentialManagerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_encrypt(self):
|
||||
self.assertEqual("ZkVjSkplQUx4aW1zWHlPbA==", CredentialManager.encrypt("fEcJJeALximsXyOl"))
|
||||
self.assertEqual("QmtVd1l4dW5Sck9jRmVTQQ==", CredentialManager.encrypt("BkUwYxunRrOcFeSA"))
|
||||
self.assertEqual("c2FtaHF1VkNSdmZpSGxDcQ==", CredentialManager.encrypt("samhquVCRvfiHlCq"))
|
||||
self.assertEqual("S05aWHBPYW9DbkRSV01rWQ==", CredentialManager.encrypt("KNZXpOaoCnDRWMkY"))
|
||||
self.assertEqual("QmtUV0Zsb3h1Y254UkJWeg==", CredentialManager.encrypt("BkTWFloxucnxRBVz"))
|
||||
self.assertEqual("VFdNTkRuYXB1b1dndXNKdw==", CredentialManager.encrypt("TWMNDnapuoWgusJw"))
|
||||
self.assertEqual("WVRiQXVSZXRMblpicWNrcQ==", CredentialManager.encrypt("YTbAuRetLnZbqckq"))
|
||||
self.assertEqual("bmN4aExackxhYUVVdnV2VA==", CredentialManager.encrypt("ncxhLZrLaaEUvuvT"))
|
||||
self.assertEqual("dmpNT0J5U0lLQmFrc0pIYQ==", CredentialManager.encrypt("vjMOBySIKBaksJHa"))
|
||||
self.assertEqual("ZHd6WHFzSlFvQlhRbGtVZw==", CredentialManager.encrypt("dwzXqsJQoBXQlkUg"))
|
||||
self.assertEqual("Q0lmUUhOREtiUmxnY2VCbQ==", CredentialManager.encrypt("CIfQHNDKbRlgceBm"))
|
||||
|
||||
def test_decrypt(self):
|
||||
self.assertEqual("fEcJJeALximsXyOl", CredentialManager.decrypt("ZkVjSkplQUx4aW1zWHlPbA=="))
|
||||
self.assertEqual("BkUwYxunRrOcFeSA", CredentialManager.decrypt("QmtVd1l4dW5Sck9jRmVTQQ=="))
|
||||
self.assertEqual("samhquVCRvfiHlCq", CredentialManager.decrypt("c2FtaHF1VkNSdmZpSGxDcQ=="))
|
||||
self.assertEqual("KNZXpOaoCnDRWMkY", CredentialManager.decrypt("S05aWHBPYW9DbkRSV01rWQ=="))
|
||||
self.assertEqual("BkTWFloxucnxRBVz", CredentialManager.decrypt("QmtUV0Zsb3h1Y254UkJWeg=="))
|
||||
self.assertEqual("TWMNDnapuoWgusJw", CredentialManager.decrypt("VFdNTkRuYXB1b1dndXNKdw=="))
|
||||
self.assertEqual("YTbAuRetLnZbqckq", CredentialManager.decrypt("WVRiQXVSZXRMblpicWNrcQ=="))
|
||||
self.assertEqual("ncxhLZrLaaEUvuvT", CredentialManager.decrypt("bmN4aExackxhYUVVdnV2VA=="))
|
||||
self.assertEqual("vjMOBySIKBaksJHa", CredentialManager.decrypt("dmpNT0J5U0lLQmFrc0pIYQ=="))
|
||||
self.assertEqual("dwzXqsJQoBXQlkUg", CredentialManager.decrypt("ZHd6WHFzSlFvQlhRbGtVZw=="))
|
||||
self.assertEqual("CIfQHNDKbRlgceBm", CredentialManager.decrypt("Q0lmUUhOREtiUmxnY2VCbQ=="))
|
||||
|
||||
def test_build_string(self):
|
||||
self.assertEqual(
|
||||
"TestStringWithCredentialsfEcJJeALximsXyOlHere",
|
||||
CredentialManager.build_string("TestStringWithCredentials$credentialsHere", "ZkVjSkplQUx4aW1zWHlPbA=="),
|
||||
)
|
38
unittests/unittests_core/utils/json_processor_test_case.py
Normal file
38
unittests/unittests_core/utils/json_processor_test_case.py
Normal file
@ -0,0 +1,38 @@
|
||||
import unittest
|
||||
|
||||
from cpl_core.utils.json_processor import JSONProcessor
|
||||
|
||||
|
||||
class SubTestClass:
|
||||
def __init__(self, value: str = None):
|
||||
self.value = value
|
||||
|
||||
|
||||
class TestClass:
|
||||
def __init__(self, i: int = None, s: str = None, d: dict = None, l: list = None, value: SubTestClass = None):
|
||||
self.i = i
|
||||
self.s = s
|
||||
self.d = d
|
||||
self.l = l
|
||||
self.value = value
|
||||
|
||||
|
||||
class JSONProcessorTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_process(self):
|
||||
test_dict = {
|
||||
"i": 10,
|
||||
"s": "Hello World",
|
||||
"d": {"test": "Test"},
|
||||
"l": list(range(0, 11)),
|
||||
"value": {"value": "Hello World"},
|
||||
}
|
||||
test: TestClass = JSONProcessor.process(TestClass, test_dict)
|
||||
|
||||
self.assertEqual(test.i, test_dict["i"])
|
||||
self.assertEqual(test.s, test_dict["s"])
|
||||
self.assertEqual(test.d, test_dict["d"])
|
||||
self.assertEqual(test.l, test_dict["l"])
|
||||
self.assertEqual(test.value.value, test_dict["value"]["value"])
|
58
unittests/unittests_core/utils/string_test_case.py
Normal file
58
unittests/unittests_core/utils/string_test_case.py
Normal file
@ -0,0 +1,58 @@
|
||||
import string
|
||||
import unittest
|
||||
|
||||
from cpl_core.utils import String
|
||||
|
||||
|
||||
class StringTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_convert_to_camel_case(self):
|
||||
expected = "HelloWorld"
|
||||
|
||||
self.assertEqual(expected, String.convert_to_camel_case("hello-world"))
|
||||
self.assertEqual(expected, String.convert_to_camel_case("hello-World"))
|
||||
self.assertEqual(expected, String.convert_to_camel_case("hello_world"))
|
||||
self.assertEqual("helloWorld", String.convert_to_camel_case("helloWorld"))
|
||||
self.assertEqual(expected, String.convert_to_camel_case("Hello_world"))
|
||||
self.assertEqual(expected, String.convert_to_camel_case("Hello_World"))
|
||||
self.assertEqual(expected, String.convert_to_camel_case("hello world"))
|
||||
|
||||
def test_convert_to_snake_case(self):
|
||||
expected = "hello_world"
|
||||
|
||||
self.assertEqual(expected, String.convert_to_snake_case("Hello World"))
|
||||
self.assertEqual(expected, String.convert_to_snake_case("hello-world"))
|
||||
self.assertEqual(expected, String.convert_to_snake_case("hello_world"))
|
||||
self.assertEqual(expected, String.convert_to_snake_case("helloWorld"))
|
||||
self.assertEqual(expected, String.convert_to_snake_case("Hello_world"))
|
||||
self.assertEqual(expected, String.convert_to_snake_case("Hello_World"))
|
||||
self.assertEqual(expected, String.convert_to_snake_case("hello world"))
|
||||
|
||||
def test_first_to_upper(self):
|
||||
expected = "HelloWorld"
|
||||
|
||||
self.assertEqual(expected, String.first_to_upper("helloWorld"))
|
||||
self.assertEqual(expected, String.first_to_upper("HelloWorld"))
|
||||
|
||||
def test_first_to_lower(self):
|
||||
expected = "helloWorld"
|
||||
|
||||
self.assertEqual(expected, String.first_to_lower("helloWorld"))
|
||||
self.assertEqual(expected, String.first_to_lower("HelloWorld"))
|
||||
|
||||
def test_random_string(self):
|
||||
expected = ""
|
||||
|
||||
for x in range(0, 100):
|
||||
rstr = String.random_string(string.ascii_letters, 4)
|
||||
self.assertNotEqual(expected, rstr)
|
||||
self.assertEqual(4, len(rstr))
|
||||
expected = rstr
|
||||
|
||||
for x in range(0, 100):
|
||||
rstr = String.random_string(string.ascii_letters, 16)
|
||||
self.assertNotEqual(expected, rstr)
|
||||
self.assertEqual(16, len(rstr))
|
||||
expected = rstr
|
Loading…
Reference in New Issue
Block a user