From d9c4268a024f14db618021f2e00048f417f08464 Mon Sep 17 00:00:00 2001 From: Y_SAKAI Date: Tue, 16 Aug 2022 18:17:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20logger=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=81=AE=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/util/test_logger_logger_class.py | 78 +++++++++++++++++++ .../tests/util/test_logger_out_of_class.py | 52 ++++++++++++- 2 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 ecs/crm-datafetch/tests/util/test_logger_logger_class.py diff --git a/ecs/crm-datafetch/tests/util/test_logger_logger_class.py b/ecs/crm-datafetch/tests/util/test_logger_logger_class.py new file mode 100644 index 00000000..172b7cf7 --- /dev/null +++ b/ecs/crm-datafetch/tests/util/test_logger_logger_class.py @@ -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 diff --git a/ecs/crm-datafetch/tests/util/test_logger_out_of_class.py b/ecs/crm-datafetch/tests/util/test_logger_out_of_class.py index b5a54f01..170516ef 100644 --- a/ecs/crm-datafetch/tests/util/test_logger_out_of_class.py +++ b/ecs/crm-datafetch/tests/util/test_logger_out_of_class.py @@ -1,7 +1,5 @@ import logging -from unittest.mock import MagicMock, patch -import pytest from src.util.logger import logger_instance as logger @@ -18,6 +16,7 @@ class TestLogger: - 期待値通りのログが出力されること """ + # Arranges caplog.set_level(logging.DEBUG) logger.debug('debugログ出力') @@ -26,6 +25,7 @@ class TestLogger: 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 @@ -43,6 +43,7 @@ class TestLogger: - 期待値通りのログが出力されること """ + # Arranges caplog.set_level(logging.INFO) logger.debug('debugログ出力') @@ -51,6 +52,7 @@ class TestLogger: 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 @@ -68,6 +70,7 @@ class TestLogger: - 期待値通りのログが出力されること """ + # Arranges caplog.set_level(logging.WARNING) logger.debug('debugログ出力') @@ -76,6 +79,7 @@ class TestLogger: 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 @@ -93,6 +97,7 @@ class TestLogger: - 期待値通りのログが出力されること """ + # Arranges caplog.set_level(logging.ERROR) logger.debug('debugログ出力') @@ -101,6 +106,7 @@ class TestLogger: 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 @@ -118,6 +124,7 @@ class TestLogger: - 期待値通りのログが出力されること """ + # Arranges caplog.set_level(logging.CRITICAL) logger.debug('debugログ出力') @@ -126,6 +133,7 @@ class TestLogger: 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 @@ -133,13 +141,53 @@ class TestLogger: 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