Refactored code
This commit is contained in:
45
src/tests/Application.py
Normal file
45
src/tests/Application.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from typing import Optional
|
||||
|
||||
from cpl.application.application_abc import ApplicationABC
|
||||
from cpl.console.console import Console
|
||||
from cpl.logging.logger_abc import LoggerABC
|
||||
from cpl.mailing.email import EMail
|
||||
from cpl.mailing.email_client_abc import EMailClientABC
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
|
||||
def __init__(self):
|
||||
ApplicationABC.__init__(self)
|
||||
self._logger: Optional[LoggerABC] = None
|
||||
self._mailer: Optional[EMailClientABC] = None
|
||||
|
||||
def test_send_mail(self):
|
||||
mail = EMail()
|
||||
mail.add_header('Mime-Version: 1.0')
|
||||
mail.add_header('Content-Type: text/plain; charset=utf-8')
|
||||
mail.add_header('Content-Transfer-Encoding: quoted-printable')
|
||||
mail.add_receiver('sven.heidemann@sh-edraft.de')
|
||||
mail.subject = f'Test - {self._configuration.environment.host_name}'
|
||||
mail.body = 'Dies ist ein Test :D'
|
||||
self._mailer.send_mail(mail)
|
||||
|
||||
def test_console(self):
|
||||
self._logger.debug(__name__, 'Started console test')
|
||||
Console.write_line('Hello World')
|
||||
Console.write('\nName: ')
|
||||
Console.write_line('Hello', Console.read_line())
|
||||
Console.clear()
|
||||
Console.write_at(5, 5, 'at 5, 5')
|
||||
Console.write_at(10, 10, 'at 10, 10')
|
||||
|
||||
def run(self):
|
||||
self._logger = self._services.get_service(LoggerABC)
|
||||
self._mailer = self._services.get_service(EMailClientABC)
|
||||
|
||||
self._logger.header(f'{self._configuration.environment.application_name}:')
|
||||
self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
|
||||
self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
|
||||
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
|
||||
self.test_send_mail()
|
||||
# self.test_console()
|
52
src/tests/Startup.py
Normal file
52
src/tests/Startup.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from typing import Optional
|
||||
|
||||
from cpl.application.application_host import ApplicationHost
|
||||
from cpl.application.application_host_abc import ApplicationHostABC
|
||||
from cpl.application.startup_abc import StartupABC
|
||||
from cpl.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl.database.context.database_context import DatabaseContext
|
||||
from cpl.database.database_settings import DatabaseSettings
|
||||
from cpl.dependency_injection.service_provider_base import ServiceProviderABC
|
||||
from cpl.logging.logger import Logger
|
||||
from cpl.logging.logger_abc import LoggerABC
|
||||
from cpl.mailing.email_client import EMailClient
|
||||
from cpl.mailing.email_client_abc import EMailClientABC
|
||||
from cpl.utils.credential_manager import CredentialManager
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
self._app_host: Optional[ApplicationHostABC] = None
|
||||
self._configuration: Optional[ConfigurationABC] = None
|
||||
self._services: Optional[ServiceProviderABC] = None
|
||||
|
||||
def create_application_host(self) -> ApplicationHostABC:
|
||||
self._app_host = ApplicationHost()
|
||||
self._configuration = self._app_host.configuration
|
||||
self._services = self._app_host.services
|
||||
return self._app_host
|
||||
|
||||
def create_configuration(self) -> ConfigurationABC:
|
||||
self._configuration.add_environment_variables('PYTHON_')
|
||||
self._configuration.add_environment_variables('CPL_')
|
||||
self._configuration.add_argument_variables()
|
||||
self._configuration.add_json_file(f'appsettings.json')
|
||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
|
||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
|
||||
|
||||
return self._configuration
|
||||
|
||||
def create_services(self) -> ServiceProviderABC:
|
||||
# Create and connect to database
|
||||
db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings)
|
||||
self._services.add_db_context(DatabaseContext)
|
||||
db: DatabaseContext = self._services.get_db_context()
|
||||
db.connect(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
|
||||
|
||||
self._services.add_singleton(LoggerABC, Logger)
|
||||
self._services.add_singleton(EMailClientABC, EMailClient)
|
||||
|
||||
return self._services
|
@@ -1,25 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
tests
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'tests'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.9'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=9)
|
||||
|
8
src/tests/appsettings.development.json
Normal file
8
src/tests/appsettings.development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "TRACE",
|
||||
"FileLogLevel": "TRACE"
|
||||
}
|
||||
}
|
@@ -16,5 +16,48 @@
|
||||
"Port": "587",
|
||||
"UserName": "dev-srv@sh-edraft.de",
|
||||
"Credentials": "RmBOQX1eNFYiYjgsSid3fV1nelc2WA=="
|
||||
},
|
||||
"PublishSettings": {
|
||||
"SourcePath": "../",
|
||||
"DistPath": "../../dist",
|
||||
"Templates": [
|
||||
{
|
||||
"TemplatePath": "../../publish_templates/all_template.txt",
|
||||
"Name": "all",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"CopyrightDate": "2020",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": ", see LICENSE for more details.",
|
||||
"Title": "",
|
||||
"Author": "Sven Heidemann",
|
||||
"Version": {
|
||||
"Major": 2020,
|
||||
"Minor": 12,
|
||||
"Micro": 9
|
||||
}
|
||||
},
|
||||
{
|
||||
"TemplatePath": "../../publish_templates/all_template.txt",
|
||||
"Name": "sh_edraft",
|
||||
"Description": "common python library",
|
||||
"LongDescription": "Library to share common classes and models used at sh-edraft.de",
|
||||
"CopyrightDate": "2020",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": ", see LICENSE for more details.",
|
||||
"Title": "",
|
||||
"Author": "Sven Heidemann",
|
||||
"Version": {
|
||||
"Major": 2020,
|
||||
"Minor": 12,
|
||||
"Micro": 9
|
||||
}
|
||||
}
|
||||
],
|
||||
"IncludedFiles": [],
|
||||
"ExcludedFiles": [],
|
||||
"TemplateEnding": "_template.txt"
|
||||
}
|
||||
}
|
@@ -5,16 +5,25 @@
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "TRACE",
|
||||
"FileLogLevel": "TRACE"
|
||||
},
|
||||
|
||||
"EMailClientSettings": {
|
||||
"Host": "mail.sh-edraft.de",
|
||||
"Port": "587",
|
||||
"UserName": "dev-srv@sh-edraft.de",
|
||||
"Credentials": "RmBOQX1eNFYiYjgsSid3fV1nelc2WA=="
|
||||
},
|
||||
|
||||
"DatabaseSettings": {
|
||||
"AuthPlugin": "mysql_native_password",
|
||||
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
|
||||
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
||||
"Encoding": "utf8mb4"
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "TRACE",
|
||||
"FileLogLevel": "TRACE"
|
||||
},
|
||||
|
||||
"DatabaseSettings": {
|
||||
"ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
|
||||
"Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
|
||||
"Encoding": "utf8mb4"
|
||||
}
|
||||
}
|
@@ -1,59 +0,0 @@
|
||||
{
|
||||
"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": "../../build_test/logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "INFO",
|
||||
"FileLogLevel": "TRACE"
|
||||
},
|
||||
"PublishSettings": {
|
||||
"SourcePath": "./",
|
||||
"DistPath": "../../build_test/dist",
|
||||
"Templates": [
|
||||
{
|
||||
"TemplatePath": "../../publish_templates/all_template.txt",
|
||||
"Name": "all",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"CopyrightDate": "2020",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": ", see LICENSE for more details.",
|
||||
"Title": "",
|
||||
"Author": "Sven Heidemann",
|
||||
"Version": {
|
||||
"Major": 2020,
|
||||
"Minor": 12,
|
||||
"Micro": 10
|
||||
}
|
||||
},
|
||||
{
|
||||
"TemplatePath": "../../publish_templates/all_template.txt",
|
||||
"Name": "sh_edraft",
|
||||
"Description": "common python library",
|
||||
"LongDescription": "Library to share common classes and models used at sh-edraft.de",
|
||||
"CopyrightDate": "2020",
|
||||
"CopyrightName": "sh-edraft.de",
|
||||
"LicenseName": "MIT",
|
||||
"LicenseDescription": ", see LICENSE for more details.",
|
||||
"Title": "",
|
||||
"Author": "Sven Heidemann",
|
||||
"Version": {
|
||||
"Major": 2020,
|
||||
"Minor": 12,
|
||||
"Micro": 10
|
||||
}
|
||||
}
|
||||
],
|
||||
"IncludedFiles": [
|
||||
],
|
||||
"ExcludedFiles": [
|
||||
],
|
||||
"TemplateEnding": "_template.txt"
|
||||
}
|
||||
}
|
8
src/tests/main.py
Normal file
8
src/tests/main.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from tests.Application import Application
|
||||
from tests.Startup import Startup
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = Application()
|
||||
app.use_startup(Startup)
|
||||
app.build()
|
||||
app.run()
|
@@ -1,52 +0,0 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from sh_edraft.console import Console
|
||||
from sh_edraft.hosting import ApplicationHost
|
||||
from sh_edraft.logging import Logger
|
||||
from sh_edraft.logging.base import LoggerBase
|
||||
from sh_edraft.publish import Publisher
|
||||
from sh_edraft.publish.base import PublisherBase
|
||||
from sh_edraft.publish.model import PublishSettings
|
||||
|
||||
|
||||
class PublisherTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
Console.disable()
|
||||
self._app_host = ApplicationHost()
|
||||
self._configuration = self._app_host.configuration
|
||||
self._services = self._app_host.services
|
||||
|
||||
self._configuration.add_environment_variables('CPL_')
|
||||
self._configuration.add_argument_variables()
|
||||
self._configuration.add_json_file(f'build.json')
|
||||
|
||||
self._services.add_singleton(LoggerBase, Logger)
|
||||
self._services.add_singleton(PublisherBase, Publisher)
|
||||
self._publisher: Publisher = self._services.get_service(PublisherBase)
|
||||
|
||||
def test_include(self):
|
||||
value = './test.py'
|
||||
self._publisher.include(value)
|
||||
self.assertTrue(value in self._publisher._publish_settings.included_files)
|
||||
|
||||
def test_exclude(self):
|
||||
value = './test.py'
|
||||
self._publisher.exclude(value)
|
||||
self.assertTrue(value in self._publisher._publish_settings.excluded_files)
|
||||
|
||||
def test_create(self):
|
||||
self._publisher.create()
|
||||
self.assertTrue(os.path.isdir(self._configuration.get_configuration(PublishSettings).dist_path))
|
||||
|
||||
def test_build(self):
|
||||
self._publisher.create()
|
||||
self._publisher.build()
|
||||
self.assertTrue(os.path.isdir(self._configuration.get_configuration(PublishSettings).dist_path))
|
||||
|
||||
def test_publish(self):
|
||||
self._publisher.create()
|
||||
self._publisher.build()
|
||||
self._publisher.publish()
|
||||
self.assertTrue(os.path.isdir(self._configuration.get_configuration(PublishSettings).dist_path))
|
@@ -1,79 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from sh_edraft.console import Console
|
||||
from sh_edraft.database.context import DatabaseContext
|
||||
from sh_edraft.hosting import ApplicationHost
|
||||
from sh_edraft.logging import Logger
|
||||
from sh_edraft.logging.base import LoggerBase
|
||||
|
||||
|
||||
class ProviderTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
Console.disable()
|
||||
self._app_host = ApplicationHost()
|
||||
self._configuration = self._app_host.configuration
|
||||
self._services = self._app_host.services
|
||||
|
||||
self._configuration.add_environment_variables('CPL_')
|
||||
self._configuration.add_argument_variables()
|
||||
self._configuration.add_json_file(f'appsettings.json')
|
||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
|
||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
|
||||
|
||||
def test_get_db_context(self):
|
||||
self._services.add_db_context(DatabaseContext)
|
||||
db: DatabaseContext = self._services.get_db_context()
|
||||
|
||||
self.assertIsNotNone(db)
|
||||
|
||||
def test_get_service_singleton(self):
|
||||
self._services.add_singleton(LoggerBase, Logger)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNotNone(logger)
|
||||
|
||||
def test_get_service_scoped(self):
|
||||
self._services.add_scoped(LoggerBase, Logger)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNotNone(logger)
|
||||
|
||||
def test_get_service_transient(self):
|
||||
self._services.add_transient(LoggerBase, Logger)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNotNone(logger)
|
||||
|
||||
def test_remove_service_singleton(self):
|
||||
self._services.add_singleton(LoggerBase, Logger)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNotNone(logger)
|
||||
|
||||
self._services.remove_service(LoggerBase)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNone(logger)
|
||||
|
||||
def test_remove_service_scoped(self):
|
||||
self._services.add_scoped(LoggerBase, Logger)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNotNone(logger)
|
||||
|
||||
self._services.remove_service(LoggerBase)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNone(logger)
|
||||
|
||||
def test_remove_service_transient(self):
|
||||
self._services.add_transient(LoggerBase, Logger)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNotNone(logger)
|
||||
|
||||
self._services.remove_service(LoggerBase)
|
||||
logger = self._services.get_service(LoggerBase)
|
||||
|
||||
self.assertIsNone(logger)
|
@@ -1,24 +0,0 @@
|
||||
import setuptools
|
||||
|
||||
setuptools.setup(
|
||||
name='sh_edraft_unittests',
|
||||
version='2020.0.1',
|
||||
packages=setuptools.find_packages(exclude=["tests*"]),
|
||||
url='https://www.sh-edraft.de',
|
||||
license='MIT',
|
||||
author='Sven Heidemann',
|
||||
author_email='edraft.sh@gmail.com',
|
||||
include_package_data=True,
|
||||
description='sh-edraft python common lib unittest',
|
||||
python_requires='>=3.8',
|
||||
install_requires=[
|
||||
'discord.py',
|
||||
'flask',
|
||||
'mysql-connector',
|
||||
'SQLAlchemy',
|
||||
'termcolor',
|
||||
'pyfiglet',
|
||||
'tabulate',
|
||||
'smtplib'
|
||||
]
|
||||
)
|
@@ -1,26 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from sh_edraft.time.model import TimeFormatSettingsNames, TimeFormatSettings
|
||||
|
||||
|
||||
class TimeFormatSettingsTest(unittest.TestCase):
|
||||
|
||||
def setUp(self): pass
|
||||
|
||||
def test_from_dict(self):
|
||||
test_dict = {
|
||||
TimeFormatSettingsNames.date_format.value: '%H:%M:%S',
|
||||
TimeFormatSettingsNames.time_format.value: '%Y-%m-%d',
|
||||
TimeFormatSettingsNames.date_time_format.value: '%Y-%m-%d %H:%M:%S.%f',
|
||||
TimeFormatSettingsNames.date_time_log_format.value: '%Y-%m-%d_%H-%M-%S'
|
||||
}
|
||||
|
||||
settings = TimeFormatSettings()
|
||||
settings.from_dict(test_dict)
|
||||
|
||||
self.assertIsNotNone(settings)
|
||||
|
||||
self.assertEqual(test_dict[TimeFormatSettingsNames.date_format.value], settings.date_format)
|
||||
self.assertEqual(test_dict[TimeFormatSettingsNames.time_format.value], settings.time_format)
|
||||
self.assertEqual(test_dict[TimeFormatSettingsNames.date_time_format.value], settings.date_time_format)
|
||||
self.assertEqual(test_dict[TimeFormatSettingsNames.date_time_log_format.value], settings.date_time_log_format)
|
@@ -1,37 +0,0 @@
|
||||
import base64
|
||||
import unittest
|
||||
|
||||
from sh_edraft.utils import CredentialManager
|
||||
|
||||
|
||||
class CredentialManagerTest(unittest.TestCase):
|
||||
|
||||
def setUp(self): pass
|
||||
|
||||
def test_encode(self):
|
||||
test_string = 'Hello World'
|
||||
expected_test_result = base64.b64encode(test_string.encode('utf-8')).decode('utf-8')
|
||||
|
||||
test_result = CredentialManager.encrypt(test_string)
|
||||
|
||||
self.assertIsNotNone(test_result)
|
||||
self.assertEqual(expected_test_result, test_result)
|
||||
|
||||
def test_decode(self):
|
||||
test_string = 'SGVsbG8gV29ybGQ='
|
||||
expected_test_result = base64.b64decode(test_string).decode('utf-8')
|
||||
|
||||
test_result = CredentialManager.decrypt(test_string)
|
||||
|
||||
self.assertIsNotNone(test_result)
|
||||
self.assertEqual(expected_test_result, test_result)
|
||||
|
||||
def test_build_string(self):
|
||||
test_string = 'String is $credentials'
|
||||
test_credentials = 'SGVsbG8gV29ybGQ='
|
||||
expected_test_result = test_string.replace('$credentials', base64.b64decode(test_credentials).decode('utf-8'))
|
||||
|
||||
test_result = CredentialManager.build_string(test_string, test_credentials)
|
||||
|
||||
self.assertIsNotNone(test_result)
|
||||
self.assertEqual(expected_test_result, test_result)
|
Reference in New Issue
Block a user