Skip to content

Commit

Permalink
TST: Check more error messages in tests (#17075)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung authored and jreback committed Jul 26, 2017
1 parent 9de416a commit 5c185e0
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 139 deletions.
51 changes: 25 additions & 26 deletions pandas/tests/frame/test_validate.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
from pandas.core.frame import DataFrame

import pytest
import pandas.util.testing as tm


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

def test_validate_bool_args(self):
# Tests for error handling related to boolean arguments.
invalid_values = [1, "True", [1, 2, 3], 5.0]

for value in invalid_values:
with pytest.raises(ValueError):
self.df.query('a > b', inplace=value)

with pytest.raises(ValueError):
self.df.eval('a + b', inplace=value)
@pytest.fixture
def dataframe():
return DataFrame({'a': [1, 2], 'b': [3, 4]})

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

with pytest.raises(ValueError):
self.df.reset_index(inplace=value)

with pytest.raises(ValueError):
self.df.dropna(inplace=value)

with pytest.raises(ValueError):
self.df.drop_duplicates(inplace=value)
class TestDataFrameValidate(object):
"""Tests for error handling related to data types of method arguments."""

with pytest.raises(ValueError):
self.df.sort_values(by=['a'], inplace=value)
@pytest.mark.parametrize("func", ["query", "eval", "set_index",
"reset_index", "dropna",
"drop_duplicates", "sort_values"])
@pytest.mark.parametrize("inplace", [1, "True", [1, 2, 3], 5.0])
def test_validate_bool_args(self, dataframe, func, inplace):
msg = "For argument \"inplace\" expected type bool"
kwargs = dict(inplace=inplace)

if func == "query":
kwargs["expr"] = "a > b"
elif func == "eval":
kwargs["expr"] = "a + b"
elif func == "set_index":
kwargs["keys"] = ["a"]
elif func == "sort_values":
kwargs["by"] = ["a"]

with tm.assert_raises_regex(ValueError, msg):
getattr(dataframe, func)(**kwargs)
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def test_with_slices(self):

# slice of interval
with pytest.raises(NotImplementedError):
result = s.loc[Interval(3, 6):]
s.loc[Interval(3, 6):]

with pytest.raises(NotImplementedError):
result = s[Interval(3, 6):]
s[Interval(3, 6):]

expected = s.iloc[3:5]
result = s[[Interval(3, 6)]]
Expand Down
21 changes: 13 additions & 8 deletions pandas/tests/io/msgpack/test_except.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# coding: utf-8

import pytest

from datetime import datetime
from pandas.io.msgpack import packb, unpackb

import pytest
import pandas.util.testing as tm


class DummyException(Exception):
pass
Expand All @@ -12,12 +14,13 @@ class DummyException(Exception):
class TestExceptions(object):

def test_raise_on_find_unsupported_value(self):
import datetime
pytest.raises(TypeError, packb, datetime.datetime.now())
msg = "can\'t serialize datetime"
with tm.assert_raises_regex(TypeError, msg):
packb(datetime.now())

def test_raise_from_object_hook(self):
def hook(obj):
raise DummyException
def hook(_):
raise DummyException()

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

def test_invalidvalue(self):
pytest.raises(ValueError, unpackb, b'\xd9\x97#DL_')
def test_invalid_value(self):
msg = "Unpack failed: error"
with tm.assert_raises_regex(ValueError, msg):
unpackb(b"\xd9\x97#DL_")
29 changes: 22 additions & 7 deletions pandas/tests/io/msgpack/test_limits.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# coding: utf-8
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from pandas.io.msgpack import packb, unpackb, Packer, Unpacker, ExtType

import pytest

from pandas.io.msgpack import packb, unpackb, Packer, Unpacker, ExtType
import pandas.util.testing as tm


class TestLimits(object):
Expand Down Expand Up @@ -39,7 +39,10 @@ def test_max_str_len(self):

unpacker = Unpacker(max_str_len=2, encoding='utf-8')
unpacker.feed(packed)
pytest.raises(ValueError, unpacker.unpack)

msg = "3 exceeds max_str_len"
with tm.assert_raises_regex(ValueError, msg):
unpacker.unpack()

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

unpacker = Unpacker(max_bin_len=2)
unpacker.feed(packed)
pytest.raises(ValueError, unpacker.unpack)

msg = "3 exceeds max_bin_len"
with tm.assert_raises_regex(ValueError, msg):
unpacker.unpack()

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

unpacker = Unpacker(max_array_len=2)
unpacker.feed(packed)
pytest.raises(ValueError, unpacker.unpack)

msg = "3 exceeds max_array_len"
with tm.assert_raises_regex(ValueError, msg):
unpacker.unpack()

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

unpacker = Unpacker(max_map_len=2)
unpacker.feed(packed)
pytest.raises(ValueError, unpacker.unpack)

msg = "3 exceeds max_map_len"
with tm.assert_raises_regex(ValueError, msg):
unpacker.unpack()

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

unpacker = Unpacker(max_ext_len=2)
unpacker.feed(packed)
pytest.raises(ValueError, unpacker.unpack)

msg = "4 exceeds max_ext_len"
with tm.assert_raises_regex(ValueError, msg):
unpacker.unpack()
28 changes: 13 additions & 15 deletions pandas/tests/io/msgpack/test_sequnpack.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
# coding: utf-8

import pytest

from pandas import compat
from pandas.io.msgpack import Unpacker, BufferFull
from pandas.io.msgpack import OutOfData

import pytest
import pandas.util.testing as tm


class TestPack(object):

def test_partialdata(self):
def test_partial_data(self):
unpacker = Unpacker()
unpacker.feed(b'\xa5')
pytest.raises(StopIteration, next, iter(unpacker))
unpacker.feed(b'h')
pytest.raises(StopIteration, next, iter(unpacker))
unpacker.feed(b'a')
pytest.raises(StopIteration, next, iter(unpacker))
unpacker.feed(b'l')
pytest.raises(StopIteration, next, iter(unpacker))
unpacker.feed(b'l')
pytest.raises(StopIteration, next, iter(unpacker))
unpacker.feed(b'o')
assert next(iter(unpacker)) == b'hallo'
msg = "No more data to unpack"

for data in [b"\xa5", b"h", b"a", b"l", b"l"]:
unpacker.feed(data)
with tm.assert_raises_regex(StopIteration, msg):
next(iter(unpacker))

unpacker.feed(b"o")
assert next(iter(unpacker)) == b"hallo"

def test_foobar(self):
unpacker = Unpacker(read_size=3, use_list=1)
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/io/sas/test_sas.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import pytest

from pandas.compat import StringIO
from pandas import read_sas

import pandas.util.testing as tm


class TestSas(object):

def test_sas_buffer_format(self):

# GH14947
# see gh-14947
b = StringIO("")
with pytest.raises(ValueError):

msg = ("If this is a buffer object rather than a string "
"name, you must specify a format string")
with tm.assert_raises_regex(ValueError, msg):
read_sas(b)
Loading

0 comments on commit 5c185e0

Please sign in to comment.