From 8a7214ef58793ba1782a000327d63fc89e47412c Mon Sep 17 00:00:00 2001 From: MEHRSHAD MIRSHEKARY Date: Fri, 4 Oct 2024 17:41:14 +0330 Subject: [PATCH] :zap::white_check_mark::rotating_light: tests(utils): Add process_file tests & Update tests --- .../tests/utils/test_context_manager.py | 6 +- .../tests/utils/test_email_notifier.py | 6 +- django_logging/tests/utils/test_get_conf.py | 68 ++++++++++++------- .../tests/utils/test_log_and_notify.py | 7 +- .../tests/utils/test_process_file.py | 56 +++++++++++++++ django_logging/tests/utils/test_set_conf.py | 14 +++- 6 files changed, 128 insertions(+), 29 deletions(-) create mode 100644 django_logging/tests/utils/test_process_file.py diff --git a/django_logging/tests/utils/test_context_manager.py b/django_logging/tests/utils/test_context_manager.py index 578cf9c..53f1c92 100644 --- a/django_logging/tests/utils/test_context_manager.py +++ b/django_logging/tests/utils/test_context_manager.py @@ -4,8 +4,8 @@ import pytest -from django_logging.utils.context_manager import _restore_logging_config, config_setup from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON +from django_logging.utils.context_manager import _restore_logging_config, config_setup pytestmark = [ pytest.mark.utils, @@ -74,6 +74,8 @@ def test_config_setup_applies_custom_config( "log_levels": ["INFO"], "log_dir": "/tmp/logs", "log_file_formats": {"INFO": 1}, + "log_file_format_types": {"INFO": "normal"}, + "extra_log_files": {"INFO": False}, "console_level": "DEBUG", "console_format": 2, "colorize_console": False, @@ -127,6 +129,8 @@ def test_config_context_restores_original_config( "log_levels": ["INFO"], "log_dir": "/tmp/logs", "log_file_formats": {"INFO": 1}, + "log_file_format_types": {"INFO": "normal"}, + "extra_log_files": {"INFO": False}, "console_level": "DEBUG", "console_format": 2, "colorize_console": False, diff --git a/django_logging/tests/utils/test_email_notifier.py b/django_logging/tests/utils/test_email_notifier.py index b68020e..d83ac7a 100644 --- a/django_logging/tests/utils/test_email_notifier.py +++ b/django_logging/tests/utils/test_email_notifier.py @@ -6,8 +6,8 @@ import pytest -from django_logging.utils.log_email_notifier.notifier import send_email_async from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON +from django_logging.utils.log_email_notifier.notifier import send_email_async pytestmark = [ pytest.mark.utils, @@ -84,7 +84,7 @@ def test_send_email_async_success( mock_smtp_instance.quit.assert_called_once() mock_info.assert_called_once_with( - "Log Record has been sent to ADMIN EMAIL successfully." + "The Record has been sent to ADMIN EMAIL successfully." ) mock_warning.assert_not_called() @@ -122,6 +122,6 @@ def test_send_email_async_failure( email_sent_event.wait() mock_warning.assert_called_once_with( - "Email Notifier failed to send Log Record: %s", ANY + "Email Notifier failed to send the Record: %s", ANY ) mock_info.assert_not_called() diff --git a/django_logging/tests/utils/test_get_conf.py b/django_logging/tests/utils/test_get_conf.py index 6b6df79..38bd2ef 100644 --- a/django_logging/tests/utils/test_get_conf.py +++ b/django_logging/tests/utils/test_get_conf.py @@ -5,13 +5,13 @@ import pytest from django.conf import settings +from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON from django_logging.utils.get_conf import ( get_config, is_auto_initialization_enabled, is_initialization_message_enabled, use_email_notifier_template, ) -from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON pytestmark = [ pytest.mark.utils, @@ -44,6 +44,14 @@ def test_get_conf(self, mock_settings: Dict) -> None: "log_file_formats": { "DEBUG": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" }, + "log_file_format_types": { + "DEBUG": "JSON", + "INFO": "XML", + }, + "extra_log_files": { + "DEBUG": False, + "INFO": True, + }, "console_level": "WARNING", "console_format": "%(levelname)s - %(message)s", "colorize_console": True, @@ -52,32 +60,31 @@ def test_get_conf(self, mock_settings: Dict) -> None: "log_email_notifier_log_levels": ["ERROR", None], "log_email_notifier_log_format": "custom_format", } - print(expected) result = get_config() assert result == expected result = get_config(extra_info=True) - - expected_extra = { - "log_levels": ["DEBUG", "INFO"], - "log_dir": "/custom/log/dir", - "log_file_formats": { - "DEBUG": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" - }, - "console_level": "WARNING", - "console_format": "%(levelname)s - %(message)s", - "colorize_console": True, - "log_date_format": "%Y-%m-%d", - "log_email_notifier_enable": True, - "log_email_notifier_log_levels": ["ERROR", None], - "log_email_notifier_log_format": "custom_format", - "log_email_notifier": { - "ENABLE": True, - "NOTIFY_ERROR": True, - "NOTIFY_CRITICAL": False, - "LOG_FORMAT": "custom_format", - }, - } + result.pop("log_settings") + + expected_extra = expected.copy() + expected_extra.update( + { + "log_file_format_types": { + "DEBUG": "JSON", + "INFO": "XML", + }, + "extra_log_files": { + "DEBUG": False, + "INFO": True, + }, + "log_email_notifier": { + "ENABLE": True, + "NOTIFY_ERROR": True, + "NOTIFY_CRITICAL": False, + "LOG_FORMAT": "custom_format", + }, + } + ) assert result == expected_extra @@ -152,3 +159,18 @@ def test_is_initialization_message_enabled(self, mock_settings: Dict) -> None: mock_settings["DJANGO_LOGGING"]["INITIALIZATION_MESSAGE_ENABLE"] = False with patch.object(settings, "DJANGO_LOGGING", mock_settings["DJANGO_LOGGING"]): assert is_initialization_message_enabled() is False + + def test_logging_settings_none(self) -> None: + """ + Test that logging settings is None and raise `ValueError`. + + This test verifies that when logging settings (DJANGO_LOGGING) is None, + the `get_config` function raises `ValueError`. + + Asserts: + ------- + - ValueError raised by `check_logging_settings`. + """ + settings.DJANGO_LOGGING = None + with pytest.raises(ValueError, match="DJANGO_LOGGING must be a dictionary with configs as keys"): + get_config() diff --git a/django_logging/tests/utils/test_log_and_notify.py b/django_logging/tests/utils/test_log_and_notify.py index b2262c4..709bb84 100644 --- a/django_logging/tests/utils/test_log_and_notify.py +++ b/django_logging/tests/utils/test_log_and_notify.py @@ -6,8 +6,8 @@ import pytest from django.conf import settings -from django_logging.utils.log_email_notifier.log_and_notify import log_and_notify_admin from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON +from django_logging.utils.log_email_notifier.log_and_notify import log_and_notify_admin pytestmark = [ pytest.mark.utils, @@ -94,6 +94,8 @@ def test_log_and_notify_admin_success( args=None, exc_info=None, ) + # assign the custom value 'context' + log_record.context = "" with patch( "django_logging.utils.log_email_notifier.log_and_notify.get_config", @@ -210,6 +212,9 @@ def test_log_and_notify_admin_missing_admin_email( exc_info=None, ) + # assign the custom value 'context' + log_record.context = "" + with patch( "django_logging.utils.log_email_notifier.log_and_notify.get_config", return_value=self.mock_log_config(True), diff --git a/django_logging/tests/utils/test_process_file.py b/django_logging/tests/utils/test_process_file.py new file mode 100644 index 0000000..850018a --- /dev/null +++ b/django_logging/tests/utils/test_process_file.py @@ -0,0 +1,56 @@ +import os +import sys +from unittest.mock import Mock, patch + +import pytest + +from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON +from django_logging.utils.command.process_file import setup_directories + +pytestmark = [ + pytest.mark.utils, + pytest.mark.utils_process_file, + pytest.mark.skipif(sys.version_info < PYTHON_VERSION, reason=PYTHON_VERSION_REASON), +] + + +class TestProcessFile: + """ + Test suite for the process_file module focusing on FileNotFoundError. + """ + + @patch("os.path.exists", return_value=False) + def test_log_directory_not_found(self, mock_exists: Mock) -> None: + """ + Test if setup_directories raises FileNotFoundError when the log directory does not exist. + + Args: + ---- + mock_exists (Callable): Mock for os.path.exists. + """ + log_dir = "/non/existent/log_dir" + sub_dir = "sub_dir" + + with pytest.raises(FileNotFoundError, match=f"does not exist."): + setup_directories(log_dir, sub_dir) + + mock_exists.assert_called_once_with(log_dir) + + @patch("os.path.exists", side_effect=[True, False]) + def test_sub_directory_not_found(self, mock_exists: Mock) -> None: + """ + Test if setup_directories raises FileNotFoundError when the subdirectory does not exist. + + Args: + ---- + mock_exists (Callable): Mock for os.path.exists. + """ + log_dir = "/existent/log_dir" + sub_dir = "sub_dir" + sub_directory = os.path.join(log_dir, sub_dir) + + with pytest.raises(FileNotFoundError, match=f"does not exist."): + setup_directories(log_dir, sub_dir) + + mock_exists.assert_any_call(log_dir) + mock_exists.assert_any_call(sub_directory) diff --git a/django_logging/tests/utils/test_set_conf.py b/django_logging/tests/utils/test_set_conf.py index 900dcbd..8c986ba 100644 --- a/django_logging/tests/utils/test_set_conf.py +++ b/django_logging/tests/utils/test_set_conf.py @@ -5,8 +5,8 @@ import pytest from django_logging.constants.ansi_colors import AnsiColors -from django_logging.utils.set_conf import set_config from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON +from django_logging.utils.set_conf import set_config pytestmark = [ pytest.mark.utils, @@ -61,6 +61,8 @@ def test_set_config_success( ["DEBUG", "INFO"], "/path/to/logs", {"DEBUG": 1, "INFO": 2}, + {"DEBUG": "XML", "INFO": "JSON"}, + {"DEBUG": False, "INFO": True}, "DEBUG", 1, True, @@ -79,6 +81,8 @@ def test_set_config_success( ["DEBUG", "INFO"], "/path/to/logs", {"DEBUG": 1, "INFO": 2}, + {"DEBUG": "XML", "INFO": "JSON"}, + {"DEBUG": False, "INFO": True}, "DEBUG", 1, True, @@ -93,6 +97,8 @@ def test_set_config_success( ["DEBUG", "INFO"], "/path/to/logs", {"DEBUG": 1, "INFO": 2}, + {"DEBUG": "XML", "INFO": "JSON"}, + {"DEBUG": False, "INFO": True}, "DEBUG", 1, True, @@ -114,6 +120,8 @@ def test_set_config_success( ["DEBUG", "INFO"], "/path/to/logs", {"DEBUG": 1, "INFO": 2}, + {"DEBUG": "XML", "INFO": "JSON"}, + {"DEBUG": False, "INFO": True}, "DEBUG", 1, True, @@ -156,6 +164,8 @@ def test_set_config_auto_initialization_disabled( ["DEBUG", "INFO"], "/path/to/logs", {"DEBUG": 1, "INFO": 2}, + {"DEBUG": "XML", "INFO": "JSON"}, + {"DEBUG": False, "INFO": True}, "DEBUG", 1, True, @@ -203,6 +213,8 @@ def test_set_config_exception_handling( ["DEBUG", "INFO"], "/path/to/logs", {"DEBUG": 1, "INFO": 2}, + {"DEBUG": "XML", "INFO": "JSON"}, + {"DEBUG": False, "INFO": True}, "DEBUG", 1, True,