Skip to content

Commit

Permalink
TST: Add message checks to tests/arrays/sparse/ (pandas-dev#30244)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and proost committed Dec 19, 2019
1 parent 7137389 commit 1b6e867
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
26 changes: 15 additions & 11 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,12 @@ def test_take_filling(self):
with pytest.raises(ValueError, match=msg):
sparse.take(np.array([1, 0, -5]), allow_fill=True)

with pytest.raises(IndexError):
msg = "out of bounds value in 'indices'"
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, -6]))
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, 5]))
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, 5]), allow_fill=True)

def test_take_filling_fill_value(self):
Expand Down Expand Up @@ -340,11 +341,12 @@ def test_take_filling_fill_value(self):
with pytest.raises(ValueError, match=msg):
sparse.take(np.array([1, 0, -5]), allow_fill=True)

with pytest.raises(IndexError):
msg = "out of bounds value in 'indices'"
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, -6]))
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, 5]))
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, 5]), fill_value=True)

def test_take_filling_all_nan(self):
Expand All @@ -358,11 +360,12 @@ def test_take_filling_all_nan(self):
expected = SparseArray([np.nan, np.nan, np.nan], kind="block")
tm.assert_sp_array_equal(result, expected)

with pytest.raises(IndexError):
msg = "out of bounds value in 'indices'"
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, -6]))
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, 5]))
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
sparse.take(np.array([1, 5]), fill_value=True)

def test_set_item(self):
Expand Down Expand Up @@ -670,10 +673,11 @@ def test_getslice_tuple(self):
exp = SparseArray(dense[4:,], fill_value=0) # noqa: E231
tm.assert_sp_array_equal(res, exp)

with pytest.raises(IndexError):
msg = "too many indices for array"
with pytest.raises(IndexError, match=msg):
sparse[4:, :]

with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
# check numpy compat
dense[4:, :]

Expand Down
25 changes: 20 additions & 5 deletions pandas/tests/arrays/sparse/test_dtype.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

Expand Down Expand Up @@ -80,7 +82,9 @@ def test_not_equal(a, b):


def test_construct_from_string_raises():
with pytest.raises(TypeError):
with pytest.raises(
TypeError, match="Could not construct SparseDtype from 'not a dtype'"
):
SparseDtype.construct_from_string("not a dtype")


Expand Down Expand Up @@ -175,9 +179,20 @@ def test_update_dtype(original, dtype, expected):


@pytest.mark.parametrize(
"original, dtype",
[(SparseDtype(float, np.nan), int), (SparseDtype(str, "abc"), int)],
"original, dtype, expected_error_msg",
[
(
SparseDtype(float, np.nan),
int,
re.escape("Cannot convert non-finite values (NA or inf) to integer"),
),
(
SparseDtype(str, "abc"),
int,
re.escape("invalid literal for int() with base 10: 'abc'"),
),
],
)
def test_update_dtype_raises(original, dtype):
with pytest.raises(ValueError):
def test_update_dtype_raises(original, dtype, expected_error_msg):
with pytest.raises(ValueError, match=expected_error_msg):
original.update_dtype(dtype)

0 comments on commit 1b6e867

Please sign in to comment.