Added first publisher tests, fixed small bugs

This commit is contained in:
Sven Heidemann 2020-11-24 19:53:24 +01:00
parent 45154950ee
commit cd9d4d3c3d
14 changed files with 118 additions and 72 deletions

View File

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

View File

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

View File

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

View File

@ -2,7 +2,7 @@ from abc import abstractmethod
from typing import Optional 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.model.template import Template from sh_edraft.publishing.model.template import Template
from sh_edraft.service.base.service_base import ServiceBase from sh_edraft.service.base.service_base import ServiceBase

View File

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

View File

@ -2,7 +2,7 @@ from typing import Optional
from sh_edraft.source_code.model.version import Version from sh_edraft.source_code.model.version import Version
from sh_edraft.configuration.model import ConfigurationModelBase from sh_edraft.configuration.model import ConfigurationModelBase
from sh_edraft.publish.model.template_enum import TemplateEnum from sh_edraft.publishing.model.template_enum import TemplateEnum
class Template(ConfigurationModelBase): class Template(ConfigurationModelBase):

View File

@ -3,8 +3,8 @@ import shutil
from string import Template as stringTemplate 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.publish.base.publisher_base import PublisherBase from sh_edraft.publishing.base.publisher_base import PublisherBase
from sh_edraft.publish.model.template import Template from sh_edraft.publishing.model.template import Template
class Publisher(PublisherBase): class Publisher(PublisherBase):
@ -66,13 +66,13 @@ class Publisher(PublisherBase):
for t in self._settings: for t in self._settings:
output_template: str = '' output_template: str = ''
if not os.path.isfile(t.template_path): if not os.path.isfile(t.template_path):
raise Exception(f'Template not found: {t.template_path}') self._logger.fatal(__name__, f'Template not found: {t.template_path}')
with open(t.template_path) as template: with open(t.template_path) as template:
t.file_content = template.read() t.file_content = template.read()
template.close() template.close()
if t.file_content == '': if t.file_content == '':
raise Exception(f'Template is empty: {t.template_path}') self._logger.fatal(__name__, f'Template is empty: {t.template_path}')
self._logger.trace(__name__, f'Stopped {__name__}._read_templates') self._logger.trace(__name__, f'Stopped {__name__}._read_templates')

View File

@ -1,50 +0,0 @@
import os
from string import Template
from sh_edraft.configuration import ApplicationHost
from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.logging.logger import Logger
from sh_edraft.logging.model.log_settings import LoggingSettings
from sh_edraft.time.model.time_format_settings import TimeFormatSettings
class LoggerTest:
@staticmethod
def start(app_host: ApplicationHost):
services = app_host.services
log_settings = LoggingSettings()
log_settings.from_dict({
"Path": "logs/",
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
})
time_format_settings = TimeFormatSettings()
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"
})
services.add_singleton(Logger, log_settings, time_format_settings, app_host)
logger: Logger = services.get_service(LoggerBase)
if logger is None:
raise Exception(f'{__name__}: Service is None')
logger.create()
logger.info(__name__, 'test')
if not os.path.isdir(log_settings.path):
raise Exception(f'{__name__}: Log path was not created')
log_file = Template(log_settings.filename).substitute(
date_time_now=app_host.date_time_now.strftime(time_format_settings.date_time_format),
start_time=app_host.start_time.strftime(time_format_settings.date_time_log_format)
)
if not os.path.isfile(log_settings.path + log_file):
raise Exception(f'{__name__}: Log file was not created')

View File

@ -1,11 +1,11 @@
import os import os
from sh_edraft.logging.base.logger_base import LoggerBase from sh_edraft.logging.base.logger_base import LoggerBase
from sh_edraft.publish.base import PublisherBase from sh_edraft.publishing.base import PublisherBase
from sh_edraft.service.base import ServiceProviderBase from sh_edraft.service.base import ServiceProviderBase
from sh_edraft.source_code.model import Version from sh_edraft.source_code.model import Version
from sh_edraft.publish import Publisher from sh_edraft.publishing import Publisher
from sh_edraft.publish.model import Template from sh_edraft.publishing.model import Template
class PublisherTest: class PublisherTest:

View File

View File

@ -0,0 +1,90 @@
import os
import shutil
import unittest
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.publishing import Publisher
from sh_edraft.publishing.base import PublisherBase
from sh_edraft.publishing.model import Template
from sh_edraft.source_code.model import Version
from sh_edraft.time.model import TimeFormatSettings
class PublisherTest(unittest.TestCase):
def _config(self):
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"
})
self._version = Version(2020, 12, 5).to_dict()
self._templates = [
Template(
'../../publish_templates/*_template.txt',
'*',
'',
'',
'2020',
'sh-edraft.de',
'MIT',
', see LICENSE for more details.',
'',
'Sven Heidemann',
self._version
),
Template(
'../../publish_templates/*_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 = '../'
self._dist = '../../dist'
def setUp(self):
self._config()
self._app_host = ApplicationHost()
self._services = self._app_host.services
self._services.init(())
self._services.create()
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
logger: Logger = self._services.get_service(LoggerBase)
logger.create()
def tearDown(self):
if os.path.isdir(self._log_settings.path):
shutil.rmtree(self._log_settings.path)
def test_create(self):
self._services.add_transient(Publisher, self._services.get_service(LoggerBase), self._source, self._dist, self._templates)
publisher: Publisher = self._services.get_service(PublisherBase)
self.assertIsNotNone(publisher)
publisher.create()
self.assertTrue(os.path.isdir(self._dist))

View File

@ -4,8 +4,8 @@ from sh_edraft.configuration 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.logging.model import LoggingSettings
from sh_edraft.publish import Publisher from sh_edraft.publishing import Publisher
from sh_edraft.publish.base import PublisherBase from sh_edraft.publishing.base import PublisherBase
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 from sh_edraft.time.model import TimeFormatSettings
@ -42,6 +42,11 @@ class ServiceProviderTest(unittest.TestCase):
self.assertIsNotNone(self._log_settings) self.assertIsNotNone(self._log_settings)
self.assertIsNotNone(self._time_format_settings) self.assertIsNotNone(self._time_format_settings)
def _add_logger(self):
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host)
logger: Logger = self._services.get_service(LoggerBase)
logger.create()
def test_create(self): def test_create(self):
provider = ServiceProvider() provider = ServiceProvider()
self.assertIsNotNone(provider) self.assertIsNotNone(provider)
@ -88,14 +93,13 @@ class ServiceProviderTest(unittest.TestCase):
self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host) 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._services.add_scoped(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
self.assertGreater(len(self._services._scoped_services), 0)
def test_get_scoped(self): def test_get_scoped(self):
print(f'{__name__}.test_get_scoped:') print(f'{__name__}.test_get_scoped:')
self._check_general_requirements() self._check_general_requirements()
self._check_logger_requirements() self._check_logger_requirements()
self._add_logger()
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._services.add_scoped(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
publisher: Publisher = self._services.get_service(PublisherBase) publisher: Publisher = self._services.get_service(PublisherBase)
self.assertIsNotNone(publisher) self.assertIsNotNone(publisher)
@ -111,8 +115,8 @@ class ServiceProviderTest(unittest.TestCase):
print(f'{__name__}.test_add_transient:') print(f'{__name__}.test_add_transient:')
self._check_general_requirements() self._check_general_requirements()
self._check_logger_requirements() self._check_logger_requirements()
self._add_logger()
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._services.add_transient(Publisher, self._services.get_service(LoggerBase), '../', '../../dist', [])
self.assertGreater(len(self._services._transient_services), 0) self.assertGreater(len(self._services._transient_services), 0)

View File

@ -1,6 +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.service_providing.service_provider import ServiceProviderTest from tests.service_providing.service_provider import ServiceProviderTest
@ -30,6 +31,7 @@ class Tester:
self._suite.addTest(LoggerTest('test_fatal')) self._suite.addTest(LoggerTest('test_fatal'))
# publishing # publishing
self._suite.addTest(PublisherTest('test_create'))
def start(self): def start(self):
runner = unittest.TextTestRunner() runner = unittest.TextTestRunner()