Skip to content

Commit 05a8a3f

Browse files
committed
TST: Check more error messages in tests
1 parent a7b4a9c commit 05a8a3f

File tree

8 files changed

+117
-95
lines changed

8 files changed

+117
-95
lines changed

pandas/tests/frame/test_validate.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
from pandas.core.frame import DataFrame
22

33
import pytest
4+
import pandas.util.testing as tm
45

56

67
class TestDataFrameValidate(object):
78
"""Tests for error handling related to data types of method arguments."""
8-
df = DataFrame({'a': [1, 2], 'b': [3, 4]})
99

10-
def test_validate_bool_args(self):
11-
# Tests for error handling related to boolean arguments.
12-
invalid_values = [1, "True", [1, 2, 3], 5.0]
13-
14-
for value in invalid_values:
15-
with pytest.raises(ValueError):
16-
self.df.query('a > b', inplace=value)
17-
18-
with pytest.raises(ValueError):
19-
self.df.eval('a + b', inplace=value)
20-
21-
with pytest.raises(ValueError):
22-
self.df.set_index(keys=['a'], inplace=value)
23-
24-
with pytest.raises(ValueError):
25-
self.df.reset_index(inplace=value)
26-
27-
with pytest.raises(ValueError):
28-
self.df.dropna(inplace=value)
29-
30-
with pytest.raises(ValueError):
31-
self.df.drop_duplicates(inplace=value)
32-
33-
with pytest.raises(ValueError):
34-
self.df.sort_values(by=['a'], inplace=value)
10+
@classmethod
11+
def setup_class(cls):
12+
cls.df = DataFrame({'a': [1, 2], 'b': [3, 4]})
13+
14+
@pytest.mark.parametrize("func", ["query", "eval", "set_index",
15+
"reset_index", "dropna",
16+
"drop_duplicates", "sort_values"])
17+
@pytest.mark.parametrize("inplace", [1, "True", [1, 2, 3], 5.0])
18+
def test_validate_bool_args(self, func, inplace):
19+
msg = "For argument \"inplace\" expected type bool"
20+
kwargs = dict(inplace=inplace)
21+
22+
if func == "query":
23+
kwargs["expr"] = "a > b"
24+
elif func == "eval":
25+
kwargs["expr"] = "a + b"
26+
elif func == "set_index":
27+
kwargs["keys"] = ["a"]
28+
elif func == "sort_values":
29+
kwargs["by"] = ["a"]
30+
31+
with tm.assert_raises_regex(ValueError, msg):
32+
getattr(self.df, func)(**kwargs)

pandas/tests/indexing/test_interval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ def test_with_slices(self):
109109

110110
# slice of interval
111111
with pytest.raises(NotImplementedError):
112-
result = s.loc[Interval(3, 6):]
112+
s.loc[Interval(3, 6):]
113113

114114
with pytest.raises(NotImplementedError):
115-
result = s[Interval(3, 6):]
115+
s[Interval(3, 6):]
116116

117117
expected = s.iloc[3:5]
118118
result = s[[Interval(3, 6)]]
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# coding: utf-8
22

3-
import pytest
4-
3+
from datetime import datetime
54
from pandas.io.msgpack import packb, unpackb
65

6+
import pytest
7+
import pandas.util.testing as tm
8+
79

810
class DummyException(Exception):
911
pass
@@ -12,12 +14,13 @@ class DummyException(Exception):
1214
class TestExceptions(object):
1315

1416
def test_raise_on_find_unsupported_value(self):
15-
import datetime
16-
pytest.raises(TypeError, packb, datetime.datetime.now())
17+
msg = "can\'t serialize datetime"
18+
with tm.assert_raises_regex(TypeError, msg):
19+
packb(datetime.now())
1720

1821
def test_raise_from_object_hook(self):
19-
def hook(obj):
20-
raise DummyException
22+
def hook(_):
23+
raise DummyException()
2124

2225
pytest.raises(DummyException, unpackb, packb({}), object_hook=hook)
2326
pytest.raises(DummyException, unpackb, packb({'fizz': 'buzz'}),
@@ -30,5 +33,7 @@ def hook(obj):
3033
packb({'fizz': {'buzz': 'spam'}}),
3134
object_pairs_hook=hook)
3235

33-
def test_invalidvalue(self):
34-
pytest.raises(ValueError, unpackb, b'\xd9\x97#DL_')
36+
def test_invalid_value(self):
37+
msg = "Unpack failed: error"
38+
with tm.assert_raises_regex(ValueError, msg):
39+
unpackb(b"\xd9\x97#DL_")

pandas/tests/io/msgpack/test_limits.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# coding: utf-8
22
from __future__ import (absolute_import, division, print_function,
33
unicode_literals)
4+
from pandas.io.msgpack import packb, unpackb, Packer, Unpacker, ExtType
45

56
import pytest
6-
7-
from pandas.io.msgpack import packb, unpackb, Packer, Unpacker, ExtType
7+
import pandas.util.testing as tm
88

99

1010
class TestLimits(object):
@@ -39,7 +39,10 @@ def test_max_str_len(self):
3939

4040
unpacker = Unpacker(max_str_len=2, encoding='utf-8')
4141
unpacker.feed(packed)
42-
pytest.raises(ValueError, unpacker.unpack)
42+
43+
msg = "3 exceeds max_str_len"
44+
with tm.assert_raises_regex(ValueError, msg):
45+
unpacker.unpack()
4346

4447
def test_max_bin_len(self):
4548
d = b'x' * 3
@@ -51,7 +54,10 @@ def test_max_bin_len(self):
5154

5255
unpacker = Unpacker(max_bin_len=2)
5356
unpacker.feed(packed)
54-
pytest.raises(ValueError, unpacker.unpack)
57+
58+
msg = "3 exceeds max_bin_len"
59+
with tm.assert_raises_regex(ValueError, msg):
60+
unpacker.unpack()
5561

5662
def test_max_array_len(self):
5763
d = [1, 2, 3]
@@ -63,7 +69,10 @@ def test_max_array_len(self):
6369

6470
unpacker = Unpacker(max_array_len=2)
6571
unpacker.feed(packed)
66-
pytest.raises(ValueError, unpacker.unpack)
72+
73+
msg = "3 exceeds max_array_len"
74+
with tm.assert_raises_regex(ValueError, msg):
75+
unpacker.unpack()
6776

6877
def test_max_map_len(self):
6978
d = {1: 2, 3: 4, 5: 6}
@@ -75,7 +84,10 @@ def test_max_map_len(self):
7584

7685
unpacker = Unpacker(max_map_len=2)
7786
unpacker.feed(packed)
78-
pytest.raises(ValueError, unpacker.unpack)
87+
88+
msg = "3 exceeds max_map_len"
89+
with tm.assert_raises_regex(ValueError, msg):
90+
unpacker.unpack()
7991

8092
def test_max_ext_len(self):
8193
d = ExtType(42, b"abc")
@@ -87,4 +99,7 @@ def test_max_ext_len(self):
8799

88100
unpacker = Unpacker(max_ext_len=2)
89101
unpacker.feed(packed)
90-
pytest.raises(ValueError, unpacker.unpack)
102+
103+
msg = "4 exceeds max_ext_len"
104+
with tm.assert_raises_regex(ValueError, msg):
105+
unpacker.unpack()

pandas/tests/io/msgpack/test_sequnpack.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
# coding: utf-8
22

3-
import pytest
4-
53
from pandas import compat
64
from pandas.io.msgpack import Unpacker, BufferFull
75
from pandas.io.msgpack import OutOfData
86

7+
import pytest
8+
import pandas.util.testing as tm
9+
910

1011
class TestPack(object):
1112

12-
def test_partialdata(self):
13+
def test_partial_data(self):
1314
unpacker = Unpacker()
14-
unpacker.feed(b'\xa5')
15-
pytest.raises(StopIteration, next, iter(unpacker))
16-
unpacker.feed(b'h')
17-
pytest.raises(StopIteration, next, iter(unpacker))
18-
unpacker.feed(b'a')
19-
pytest.raises(StopIteration, next, iter(unpacker))
20-
unpacker.feed(b'l')
21-
pytest.raises(StopIteration, next, iter(unpacker))
22-
unpacker.feed(b'l')
23-
pytest.raises(StopIteration, next, iter(unpacker))
24-
unpacker.feed(b'o')
25-
assert next(iter(unpacker)) == b'hallo'
15+
msg = "No more data to unpack"
16+
17+
for data in [b"\xa5", b"h", b"a", b"l", b"l"]:
18+
unpacker.feed(data)
19+
with tm.assert_raises_regex(StopIteration, msg):
20+
next(iter(unpacker))
21+
22+
unpacker.feed(b"o")
23+
assert next(iter(unpacker)) == b"hallo"
2624

2725
def test_foobar(self):
2826
unpacker = Unpacker(read_size=3, use_list=1)

pandas/tests/io/sas/test_sas.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import pytest
2-
31
from pandas.compat import StringIO
42
from pandas import read_sas
53

4+
import pandas.util.testing as tm
5+
66

77
class TestSas(object):
88

99
def test_sas_buffer_format(self):
10-
11-
# GH14947
10+
# see gh-14947
1211
b = StringIO("")
13-
with pytest.raises(ValueError):
12+
13+
msg = ("If this is a buffer object rather than a string "
14+
"name, you must specify a format string")
15+
with tm.assert_raises_regex(ValueError, msg):
1416
read_sas(b)

pandas/tests/scalar/test_interval.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import division
22

3-
import pytest
43
from pandas import Interval
4+
55
import pandas.util.testing as tm
66

77

88
class TestInterval(object):
9-
def setup_method(self, method):
9+
def setup_method(self, _):
1010
self.interval = Interval(0, 1)
1111

1212
def test_properties(self):
@@ -27,7 +27,10 @@ def test_contains(self):
2727
assert 0.5 in self.interval
2828
assert 1 in self.interval
2929
assert 0 not in self.interval
30-
pytest.raises(TypeError, lambda: self.interval in self.interval)
30+
31+
msg = "__contains__ not defined for two intervals"
32+
with tm.assert_raises_regex(TypeError, msg):
33+
self.interval in self.interval
3134

3235
interval = Interval(0, 1, closed='both')
3336
assert 0 in interval
@@ -71,10 +74,11 @@ def test_math_add(self):
7174
actual += 1
7275
assert expected == actual
7376

74-
with pytest.raises(TypeError):
77+
msg = "unsupported operand type\(s\) for \+"
78+
with tm.assert_raises_regex(TypeError, msg):
7579
self.interval + Interval(1, 2)
7680

77-
with pytest.raises(TypeError):
81+
with tm.assert_raises_regex(TypeError, msg):
7882
self.interval + 'foo'
7983

8084
def test_math_sub(self):
@@ -86,10 +90,11 @@ def test_math_sub(self):
8690
actual -= 1
8791
assert expected == actual
8892

89-
with pytest.raises(TypeError):
93+
msg = "unsupported operand type\(s\) for -"
94+
with tm.assert_raises_regex(TypeError, msg):
9095
self.interval - Interval(1, 2)
9196

92-
with pytest.raises(TypeError):
97+
with tm.assert_raises_regex(TypeError, msg):
9398
self.interval - 'foo'
9499

95100
def test_math_mult(self):
@@ -105,10 +110,12 @@ def test_math_mult(self):
105110
actual *= 2
106111
assert expected == actual
107112

108-
with pytest.raises(TypeError):
113+
msg = "unsupported operand type\(s\) for \*"
114+
with tm.assert_raises_regex(TypeError, msg):
109115
self.interval * Interval(1, 2)
110116

111-
with pytest.raises(TypeError):
117+
msg = "can\'t multiply sequence by non-int"
118+
with tm.assert_raises_regex(TypeError, msg):
112119
self.interval * 'foo'
113120

114121
def test_math_div(self):
@@ -120,8 +127,9 @@ def test_math_div(self):
120127
actual /= 2.0
121128
assert expected == actual
122129

123-
with pytest.raises(TypeError):
130+
msg = "unsupported operand type\(s\) for /"
131+
with tm.assert_raises_regex(TypeError, msg):
124132
self.interval / Interval(1, 2)
125133

126-
with pytest.raises(TypeError):
134+
with tm.assert_raises_regex(TypeError, msg):
127135
self.interval / 'foo'

pandas/tests/series/test_validate.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
import pytest
21
from pandas.core.series import Series
32

3+
import pytest
4+
import pandas.util.testing as tm
5+
46

57
class TestSeriesValidate(object):
68
"""Tests for error handling related to data types of method arguments."""
7-
s = Series([1, 2, 3, 4, 5])
8-
9-
def test_validate_bool_args(self):
10-
# Tests for error handling related to boolean arguments.
11-
invalid_values = [1, "True", [1, 2, 3], 5.0]
12-
13-
for value in invalid_values:
14-
with pytest.raises(ValueError):
15-
self.s.reset_index(inplace=value)
16-
17-
with pytest.raises(ValueError):
18-
self.s._set_name(name='hello', inplace=value)
199

20-
with pytest.raises(ValueError):
21-
self.s.sort_values(inplace=value)
10+
@classmethod
11+
def setup_class(cls):
12+
cls.s = Series([1, 2, 3, 4, 5])
2213

23-
with pytest.raises(ValueError):
24-
self.s.sort_index(inplace=value)
14+
@pytest.mark.parametrize("func", ["reset_index", "_set_name",
15+
"sort_values", "sort_index",
16+
"rename", "dropna"])
17+
@pytest.mark.parametrize("inplace", [1, "True", [1, 2, 3], 5.0])
18+
def test_validate_bool_args(self, func, inplace):
19+
msg = "For argument \"inplace\" expected type bool"
20+
kwargs = dict(inplace=inplace)
2521

26-
with pytest.raises(ValueError):
27-
self.s.rename(inplace=value)
22+
if func == "_set_name":
23+
kwargs["name"] = "hello"
2824

29-
with pytest.raises(ValueError):
30-
self.s.dropna(inplace=value)
25+
with tm.assert_raises_regex(ValueError, msg):
26+
getattr(self.s, func)(**kwargs)

0 commit comments

Comments
 (0)