Added testing for service provider with unittests. Also fixed small bugs, found while testing.

This commit is contained in:
Sven Heidemann 2020-11-23 22:22:17 +01:00
parent dd65bbb182
commit c7dff4a212
10 changed files with 178 additions and 100 deletions

View File

@ -1,6 +1,10 @@
from abc import abstractmethod from abc import abstractmethod
from typing import Optional
from sh_edraft.service.base import ServiceBase from sh_edraft.configuration.application_host import ApplicationHost
from sh_edraft.logging.model.log_settings import LoggingSettings
from sh_edraft.service.base.service_base import ServiceBase
from sh_edraft.time.model.time_format_settings import TimeFormatSettings
class LoggerBase(ServiceBase): class LoggerBase(ServiceBase):
@ -9,6 +13,10 @@ class LoggerBase(ServiceBase):
def __init__(self): def __init__(self):
ServiceBase.__init__(self) ServiceBase.__init__(self)
self._log_settings: Optional[LoggingSettings] = None
self._time_format_settings: Optional[TimeFormatSettings] = None
self._app_host: Optional[ApplicationHost] = None
@abstractmethod @abstractmethod
def header(self, string: str): pass def header(self, string: str): pass

View File

@ -4,11 +4,8 @@ import traceback
from string import Template from string import Template
from typing import Optional from typing import Optional
from sh_edraft.configuration.application_host import ApplicationHost
from sh_edraft.logging.base.logger_base import LoggerBase from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.logging.model.log_level import LoggingLevel from sh_edraft.logging.model.log_level import LoggingLevel
from sh_edraft.logging.model.log_settings import LoggingSettings
from sh_edraft.time.model.time_format_settings import TimeFormatSettings
from sh_edraft.utils.console import Console from sh_edraft.utils.console import Console
@ -17,10 +14,6 @@ class Logger(LoggerBase):
def __init__(self): def __init__(self):
LoggerBase.__init__(self) LoggerBase.__init__(self)
self._log_settings: Optional[LoggingSettings] = None
self._time_format_settings: Optional[TimeFormatSettings] = None
self._app_host: Optional[ApplicationHost] = None
self._log: Optional[str] = None self._log: Optional[str] = None
self._path: Optional[str] = None self._path: Optional[str] = None
self._level: Optional[LoggingLevel] = None self._level: Optional[LoggingLevel] = None

View File

@ -1,6 +1,9 @@
from abc import abstractmethod from abc import abstractmethod
from typing import Optional
from sh_edraft.service.base import ServiceBase from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.publish.model.template import Template
from sh_edraft.service.base.service_base import ServiceBase
class PublisherBase(ServiceBase): class PublisherBase(ServiceBase):
@ -9,6 +12,14 @@ class PublisherBase(ServiceBase):
def __init__(self): def __init__(self):
ServiceBase.__init__(self) ServiceBase.__init__(self)
self._logger: Optional[LoggerBase] = None
self._source_path: Optional[str] = None
self._dist_path: Optional[str] = None
self._settings: Optional[list[Template]] = None
self._included_files: list[str] = []
self._excluded_files: list[str] = []
@property @property
@abstractmethod @abstractmethod
def source_path(self) -> str: pass def source_path(self) -> str: pass

View File

@ -1,7 +1,6 @@
import os import os
import shutil import shutil
from string import Template as stringTemplate from string import Template as stringTemplate
from typing import Optional
from sh_edraft.logging.base.logger_base import LoggerBase from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.publish.base.publisher_base import PublisherBase from sh_edraft.publish.base.publisher_base import PublisherBase
@ -13,11 +12,6 @@ class Publisher(PublisherBase):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self._logger: Optional[LoggerBase] = None
self._source_path: Optional[str] = None
self._dist_path: Optional[str] = None
self._settings: Optional[list[Template]] = None
self._included_files: list[str] = [] self._included_files: list[str] = []
self._excluded_files: list[str] = [] self._excluded_files: list[str] = []

View File

@ -30,7 +30,7 @@ class ServiceProvider(ServiceProviderBase):
self._transient_services.append(ProvideState(service, args)) self._transient_services.append(ProvideState(service, args))
def add_scoped(self, service: Type[ServiceBase], *args): def add_scoped(self, service: Type[ServiceBase], *args):
self._transient_services.append(ProvideState(service, args)) self._scoped_services.append(ProvideState(service, args))
def add_singleton(self, service: Type[ServiceBase], *args): def add_singleton(self, service: Type[ServiceBase], *args):
for known_service in self._singleton_services: for known_service in self._singleton_services:

View File

@ -1,20 +0,0 @@
from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.publish import Publisher
from sh_edraft.publish.base import PublisherBase
from sh_edraft.service.base import ServiceProviderBase
class ServiceProviderTest:
@staticmethod
def start(services: ServiceProviderBase):
services.add_transient(Publisher, services.get_service(LoggerBase), '../', '../../dist', [])
publisher: Publisher = services.get_service(PublisherBase)
if publisher is None or publisher.source_path != '../' or publisher.dist_path != '../../dist':
raise Exception(f'{__name__}: Invalid value in {Publisher.__name__}')
services.remove_service(PublisherBase)
if services.get_service(PublisherBase) is not None:
raise Exception(f'{__name__}: Service {Publisher.__name__} was not removed')

View File

View File

@ -0,0 +1,13 @@
import unittest
from sh_edraft.service import ServiceProvider
class ServiceProviderCreate(unittest.TestCase):
def test_create(self):
provider = ServiceProvider()
self.assertIsNotNone(provider)
provider.init(())
provider.create()
self.assertIsNotNone(provider)

View File

@ -0,0 +1,130 @@
import os
import unittest
from string import Template
from sh_edraft.configuration import ApplicationHost
from sh_edraft.logging import Logger
from sh_edraft.logging.base import LoggerBase
from sh_edraft.logging.model import LoggingSettings
from sh_edraft.publish import Publisher
from sh_edraft.publish.base import PublisherBase
from sh_edraft.service.base import ServiceBase
from sh_edraft.time.model import TimeFormatSettings
class ServiceProviderServices(unittest.TestCase):
def setUp(self):
self._app_host = ApplicationHost()
self._services = self._app_host.services
self._services.init(())
self._services.create()
self._log_settings = LoggingSettings()
self._log_settings.from_dict({
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
})
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"
})
def _check_general_requirements(self):
self.assertIsNotNone(self._services)
def _check_logger_requirements(self):
self.assertIsNotNone(self._log_settings)
self.assertIsNotNone(self._time_format_settings)
def test_add_singleton(self):
print(f'{__name__}.test_add_singleton:')
self._check_general_requirements()
self._check_logger_requirements()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
self.assertGreater(len(self._services._singleton_services), 0)
found = False
for service in self._services._singleton_services:
if isinstance(service, Logger) and isinstance(service, LoggerBase) and isinstance(service, ServiceBase):
if not found:
found = True
self.assertTrue(found)
def test_get_singleton(self):
print(f'{__name__}.test_get_singleton:')
self._check_general_requirements()
self._check_logger_requirements()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
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._log_settings, self._log_settings)
self.assertEqual(logger._time_format_settings, self._time_format_settings)
self.assertEqual(logger._app_host, self._app_host)
def test_add_scoped(self):
print(f'{__name__}.test_add_scoped:')
self._check_general_requirements()
self._check_logger_requirements()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
self._services.add_scoped(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
self.assertGreater(len(self._services._scoped_services), 0)
def test_get_scoped(self):
print(f'{__name__}.test_get_scoped:')
self._check_general_requirements()
self._check_logger_requirements()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
self._services.add_scoped(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
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._check_logger_requirements()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
self._services.add_transient(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
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._check_logger_requirements()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
self._services.add_transient(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
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,79 +1,28 @@
import os import unittest
import sys
import traceback
from termcolor import colored from tests.service_provider.service_provider_create import ServiceProviderCreate
from tests.service_provider.service_provider_services import ServiceProviderServices
from sh_edraft.configuration import ApplicationHost
from tests.logger import LoggerTest
from tests.publisher import PublisherTest
from tests.service_provider import ServiceProviderTest
class Tester: class Tester:
def __init__(self): def __init__(self):
self._app_host = ApplicationHost() self._suite = unittest.TestSuite()
self._error: bool = False def create(self):
self._suite.addTest(ServiceProviderCreate('test_create'))
@staticmethod self._suite.addTest(ServiceProviderServices('test_add_singleton'))
def disable_print(): self._suite.addTest(ServiceProviderServices('test_get_singleton'))
sys.stdout = open(os.devnull, 'w') self._suite.addTest(ServiceProviderServices('test_add_scoped'))
self._suite.addTest(ServiceProviderServices('test_get_scoped'))
@staticmethod self._suite.addTest(ServiceProviderServices('test_add_transient'))
def enable_print(): self._suite.addTest(ServiceProviderServices('test_get_transient'))
sys.stdout = sys.__stdout__
def success(self, message: str):
self.enable_print()
print(colored(message, 'green'))
self.disable_print()
def failed(self, message: str):
self.enable_print()
print(colored(message, 'red'))
self.disable_print()
def exception(self):
self.enable_print()
print(colored(traceback.format_exc(), 'red'))
self.disable_print()
def create(self): pass
def start(self): def start(self):
if not self._error: unittest.main()
try:
LoggerTest.start(self._app_host)
self.success(f'{LoggerTest.__name__} test succeeded.')
except Exception as e:
self._error = True
self.failed(f'{LoggerTest.__name__} test failed!\n{e}')
self.exception()
if not self._error:
try:
ServiceProviderTest.start(self._app_host.services)
self.success(f'{ServiceProviderTest.__name__} test succeeded.')
except Exception as e:
self._error = True
self.failed(f'{ServiceProviderTest.__name__} test failed!\n{e}')
self.exception()
if not self._error:
try:
PublisherTest.start(self._app_host.services)
self.success(f'{PublisherTest.__name__} test succeeded.')
except Exception as e:
self._error = True
self.failed(f'{PublisherTest.__name__} test failed!\n{e}')
self.exception()
if __name__ == '__main__': if __name__ == '__main__':
tester = Tester() tester = Tester()
tester.create() tester.create()
tester.disable_print()
tester.start() tester.start()
tester.enable_print()