test_util.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import pytest
  5. import pandas.compat as compat
  6. from pandas.compat import raise_with_traceback
  7. from pandas.util._decorators import deprecate_kwarg, make_signature
  8. from pandas.util._validators import validate_kwargs
  9. import pandas.util.testing as tm
  10. def test_rands():
  11. r = tm.rands(10)
  12. assert len(r) == 10
  13. def test_rands_array_1d():
  14. arr = tm.rands_array(5, size=10)
  15. assert arr.shape == (10,)
  16. assert len(arr[0]) == 5
  17. def test_rands_array_2d():
  18. arr = tm.rands_array(7, size=(10, 10))
  19. assert arr.shape == (10, 10)
  20. assert len(arr[1, 1]) == 7
  21. def test_numpy_err_state_is_default():
  22. expected = {"over": "warn", "divide": "warn",
  23. "invalid": "warn", "under": "ignore"}
  24. import numpy as np
  25. # The error state should be unchanged after that import.
  26. assert np.geterr() == expected
  27. @pytest.mark.parametrize("func,expected", [
  28. # Case where the func does not have default kwargs.
  29. (validate_kwargs, (["fname", "kwargs", "compat_args"],
  30. ["fname", "kwargs", "compat_args"])),
  31. # Case where the func does have default kwargs.
  32. (deprecate_kwarg, (["old_arg_name", "new_arg_name",
  33. "mapping=None", "stacklevel=2"],
  34. ["old_arg_name", "new_arg_name",
  35. "mapping", "stacklevel"]))
  36. ])
  37. def test_make_signature(func, expected):
  38. # see gh-17608
  39. assert make_signature(func) == expected
  40. def test_raise_with_traceback():
  41. with pytest.raises(LookupError, match="error_text"):
  42. try:
  43. raise ValueError("THIS IS AN ERROR")
  44. except ValueError:
  45. e = LookupError("error_text")
  46. raise_with_traceback(e)
  47. with pytest.raises(LookupError, match="error_text"):
  48. try:
  49. raise ValueError("This is another error")
  50. except ValueError:
  51. e = LookupError("error_text")
  52. _, _, traceback = sys.exc_info()
  53. raise_with_traceback(e, traceback)
  54. def test_convert_rows_list_to_csv_str():
  55. rows_list = ["aaa", "bbb", "ccc"]
  56. ret = tm.convert_rows_list_to_csv_str(rows_list)
  57. if compat.is_platform_windows():
  58. expected = "aaa\r\nbbb\r\nccc\r\n"
  59. else:
  60. expected = "aaa\nbbb\nccc\n"
  61. assert ret == expected
  62. def test_create_temp_directory():
  63. with tm.ensure_clean_dir() as path:
  64. assert os.path.exists(path)
  65. assert os.path.isdir(path)
  66. assert not os.path.exists(path)
  67. def test_assert_raises_regex_deprecated():
  68. # see gh-23592
  69. with tm.assert_produces_warning(FutureWarning):
  70. msg = "Not equal!"
  71. with tm.assert_raises_regex(AssertionError, msg):
  72. assert 1 == 2, msg
  73. @pytest.mark.parametrize('strict_data_files', [True, False])
  74. def test_datapath_missing(datapath):
  75. with pytest.raises(ValueError, match="Could not find file"):
  76. datapath("not_a_file")
  77. def test_datapath(datapath):
  78. args = ("data", "iris.csv")
  79. result = datapath(*args)
  80. expected = os.path.join(os.path.dirname(os.path.dirname(__file__)), *args)
  81. assert result == expected
  82. def test_rng_context():
  83. import numpy as np
  84. expected0 = 1.764052345967664
  85. expected1 = 1.6243453636632417
  86. with tm.RNGContext(0):
  87. with tm.RNGContext(1):
  88. assert np.random.randn() == expected1
  89. assert np.random.randn() == expected0