Skip to content

Commit

Permalink
Add unit tests for tenacity's default configurations
Browse files Browse the repository at this point in the history
This commit introduces a new test class `TestRetryDefaults` to verify `dict_config` functionalities within tenacity. Included tests cover setting, getting, overriding, and deleting configuration attributes, as well as testing retry behavior with default and overridden configurations.
  • Loading branch information
SalvatoreZagaria committed Sep 28, 2024
1 parent 25efec0 commit 58b9993
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tests/test_tenacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import pytest

import tenacity
from tenacity import RetryCallState, RetryError, Retrying, retry
from tenacity import RetryCallState, RetryError, Retrying, retry, dict_config

_unset = object()

Expand Down Expand Up @@ -1793,5 +1793,56 @@ def test_decorated_retry_with(self, mock_sleep):
assert mock_sleep.call_count == 1


class TestRetryDefaults(unittest.TestCase):
def setUp(self):
# Reset config before each test
dict_config.reset_config()

def test_set_and_get_config(self):
# Set new configuration attributes
dict_config.set_config(stop=tenacity.stop_after_attempt(3), wait=tenacity.wait_fixed(1))
self.assertIsInstance(dict_config.get("stop"), tenacity.stop_after_attempt)
self.assertIsInstance(dict_config.get("wait"), tenacity.wait_fixed)

def test_override_config(self):
# Set initial configuration
dict_config.set_config(stop=tenacity.stop_after_attempt(3), wait=tenacity.wait_fixed(1))

# Override specific attribute
custom_config = dict_config.get_config(override={"wait": tenacity.wait_fixed(2)})
self.assertIsInstance(custom_config["wait"], tenacity.wait_fixed)
self.assertIsInstance(custom_config["stop"], tenacity.stop_after_attempt)

def test_delete_config(self):
# Set and then delete configuration attribute
dict_config.set_attribute("stop", tenacity.stop_after_attempt(3))
self.assertIn("stop", dict_config)
dict_config.delete_attribute("stop")
self.assertNotIn("stop", dict_config)
with self.assertRaises(KeyError):
dict_config.delete_attribute("stop")

def test_retry_with_default_config(self):
# Set default configuration
dict_config.set_config(stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_fixed(0.1))

@retry
def failing_func():
raise ValueError("This should trigger retries")
with self.assertRaises(tenacity.RetryError):
failing_func() # Should raise a RetryError

def test_retry_with_override(self):
# Set default configuration
dict_config.set_config(stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_fixed(0.1))

@retry(reraise=True)
def failing_func():
raise ValueError("This should trigger retries")

with self.assertRaises(ValueError):
failing_func() # Should raise a ValueError


if __name__ == "__main__":
unittest.main()

0 comments on commit 58b9993

Please sign in to comment.