Skip to content

TST: Mock clipboard IO #22715

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 6 commits into from
Sep 16, 2018
Merged
Show file tree
Hide file tree
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
62 changes: 54 additions & 8 deletions pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'])
Expand All @@ -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")

Expand All @@ -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
Expand All @@ -159,15 +205,15 @@ 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("""
a b
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)
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down