Commit 01d4eaab authored by Arnaud ROUX's avatar Arnaud ROUX

Add test for avoid duplicate loggers, make tempdir on setup.

parent 17c9383e
......@@ -5,7 +5,7 @@ import os
from django.conf import settings
def get_logger(log_name, create_console_handler=False) -> logging.Logger:
def get_logger(log_name) -> logging.Logger:
"""Return logger object which own two handlers at differents log level."""
os.makedirs(settings.LOG_DIR, exist_ok=True)
......
# -*- coding: utf-8 -*-
import logging
from unittest import mock
import shutil
import tempfile
from django.test import TestCase, override_settings
from django.conf import settings
from hexack.utils.logging import get_logger
@mock.patch('os.makedirs', new=mock.Mock())
class LoggingTestCase(TestCase):
def setUp(self):
self.temporary_folder = tempfile.mkdtemp(prefix='hexack-utils-tests')
settings.LOG_DIR = self.temporary_folder
def tearDown(self):
shutil.rmtree(self.temporary_folder)
def test_get_logger(self):
'''
Test logging using our get_logger().
'''
logger = get_logger('test')
# Two handlers have been created.
self.assertEqual(len(logger.handlers), 2)
self.assertEqual(logger.level, logging.INFO)
with self.assertLogs(logger, level=logging.CRITICAL):
......@@ -29,3 +38,13 @@ class LoggingTestCase(TestCase):
'''
logger = get_logger('debug')
self.assertEqual(logger.level, logging.DEBUG)
def test_loggers_not_duplicate(self):
'''
Twice `get_loger` calls with same name may not create duplicates handlers.
'''
logger1 = get_logger('debug')
handlers1 = [id(handler) for handler in logger1.handlers]
logger2 = get_logger('debug')
handlers2 = [id(handler) for handler in logger2.handlers]
self.assertEqual(handlers1, handlers2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment