Skip to content

TST: Add message checks to tests/arrays/sparse/ #30244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
@@ -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):
@@ -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):
@@ -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):
@@ -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:, :]

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

@@ -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")


@@ -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)