Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: akgnah <1024@setq.me> Co-authored-by: akgnah <1024@setq.me>
- Loading branch information
1 parent
06413c3
commit a7f5485
Showing
3 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
fixes: | ||
- "Fix after_log logger format: function name was used with delay formatting." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import random | ||
import unittest.mock | ||
|
||
from tenacity import after_log | ||
from tenacity import _utils # noqa | ||
|
||
from . import test_tenacity | ||
|
||
|
||
class TestAfterLogFormat(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.log_level = random.choice((logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)) | ||
self.previous_attempt_number = random.randint(1, 512) | ||
|
||
def test_01_default(self): | ||
"""Test log formatting.""" | ||
log = unittest.mock.MagicMock(spec="logging.Logger.log") | ||
logger = unittest.mock.MagicMock(spec="logging.Logger", log=log) | ||
|
||
sec_format = "%0.3f" | ||
delay_since_first_attempt = 0.1 | ||
|
||
retry_state = test_tenacity.make_retry_state(self.previous_attempt_number, delay_since_first_attempt) | ||
fun = after_log(logger=logger, log_level=self.log_level) # use default sec_format | ||
fun(retry_state) | ||
log.assert_called_once_with( | ||
self.log_level, | ||
f"Finished call to '{_utils.get_callback_name(retry_state.fn)}' " | ||
f"after {sec_format % retry_state.seconds_since_start}(s), " | ||
f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it.", | ||
) | ||
|
||
def test_02_custom_sec_format(self): | ||
"""Test log formatting with custom int format..""" | ||
log = unittest.mock.MagicMock(spec="logging.Logger.log") | ||
logger = unittest.mock.MagicMock(spec="logging.Logger", log=log) | ||
|
||
sec_format = "%.1f" | ||
delay_since_first_attempt = 0.1 | ||
|
||
retry_state = test_tenacity.make_retry_state(self.previous_attempt_number, delay_since_first_attempt) | ||
fun = after_log(logger=logger, log_level=self.log_level, sec_format=sec_format) | ||
fun(retry_state) | ||
log.assert_called_once_with( | ||
self.log_level, | ||
f"Finished call to '{_utils.get_callback_name(retry_state.fn)}' " | ||
f"after {sec_format % retry_state.seconds_since_start}(s), " | ||
f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it.", | ||
) |