13
13
from pandas .util import testing as tm
14
14
from pandas .util .testing import makeCustomDataframe as mkdf
15
15
from pandas .io .clipboard .exceptions import PyperclipException
16
- from pandas .io .clipboard import clipboard_set , clipboard_get
17
16
18
17
19
18
try :
@@ -76,10 +75,53 @@ def df(request):
76
75
raise ValueError
77
76
78
77
78
+ @pytest .fixture
79
+ def mock_clipboard (mock , request ):
80
+ """Fixture mocking clipboard IO.
81
+
82
+ This mocks pandas.io.clipboard.clipboard_get and
83
+ pandas.io.clipboard.clipboard_set.
84
+
85
+ This uses a local dict for storing data. The dictionary
86
+ key used is the test ID, available with ``request.node.name``.
87
+
88
+ This returns the local dictionary, for direct manipulation by
89
+ tests.
90
+ """
91
+
92
+ # our local clipboard for tests
93
+ _mock_data = {}
94
+
95
+ def _mock_set (data ):
96
+ _mock_data [request .node .name ] = data
97
+
98
+ def _mock_get ():
99
+ return _mock_data [request .node .name ]
100
+
101
+ mock_set = mock .patch ("pandas.io.clipboard.clipboard_set" ,
102
+ side_effect = _mock_set )
103
+ mock_get = mock .patch ("pandas.io.clipboard.clipboard_get" ,
104
+ side_effect = _mock_get )
105
+ with mock_get , mock_set :
106
+ yield _mock_data
107
+
108
+
109
+ @pytest .mark .clipboard
110
+ def test_mock_clipboard (mock_clipboard ):
111
+ import pandas .io .clipboard
112
+ pandas .io .clipboard .clipboard_set ("abc" )
113
+ assert "abc" in set (mock_clipboard .values ())
114
+ result = pandas .io .clipboard .clipboard_get ()
115
+ assert result == "abc"
116
+
117
+
79
118
@pytest .mark .single
119
+ @pytest .mark .clipboard
80
120
@pytest .mark .skipif (not _DEPS_INSTALLED ,
81
121
reason = "clipboard primitives not installed" )
122
+ @pytest .mark .usefixtures ("mock_clipboard" )
82
123
class TestClipboard (object ):
124
+
83
125
def check_round_trip_frame (self , data , excel = None , sep = None ,
84
126
encoding = None ):
85
127
data .to_clipboard (excel = excel , sep = sep , encoding = encoding )
@@ -118,15 +160,18 @@ def test_copy_delim_warning(self, df):
118
160
# delimited and excel="True"
119
161
@pytest .mark .parametrize ('sep' , ['\t ' , None , 'default' ])
120
162
@pytest .mark .parametrize ('excel' , [True , None , 'default' ])
121
- def test_clipboard_copy_tabs_default (self , sep , excel , df ):
163
+ def test_clipboard_copy_tabs_default (self , sep , excel , df , request ,
164
+ mock_clipboard ):
122
165
kwargs = build_kwargs (sep , excel )
123
166
df .to_clipboard (** kwargs )
124
167
if PY2 :
125
168
# to_clipboard copies unicode, to_csv produces bytes. This is
126
169
# expected behavior
127
- assert clipboard_get ().encode ('utf-8' ) == df .to_csv (sep = '\t ' )
170
+ result = mock_clipboard [request .node .name ].encode ('utf-8' )
171
+ expected = df .to_csv (sep = '\t ' )
172
+ assert result == expected
128
173
else :
129
- assert clipboard_get () == df .to_csv (sep = '\t ' )
174
+ assert mock_clipboard [ request . node . name ] == df .to_csv (sep = '\t ' )
130
175
131
176
# Tests reading of white space separated tables
132
177
@pytest .mark .parametrize ('sep' , [None , 'default' ])
@@ -138,7 +183,8 @@ def test_clipboard_copy_strings(self, sep, excel, df):
138
183
assert result .to_string () == df .to_string ()
139
184
assert df .shape == result .shape
140
185
141
- def test_read_clipboard_infer_excel (self ):
186
+ def test_read_clipboard_infer_excel (self , request ,
187
+ mock_clipboard ):
142
188
# gh-19010: avoid warnings
143
189
clip_kwargs = dict (engine = "python" )
144
190
@@ -147,7 +193,7 @@ def test_read_clipboard_infer_excel(self):
147
193
1 2
148
194
4 Harry Carney
149
195
""" .strip ())
150
- clipboard_set ( text )
196
+ mock_clipboard [ request . node . name ] = text
151
197
df = pd .read_clipboard (** clip_kwargs )
152
198
153
199
# excel data is parsed correctly
@@ -159,15 +205,15 @@ def test_read_clipboard_infer_excel(self):
159
205
1 2
160
206
3 4
161
207
""" .strip ())
162
- clipboard_set ( text )
208
+ mock_clipboard [ request . node . name ] = text
163
209
res = pd .read_clipboard (** clip_kwargs )
164
210
165
211
text = dedent ("""
166
212
a b
167
213
1 2
168
214
3 4
169
215
""" .strip ())
170
- clipboard_set ( text )
216
+ mock_clipboard [ request . node . name ] = text
171
217
exp = pd .read_clipboard (** clip_kwargs )
172
218
173
219
tm .assert_frame_equal (res , exp )
0 commit comments