Improved configuration and testing

This commit is contained in:
Sven Heidemann 2020-11-29 11:32:35 +01:00
parent 44ffb4e574
commit b14446272a
10 changed files with 73 additions and 59 deletions

View File

@ -98,7 +98,7 @@ class Configuration(ConfigurationBase):
self._print_error(__name__, f'Cannot load config file: {file}! -> {e}') self._print_error(__name__, f'Cannot load config file: {file}! -> {e}')
return {} return {}
def add_configuration(self, key_type: type, value: object): def add_configuration(self, key_type: type, value: ConfigurationModelBase):
self._config[key_type] = value self._config[key_type] = value
def get_configuration(self, search_type: type) -> ConfigurationModelBase: def get_configuration(self, search_type: type) -> ConfigurationModelBase:

View File

@ -22,7 +22,7 @@ from collections import namedtuple
# imports: # imports:
from .template import Template from .template import Template
from .template_enum import TemplateEnum from .template_enum import TemplateEnum
from .publish_settings_model import PublishSettingsModel from .publish_settings_model import PublishSettings
from .publish_settings_name import PublishSettingsName from .publish_settings_name import PublishSettingsName
VersionInfo = namedtuple('VersionInfo', 'major minor micro') VersionInfo = namedtuple('VersionInfo', 'major minor micro')

View File

@ -7,7 +7,7 @@ from sh_edraft.publishing.model.publish_settings_name import PublishSettingsName
from sh_edraft.utils import Console from sh_edraft.utils import Console
class PublishSettingsModel(ConfigurationModelBase): class PublishSettings(ConfigurationModelBase):
def __init__(self): def __init__(self):
ConfigurationModelBase.__init__(self) ConfigurationModelBase.__init__(self)

View File

@ -4,17 +4,17 @@ from string import Template as stringTemplate
from sh_edraft.logging.base.logger_base import LoggerBase from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.publishing.base.publisher_base import PublisherBase from sh_edraft.publishing.base.publisher_base import PublisherBase
from sh_edraft.publishing.model.publish_settings_model import PublishSettingsModel from sh_edraft.publishing.model.publish_settings_model import PublishSettings
from sh_edraft.publishing.model.template import Template from sh_edraft.publishing.model.template import Template
class Publisher(PublisherBase): class Publisher(PublisherBase):
def __init__(self, logger: LoggerBase, publish_settings: PublishSettingsModel): def __init__(self, logger: LoggerBase, publish_settings: PublishSettings):
PublisherBase.__init__(self) PublisherBase.__init__(self)
self._logger: LoggerBase = logger self._logger: LoggerBase = logger
self._publish_settings: PublishSettingsModel = publish_settings self._publish_settings: PublishSettings = publish_settings
@property @property
def source_path(self) -> str: def source_path(self) -> str:

View File

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

View File

@ -0,0 +1,15 @@
{
"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": "ERROR",
"FileLogLevel": "WARN"
}
}

View File

@ -0,0 +1,17 @@
{
"LoggingSettings": {
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
},
"PublishSettings": {
"SourcePath": "../",
"DistPath": "../../dist",
"Templates": [],
"IncludedFiles": [],
"ExcludedFiles": [],
"TemplateEnding": "_template.txt"
}
}

View File

@ -8,7 +8,7 @@ from sh_edraft.logging import Logger
from sh_edraft.logging.model import LoggingSettings from sh_edraft.logging.model import LoggingSettings
from sh_edraft.publishing import Publisher from sh_edraft.publishing import Publisher
from sh_edraft.publishing.model import Template from sh_edraft.publishing.model import Template
from sh_edraft.publishing.model import PublishSettingsModel from sh_edraft.publishing.model import PublishSettings
from sh_edraft.coding.model import Version from sh_edraft.coding.model import Version
from sh_edraft.time.model import TimeFormatSettings from sh_edraft.time.model import TimeFormatSettings
@ -65,7 +65,7 @@ class PublisherTest(unittest.TestCase):
self._source_path = '../' self._source_path = '../'
self._dist_path = '../../dist' self._dist_path = '../../dist'
self._publish_settings_model = PublishSettingsModel() self._publish_settings_model = PublishSettings()
self._publish_settings_model.from_dict({ self._publish_settings_model.from_dict({
"SourcePath": self._source_path, "SourcePath": self._source_path,
"DistPath": self._dist_path, "DistPath": self._dist_path,

View File

@ -1,58 +1,30 @@
import unittest import unittest
from collections import Callable from typing import cast
from typing import Type, cast
from sh_edraft.hosting import ApplicationHost from sh_edraft.hosting import ApplicationHost
from sh_edraft.logging import Logger from sh_edraft.logging import Logger
from sh_edraft.logging.base import LoggerBase from sh_edraft.logging.base import LoggerBase
from sh_edraft.logging.model import LoggingSettings
from sh_edraft.publishing import Publisher from sh_edraft.publishing import Publisher
from sh_edraft.publishing.base import PublisherBase from sh_edraft.publishing.base import PublisherBase
from sh_edraft.publishing.model import PublishSettingsModel
from sh_edraft.service import ServiceProvider from sh_edraft.service import ServiceProvider
from sh_edraft.service.base import ServiceBase from sh_edraft.service.base import ServiceBase
from sh_edraft.time.model import TimeFormatSettings
class ServiceProviderTest(unittest.TestCase): class ServiceProviderTest(unittest.TestCase):
def setUp(self): def setUp(self):
self._app_host = ApplicationHost('CPL_Test') self._app_host = ApplicationHost()
self._config = self._app_host.configuration self._config = self._app_host.configuration
self._config.create() 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: ServiceProvider = cast(ServiceProvider, self._app_host.services)
self._services.create() self._services.create()
self._log_settings = LoggingSettings()
self._log_settings.from_dict({
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
})
self._config.add_config_by_type(LoggingSettings, self._log_settings)
self._time_format_settings = TimeFormatSettings()
self._time_format_settings.from_dict({
"DateFormat": "%Y-%m-%d",
"TimeFormat": "%H:%M:%S",
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
})
self._config.add_config_by_type(TimeFormatSettings, self._time_format_settings)
self._config.add_config_by_type(ApplicationHost, self._app_host)
self._publish_settings_model = PublishSettingsModel()
self._publish_settings_model.from_dict({
"SourcePath": "../",
"DistPath": "../../dist",
"Templates": [],
"IncludedFiles": [],
"ExcludedFiles": [],
"TemplateEnding": "_template.txt",
})
self._config.add_config_by_type(PublishSettingsModel, self._publish_settings_model)
def _check_general_requirements(self): def _check_general_requirements(self):
self.assertIsNotNone(self._services) self.assertIsNotNone(self._services)
@ -79,7 +51,9 @@ class ServiceProviderTest(unittest.TestCase):
for service_type in self._services._singleton_services: for service_type in self._services._singleton_services:
service = self._services._singleton_services[service_type] service = self._services._singleton_services[service_type]
if service_type == LoggerBase and ( if service_type == LoggerBase and (
isinstance(service, Logger) and isinstance(service, LoggerBase) and isinstance(service, ServiceBase) isinstance(service, Logger) and
isinstance(service, LoggerBase) and
isinstance(service, ServiceBase)
): ):
if not found: if not found:
found = True found = True
@ -90,7 +64,9 @@ class ServiceProviderTest(unittest.TestCase):
for service_type in self._services._singleton_services: for service_type in self._services._singleton_services:
service = self._services._singleton_services[service_type] service = self._services._singleton_services[service_type]
if service_type == PublisherBase and ( if service_type == PublisherBase and (
isinstance(service, Publisher) and isinstance(service, PublisherBase) and isinstance(service, ServiceBase) isinstance(service, Publisher) and
isinstance(service, PublisherBase) and
isinstance(service, ServiceBase)
): ):
if not found: if not found:
found = True found = True
@ -108,8 +84,6 @@ class ServiceProviderTest(unittest.TestCase):
self.assertTrue(isinstance(logger, LoggerBase)) self.assertTrue(isinstance(logger, LoggerBase))
self.assertTrue(isinstance(logger, ServiceBase)) self.assertTrue(isinstance(logger, ServiceBase))
self.assertEqual(logger._log_settings, self._log_settings)
self.assertEqual(logger._time_format_settings, self._time_format_settings)
self.assertEqual(logger._app_runtime, self._app_host.application_runtime) self.assertEqual(logger._app_runtime, self._app_host.application_runtime)
def test_add_scoped(self): def test_add_scoped(self):

View File

@ -1,7 +1,7 @@
import unittest import unittest
from tests.logging.logger import LoggerTest # from tests.logging.logger import LoggerTest
from tests.publishing.publisher import PublisherTest # from tests.publishing.publisher import PublisherTest
from tests.service_providing.service_provider import ServiceProviderTest from tests.service_providing.service_provider import ServiceProviderTest
@ -21,17 +21,17 @@ class Tester:
self._suite.addTest(ServiceProviderTest('test_get_transient')) self._suite.addTest(ServiceProviderTest('test_get_transient'))
# logging # logging
self._suite.addTest(LoggerTest('test_create')) # self._suite.addTest(LoggerTest('test_create'))
self._suite.addTest(LoggerTest('test_header')) # self._suite.addTest(LoggerTest('test_header'))
self._suite.addTest(LoggerTest('test_trace')) # self._suite.addTest(LoggerTest('test_trace'))
self._suite.addTest(LoggerTest('test_debug')) # self._suite.addTest(LoggerTest('test_debug'))
self._suite.addTest(LoggerTest('test_info')) # self._suite.addTest(LoggerTest('test_info'))
self._suite.addTest(LoggerTest('test_warn')) # self._suite.addTest(LoggerTest('test_warn'))
self._suite.addTest(LoggerTest('test_error')) # self._suite.addTest(LoggerTest('test_error'))
self._suite.addTest(LoggerTest('test_fatal')) # self._suite.addTest(LoggerTest('test_fatal'))
# publishing # publishing
self._suite.addTest(PublisherTest('test_create')) # self._suite.addTest(PublisherTest('test_create'))
def start(self): def start(self):
runner = unittest.TextTestRunner() runner = unittest.TextTestRunner()