test_util.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import pytest
  3. import pandas.compat as compat
  4. import pandas._testing as tm
  5. def test_rands():
  6. r = tm.rands(10)
  7. assert len(r) == 10
  8. def test_rands_array_1d():
  9. arr = tm.rands_array(5, size=10)
  10. assert arr.shape == (10,)
  11. assert len(arr[0]) == 5
  12. def test_rands_array_2d():
  13. arr = tm.rands_array(7, size=(10, 10))
  14. assert arr.shape == (10, 10)
  15. assert len(arr[1, 1]) == 7
  16. def test_numpy_err_state_is_default():
  17. expected = {"over": "warn", "divide": "warn", "invalid": "warn", "under": "ignore"}
  18. import numpy as np
  19. # The error state should be unchanged after that import.
  20. assert np.geterr() == expected
  21. def test_convert_rows_list_to_csv_str():
  22. rows_list = ["aaa", "bbb", "ccc"]
  23. ret = tm.convert_rows_list_to_csv_str(rows_list)
  24. if compat.is_platform_windows():
  25. expected = "aaa\r\nbbb\r\nccc\r\n"
  26. else:
  27. expected = "aaa\nbbb\nccc\n"
  28. assert ret == expected
  29. def test_create_temp_directory():
  30. with tm.ensure_clean_dir() as path:
  31. assert os.path.exists(path)
  32. assert os.path.isdir(path)
  33. assert not os.path.exists(path)
  34. @pytest.mark.parametrize("strict_data_files", [True, False])
  35. def test_datapath_missing(datapath):
  36. with pytest.raises(ValueError, match="Could not find file"):
  37. datapath("not_a_file")
  38. def test_datapath(datapath):
  39. args = ("data", "iris.csv")
  40. result = datapath(*args)
  41. expected = os.path.join(os.path.dirname(os.path.dirname(__file__)), *args)
  42. assert result == expected
  43. def test_rng_context():
  44. import numpy as np
  45. expected0 = 1.764052345967664
  46. expected1 = 1.6243453636632417
  47. with tm.RNGContext(0):
  48. with tm.RNGContext(1):
  49. assert np.random.randn() == expected1
  50. assert np.random.randn() == expected0