Merge pull request #69 feature-NEWDWH2021-643-logger into develop-6crm

This commit is contained in:
坂井 祐輔 2022-08-17 16:28:51 +09:00
commit ed1da4b4df
2 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,78 @@
import logging
from logging import handlers
from unittest.mock import MagicMock, patch
from src.util.logger import Logger
class TestLogger:
def test_constructor_loglevel_test(self, caplog, monkeypatch):
"""
Cases:
ログレベルに対象外の文字列を指定した場合デフォルトであるInfoが指定されること
Arranges:
- LOG_LEVELの変数を想定外の文字列に置き換える
- loggerインスタンスの生成
- ログ出力
Expects:
- 期待値通りのログが出力されること
- ログレベルがInfoで設定されていること
"""
# Arranges
monkeypatch.setattr('src.util.logger.LOG_LEVEL', "test")
sut = Logger()
logger = sut.get_logger()
logger.info('infoログ出力')
# Expects
assert ("root", logging.INFO, "infoログ出力") in caplog.record_tuples
assert logger.getEffectiveLevel() == logging.INFO
def test_constructor_hashandlers(self, monkeypatch):
"""
Cases:
生成したloggerインスタンスを利用し期待値通りのログが出力されること
Arranges:
- `if not self.__logger.hasHandlers():`の分岐に入るようにモックを準備
- loggerインスタンスの生成
- ログ出力
Expects:
- loggerインスタンスに1件以上のハンドラーが存在すること
- loggerインスタンスにStreamHandlerが含まれていること
"""
# Arranges
def dummy_method(arg):
return False
monkeypatch.setattr("logging.Logger.hasHandlers", dummy_method)
sut = Logger()
logger = sut.get_logger()
logger.warning('warningログ出力')
# Expects
assert len(logger.handlers) >= 1
assert "StreamHandler" in str(logger.handlers)
def test_get_logger(self, caplog):
"""
Cases:
生成したloggerインスタンスを利用し期待値通りのログが出力されること
Arranges:
- loggerインスタンスの生成
- ログ出力
Expects:
- 期待値通りのログが出力されること
"""
# Arranges
sut = Logger()
logger = sut.get_logger()
logger.warning('warningログ出力')
# Expects
assert ("root", logging.WARNING, "warningログ出力") in caplog.record_tuples

View File

@ -0,0 +1,193 @@
import logging
from src.util.logger import logger_instance as logger
class TestLogger:
def test_logging_debug(self, caplog):
"""
Cases:
Debugレベルを設定したloggerについてそれ以下のログが出力されないこと
Arranges:
- 各レベルでのログを出力する
- ログレベルの設定
Expects:
- 期待値通りのログが出力されること
"""
# Arranges
caplog.set_level(logging.DEBUG)
logger.debug('debugログ出力')
logger.info('infoログ出力')
logger.warning("warningログ出力")
logger.error("errorログ出力")
logger.critical("criticalログ出力")
# Expects
assert ("root", logging.DEBUG, "debugログ出力") in caplog.record_tuples
assert ("root", logging.INFO, "infoログ出力") in caplog.record_tuples
assert ("root", logging.WARNING, "warningログ出力") in caplog.record_tuples
assert ("root", logging.ERROR, "errorログ出力") in caplog.record_tuples
assert ("root", logging.CRITICAL, "criticalログ出力") in caplog.record_tuples
def test_logging_info(self, caplog):
"""
Cases:
Infoレベルを設定したloggerについてそれ以下のログが出力されないこと
Arranges:
- 各レベルでのログを出力する
- ログレベルの設定
Expects:
- 期待値通りのログが出力されること
"""
# Arranges
caplog.set_level(logging.INFO)
logger.debug('debugログ出力')
logger.info('infoログ出力')
logger.warning("warningログ出力")
logger.error("errorログ出力")
logger.critical("criticalログ出力")
# Expects
assert ("root", logging.DEBUG, "debugログ出力") not in caplog.record_tuples
assert ("root", logging.INFO, "infoログ出力") in caplog.record_tuples
assert ("root", logging.WARNING, "warningログ出力") in caplog.record_tuples
assert ("root", logging.ERROR, "errorログ出力") in caplog.record_tuples
assert ("root", logging.CRITICAL, "criticalログ出力") in caplog.record_tuples
def test_logging_warning(self, caplog):
"""
Cases:
Warningレベルを設定したloggerについてそれ以下のログが出力されないこと
Arranges:
- 各レベルでのログを出力する
- ログレベルの設定
Expects:
- 期待値通りのログが出力されること
"""
# Arranges
caplog.set_level(logging.WARNING)
logger.debug('debugログ出力')
logger.info('infoログ出力')
logger.warning("warningログ出力")
logger.error("errorログ出力")
logger.critical("criticalログ出力")
# Expects
assert ("root", logging.DEBUG, "debugログ出力") not in caplog.record_tuples
assert ("root", logging.INFO, "infoログ出力") not in caplog.record_tuples
assert ("root", logging.WARNING, "warningログ出力") in caplog.record_tuples
assert ("root", logging.ERROR, "errorログ出力") in caplog.record_tuples
assert ("root", logging.CRITICAL, "criticalログ出力") in caplog.record_tuples
def test_logging_error(self, caplog):
"""
Cases:
Errorレベルを設定したloggerについてそれ以下のログが出力されないこと
Arranges:
- 各レベルでのログを出力する
- ログレベルの設定
Expects:
- 期待値通りのログが出力されること
"""
# Arranges
caplog.set_level(logging.ERROR)
logger.debug('debugログ出力')
logger.info('infoログ出力')
logger.warning("warningログ出力")
logger.error("errorログ出力")
logger.critical("criticalログ出力")
# Expects
assert ("root", logging.DEBUG, "debugログ出力") not in caplog.record_tuples
assert ("root", logging.INFO, "infoログ出力") not in caplog.record_tuples
assert ("root", logging.WARNING, "warningログ出力") not in caplog.record_tuples
assert ("root", logging.ERROR, "errorログ出力") in caplog.record_tuples
assert ("root", logging.CRITICAL, "criticalログ出力") in caplog.record_tuples
def test_logging_critical(self, caplog):
"""
Cases:
Criticalレベルを設定したloggerについてそれ以下のログが出力されないこと
Arranges:
- 各レベルでのログを出力する
- ログレベルの設定
Expects:
- 期待値通りのログが出力されること
"""
# Arranges
caplog.set_level(logging.CRITICAL)
logger.debug('debugログ出力')
logger.info('infoログ出力')
logger.warning("warningログ出力")
logger.error("errorログ出力")
logger.critical("criticalログ出力")
# Expects
assert ("root", logging.DEBUG, "debugログ出力") not in caplog.record_tuples
assert ("root", logging.INFO, "infoログ出力") not in caplog.record_tuples
assert ("root", logging.WARNING, "warningログ出力") not in caplog.record_tuples
assert ("root", logging.ERROR, "errorログ出力") not in caplog.record_tuples
assert ("root", logging.CRITICAL, "criticalログ出力") in caplog.record_tuples
def test_logging_getlogger_boto3(self):
"""
Cases:
個別設定したboto3モジュールのログレベル確認
Arranges:
なし
Expects:
設定されているログレベルが期待値と一致すること
"""
# Expects
assert logging.getLogger("boto3").getEffectiveLevel() == logging.WARNING
def test_logging_getlogger_botocore(self):
"""
Cases:
個別設定したbotocoreモジュールのログレベル確認
Arranges:
なし
Expects:
設定されているログレベルが期待値と一致すること
"""
# Expects
assert logging.getLogger("botocore").getEffectiveLevel() == logging.WARNING
def test_logging_getlogger_s3transfer(self):
"""
Cases:
個別設定したs3transferモジュールのログレベル確認
Arranges:
なし
Expects:
設定されているログレベルが期待値と一致すること
"""
# Expects
assert logging.getLogger("s3transfer").getEffectiveLevel() == logging.WARNING
def test_logging_getlogger_urllib3(self):
"""
Cases:
個別設定したurllib3モジュールのログレベル確認
Arranges:
なし
Expects:
設定されているログレベルが期待値と一致すること
"""
# Expects
assert logging.getLogger("s3transfer").getEffectiveLevel() == logging.WARNING