forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_util.py
128 lines (90 loc) · 3.4 KB
/
test_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
import os
import sys
import pytest
import pandas.compat as compat
from pandas.compat import raise_with_traceback
from pandas.util._decorators import deprecate_kwarg, make_signature
from pandas.util._validators import validate_kwargs
import pandas.util.testing as tm
def test_rands():
r = tm.rands(10)
assert len(r) == 10
def test_rands_array_1d():
arr = tm.rands_array(5, size=10)
assert arr.shape == (10,)
assert len(arr[0]) == 5
def test_rands_array_2d():
arr = tm.rands_array(7, size=(10, 10))
assert arr.shape == (10, 10)
assert len(arr[1, 1]) == 7
def test_numpy_err_state_is_default():
# The defaults since numpy 1.6.0
expected = {"over": "warn", "divide": "warn",
"invalid": "warn", "under": "ignore"}
import numpy as np
# The error state should be unchanged after that import.
assert np.geterr() == expected
@pytest.mark.parametrize("func,expected", [
# Case where the func does not have default kwargs.
(validate_kwargs, (["fname", "kwargs", "compat_args"],
["fname", "kwargs", "compat_args"])),
# Case where the func does have default kwargs.
(deprecate_kwarg, (["old_arg_name", "new_arg_name",
"mapping=None", "stacklevel=2"],
["old_arg_name", "new_arg_name",
"mapping", "stacklevel"]))
])
def test_make_signature(func, expected):
# see gh-17608
assert make_signature(func) == expected
def test_raise_with_traceback():
with pytest.raises(LookupError, match="error_text"):
try:
raise ValueError("THIS IS AN ERROR")
except ValueError:
e = LookupError("error_text")
raise_with_traceback(e)
with pytest.raises(LookupError, match="error_text"):
try:
raise ValueError("This is another error")
except ValueError:
e = LookupError("error_text")
_, _, traceback = sys.exc_info()
raise_with_traceback(e, traceback)
def test_convert_rows_list_to_csv_str():
rows_list = ["aaa", "bbb", "ccc"]
ret = tm.convert_rows_list_to_csv_str(rows_list)
if compat.is_platform_windows():
expected = "aaa\r\nbbb\r\nccc\r\n"
else:
expected = "aaa\nbbb\nccc\n"
assert ret == expected
def test_create_temp_directory():
with tm.ensure_clean_dir() as path:
assert os.path.exists(path)
assert os.path.isdir(path)
assert not os.path.exists(path)
def test_assert_raises_regex_deprecated():
# see gh-23592
with tm.assert_produces_warning(FutureWarning):
msg = "Not equal!"
with tm.assert_raises_regex(AssertionError, msg):
assert 1 == 2, msg
def test_datapath_missing(datapath, request):
if not request.config.getoption("--strict-data-files"):
pytest.skip("Need to set '--strict-data-files'")
with pytest.raises(ValueError, match="Could not find file"):
datapath("not_a_file")
args = ("data", "iris.csv")
result = datapath(*args)
expected = os.path.join(os.path.dirname(os.path.dirname(__file__)), *args)
assert result == expected
def test_rng_context():
import numpy as np
expected0 = 1.764052345967664
expected1 = 1.6243453636632417
with tm.RNGContext(0):
with tm.RNGContext(1):
assert np.random.randn() == expected1
assert np.random.randn() == expected0