diff --git a/src/tests/service_provider/__init__.py b/src/tests/logging/__init__.py similarity index 100% rename from src/tests/service_provider/__init__.py rename to src/tests/logging/__init__.py diff --git a/src/tests/logging/logger.py b/src/tests/logging/logger.py new file mode 100644 index 00000000..c7332093 --- /dev/null +++ b/src/tests/logging/logger.py @@ -0,0 +1,100 @@ +import os +import unittest +from datetime import datetime +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.time.model import TimeFormatSettings + + +class LoggerTest(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" + }) + + self._services.add_singleton(Logger, self._log_settings, self._time_format_settings, self._app_host) + + def _check_general_requirements(self): + self.assertIsNotNone(self._services) + self.assertIsNotNone(self._log_settings) + self.assertIsNotNone(self._time_format_settings) + + def test_create(self): + print(f'{__name__}.test_create:') + logger: Logger = self._services.get_service(LoggerBase) + self.assertIsNotNone(logger) + + logger.create() + self.assertTrue(os.path.isdir(self._log_settings.path)) + + log_file = Template(self._log_settings.filename).substitute( + date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format), + start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format) + ) + self.assertTrue(os.path.isfile(self._log_settings.path + log_file)) + + def test_header(self): + print(f'{__name__}.test_header:') + logger: Logger = self._services.get_service(LoggerBase) + logger.create() + logger.header('HeaderTest:') + + log_file = Template(self._log_settings.filename).substitute( + date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format), + start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format) + ) + log_content = [] + + try: + with open(self._log_settings.path + log_file, "r") as log: + log_content = log.readlines() + log.close() + except Exception as e: + print('Cannot open log file', e) + + self.assertGreater(len(log_content), 0) + self.assertEqual(log_content[len(log_content) - 1], 'HeaderTest:\n') + + def test_trace(self): + print(f'{__name__}.test_trace:') + logger: Logger = self._services.get_service(LoggerBase) + logger.create() + logger.trace(__name__, f'{__name__}.test_trace:') + + log_file = Template(self._log_settings.filename).substitute( + date_time_now=self._app_host.date_time_now.strftime(self._time_format_settings.date_time_format), + start_time=self._app_host.start_time.strftime(self._time_format_settings.date_time_log_format) + ) + log_content = [] + + try: + with open(self._log_settings.path + log_file, "r") as log: + log_content = log.readlines() + log.close() + except Exception as e: + print('Cannot open log file', e) + + self.assertGreater(len(log_content), 0) + self.assertTrue(log_content[len(log_content) - 1].endswith(f'[ TRACE ] [ {__name__} ]: {__name__}.test_trace:\n')) diff --git a/src/tests/service_provider/service_provider_create.py b/src/tests/service_provider/service_provider_create.py deleted file mode 100644 index 870b5d14..00000000 --- a/src/tests/service_provider/service_provider_create.py +++ /dev/null @@ -1,13 +0,0 @@ -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) diff --git a/src/tests/service_providing/__init__.py b/src/tests/service_providing/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/service_provider/service_provider_services.py b/src/tests/service_providing/service_provider.py similarity index 95% rename from src/tests/service_provider/service_provider_services.py rename to src/tests/service_providing/service_provider.py index 5e65a2ea..ed590490 100644 --- a/src/tests/service_provider/service_provider_services.py +++ b/src/tests/service_providing/service_provider.py @@ -1,6 +1,4 @@ -import os import unittest -from string import Template from sh_edraft.configuration import ApplicationHost from sh_edraft.logging import Logger @@ -8,11 +6,12 @@ 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 import ServiceProvider from sh_edraft.service.base import ServiceBase from sh_edraft.time.model import TimeFormatSettings -class ServiceProviderServices(unittest.TestCase): +class ServiceProviderTest(unittest.TestCase): def setUp(self): self._app_host = ApplicationHost() @@ -43,6 +42,13 @@ class ServiceProviderServices(unittest.TestCase): self.assertIsNotNone(self._log_settings) self.assertIsNotNone(self._time_format_settings) + def test_create(self): + provider = ServiceProvider() + self.assertIsNotNone(provider) + provider.init(()) + provider.create() + self.assertIsNotNone(provider) + def test_add_singleton(self): print(f'{__name__}.test_add_singleton:') self._check_general_requirements() diff --git a/src/tests/tester.py b/src/tests/tester.py index 234ad59a..534572db 100644 --- a/src/tests/tester.py +++ b/src/tests/tester.py @@ -1,7 +1,7 @@ import unittest -from tests.service_provider.service_provider_create import ServiceProviderCreate -from tests.service_provider.service_provider_services import ServiceProviderServices +from tests.logging.logger import LoggerTest +from tests.service_providing.service_provider import ServiceProviderTest class Tester: @@ -10,13 +10,21 @@ class Tester: self._suite = unittest.TestSuite() def create(self): - self._suite.addTest(ServiceProviderCreate('test_create')) - self._suite.addTest(ServiceProviderServices('test_add_singleton')) - self._suite.addTest(ServiceProviderServices('test_get_singleton')) - self._suite.addTest(ServiceProviderServices('test_add_scoped')) - self._suite.addTest(ServiceProviderServices('test_get_scoped')) - self._suite.addTest(ServiceProviderServices('test_add_transient')) - self._suite.addTest(ServiceProviderServices('test_get_transient')) + # providing + self._suite.addTest(ServiceProviderTest('test_create')) + self._suite.addTest(ServiceProviderTest('test_add_singleton')) + self._suite.addTest(ServiceProviderTest('test_get_singleton')) + self._suite.addTest(ServiceProviderTest('test_add_scoped')) + self._suite.addTest(ServiceProviderTest('test_get_scoped')) + self._suite.addTest(ServiceProviderTest('test_add_transient')) + self._suite.addTest(ServiceProviderTest('test_get_transient')) + + # logging + self._suite.addTest(LoggerTest('test_create')) + self._suite.addTest(LoggerTest('test_header')) + self._suite.addTest(LoggerTest('test_trace')) + + # publishing def start(self): unittest.main()