From cd9d4d3c3d7ab7e4afeedd9bd5c8519742bddac5 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 24 Nov 2020 19:53:24 +0100 Subject: [PATCH] Added first publisher tests, fixed small bugs --- .../{messenger => messaging}/__init__.py | 4 +- .../{publish => publishing}/__init__.py | 4 +- .../{publish => publishing}/base/__init__.py | 4 +- .../base/publisher_base.py | 2 +- .../{publish => publishing}/model/__init__.py | 4 +- .../{publish => publishing}/model/template.py | 2 +- .../model/template_enum.py | 0 .../{publish => publishing}/publisher.py | 8 +- src/tests/logger.py | 50 ----------- src/tests/publisher.py | 6 +- src/tests/publishing/__init__.py | 0 src/tests/publishing/publisher.py | 90 +++++++++++++++++++ .../service_providing/service_provider.py | 14 +-- src/tests/tester.py | 2 + 14 files changed, 118 insertions(+), 72 deletions(-) rename src/sh_edraft/{messenger => messaging}/__init__.py (88%) rename src/sh_edraft/{publish => publishing}/__init__.py (89%) rename src/sh_edraft/{publish => publishing}/base/__init__.py (87%) rename src/sh_edraft/{publish => publishing}/base/publisher_base.py (93%) rename src/sh_edraft/{publish => publishing}/model/__init__.py (88%) rename src/sh_edraft/{publish => publishing}/model/template.py (98%) rename src/sh_edraft/{publish => publishing}/model/template_enum.py (100%) rename src/sh_edraft/{publish => publishing}/publisher.py (96%) delete mode 100644 src/tests/logger.py create mode 100644 src/tests/publishing/__init__.py create mode 100644 src/tests/publishing/publisher.py diff --git a/src/sh_edraft/messenger/__init__.py b/src/sh_edraft/messaging/__init__.py similarity index 88% rename from src/sh_edraft/messenger/__init__.py rename to src/sh_edraft/messaging/__init__.py index 8d501a96..db61154c 100644 --- a/src/sh_edraft/messenger/__init__.py +++ b/src/sh_edraft/messaging/__init__.py @@ -1,7 +1,7 @@ # -*- 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' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 sh-edraft.de' diff --git a/src/sh_edraft/publish/__init__.py b/src/sh_edraft/publishing/__init__.py similarity index 89% rename from src/sh_edraft/publish/__init__.py rename to src/sh_edraft/publishing/__init__.py index 7b162462..3147018a 100644 --- a/src/sh_edraft/publish/__init__.py +++ b/src/sh_edraft/publishing/__init__.py @@ -1,7 +1,7 @@ # -*- 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' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 sh-edraft.de' diff --git a/src/sh_edraft/publish/base/__init__.py b/src/sh_edraft/publishing/base/__init__.py similarity index 87% rename from src/sh_edraft/publish/base/__init__.py rename to src/sh_edraft/publishing/base/__init__.py index a7e178d1..041cfa8f 100644 --- a/src/sh_edraft/publish/base/__init__.py +++ b/src/sh_edraft/publishing/base/__init__.py @@ -1,7 +1,7 @@ # -*- 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' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 sh-edraft.de' diff --git a/src/sh_edraft/publish/base/publisher_base.py b/src/sh_edraft/publishing/base/publisher_base.py similarity index 93% rename from src/sh_edraft/publish/base/publisher_base.py rename to src/sh_edraft/publishing/base/publisher_base.py index 2a2bea92..0bc6a369 100644 --- a/src/sh_edraft/publish/base/publisher_base.py +++ b/src/sh_edraft/publishing/base/publisher_base.py @@ -2,7 +2,7 @@ from abc import abstractmethod from typing import Optional 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 diff --git a/src/sh_edraft/publish/model/__init__.py b/src/sh_edraft/publishing/model/__init__.py similarity index 88% rename from src/sh_edraft/publish/model/__init__.py rename to src/sh_edraft/publishing/model/__init__.py index dbc1c3b5..b18feff0 100644 --- a/src/sh_edraft/publish/model/__init__.py +++ b/src/sh_edraft/publishing/model/__init__.py @@ -1,7 +1,7 @@ # -*- 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' __license__ = 'MIT' __copyright__ = 'Copyright (c) 2020 sh-edraft.de' diff --git a/src/sh_edraft/publish/model/template.py b/src/sh_edraft/publishing/model/template.py similarity index 98% rename from src/sh_edraft/publish/model/template.py rename to src/sh_edraft/publishing/model/template.py index d917374b..05a37f31 100644 --- a/src/sh_edraft/publish/model/template.py +++ b/src/sh_edraft/publishing/model/template.py @@ -2,7 +2,7 @@ from typing import Optional from sh_edraft.source_code.model.version import Version 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): diff --git a/src/sh_edraft/publish/model/template_enum.py b/src/sh_edraft/publishing/model/template_enum.py similarity index 100% rename from src/sh_edraft/publish/model/template_enum.py rename to src/sh_edraft/publishing/model/template_enum.py diff --git a/src/sh_edraft/publish/publisher.py b/src/sh_edraft/publishing/publisher.py similarity index 96% rename from src/sh_edraft/publish/publisher.py rename to src/sh_edraft/publishing/publisher.py index 1b371ed6..43cf8d53 100644 --- a/src/sh_edraft/publish/publisher.py +++ b/src/sh_edraft/publishing/publisher.py @@ -3,8 +3,8 @@ import shutil from string import Template as stringTemplate from sh_edraft.logging.base.logger_base import LoggerBase -from sh_edraft.publish.base.publisher_base import PublisherBase -from sh_edraft.publish.model.template import Template +from sh_edraft.publishing.base.publisher_base import PublisherBase +from sh_edraft.publishing.model.template import Template class Publisher(PublisherBase): @@ -66,13 +66,13 @@ class Publisher(PublisherBase): for t in self._settings: output_template: str = '' 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: t.file_content = template.read() template.close() 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') diff --git a/src/tests/logger.py b/src/tests/logger.py deleted file mode 100644 index 47b5e3ab..00000000 --- a/src/tests/logger.py +++ /dev/null @@ -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') diff --git a/src/tests/publisher.py b/src/tests/publisher.py index 73923471..086299ce 100644 --- a/src/tests/publisher.py +++ b/src/tests/publisher.py @@ -1,11 +1,11 @@ import os 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.source_code.model import Version -from sh_edraft.publish import Publisher -from sh_edraft.publish.model import Template +from sh_edraft.publishing import Publisher +from sh_edraft.publishing.model import Template class PublisherTest: diff --git a/src/tests/publishing/__init__.py b/src/tests/publishing/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/publishing/publisher.py b/src/tests/publishing/publisher.py new file mode 100644 index 00000000..bf77749c --- /dev/null +++ b/src/tests/publishing/publisher.py @@ -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)) diff --git a/src/tests/service_providing/service_provider.py b/src/tests/service_providing/service_provider.py index ed590490..255b77ce 100644 --- a/src/tests/service_providing/service_provider.py +++ b/src/tests/service_providing/service_provider.py @@ -4,8 +4,8 @@ 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.publishing import Publisher +from sh_edraft.publishing.base import PublisherBase from sh_edraft.service import ServiceProvider from sh_edraft.service.base import ServiceBase from sh_edraft.time.model import TimeFormatSettings @@ -42,6 +42,11 @@ class ServiceProviderTest(unittest.TestCase): self.assertIsNotNone(self._log_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): provider = ServiceProvider() 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_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._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', []) publisher: Publisher = self._services.get_service(PublisherBase) self.assertIsNotNone(publisher) @@ -111,8 +115,8 @@ class ServiceProviderTest(unittest.TestCase): print(f'{__name__}.test_add_transient:') self._check_general_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.assertGreater(len(self._services._transient_services), 0) diff --git a/src/tests/tester.py b/src/tests/tester.py index 1d851ac5..dfb11e49 100644 --- a/src/tests/tester.py +++ b/src/tests/tester.py @@ -1,6 +1,7 @@ import unittest from tests.logging.logger import LoggerTest +from tests.publishing.publisher import PublisherTest from tests.service_providing.service_provider import ServiceProviderTest @@ -30,6 +31,7 @@ class Tester: self._suite.addTest(LoggerTest('test_fatal')) # publishing + self._suite.addTest(PublisherTest('test_create')) def start(self): runner = unittest.TextTestRunner()