Improved testing

This commit is contained in:
Sven Heidemann 2020-11-29 17:29:16 +01:00
parent fea84418d3
commit 16516b7fe6
19 changed files with 173 additions and 27 deletions

View File

@ -7,6 +7,7 @@ from sh_edraft.configuration.base.configuration_base import ConfigurationBase
from sh_edraft.configuration.model.configuration_variable_name import ConfigurationVariableName
from sh_edraft.environment.base.environment_base import EnvironmentBase
from sh_edraft.environment.hosting_environment import HostingEnvironment
from sh_edraft.environment.model import EnvironmentName
from sh_edraft.utils import Console
@ -36,7 +37,7 @@ class Configuration(ConfigurationBase):
def _set_variable(self, name: str, value: str):
if name == ConfigurationVariableName.environment.value:
self._hosting_environment.environment_name = value
self._hosting_environment.environment_name = EnvironmentName(value)
elif name == ConfigurationVariableName.name.value:
self._hosting_environment.application_name = value

View File

@ -1,7 +1,5 @@
from abc import ABC, abstractmethod
from sh_edraft.environment.model.environment_name import EnvironmentName
class EnvironmentBase(ABC):
@ -10,11 +8,11 @@ class EnvironmentBase(ABC):
@property
@abstractmethod
def environment_name(self) -> EnvironmentName: pass
def environment_name(self) -> str: pass
@environment_name.setter
@abstractmethod
def environment_name(self, environment_name: EnvironmentName): pass
def environment_name(self, environment_name: str): pass
@property
@abstractmethod

View File

@ -16,12 +16,12 @@ class HostingEnvironment(EnvironmentBase):
self._content_root_path: Optional[str] = crp
@property
def environment_name(self) -> EnvironmentName:
return self._environment_name
def environment_name(self) -> str:
return str(self._environment_name.value)
@environment_name.setter
def environment_name(self, environment_name: EnvironmentName):
self._environment_name = environment_name
def environment_name(self, environment_name: str):
self._environment_name = EnvironmentName(environment_name)
@property
def application_name(self) -> str:

View File

@ -1,4 +1,3 @@
import sys
from datetime import datetime
from sh_edraft.configuration.configuration import Configuration

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
sh_edraft.service
sh_edraft.services
~~~~~~~~~~~~~~~~~~~
@ -11,7 +11,7 @@ sh_edraft.service
"""
__title__ = 'sh_edraft.service'
__title__ = 'sh_edraft.services'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
sh_edraft.service.base
sh_edraft.services.base
~~~~~~~~~~~~~~~~~~~
@ -11,7 +11,7 @@ sh_edraft.service.base
"""
__title__ = 'sh_edraft.service.base'
__title__ = 'sh_edraft.services.base'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""
sh_edraft.service.base
sh_edraft.services.base
~~~~~~~~~~~~~~~~~~~
@ -11,7 +11,7 @@ sh_edraft.service.base
"""
__title__ = 'sh_edraft.service.base'
__title__ = 'sh_edraft.services.base'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'

View File

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

View File

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

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

View File

View File

@ -83,6 +83,7 @@ class PublisherTest(unittest.TestCase):
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)

View File

@ -1,7 +1,9 @@
import unittest
from tests.logging.logger import LoggerTest
from tests.publishing.publisher import PublisherTest
from tests.configuration.config import ConfigTest
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
@ -11,6 +13,18 @@ class Tester:
self._suite = unittest.TestSuite()
def create(self):
# hosting app host
self._suite.addTest(AppHostTest('test_create'))
# 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'))

View File

@ -14,14 +14,14 @@ class Program(ApplicationBase):
ApplicationBase.__init__(self)
self._app_host: Optional[ApplicationHost] = None
self._services: Optional[ServiceProviderBase] = None
self._configuration: Optional[ConfigurationBase] = None
self._logger: Optional[LoggerBase] = None
def create_application_host(self):
self._app_host = ApplicationHost()
self._services = self._app_host.services
self._configuration = self._app_host.configuration
self._services = self._app_host.services
def create_configuration(self):
self._configuration.create()
@ -35,12 +35,11 @@ class Program(ApplicationBase):
def create_services(self):
self._services.create()
self._services.add_singleton(LoggerBase, Logger)
logger: Logger = self._services.get_service(LoggerBase)
logger.create()
logger.header(f'{self._configuration.environment.application_name}:')
logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
self._logger = self._services.get_service(LoggerBase)
self._logger.create()
def main(self):
print('RUN')
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}')