From 305efd320354542c13759215df6683a86995f704 Mon Sep 17 00:00:00 2001 From: Nearsoft Date: Mon, 7 Dec 2020 17:26:40 -0700 Subject: [PATCH 1/4] TST: add message matches to pytest.raises in various tests --- pandas/tests/io/pytables/test_complex.py | 9 +++++++-- pandas/tests/io/test_clipboard.py | 5 +++-- pandas/tests/plotting/frame/test_frame_subplots.py | 10 +++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/tests/io/pytables/test_complex.py b/pandas/tests/io/pytables/test_complex.py index 3a7aff3b551c2..26b49f6f268b9 100644 --- a/pandas/tests/io/pytables/test_complex.py +++ b/pandas/tests/io/pytables/test_complex.py @@ -149,8 +149,11 @@ def test_complex_indexing_error(setup_path): {"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"], "C": complex128}, index=list("abcd"), ) + + msg = "indexing error with complex numbers" + with ensure_clean_store(setup_path) as store: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): store.append("df", df, data_columns=["C"]) @@ -158,8 +161,10 @@ def test_complex_series_error(setup_path): complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) s = Series(complex128, index=list("abcd")) + msg = "type error in series of complex" + with ensure_clean_path(setup_path) as path: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s.to_hdf(path, "obj", format="t") with ensure_clean_path(setup_path) as path: diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index fca98175a0a24..8129a1b902ccd 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -238,10 +238,11 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): tm.assert_frame_equal(res, exp) def test_invalid_encoding(self, df): + msg = "invalid coding, encoding must be ascii" # test case for testing invalid encoding - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.to_clipboard(encoding="ascii") - with pytest.raises(NotImplementedError): + with pytest.raises(NotImplementedError, match=msg): pd.read_clipboard(encoding="ascii") @pytest.mark.parametrize("enc", ["UTF-8", "utf-8", "utf8"]) diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index 427b2c1c3a180..376742b257b16 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -218,9 +218,11 @@ def test_subplots_layout_multi_column(self): self._check_axes_shape(axes, axes_num=3, layout=(4, 1)) assert axes.shape == (4, 1) - with pytest.raises(ValueError): + msg = "can't create subplots with layout (1,1) or (-1,-1)" + + with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(1, 1)) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(-1, -1)) @pytest.mark.parametrize( @@ -272,7 +274,9 @@ def test_subplots_multiple_axes(self): self._check_axes_shape(axes, axes_num=6, layout=(2, 3)) tm.close() - with pytest.raises(ValueError): + msg = "number of axes passed is different than the ones required" + + with pytest.raises(ValueError, match=msg): fig, axes = self.plt.subplots(2, 3) # pass different number of axes from required df.plot(subplots=True, ax=axes) From a4bbb8d3eb833ad41656c4054403638b1fafae65 Mon Sep 17 00:00:00 2001 From: Nearsoft Date: Mon, 7 Dec 2020 17:26:40 -0700 Subject: [PATCH 2/4] TST: add message matches to pytest.raises in various tests GH30999 --- pandas/tests/io/pytables/test_complex.py | 9 +++++++-- pandas/tests/io/test_clipboard.py | 5 +++-- pandas/tests/plotting/frame/test_frame_subplots.py | 10 +++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/tests/io/pytables/test_complex.py b/pandas/tests/io/pytables/test_complex.py index 3a7aff3b551c2..26b49f6f268b9 100644 --- a/pandas/tests/io/pytables/test_complex.py +++ b/pandas/tests/io/pytables/test_complex.py @@ -149,8 +149,11 @@ def test_complex_indexing_error(setup_path): {"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"], "C": complex128}, index=list("abcd"), ) + + msg = "indexing error with complex numbers" + with ensure_clean_store(setup_path) as store: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): store.append("df", df, data_columns=["C"]) @@ -158,8 +161,10 @@ def test_complex_series_error(setup_path): complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) s = Series(complex128, index=list("abcd")) + msg = "type error in series of complex" + with ensure_clean_path(setup_path) as path: - with pytest.raises(TypeError): + with pytest.raises(TypeError, match=msg): s.to_hdf(path, "obj", format="t") with ensure_clean_path(setup_path) as path: diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index fca98175a0a24..8129a1b902ccd 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -238,10 +238,11 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): tm.assert_frame_equal(res, exp) def test_invalid_encoding(self, df): + msg = "invalid coding, encoding must be ascii" # test case for testing invalid encoding - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.to_clipboard(encoding="ascii") - with pytest.raises(NotImplementedError): + with pytest.raises(NotImplementedError, match=msg): pd.read_clipboard(encoding="ascii") @pytest.mark.parametrize("enc", ["UTF-8", "utf-8", "utf8"]) diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index 427b2c1c3a180..376742b257b16 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -218,9 +218,11 @@ def test_subplots_layout_multi_column(self): self._check_axes_shape(axes, axes_num=3, layout=(4, 1)) assert axes.shape == (4, 1) - with pytest.raises(ValueError): + msg = "can't create subplots with layout (1,1) or (-1,-1)" + + with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(1, 1)) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(-1, -1)) @pytest.mark.parametrize( @@ -272,7 +274,9 @@ def test_subplots_multiple_axes(self): self._check_axes_shape(axes, axes_num=6, layout=(2, 3)) tm.close() - with pytest.raises(ValueError): + msg = "number of axes passed is different than the ones required" + + with pytest.raises(ValueError, match=msg): fig, axes = self.plt.subplots(2, 3) # pass different number of axes from required df.plot(subplots=True, ax=axes) From b9394069926559479d42c48352f7bc6c8aff7271 Mon Sep 17 00:00:00 2001 From: Nearsoft Date: Mon, 7 Dec 2020 20:20:28 -0700 Subject: [PATCH 3/4] Change message to actual error message --- pandas/tests/io/pytables/test_complex.py | 16 ++++++++++++++-- pandas/tests/io/test_clipboard.py | 2 +- .../tests/plotting/frame/test_frame_subplots.py | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/pandas/tests/io/pytables/test_complex.py b/pandas/tests/io/pytables/test_complex.py index 26b49f6f268b9..71bb6584889aa 100644 --- a/pandas/tests/io/pytables/test_complex.py +++ b/pandas/tests/io/pytables/test_complex.py @@ -150,7 +150,13 @@ def test_complex_indexing_error(setup_path): index=list("abcd"), ) - msg = "indexing error with complex numbers" + msg = ( + "Columns containing complex values can be stored " + "but cannot be indexed when using table format. " + "Either use fixed format, set index=False, " + "or do not include the columns containing complex " + "values to data_columns when initializing the table." + ) with ensure_clean_store(setup_path) as store: with pytest.raises(TypeError, match=msg): @@ -161,7 +167,13 @@ def test_complex_series_error(setup_path): complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) s = Series(complex128, index=list("abcd")) - msg = "type error in series of complex" + msg = ( + "Columns containing complex values can be stored " + "but cannot be indexed when using table format. " + "Either use fixed format, set index=False, " + "or do not include the columns containing complex " + "values to data_columns when initializing the table." + ) with ensure_clean_path(setup_path) as path: with pytest.raises(TypeError, match=msg): diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index 8129a1b902ccd..440c370857eef 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -238,7 +238,7 @@ def test_read_clipboard_infer_excel(self, request, mock_clipboard): tm.assert_frame_equal(res, exp) def test_invalid_encoding(self, df): - msg = "invalid coding, encoding must be ascii" + msg = "clipboard only supports utf-8 encoding" # test case for testing invalid encoding with pytest.raises(ValueError, match=msg): df.to_clipboard(encoding="ascii") diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index 376742b257b16..fea980a3c287b 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -218,7 +218,7 @@ def test_subplots_layout_multi_column(self): self._check_axes_shape(axes, axes_num=3, layout=(4, 1)) assert axes.shape == (4, 1) - msg = "can't create subplots with layout (1,1) or (-1,-1)" + msg = "Layout of 1x1 must be larger than required size 3" with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(1, 1)) @@ -274,7 +274,7 @@ def test_subplots_multiple_axes(self): self._check_axes_shape(axes, axes_num=6, layout=(2, 3)) tm.close() - msg = "number of axes passed is different than the ones required" + msg = "The number of passed axes must be 3, the same as the output plot" with pytest.raises(ValueError, match=msg): fig, axes = self.plt.subplots(2, 3) From 0389ace7fb8e3626710031994ed1ebea54e952c2 Mon Sep 17 00:00:00 2001 From: JoseNavy Date: Tue, 8 Dec 2020 12:18:45 -0700 Subject: [PATCH 4/4] Change message of test_frame_subplot --- pandas/tests/plotting/frame/test_frame_subplots.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index fea980a3c287b..2e0eecbeaacaa 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -222,6 +222,8 @@ def test_subplots_layout_multi_column(self): with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(1, 1)) + + msg = "At least one dimension of layout must be positive" with pytest.raises(ValueError, match=msg): df.plot(subplots=True, layout=(-1, -1))