Skip to content

Commit

Permalink
Merge pull request #1 from willmj/dataloader-v2-impl-unit-fix
Browse files Browse the repository at this point in the history
tests: reformat `mock.patch` to inside unit tests
Signed-off-by: Dushyant Behl <dushyantbehl@users.noreply.github.com>
  • Loading branch information
dushyantbehl committed Nov 12, 2024
2 parents ac148eb + 3fbeed0 commit 4c0b109
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions tests/utils/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,57 +28,57 @@
from tuning.utils.logging import set_log_level


@mock.patch.dict(os.environ, {}, clear=True)
def test_set_log_level_for_logger_default():
"""
Ensure that the correct log level is being set for python native logger and
transformers logger when no env var or CLI flag is passed
"""

train_args = copy.deepcopy(TRAIN_ARGS)
training_args, logger = set_log_level(train_args)
assert logger.getEffectiveLevel() == logging.WARNING
assert training_args.log_level == "passive"
with mock.patch.dict(os.environ, {}, clear=True):
train_args = copy.deepcopy(TRAIN_ARGS)
training_args, logger = set_log_level(train_args)
assert logger.getEffectiveLevel() == logging.WARNING
assert training_args.log_level == "passive"


@mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True)
def test_set_log_level_for_logger_with_env_var():
"""
Ensure that the correct log level is being set for python native logger and
transformers logger when env var LOG_LEVEL is used
"""

train_args_env = copy.deepcopy(TRAIN_ARGS)
training_args, logger = set_log_level(train_args_env)
assert logger.getEffectiveLevel() == logging.INFO
assert training_args.log_level == "info"
with mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True):
train_args_env = copy.deepcopy(TRAIN_ARGS)
training_args, logger = set_log_level(train_args_env)
assert logger.getEffectiveLevel() == logging.INFO
assert training_args.log_level == "info"


@mock.patch.dict(os.environ, {"TRANSFORMERS_VERBOSITY": "info"}, clear=True)
def test_set_log_level_for_logger_with_set_verbosity_and_cli():
"""
Ensure that the correct log level is being set for python native logger and
log_level of transformers logger is unchanged when env var TRANSFORMERS_VERBOSITY is used
and CLI flag is passed
"""

train_args = copy.deepcopy(TRAIN_ARGS)
train_args.log_level = "error"
training_args, logger = set_log_level(train_args)
assert logger.getEffectiveLevel() == logging.ERROR
assert training_args.log_level == "error"
with mock.patch.dict(os.environ, {"TRANSFORMERS_VERBOSITY": "info"}, clear=True):
train_args = copy.deepcopy(TRAIN_ARGS)
train_args.log_level = "error"
training_args, logger = set_log_level(train_args)
assert logger.getEffectiveLevel() == logging.ERROR
assert training_args.log_level == "error"


@mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True)
def test_set_log_level_for_logger_with_env_var_and_cli():
"""
Ensure that the correct log level is being set for python native logger and
transformers logger when env var LOG_LEVEL is used and CLI flag is passed.
In this case, CLI arg takes precedence over the set env var LOG_LEVEL.
"""

train_args = copy.deepcopy(TRAIN_ARGS)
train_args.log_level = "error"
training_args, logger = set_log_level(train_args)
assert logger.getEffectiveLevel() == logging.ERROR
assert training_args.log_level == "error"
with mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True):
train_args = copy.deepcopy(TRAIN_ARGS)
train_args.log_level = "error"
training_args, logger = set_log_level(train_args)
assert logger.getEffectiveLevel() == logging.ERROR
assert training_args.log_level == "error"

0 comments on commit 4c0b109

Please sign in to comment.