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 5 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
45 changes: 38 additions & 7 deletions pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,41 @@ def df(request):
raise ValueError


# our local clipboard for tests
_mock_data = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it’s really odd to keep state outside of this fixture when it’s a function level

why don’t u just make it a module level?
or it seems that function level might be ok with state inside



@pytest.fixture
def mock_clipboard(mock, request):
def _mock_set(data):
_mock_data[request.node.name] = data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u add a docstring


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


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

@pytest.mark.clipboard
def test_mock_clipboard(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can’t u use monkey patch here?

import pandas.io.clipboard
pandas.io.clipboard.clipboard_set("abc")
assert "abc" in set(_mock_data.values())
result = pandas.io.clipboard.clipboard_get()
assert result == "abc"

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 +149,15 @@ 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):
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')
assert _mock_data[request.node.name].encode('utf-8') == df.to_csv(sep='\t')
else:
assert clipboard_get() == df.to_csv(sep='\t')
assert _mock_data[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 +169,7 @@ 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):
# gh-19010: avoid warnings
clip_kwargs = dict(engine="python")

Expand All @@ -147,7 +178,7 @@ def test_read_clipboard_infer_excel(self):
1 2
4 Harry Carney
""".strip())
clipboard_set(text)
_mock_data[request.node.name] = text
df = pd.read_clipboard(**clip_kwargs)

# excel data is parsed correctly
Expand All @@ -159,15 +190,15 @@ def test_read_clipboard_infer_excel(self):
1 2
3 4
""".strip())
clipboard_set(text)
_mock_data[request.node.name] = text
res = pd.read_clipboard(**clip_kwargs)

text = dedent("""
a b
1 2
3 4
""".strip())
clipboard_set(text)
_mock_data[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