diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index a6b331685e72a..bb73c6bc6b38b 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -13,7 +13,6 @@ from pandas.util import testing as tm from pandas.util.testing import makeCustomDataframe as mkdf from pandas.io.clipboard.exceptions import PyperclipException -from pandas.io.clipboard import clipboard_set, clipboard_get try: @@ -76,10 +75,53 @@ def df(request): raise ValueError +@pytest.fixture +def mock_clipboard(mock, request): + """Fixture mocking clipboard IO. + + This mocks pandas.io.clipboard.clipboard_get and + pandas.io.clipboard.clipboard_set. + + This uses a local dict for storing data. The dictionary + key used is the test ID, available with ``request.node.name``. + + This returns the local dictionary, for direct manipulation by + tests. + """ + + # our local clipboard for tests + _mock_data = {} + + def _mock_set(data): + _mock_data[request.node.name] = data + + def _mock_get(): + return _mock_data[request.node.name] + + mock_set = mock.patch("pandas.io.clipboard.clipboard_set", + side_effect=_mock_set) + mock_get = mock.patch("pandas.io.clipboard.clipboard_get", + side_effect=_mock_get) + with mock_get, mock_set: + yield _mock_data + + +@pytest.mark.clipboard +def test_mock_clipboard(mock_clipboard): + import pandas.io.clipboard + pandas.io.clipboard.clipboard_set("abc") + assert "abc" in set(mock_clipboard.values()) + result = pandas.io.clipboard.clipboard_get() + assert result == "abc" + + @pytest.mark.single +@pytest.mark.clipboard @pytest.mark.skipif(not _DEPS_INSTALLED, reason="clipboard primitives not installed") +@pytest.mark.usefixtures("mock_clipboard") class TestClipboard(object): + def check_round_trip_frame(self, data, excel=None, sep=None, encoding=None): data.to_clipboard(excel=excel, sep=sep, encoding=encoding) @@ -118,15 +160,18 @@ def test_copy_delim_warning(self, df): # delimited and excel="True" @pytest.mark.parametrize('sep', ['\t', None, 'default']) @pytest.mark.parametrize('excel', [True, None, 'default']) - def test_clipboard_copy_tabs_default(self, sep, excel, df): + def test_clipboard_copy_tabs_default(self, sep, excel, df, request, + mock_clipboard): kwargs = build_kwargs(sep, excel) df.to_clipboard(**kwargs) if PY2: # to_clipboard copies unicode, to_csv produces bytes. This is # expected behavior - assert clipboard_get().encode('utf-8') == df.to_csv(sep='\t') + result = mock_clipboard[request.node.name].encode('utf-8') + expected = df.to_csv(sep='\t') + assert result == expected else: - assert clipboard_get() == df.to_csv(sep='\t') + assert mock_clipboard[request.node.name] == df.to_csv(sep='\t') # Tests reading of white space separated tables @pytest.mark.parametrize('sep', [None, 'default']) @@ -138,7 +183,8 @@ def test_clipboard_copy_strings(self, sep, excel, df): assert result.to_string() == df.to_string() assert df.shape == result.shape - def test_read_clipboard_infer_excel(self): + def test_read_clipboard_infer_excel(self, request, + mock_clipboard): # gh-19010: avoid warnings clip_kwargs = dict(engine="python") @@ -147,7 +193,7 @@ def test_read_clipboard_infer_excel(self): 1 2 4 Harry Carney """.strip()) - clipboard_set(text) + mock_clipboard[request.node.name] = text df = pd.read_clipboard(**clip_kwargs) # excel data is parsed correctly @@ -159,7 +205,7 @@ def test_read_clipboard_infer_excel(self): 1 2 3 4 """.strip()) - clipboard_set(text) + mock_clipboard[request.node.name] = text res = pd.read_clipboard(**clip_kwargs) text = dedent(""" @@ -167,7 +213,7 @@ def test_read_clipboard_infer_excel(self): 1 2 3 4 """.strip()) - clipboard_set(text) + mock_clipboard[request.node.name] = text exp = pd.read_clipboard(**clip_kwargs) tm.assert_frame_equal(res, exp) diff --git a/setup.cfg b/setup.cfg index 5fc0236066b93..021159bad99de 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,7 @@ markers = slow: mark a test as slow network: mark a test as network high_memory: mark a test as a high-memory only + clipboard: mark a pd.read_clipboard test doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL addopts = --strict-data-files