Skip to content

Commit 5c185e0

Browse files
gfyoungjreback
authored andcommitted
TST: Check more error messages in tests (pandas-dev#17075)
1 parent 9de416a commit 5c185e0

File tree

8 files changed

+167
-139
lines changed

8 files changed

+167
-139
lines changed

pandas/tests/frame/test_validate.py

+25-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
from pandas.core.frame import DataFrame
22

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

56

6-
class TestDataFrameValidate(object):
7-
"""Tests for error handling related to data types of method arguments."""
8-
df = DataFrame({'a': [1, 2], 'b': [3, 4]})
9-
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)
7+
@pytest.fixture
8+
def dataframe():
9+
return DataFrame({'a': [1, 2], 'b': [3, 4]})
2010

21-
with pytest.raises(ValueError):
22-
self.df.set_index(keys=['a'], inplace=value)
2311

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)
12+
class TestDataFrameValidate(object):
13+
"""Tests for error handling related to data types of method arguments."""
3214

33-
with pytest.raises(ValueError):
34-
self.df.sort_values(by=['a'], inplace=value)
15+
@pytest.mark.parametrize("func", ["query", "eval", "set_index",
16+
"reset_index", "dropna",
17+
"drop_duplicates", "sort_values"])
18+
@pytest.mark.parametrize("inplace", [1, "True", [1, 2, 3], 5.0])
19+
def test_validate_bool_args(self, dataframe, func, inplace):
20+
msg = "For argument \"inplace\" expected type bool"
21+
kwargs = dict(inplace=inplace)
22+
23+
if func == "query":
24+
kwargs["expr"] = "a > b"
25+
elif func == "eval":
26+
kwargs["expr"] = "a + b"
27+
elif func == "set_index":
28+
kwargs["keys"] = ["a"]
29+
elif func == "sort_values":
30+
kwargs["by"] = ["a"]
31+
32+
with tm.assert_raises_regex(ValueError, msg):
33+
getattr(dataframe, func)(**kwargs)

pandas/tests/indexing/test_interval.py

+2-2
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)]]
+13-8
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

+22-7
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

+13-15
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

+7-5
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)

0 commit comments

Comments
 (0)