Skip to content

Cleanup clipboard tests #21163

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 7 commits into from
Jun 26, 2018
Merged
Changes from 3 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
78 changes: 68 additions & 10 deletions pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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
from pandas.io.clipboard import clipboard_set, clipboard_get


try:
Expand Down Expand Up @@ -60,6 +60,9 @@ def setup_class(cls):
# unicode round trip test for GH 13747, GH 12529
cls.data['utf8'] = pd.DataFrame({'a': ['µasd', 'Ωœ∑´'],
Copy link
Contributor

Choose a reason for hiding this comment

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

can remove this entirely and use fixtures

'b': ['øπ∆˚¬', 'œ∑´®']})
# Test for quotes and common delimiters in text
cls.data['delim_symbols'] = pd.DataFrame({'a': ['"a,\t"b|c', 'd\tef´'],
'b': ['hi\'j', 'k\'\'lm']})
cls.data_types = list(cls.data.keys())

@classmethod
Expand All @@ -70,25 +73,80 @@ def check_round_trip_frame(self, data_type, excel=None, sep=None,
encoding=None):
data = self.data[data_type]
data.to_clipboard(excel=excel, sep=sep, encoding=encoding)
if sep is not None:
result = read_clipboard(sep=sep, index_col=0, encoding=encoding)
else:
result = read_clipboard(encoding=encoding)
result = read_clipboard(sep=sep or '\t', index_col=0,
encoding=encoding)
tm.assert_frame_equal(data, result, check_dtype=False)

def test_round_trip_frame(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be parameterized or a fixture

for dt in self.data_types:
self.check_round_trip_frame(dt)

def test_round_trip_frame_sep(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

these can all be parameterized

for dt in self.data_types:
self.check_round_trip_frame(dt, sep=',')
self.check_round_trip_frame(dt, sep=r'\s+')
self.check_round_trip_frame(dt, sep='|')
self.check_round_trip_frame(dt, sep='\t')

def test_round_trip_frame_string(self):
for dt in self.data_types:
self.check_round_trip_frame(dt, excel=False)

def test_round_trip_frame(self):
data = self.data[dt]
data.to_clipboard(excel=False, sep=None)
result = read_clipboard()
assert data.to_string() == result.to_string()
assert data.shape == result.shape

def test_excel_sep_warning(self):
with tm.assert_produces_warning():
self.data['string'].to_clipboard(excel=True, sep=r'\t')

def build_kwargs(self, sep, excel):
Copy link
Contributor

Choose a reason for hiding this comment

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

you can make this a module level function

kwargs = {}
if excel != 'default':
kwargs['excel'] = excel
if sep != 'default':
kwargs['sep'] = sep
return kwargs

@pytest.mark.parametrize('sep, excel', [
('\t', True),
(None, True),
('default', True),
('\t', None),
(None, None),
('\t', 'default'),
(None, 'default')
])
def test_clipboard_copy_tabs_default(self, sep, excel):
for dt in self.data_types:
self.check_round_trip_frame(dt)
data = self.data[dt]
kwargs = self.build_kwargs(sep, excel)
data.to_clipboard(**kwargs)
assert clipboard_get() == data.to_csv(sep='\t')

@pytest.mark.parametrize('sep, excel', [
(',', True),
('|', True)
])
def test_clipboard_copy_delim(self, sep, excel):
for dt in self.data_types:
data = self.data[dt]
kwargs = self.build_kwargs(sep, excel)
data.to_clipboard(**kwargs)
assert clipboard_get() == data.to_csv(sep=sep)

@pytest.mark.parametrize('sep, excel', [
('\t', False),
(None, False),
('default', False)
])
def test_clipboard_copy_strings(self, sep, excel):
Copy link
Contributor

Choose a reason for hiding this comment

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

can parameterize over data_types as well

for dt in self.data_types:
data = self.data[dt]
kwargs = self.build_kwargs(sep, excel)
data.to_clipboard(**kwargs)
result = read_clipboard(sep=r'\s+')
assert result.to_string() == data.to_string()
assert data.shape == result.shape

def test_read_clipboard_infer_excel(self):
# gh-19010: avoid warnings
Expand Down