Added first unittests for logging

This commit is contained in:
Sven Heidemann 2020-11-23 22:47:37 +01:00
parent c7dff4a212
commit 09a3fb166f
6 changed files with 126 additions and 25 deletions

100
src/tests/logging/logger.py Normal file
View File

@ -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'))

View File

@ -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)

View File

View File

@ -1,6 +1,4 @@
import os
import unittest import unittest
from string import Template
from sh_edraft.configuration import ApplicationHost from sh_edraft.configuration import ApplicationHost
from sh_edraft.logging import Logger 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.logging.model import LoggingSettings
from sh_edraft.publish import Publisher from sh_edraft.publish import Publisher
from sh_edraft.publish.base import PublisherBase from sh_edraft.publish.base import PublisherBase
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
class ServiceProviderServices(unittest.TestCase): class ServiceProviderTest(unittest.TestCase):
def setUp(self): def setUp(self):
self._app_host = ApplicationHost() self._app_host = ApplicationHost()
@ -43,6 +42,13 @@ class ServiceProviderServices(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 test_create(self):
provider = ServiceProvider()
self.assertIsNotNone(provider)
provider.init(())
provider.create()
self.assertIsNotNone(provider)
def test_add_singleton(self): def test_add_singleton(self):
print(f'{__name__}.test_add_singleton:') print(f'{__name__}.test_add_singleton:')
self._check_general_requirements() self._check_general_requirements()

View File

@ -1,7 +1,7 @@
import unittest import unittest
from tests.service_provider.service_provider_create import ServiceProviderCreate from tests.logging.logger import LoggerTest
from tests.service_provider.service_provider_services import ServiceProviderServices from tests.service_providing.service_provider import ServiceProviderTest
class Tester: class Tester:
@ -10,13 +10,21 @@ class Tester:
self._suite = unittest.TestSuite() self._suite = unittest.TestSuite()
def create(self): def create(self):
self._suite.addTest(ServiceProviderCreate('test_create')) # providing
self._suite.addTest(ServiceProviderServices('test_add_singleton')) self._suite.addTest(ServiceProviderTest('test_create'))
self._suite.addTest(ServiceProviderServices('test_get_singleton')) self._suite.addTest(ServiceProviderTest('test_add_singleton'))
self._suite.addTest(ServiceProviderServices('test_add_scoped')) self._suite.addTest(ServiceProviderTest('test_get_singleton'))
self._suite.addTest(ServiceProviderServices('test_get_scoped')) self._suite.addTest(ServiceProviderTest('test_add_scoped'))
self._suite.addTest(ServiceProviderServices('test_add_transient')) self._suite.addTest(ServiceProviderTest('test_get_scoped'))
self._suite.addTest(ServiceProviderServices('test_get_transient')) 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): def start(self):
unittest.main() unittest.main()