Added new tester and CredentialManager TestCase

This commit is contained in:
Sven Heidemann 2020-12-25 12:12:25 +01:00
parent c6bbbb1ded
commit d284cec2a2
20 changed files with 222 additions and 764 deletions

View File

@ -1,8 +0,0 @@
{
"LoggingSettings": {
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
}
}

View File

@ -0,0 +1,63 @@
{
"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/",
"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=="
},
"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"
}
}

View File

@ -1,8 +1,63 @@
{ {
"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": { "LoggingSettings": {
"Path": "logs/", "Path": "logs/",
"Filename": "log_$start_time.log", "Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
},
"EMailClientSettings": {
"Host": "mail.sh-edraft.de",
"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"
} }
} }

View File

@ -6,12 +6,9 @@
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
}, },
"PublishSettings": { "DatabaseSettings": {
"SourcePath": "../", "ConnectionString": "mysql+mysqlconnector://sh_cpl:$credentials@localhost/sh_cpl",
"DistPath": "../../dist", "Credentials": "MHZhc0Y2bjhKc1VUMWV0Qw==",
"Templates": [], "Encoding": "utf8mb4"
"IncludedFiles": [],
"ExcludedFiles": [],
"TemplateEnding": "_template.txt"
} }
} }

59
src/tests/build.json Normal file
View File

@ -0,0 +1,59 @@
{
"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/logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "INFO",
"FileLogLevel": "TRACE"
},
"PublishSettings": {
"SourcePath": "./",
"DistPath": "../build/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"
}
}

View File

View File

View File

@ -0,0 +1,37 @@
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)

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.configuration
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'tests.configuration'
__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)

View File

@ -1,93 +0,0 @@
import os
import unittest
from typing import cast
from sh_edraft.configuration import Configuration
from sh_edraft.environment.model import EnvironmentName
from sh_edraft.hosting import ApplicationHost
from sh_edraft.logging.model import LoggingSettings, LoggingLevel
from sh_edraft.publishing.model import PublishSettings
from sh_edraft.time.model import TimeFormatSettings
class ConfigTest(unittest.TestCase):
def setUp(self):
self._app_host = ApplicationHost()
self._config = cast(Configuration, self._app_host.configuration)
def test_create(self):
print(f'{__name__}.test_create:')
self.assertIsNotNone(self._config)
self._config.create()
self.assertIsNotNone(self._config)
self.assertEqual(len(self._config._config), 0)
self.assertIsNotNone(self._app_host.application_runtime)
def test_env_vars(self):
print(f'{__name__}.test_env_vars:')
self._config.add_environment_variables('PYTHON_')
self._config.add_environment_variables('CPL_')
def test_arguments(self):
print(f'{__name__}.test_arguments:')
self._config.add_argument_variables()
self.assertEqual(self._config.environment.environment_name, EnvironmentName.testing.value)
def test_appsettings(self):
print(f'{__name__}.test_appsettings:')
self._config.add_json_file(f'appsettings.json')
time_formats: TimeFormatSettings = cast(TimeFormatSettings, self._config.get_configuration(TimeFormatSettings))
self.assertIsNotNone(time_formats)
self.assertEqual(time_formats.date_format, '%Y-%m-%d')
self.assertEqual(time_formats.time_format, '%H:%M:%S')
self.assertEqual(time_formats.date_time_format, '%Y-%m-%d %H:%M:%S.%f')
self.assertEqual(time_formats.date_time_log_format, '%Y-%m-%d_%H-%M-%S')
logging = cast(LoggingSettings, self._config.get_configuration(LoggingSettings))
self.assertIsNotNone(logging)
self.assertEqual(logging.path, 'logs/')
self.assertEqual(logging.filename, 'log_$start_time.log')
self.assertEqual(logging.console.value, LoggingLevel.ERROR.value)
self.assertEqual(logging.level.value, LoggingLevel.WARN.value)
with self.assertRaises(Exception):
publish: PublishSettings = cast(PublishSettings, self._config.get_configuration(PublishSettings))
def test_appsettings_environment(self):
print(f'{__name__}.test_appsettings_environment:')
self._config.add_argument_variables()
self._config.add_json_file(f'appsettings.{self._config.environment.environment_name}.json')
logging = cast(LoggingSettings, self._config.get_configuration(LoggingSettings))
self.assertIsNotNone(logging)
self.assertEqual(logging.path, 'logs/')
self.assertEqual(logging.filename, 'log_$start_time.log')
self.assertEqual(logging.console.value, LoggingLevel.TRACE.value)
self.assertEqual(logging.level.value, LoggingLevel.TRACE.value)
publish: PublishSettings = cast(PublishSettings, self._config.get_configuration(PublishSettings))
self.assertIsNotNone(publish)
self.assertEqual(publish.source_path, '../')
self.assertEqual(publish.dist_path, '../../dist')
self.assertEqual(publish.templates, [])
self.assertEqual(publish.included_files, [])
self.assertEqual(publish.excluded_files, [])
self.assertEqual(publish.template_ending, '_template.txt')
def test_appsettings_host(self):
print(f'{__name__}.test_appsettings_host:')
self._config.add_json_file(f'appsettings.{self._config.environment.host_name}.json')
def test_appsettings_customer(self):
print(f'{__name__}.test_appsettings_customer:')
file_name = f'appsettings.{self._config.environment.customer}.json'
with self.assertRaises(SystemExit):
if os.path.isfile(f'{self._config.environment.content_root_path}/{file_name}'):
os.remove(f'{self._config.environment.content_root_path}/{file_name}')
self._config.add_json_file(file_name)
self._config.add_json_file(file_name, optional=True)

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.hosting
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'tests.hosting'
__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)

View File

@ -1,33 +0,0 @@
import unittest
import datetime
from sh_edraft.configuration.base import ConfigurationBase
from sh_edraft.hosting import ApplicationHost
from sh_edraft.hosting.base import ApplicationRuntimeBase
from sh_edraft.service.base import ServiceProviderBase
class AppHostTest(unittest.TestCase):
def setUp(self):
pass
def test_create(self):
print(f'{__name__}.test_create:')
app_host = ApplicationHost()
self.assertIsNotNone(app_host)
app_host.create()
self.assertIsNotNone(app_host.configuration)
self.assertTrue(isinstance(app_host.configuration, ConfigurationBase))
self.assertIsNotNone(app_host.application_runtime)
self.assertTrue(isinstance(app_host.application_runtime, ApplicationRuntimeBase))
self.assertIsNotNone(app_host.services)
self.assertTrue(isinstance(app_host.services, ServiceProviderBase))
self.assertIsNotNone(app_host._start_time)
self.assertTrue(isinstance(app_host._start_time, datetime.datetime))
self.assertIsNotNone(app_host._end_time)
self.assertTrue(isinstance(app_host._end_time, datetime.datetime))

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.service_providing
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'tests.service_providing'
__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)

View File

@ -1,136 +0,0 @@
import unittest
from typing import cast
from sh_edraft.hosting import ApplicationHost
from sh_edraft.logging import Logger
from sh_edraft.logging.base import LoggerBase
from sh_edraft.publishing import Publisher
from sh_edraft.publishing.base import PublisherBase
from sh_edraft.service import ServiceProvider
from sh_edraft.service.base import ServiceBase
class ServiceProviderTest(unittest.TestCase):
def setUp(self):
self._app_host = ApplicationHost()
self._config = self._app_host.configuration
self._config.create()
self._config.add_environment_variables('PYTHON_')
self._config.add_environment_variables('CPL_')
self._config.add_argument_variables()
self._config.add_json_file(f'appsettings.json')
self._config.add_json_file(f'appsettings.{self._config.environment.environment_name}.json')
self._config.add_json_file(f'appsettings.{self._config.environment.host_name}.json', optional=True)
self._services: ServiceProvider = cast(ServiceProvider, self._app_host.services)
self._services.create()
def _check_general_requirements(self):
self.assertIsNotNone(self._services)
def _add_logger(self):
self._services.add_singleton(LoggerBase, Logger)
logger: Logger = self._services.get_service(LoggerBase)
logger.create()
def test_create(self):
print(f'{__name__}.test_create:')
provider = ServiceProvider(self._app_host.application_runtime)
self.assertIsNotNone(provider)
provider.create()
self.assertIsNotNone(provider)
def test_add_singleton(self):
print(f'{__name__}.test_add_singleton:')
self._check_general_requirements()
self._services.add_singleton(LoggerBase, Logger)
self.assertGreater(len(self._services._singleton_services), 0)
found = False
for service_type in self._services._singleton_services:
service = self._services._singleton_services[service_type]
if service_type == LoggerBase and (
isinstance(service, Logger) and
isinstance(service, LoggerBase) and
isinstance(service, ServiceBase)
):
if not found:
found = True
self.assertTrue(found)
found = False
for service_type in self._services._singleton_services:
service = self._services._singleton_services[service_type]
if service_type == PublisherBase and (
isinstance(service, Publisher) and
isinstance(service, PublisherBase) and
isinstance(service, ServiceBase)
):
if not found:
found = True
self.assertFalse(found)
def test_get_singleton(self):
print(f'{__name__}.test_get_singleton:')
self._check_general_requirements()
self._services.add_singleton(LoggerBase, Logger)
logger: Logger = self._services.get_service(LoggerBase)
self.assertIsNotNone(logger)
self.assertTrue(isinstance(logger, Logger))
self.assertTrue(isinstance(logger, LoggerBase))
self.assertTrue(isinstance(logger, ServiceBase))
self.assertEqual(logger._app_runtime, self._app_host.application_runtime)
def test_add_scoped(self):
print(f'{__name__}.test_add_scoped:')
self._check_general_requirements()
self._add_logger()
self._services.add_scoped(PublisherBase, Publisher)
def test_get_scoped(self):
print(f'{__name__}.test_get_scoped:')
self._check_general_requirements()
self._add_logger()
self._services.add_scoped(PublisherBase, Publisher)
publisher: Publisher = self._services.get_service(PublisherBase)
self.assertIsNotNone(publisher)
self.assertTrue(isinstance(publisher, Publisher))
self.assertTrue(isinstance(publisher, PublisherBase))
self.assertTrue(isinstance(publisher, ServiceBase))
self.assertTrue(isinstance(publisher._logger, Logger))
self.assertTrue(isinstance(publisher._logger, LoggerBase))
self.assertTrue(isinstance(publisher._logger, ServiceBase))
def test_add_transient(self):
print(f'{__name__}.test_add_transient:')
self._check_general_requirements()
self._add_logger()
self._services.add_transient(PublisherBase, Publisher)
self.assertGreater(len(self._services._transient_services), 0)
self.assertTrue(bool(isinstance(service, ServiceBase) for service in self._services._transient_services))
def test_get_transient(self):
print(f'{__name__}.test_get_transient:')
self._check_general_requirements()
self._add_logger()
self._services.add_transient(PublisherBase, Publisher)
publisher: Publisher = self._services.get_service(PublisherBase)
self.assertIsNotNone(publisher)
self.assertTrue(isinstance(publisher, Publisher))
self.assertTrue(isinstance(publisher, PublisherBase))
self.assertTrue(isinstance(publisher, ServiceBase))
self.assertTrue(isinstance(publisher._logger, Logger))
self.assertTrue(isinstance(publisher._logger, LoggerBase))
self.assertTrue(isinstance(publisher._logger, ServiceBase))

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.services
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'tests.services'
__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)

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.services.logging
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'tests.services.logging'
__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)

View File

@ -1,206 +0,0 @@
import os
import shutil
import unittest
from string import Template
from typing import cast
from sh_edraft.hosting import ApplicationHost
from sh_edraft.logging import Logger
from sh_edraft.logging.model import LoggingSettings
from sh_edraft.service import ServiceProvider
from sh_edraft.time.model import TimeFormatSettings
class LoggerTest(unittest.TestCase):
def setUp(self):
self._app_host = ApplicationHost()
self._config = self._app_host.configuration
self._config.create()
self._config.add_environment_variables('PYTHON_')
self._config.add_environment_variables('CPL_')
self._config.add_argument_variables()
self._config.add_json_file(f'appsettings.json')
self._config.add_json_file(f'appsettings.{self._config.environment.environment_name}.json')
self._config.add_json_file(f'appsettings.{self._config.environment.host_name}.json', optional=True)
self._services: ServiceProvider = cast(ServiceProvider, self._app_host.services)
self._services.create()
self._app_runtime = self._app_host.application_runtime
self._log_settings: LoggingSettings = self._config.get_configuration(LoggingSettings)
self._time_format_settings: TimeFormatSettings = self._config.get_configuration(TimeFormatSettings)
def tearDown(self):
if os.path.isdir(self._log_settings.path):
shutil.rmtree(self._log_settings.path)
def _check_general_requirements(self):
self.assertIsNotNone(self._log_settings)
self.assertIsNotNone(self._time_format_settings)
def test_create(self):
print(f'{__name__}.test_create:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
self.assertTrue(os.path.isdir(self._log_settings.path))
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
self.assertTrue(os.path.isfile(self._log_settings.path + log_file))
def test_header(self):
print(f'{__name__}.test_header:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
logger.header('HeaderTest:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertEqual(log_content[len(log_content) - 1], 'HeaderTest:\n')
def test_trace(self):
print(f'{__name__}.test_trace:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
logger.trace(__name__, f'{__name__}.test_trace:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ TRACE ] [ {__name__} ]: {__name__}.test_trace:\n'))
def test_debug(self):
print(f'{__name__}.test_debug:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
logger.debug(__name__, f'{__name__}.test_debug:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ DEBUG ] [ {__name__} ]: {__name__}.test_debug:\n'))
def test_info(self):
print(f'{__name__}.test_info:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
logger.info(__name__, f'{__name__}.test_info:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ INFO ] [ {__name__} ]: {__name__}.test_info:\n'))
def test_warn(self):
print(f'{__name__}.test_warn:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
logger.warn(__name__, f'{__name__}.test_warn:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ WARN ] [ {__name__} ]: {__name__}.test_warn:\n'))
def test_error(self):
print(f'{__name__}.test_error:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
logger.error(__name__, f'{__name__}.test_error:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ ERROR ] [ {__name__} ]: {__name__}.test_error:\n'))
def test_fatal(self):
print(f'{__name__}.test_fatal:')
logger = Logger(self._log_settings, self._time_format_settings, self._app_runtime)
logger.create()
with self.assertRaises(SystemExit):
logger.fatal(__name__, f'{__name__}.test_fatal:')
log_file = Template(self._log_settings.filename).substitute(
date_time_now=self._app_runtime.date_time_now.strftime(self._time_format_settings.date_time_format),
start_time=self._app_runtime.start_time.strftime(self._time_format_settings.date_time_log_format)
)
log_content = []
try:
with open(self._log_settings.path + log_file, "r") as log:
log_content = log.readlines()
log.close()
except Exception as e:
print('Cannot open log file', e)
self.assertGreater(len(log_content), 0)
self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ FATAL ] [ {__name__} ]: {__name__}.test_fatal:\n'))

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests.services.publishing
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2020 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'tests.services.publishing'
__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)

View File

@ -1,92 +0,0 @@
import os
import shutil
import unittest
from sh_edraft.hosting import ApplicationHost
from sh_edraft.logging import Logger
from sh_edraft.logging.model import LoggingSettings
from sh_edraft.publishing import Publisher
from sh_edraft.publishing.model import Template
from sh_edraft.publishing.model import PublishSettings
from sh_edraft.coding.model import Version
from sh_edraft.time.model import TimeFormatSettings
class PublisherTest(unittest.TestCase):
def _configure(self):
self._version = Version(2020, 12, 5)
templates = [
Template(
'../../publish_templates/all_template.txt',
'all',
'',
'',
'2020',
'sh-edraft.de',
'MIT',
', see LICENSE for more details.',
'',
'Sven Heidemann',
self._version
),
Template(
'../../publish_templates/all_template.txt',
'sh_edraft',
'common python library',
'Library to share common classes and models used at sh-edraft.de',
'2020',
'sh-edraft.de',
'MIT',
', see LICENSE for more details.',
'',
'Sven Heidemann',
self._version
)
]
self._source_path = '../'
self._dist_path = '../../dist'
self._publish_settings_model = PublishSettings()
self._publish_settings_model.from_dict({
"SourcePath": self._source_path,
"DistPath": self._dist_path,
"Templates": templates,
"IncludedFiles": [],
"ExcludedFiles": [],
"TemplateEnding": "_template.txt",
})
def setUp(self):
self._app_host = ApplicationHost()
self._config = self._app_host.configuration
self._config.create()
self._config.add_environment_variables('PYTHON_')
self._config.add_environment_variables('CPL_')
self._config.add_argument_variables()
self._config.add_json_file(f'appsettings.json')
self._config.add_json_file(f'appsettings.{self._config.environment.environment_name}.json')
self._config.add_json_file(f'appsettings.{self._config.environment.host_name}.json', optional=True)
self._app_runtime = self._app_host.application_runtime
self._configure()
self._log_settings: LoggingSettings = self._config.get_configuration(LoggingSettings)
self._time_format_settings: TimeFormatSettings = self._config.get_configuration(TimeFormatSettings)
self._logger = Logger(self._log_settings, self._time_format_settings, self._app_host.application_runtime)
self._logger.create()
def tearDown(self):
if os.path.isdir(self._log_settings.path):
shutil.rmtree(self._log_settings.path)
def test_create(self):
print(f'{__name__}.test_create:')
publisher: Publisher = Publisher(self._logger, self._publish_settings_model)
self.assertIsNotNone(publisher)
publisher.create()
self.assertTrue(os.path.isdir(self._dist_path))
self.assertEqual(publisher._publish_settings, self._publish_settings_model)

View File

@ -1,10 +1,6 @@
import unittest import unittest
from tests.configuration.config import ConfigTest from tests.cases.utils.credential_manager import CredentialManagerTest
from tests.hosting.app_host import AppHostTest
from tests.services.logging.logger import LoggerTest
from tests.services.publishing.publisher import PublisherTest
from tests.service_providing.service_provider import ServiceProviderTest
class Tester: class Tester:
@ -13,44 +9,13 @@ class Tester:
self._suite = unittest.TestSuite() self._suite = unittest.TestSuite()
def create(self): def create(self):
# hosting app host self._suite.addTest(CredentialManagerTest(CredentialManagerTest.test_encode.__name__))
self._suite.addTest(AppHostTest('test_create')) self._suite.addTest(CredentialManagerTest(CredentialManagerTest.test_decode.__name__))
self._suite.addTest(CredentialManagerTest(CredentialManagerTest.test_build_string.__name__))
# configuration
self._suite.addTest(ConfigTest('test_create'))
self._suite.addTest(ConfigTest('test_env_vars'))
self._suite.addTest(ConfigTest('test_arguments'))
self._suite.addTest(ConfigTest('test_appsettings'))
self._suite.addTest(ConfigTest('test_appsettings_environment'))
self._suite.addTest(ConfigTest('test_appsettings_host'))
self._suite.addTest(ConfigTest('test_appsettings_customer'))
# providing
self._suite.addTest(ServiceProviderTest('test_create'))
self._suite.addTest(ServiceProviderTest('test_add_singleton'))
self._suite.addTest(ServiceProviderTest('test_get_singleton'))
self._suite.addTest(ServiceProviderTest('test_add_scoped'))
self._suite.addTest(ServiceProviderTest('test_get_scoped'))
self._suite.addTest(ServiceProviderTest('test_add_transient'))
self._suite.addTest(ServiceProviderTest('test_get_transient'))
# logging
self._suite.addTest(LoggerTest('test_create'))
self._suite.addTest(LoggerTest('test_header'))
self._suite.addTest(LoggerTest('test_trace'))
self._suite.addTest(LoggerTest('test_debug'))
self._suite.addTest(LoggerTest('test_info'))
self._suite.addTest(LoggerTest('test_warn'))
self._suite.addTest(LoggerTest('test_error'))
self._suite.addTest(LoggerTest('test_fatal'))
# publishing
self._suite.addTest(PublisherTest('test_create'))
def start(self): def start(self):
runner = unittest.TextTestRunner() runner = unittest.TextTestRunner()
runner.run(self._suite) runner.run(self._suite)
# unittest.main()
if __name__ == '__main__': if __name__ == '__main__':