1- # Copyright 2020-2022 The MathWorks, Inc.
1+ # Copyright 2020-2024 The MathWorks, Inc.
22"""This file tests methods present in matlab_proxy/util/mwi_logger.py
33"""
44
55import logging
66import os
7-
7+ import pytest
88from matlab_proxy .util .mwi import logger as mwi_logger
99
1010
@@ -22,7 +22,7 @@ def test_get_mw_logger_name():
2222
2323
2424def test_get_with_no_environment_variables (monkeypatch ):
25- """This test checks if the get method returns a logger with default settings"""
25+ """This test checks if the get method returns a logger with default settings if no environment variable is set """
2626 # Delete the environment variables if they do exist
2727 env_names_list = mwi_logger .get_environment_variable_names ()
2828 monkeypatch .delenv (env_names_list [0 ], raising = False )
@@ -34,7 +34,7 @@ def test_get_with_no_environment_variables(monkeypatch):
3434
3535
3636def test_get_with_environment_variables (monkeypatch , tmp_path ):
37- """This test checks if the get method returns a logger with default settings"""
37+ """This test checks if the get method returns a logger with the specified settings"""
3838 env_names_list = mwi_logger .get_environment_variable_names ()
3939 monkeypatch .setenv (env_names_list [0 ], "CRITICAL" )
4040 monkeypatch .setenv (env_names_list [1 ], str (tmp_path / "testing123.log" ))
@@ -47,3 +47,37 @@ def test_get_with_environment_variables(monkeypatch, tmp_path):
4747 # Verify that environment variable setting the file is respected
4848 assert len (logger .handlers ) == 1
4949 assert os .path .basename (logger .handlers [0 ].baseFilename ) == "testing123.log"
50+
51+
52+ @pytest .mark .parametrize (
53+ "log_level, expected_level" ,
54+ [
55+ ("DEBUG" , logging .DEBUG ),
56+ ("INFO" , logging .INFO ),
57+ ("WARNING" , logging .WARNING ),
58+ ("debug" , logging .DEBUG ),
59+ ("info" , logging .INFO ),
60+ ("warning" , logging .WARNING ),
61+ ],
62+ )
63+ def test_set_logging_configuration_known_logging_levels (
64+ monkeypatch , log_level , expected_level
65+ ):
66+ """This test checks if the logger is set with correct level for known log levels"""
67+ env_names_list = mwi_logger .get_environment_variable_names ()
68+ monkeypatch .setenv (env_names_list [0 ], log_level )
69+ logger = mwi_logger .get (init = True )
70+ assert (
71+ logger .isEnabledFor (expected_level ) == True
72+ ), f"Error in initialising the logger with { log_level } "
73+
74+
75+ @pytest .mark .parametrize ("log_level" , ["ABC" , "abc" ])
76+ def test_set_logging_configuration_unknown_logging_levels (monkeypatch , log_level ):
77+ """This test checks if the logger is set with INFO level for unknown log levels"""
78+ env_names_list = mwi_logger .get_environment_variable_names ()
79+ monkeypatch .setenv (env_names_list [0 ], log_level )
80+ logger = mwi_logger .get (init = True )
81+ assert (
82+ logger .isEnabledFor (logging .INFO ) == True
83+ ), "Error in initialising the default logger"
0 commit comments