|
1 | 1 | from pandas.core.frame import DataFrame |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +import pandas.util.testing as tm |
4 | 5 |
|
5 | 6 |
|
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]}) |
20 | 10 |
|
21 | | - with pytest.raises(ValueError): |
22 | | - self.df.set_index(keys=['a'], inplace=value) |
23 | 11 |
|
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.""" |
32 | 14 |
|
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) |
0 commit comments