-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
TST: Mock clipboard IO #22715
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dfef31d
Attempt to fix clipboard tests
TomAugspurger 688d993
note
TomAugspurger 9142b4e
update
TomAugspurger 2a90096
Merge remote-tracking branch 'upstream/master' into clipboard
TomAugspurger 66123ef
update
TomAugspurger f1ed15b
doc
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,10 +76,41 @@ def df(request): | |
raise ValueError | ||
|
||
|
||
# our local clipboard for tests | ||
_mock_data = {} | ||
|
||
|
||
@pytest.fixture | ||
def mock_clipboard(mock, request): | ||
def _mock_set(data): | ||
_mock_data[request.node.name] = data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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']) | ||
|
@@ -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") | ||
|
||
|
@@ -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 | ||
|
@@ -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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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