test_rewrite_warning.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import warnings
  2. import pytest
  3. from pandas.util._exceptions import rewrite_warning
  4. import pandas._testing as tm
  5. @pytest.mark.parametrize(
  6. "target_category, target_message, hit",
  7. [
  8. (FutureWarning, "Target message", True),
  9. (FutureWarning, "Target", True),
  10. (FutureWarning, "get mess", True),
  11. (FutureWarning, "Missed message", False),
  12. (DeprecationWarning, "Target message", False),
  13. ],
  14. )
  15. @pytest.mark.parametrize(
  16. "new_category",
  17. [
  18. None,
  19. DeprecationWarning,
  20. ],
  21. )
  22. def test_rewrite_warning(target_category, target_message, hit, new_category):
  23. new_message = "Rewritten message"
  24. if hit:
  25. expected_category = new_category if new_category else target_category
  26. expected_message = new_message
  27. else:
  28. expected_category = FutureWarning
  29. expected_message = "Target message"
  30. with tm.assert_produces_warning(expected_category, match=expected_message):
  31. with rewrite_warning(
  32. target_message, target_category, new_message, new_category
  33. ):
  34. warnings.warn(message="Target message", category=FutureWarning)