|
17 | 17 | from pandas.util.version import Version
|
18 | 18 |
|
19 | 19 |
|
20 |
| -def test_get_callable_name(): |
21 |
| - getname = com.get_callable_name |
22 |
| - |
23 |
| - def fn(x): |
| 20 | +class TestGetCallableName: |
| 21 | + def fn(self, x): |
24 | 22 | return x
|
25 | 23 |
|
| 24 | + partial1 = partial(fn) |
| 25 | + partial2 = partial(partial1) |
26 | 26 | lambda_ = lambda x: x
|
27 |
| - part1 = partial(fn) |
28 |
| - part2 = partial(part1) |
29 | 27 |
|
30 |
| - class somecall: |
| 28 | + class SomeCall: |
31 | 29 | def __call__(self):
|
32 |
| - # This shouldn't actually get called below; somecall.__init__ |
| 30 | + # This shouldn't actually get called below; SomeCall.__init__ |
33 | 31 | # should.
|
34 | 32 | raise NotImplementedError
|
35 | 33 |
|
36 |
| - assert getname(fn) == "fn" |
37 |
| - assert getname(lambda_) |
38 |
| - assert getname(part1) == "fn" |
39 |
| - assert getname(part2) == "fn" |
40 |
| - assert getname(somecall()) == "somecall" |
41 |
| - assert getname(1) is None |
42 |
| - |
43 |
| - |
44 |
| -def test_any_none(): |
45 |
| - assert com.any_none(1, 2, 3, None) |
46 |
| - assert not com.any_none(1, 2, 3, 4) |
47 |
| - |
48 |
| - |
49 |
| -def test_all_not_none(): |
50 |
| - assert com.all_not_none(1, 2, 3, 4) |
51 |
| - assert not com.all_not_none(1, 2, 3, None) |
52 |
| - assert not com.all_not_none(None, None, None, None) |
53 |
| - |
54 |
| - |
55 |
| -def test_random_state(): |
56 |
| - # Check with seed |
57 |
| - state = com.random_state(5) |
58 |
| - assert state.uniform() == np.random.RandomState(5).uniform() |
59 |
| - |
60 |
| - # Check with random state object |
61 |
| - state2 = np.random.RandomState(10) |
62 |
| - assert com.random_state(state2).uniform() == np.random.RandomState(10).uniform() |
63 |
| - |
64 |
| - # check with no arg random state |
65 |
| - assert com.random_state() is np.random |
66 |
| - |
67 |
| - # check array-like |
68 |
| - # GH32503 |
69 |
| - state_arr_like = np.random.default_rng(None).integers( |
70 |
| - 0, 2**31, size=624, dtype="uint32" |
71 |
| - ) |
72 |
| - assert ( |
73 |
| - com.random_state(state_arr_like).uniform() |
74 |
| - == np.random.RandomState(state_arr_like).uniform() |
75 |
| - ) |
76 |
| - |
77 |
| - # Check BitGenerators |
78 |
| - # GH32503 |
79 |
| - assert ( |
80 |
| - com.random_state(np.random.MT19937(3)).uniform() |
81 |
| - == np.random.RandomState(np.random.MT19937(3)).uniform() |
82 |
| - ) |
83 |
| - assert ( |
84 |
| - com.random_state(np.random.PCG64(11)).uniform() |
85 |
| - == np.random.RandomState(np.random.PCG64(11)).uniform() |
| 34 | + @pytest.mark.parametrize( |
| 35 | + "func, expected", |
| 36 | + [ |
| 37 | + (fn, "fn"), |
| 38 | + (partial1, "fn"), |
| 39 | + (partial2, "fn"), |
| 40 | + (lambda_, "<lambda>"), |
| 41 | + (SomeCall(), "SomeCall"), |
| 42 | + (1, None), |
| 43 | + ], |
86 | 44 | )
|
| 45 | + def test_get_callable_name(self, func, expected): |
| 46 | + assert com.get_callable_name(func) == expected |
| 47 | + |
| 48 | + |
| 49 | +class TestRandomState: |
| 50 | + def test_seed(self): |
| 51 | + seed = 5 |
| 52 | + assert com.random_state(seed).uniform() == np.random.RandomState(seed).uniform() |
| 53 | + |
| 54 | + def test_object(self): |
| 55 | + seed = 10 |
| 56 | + state_obj = np.random.RandomState(seed) |
| 57 | + assert ( |
| 58 | + com.random_state(state_obj).uniform() |
| 59 | + == np.random.RandomState(seed).uniform() |
| 60 | + ) |
| 61 | + |
| 62 | + def test_default(self): |
| 63 | + assert com.random_state() is np.random |
| 64 | + |
| 65 | + def test_array_like(self): |
| 66 | + state = np.random.default_rng(None).integers(0, 2**31, size=624, dtype="uint32") |
| 67 | + assert ( |
| 68 | + com.random_state(state).uniform() == np.random.RandomState(state).uniform() |
| 69 | + ) |
| 70 | + |
| 71 | + def test_bit_generators(self): |
| 72 | + seed = 3 |
| 73 | + assert ( |
| 74 | + com.random_state(np.random.MT19937(seed)).uniform() |
| 75 | + == np.random.RandomState(np.random.MT19937(seed)).uniform() |
| 76 | + ) |
| 77 | + |
| 78 | + seed = 11 |
| 79 | + assert ( |
| 80 | + com.random_state(np.random.PCG64(seed)).uniform() |
| 81 | + == np.random.RandomState(np.random.PCG64(seed)).uniform() |
| 82 | + ) |
| 83 | + |
| 84 | + @pytest.mark.parametrize("state", ["test", 5.5]) |
| 85 | + def test_error(self, state): |
| 86 | + msg = ( |
| 87 | + "random_state must be an integer, array-like, a BitGenerator, Generator, " |
| 88 | + "a numpy RandomState, or None" |
| 89 | + ) |
| 90 | + with pytest.raises(ValueError, match=msg): |
| 91 | + com.random_state(state) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.parametrize("args, expected", [((1, 2, None), True), ((1, 2, 3), False)]) |
| 95 | +def test_any_none(args, expected): |
| 96 | + assert com.any_none(*args) is expected |
87 | 97 |
|
88 |
| - # Error for floats or strings |
89 |
| - msg = ( |
90 |
| - "random_state must be an integer, array-like, a BitGenerator, Generator, " |
91 |
| - "a numpy RandomState, or None" |
92 |
| - ) |
93 |
| - with pytest.raises(ValueError, match=msg): |
94 |
| - com.random_state("test") |
95 | 98 |
|
96 |
| - with pytest.raises(ValueError, match=msg): |
97 |
| - com.random_state(5.5) |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "args, expected", |
| 101 | + [((1, 2, 3), True), ((1, 2, None), False), ((None, None, None), False)], |
| 102 | +) |
| 103 | +def test_all_not_none(args, expected): |
| 104 | + assert com.all_not_none(*args) is expected |
98 | 105 |
|
99 | 106 |
|
100 | 107 | @pytest.mark.parametrize(
|
@@ -136,21 +143,32 @@ def test_maybe_match_name(left, right, expected):
|
136 | 143 | assert res is expected or res == expected
|
137 | 144 |
|
138 | 145 |
|
139 |
| -def test_standardize_mapping(): |
140 |
| - # No uninitialized defaultdicts |
141 |
| - msg = r"to_dict\(\) only accepts initialized defaultdicts" |
142 |
| - with pytest.raises(TypeError, match=msg): |
143 |
| - com.standardize_mapping(collections.defaultdict) |
144 |
| - |
145 |
| - # No non-mapping subtypes, instance |
146 |
| - msg = "unsupported type: <class 'list'>" |
| 146 | +@pytest.mark.parametrize( |
| 147 | + "into, msg", |
| 148 | + [ |
| 149 | + ( |
| 150 | + # uninitialized defaultdict |
| 151 | + collections.defaultdict, |
| 152 | + r"to_dict\(\) only accepts initialized defaultdicts", |
| 153 | + ), |
| 154 | + ( |
| 155 | + # non-mapping subtypes,, instance |
| 156 | + [], |
| 157 | + "unsupported type: <class 'list'>", |
| 158 | + ), |
| 159 | + ( |
| 160 | + # non-mapping subtypes, class |
| 161 | + list, |
| 162 | + "unsupported type: <class 'list'>", |
| 163 | + ), |
| 164 | + ], |
| 165 | +) |
| 166 | +def test_standardize_mapping_type_error(into, msg): |
147 | 167 | with pytest.raises(TypeError, match=msg):
|
148 |
| - com.standardize_mapping([]) |
| 168 | + com.standardize_mapping(into) |
149 | 169 |
|
150 |
| - # No non-mapping subtypes, class |
151 |
| - with pytest.raises(TypeError, match=msg): |
152 |
| - com.standardize_mapping(list) |
153 | 170 |
|
| 171 | +def test_standardize_mapping(): |
154 | 172 | fill = {"bad": "data"}
|
155 | 173 | assert com.standardize_mapping(fill) == dict
|
156 | 174 |
|
|
0 commit comments