Skip to content

Commit 0ad0218

Browse files
committed
Fixture for mock
1 parent c83546c commit 0ad0218

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

pandas/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
13
import pytest
24

35
import numpy as np
@@ -249,3 +251,17 @@ def any_int_dtype(request):
249251
"""
250252

251253
return request.param
254+
255+
256+
@pytest.fixture
257+
def mock():
258+
"""
259+
Fixture providing the 'mock' module.
260+
261+
Uses 'unittest.mock' for Python 3. Attempts to import the 3rd party 'mock'
262+
package for Python 2, skipping if not present.
263+
"""
264+
if PY3:
265+
return importlib.import_module("unittest.mock")
266+
else:
267+
return pytest.importorskip("mock")

pandas/tests/dtypes/test_inference.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def test_is_dict_like_fails(ll):
128128
assert not inference.is_dict_like(ll)
129129

130130

131-
def test_is_file_like():
131+
def test_is_file_like(mock):
132132
class MockFile(object):
133133
pass
134134

@@ -166,10 +166,7 @@ class MockFile(object):
166166
# Iterator but no read / write attributes
167167
data = [1, 2, 3]
168168
assert not is_file(data)
169-
170-
if PY3:
171-
from unittest import mock
172-
assert not is_file(mock.Mock())
169+
assert not is_file(mock.Mock())
173170

174171

175172
@pytest.mark.parametrize(

pandas/tests/io/parser/common.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ def test_file_handles(self):
15461546
assert not m.closed
15471547
m.close()
15481548

1549-
def test_invalid_file_buffer(self):
1549+
def test_invalid_file_buffer(self, mock):
15501550
# see gh-15337
15511551

15521552
class InvalidBuffer(object):
@@ -1577,11 +1577,8 @@ def seek(self, pos, whence=0):
15771577

15781578
tm.assert_frame_equal(result, expected)
15791579

1580-
if PY3:
1581-
from unittest import mock
1582-
1583-
with tm.assert_raises_regex(ValueError, msg):
1584-
self.read_csv(mock.Mock())
1580+
with tm.assert_raises_regex(ValueError, msg):
1581+
self.read_csv(mock.Mock())
15851582

15861583
@tm.capture_stderr
15871584
def test_skip_bad_lines(self):

pandas/tests/io/test_gcs.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,17 @@
88
from pandas.util.testing import assert_frame_equal
99

1010

11-
@pytest.fixture
12-
def mock_patch():
13-
try:
14-
from unittest.mock import patch
15-
except ImportError:
16-
from mock import patch
17-
18-
return patch
19-
20-
2111
def test_is_gcs_url():
2212
assert is_gcs_url("gcs://pandas/somethingelse.com")
2313
assert is_gcs_url("gs://pandas/somethingelse.com")
2414
assert not is_gcs_url("s3://pandas/somethingelse.com")
2515

2616

2717
@td.skip_if_no('gcsfs')
28-
def test_read_csv_gcs(mock_patch):
18+
def test_read_csv_gcs(mock):
2919
df1 = DataFrame({'int': [1, 3], 'float': [2.0, np.nan], 'str': ['t', 's'],
3020
'dt': date_range('2018-06-18', periods=2)})
31-
with mock_patch('gcsfs.GCSFileSystem') as MockFileSystem:
21+
with mock.patch('gcsfs.GCSFileSystem') as MockFileSystem:
3222
instance = MockFileSystem.return_value
3323
instance.open.return_value = StringIO(df1.to_csv(index=False))
3424
df2 = read_csv('gs://test/test.csv', parse_dates=['dt'])
@@ -37,10 +27,10 @@ def test_read_csv_gcs(mock_patch):
3727

3828

3929
@td.skip_if_no('gcsfs')
40-
def test_gcs_get_filepath_or_buffer(mock_patch):
30+
def test_gcs_get_filepath_or_buffer(mock):
4131
df1 = DataFrame({'int': [1, 3], 'float': [2.0, np.nan], 'str': ['t', 's'],
4232
'dt': date_range('2018-06-18', periods=2)})
43-
with mock_patch('pandas.io.gcs.get_filepath_or_buffer') as MockGetFilepath:
33+
with mock.patch('pandas.io.gcs.get_filepath_or_buffer') as MockGetFilepath:
4434
MockGetFilepath.return_value = (StringIO(df1.to_csv(index=False)),
4535
None, None, False)
4636
df2 = read_csv('gs://test/test.csv', parse_dates=['dt'])

0 commit comments

Comments
 (0)