Skip to content

Commit 387d1d2

Browse files
committed
Move tests from tests/test_common.py to tests/io/test_common.py
1 parent 24e051e commit 387d1d2

File tree

2 files changed

+126
-133
lines changed

2 files changed

+126
-133
lines changed

pandas/tests/io/test_common.py

Lines changed: 125 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
Tests for the pandas.io.common functionalities
33
"""
44
import mmap
5-
import pytest
65
import os
7-
from os.path import isabs
6+
import pytest
87

98
import pandas as pd
10-
import pandas.util.testing as tm
9+
import pandas.io.common as cmn
1110
import pandas.util._test_decorators as td
12-
13-
from pandas.io import common
14-
from pandas.compat import is_platform_windows, StringIO, FileNotFoundError
15-
16-
from pandas import read_csv, concat
11+
import pandas.util.testing as tm
12+
from pandas.compat import (
13+
is_platform_windows,
14+
StringIO,
15+
FileNotFoundError,
16+
)
1717

1818

1919
class CustomFSPath(object):
@@ -55,36 +55,36 @@ class TestCommonIOCapabilities(object):
5555

5656
def test_expand_user(self):
5757
filename = '~/sometest'
58-
expanded_name = common._expand_user(filename)
58+
expanded_name = cmn._expand_user(filename)
5959

6060
assert expanded_name != filename
61-
assert isabs(expanded_name)
61+
assert os.path.isabs(expanded_name)
6262
assert os.path.expanduser(filename) == expanded_name
6363

6464
def test_expand_user_normal_path(self):
6565
filename = '/somefolder/sometest'
66-
expanded_name = common._expand_user(filename)
66+
expanded_name = cmn._expand_user(filename)
6767

6868
assert expanded_name == filename
6969
assert os.path.expanduser(filename) == expanded_name
7070

7171
@td.skip_if_no('pathlib')
7272
def test_stringify_path_pathlib(self):
73-
rel_path = common._stringify_path(Path('.'))
73+
rel_path = cmn._stringify_path(Path('.'))
7474
assert rel_path == '.'
75-
redundant_path = common._stringify_path(Path('foo//bar'))
75+
redundant_path = cmn._stringify_path(Path('foo//bar'))
7676
assert redundant_path == os.path.join('foo', 'bar')
7777

7878
@td.skip_if_no('py.path')
7979
def test_stringify_path_localpath(self):
8080
path = os.path.join('foo', 'bar')
8181
abs_path = os.path.abspath(path)
8282
lpath = LocalPath(path)
83-
assert common._stringify_path(lpath) == abs_path
83+
assert cmn._stringify_path(lpath) == abs_path
8484

8585
def test_stringify_path_fspath(self):
8686
p = CustomFSPath('foo/bar.csv')
87-
result = common._stringify_path(p)
87+
result = cmn._stringify_path(p)
8888
assert result == 'foo/bar.csv'
8989

9090
@pytest.mark.parametrize('extension,expected', [
@@ -97,36 +97,36 @@ def test_stringify_path_fspath(self):
9797
@pytest.mark.parametrize('path_type', path_types)
9898
def test_infer_compression_from_path(self, extension, expected, path_type):
9999
path = path_type('foo/bar.csv' + extension)
100-
compression = common._infer_compression(path, compression='infer')
100+
compression = cmn._infer_compression(path, compression='infer')
101101
assert compression == expected
102102

103103
def test_get_filepath_or_buffer_with_path(self):
104104
filename = '~/sometest'
105-
filepath_or_buffer, _, _, should_close = common.get_filepath_or_buffer(
105+
filepath_or_buffer, _, _, should_close = cmn.get_filepath_or_buffer(
106106
filename)
107107
assert filepath_or_buffer != filename
108-
assert isabs(filepath_or_buffer)
108+
assert os.path.isabs(filepath_or_buffer)
109109
assert os.path.expanduser(filename) == filepath_or_buffer
110110
assert not should_close
111111

112112
def test_get_filepath_or_buffer_with_buffer(self):
113113
input_buffer = StringIO()
114-
filepath_or_buffer, _, _, should_close = common.get_filepath_or_buffer(
114+
filepath_or_buffer, _, _, should_close = cmn.get_filepath_or_buffer(
115115
input_buffer)
116116
assert filepath_or_buffer == input_buffer
117117
assert not should_close
118118

119119
def test_iterator(self):
120-
reader = read_csv(StringIO(self.data1), chunksize=1)
121-
result = concat(reader, ignore_index=True)
122-
expected = read_csv(StringIO(self.data1))
120+
reader = pd.read_csv(StringIO(self.data1), chunksize=1)
121+
result = pd.concat(reader, ignore_index=True)
122+
expected = pd.read_csv(StringIO(self.data1))
123123
tm.assert_frame_equal(result, expected)
124124

125125
# GH12153
126-
it = read_csv(StringIO(self.data1), chunksize=1)
126+
it = pd.read_csv(StringIO(self.data1), chunksize=1)
127127
first = next(it)
128128
tm.assert_frame_equal(first, expected.iloc[[0]])
129-
tm.assert_frame_equal(concat(it), expected.iloc[1:])
129+
tm.assert_frame_equal(pd.concat(it), expected.iloc[1:])
130130

131131
@pytest.mark.parametrize('reader, module, error_class, fn_ext', [
132132
(pd.read_csv, 'os', FileNotFoundError, 'csv'),
@@ -246,18 +246,18 @@ def test_constructor_bad_file(self, mmap_file):
246246
msg = "[Errno 22]"
247247
err = mmap.error
248248

249-
tm.assert_raises_regex(err, msg, common.MMapWrapper, non_file)
249+
tm.assert_raises_regex(err, msg, cmn.MMapWrapper, non_file)
250250

251251
target = open(mmap_file, 'r')
252252
target.close()
253253

254254
msg = "I/O operation on closed file"
255255
tm.assert_raises_regex(
256-
ValueError, msg, common.MMapWrapper, target)
256+
ValueError, msg, cmn.MMapWrapper, target)
257257

258258
def test_get_attr(self, mmap_file):
259259
with open(mmap_file, 'r') as target:
260-
wrapper = common.MMapWrapper(target)
260+
wrapper = cmn.MMapWrapper(target)
261261

262262
attrs = dir(wrapper.mmap)
263263
attrs = [attr for attr in attrs
@@ -271,7 +271,7 @@ def test_get_attr(self, mmap_file):
271271

272272
def test_next(self, mmap_file):
273273
with open(mmap_file, 'r') as target:
274-
wrapper = common.MMapWrapper(target)
274+
wrapper = cmn.MMapWrapper(target)
275275
lines = target.readlines()
276276

277277
for line in lines:
@@ -285,4 +285,100 @@ def test_unknown_engine(self):
285285
df = tm.makeDataFrame()
286286
df.to_csv(path)
287287
with tm.assert_raises_regex(ValueError, 'Unknown engine'):
288-
read_csv(path, engine='pyt')
288+
pd.read_csv(path, engine='pyt')
289+
290+
291+
@pytest.mark.parametrize('obj', [
292+
pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
293+
[12.32112, 123123.2, 321321.2]],
294+
columns=['X', 'Y', 'Z']),
295+
pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
296+
@pytest.mark.parametrize('method', ['to_pickle', 'to_json', 'to_csv'])
297+
def test_compression_size(obj, method, compression_only):
298+
299+
with tm.ensure_clean() as path:
300+
getattr(obj, method)(path, compression=compression_only)
301+
compressed = os.path.getsize(path)
302+
getattr(obj, method)(path, compression=None)
303+
uncompressed = os.path.getsize(path)
304+
assert uncompressed > compressed
305+
306+
307+
@pytest.mark.parametrize('obj', [
308+
pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
309+
[12.32112, 123123.2, 321321.2]],
310+
columns=['X', 'Y', 'Z']),
311+
pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
312+
@pytest.mark.parametrize('method', ['to_csv', 'to_json'])
313+
def test_compression_size_fh(obj, method, compression_only):
314+
315+
with tm.ensure_clean() as path:
316+
f, handles = cmn._get_handle(path, 'w', compression=compression_only)
317+
with f:
318+
getattr(obj, method)(f)
319+
assert not f.closed
320+
assert f.closed
321+
compressed = os.path.getsize(path)
322+
with tm.ensure_clean() as path:
323+
f, handles = cmn._get_handle(path, 'w', compression=None)
324+
with f:
325+
getattr(obj, method)(f)
326+
assert not f.closed
327+
assert f.closed
328+
uncompressed = os.path.getsize(path)
329+
assert uncompressed > compressed
330+
331+
332+
@pytest.mark.parametrize('write_method, write_kwargs, read_method', [
333+
('to_csv', {'index': False}, pd.read_csv),
334+
('to_json', {}, pd.read_json),
335+
('to_pickle', {}, pd.read_pickle),
336+
])
337+
def test_dataframe_compression_defaults_to_infer(
338+
write_method, write_kwargs, read_method, compression_only):
339+
# Test that DataFrame.to_* methods default to inferring compression from
340+
# paths. GH 22004
341+
input = pd.DataFrame([[1.0, 0, -4], [3.4, 5, 2]], columns=['X', 'Y', 'Z'])
342+
extension = cmn._compression_to_extension[compression_only]
343+
with tm.ensure_clean('compressed' + extension) as path:
344+
getattr(input, write_method)(path, **write_kwargs)
345+
output = read_method(path, compression=compression_only)
346+
tm.assert_frame_equal(output, input)
347+
348+
349+
@pytest.mark.parametrize('write_method,write_kwargs,read_method,read_kwargs', [
350+
('to_csv', {'index': False, 'header': True},
351+
pd.read_csv, {'squeeze': True}),
352+
('to_json', {}, pd.read_json, {'typ': 'series'}),
353+
('to_pickle', {}, pd.read_pickle, {}),
354+
])
355+
def test_series_compression_defaults_to_infer(
356+
write_method, write_kwargs, read_method, read_kwargs,
357+
compression_only):
358+
# Test that Series.to_* methods default to inferring compression from
359+
# paths. GH 22004
360+
input = pd.Series([0, 5, -2, 10], name='X')
361+
extension = cmn._compression_to_extension[compression_only]
362+
with tm.ensure_clean('compressed' + extension) as path:
363+
getattr(input, write_method)(path, **write_kwargs)
364+
output = read_method(path, compression=compression_only, **read_kwargs)
365+
tm.assert_series_equal(output, input, check_names=False)
366+
367+
368+
def test_compression_warning(compression_only):
369+
# Assert that passing a file object to to_csv while explicitly specifying a
370+
# compression protocol triggers a RuntimeWarning, as per GH 21227.
371+
# Note that pytest has an issue that causes assert_produces_warning to fail
372+
# in Python 2 if the warning has occurred in previous tests
373+
# (see https://git.io/fNEBm & https://git.io/fNEBC). Hence, should this
374+
# test fail in just Python 2 builds, it likely indicates that other tests
375+
# are producing RuntimeWarnings, thereby triggering the pytest bug.
376+
df = pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
377+
[12.32112, 123123.2, 321321.2]],
378+
columns=['X', 'Y', 'Z'])
379+
with tm.ensure_clean() as path:
380+
f, handles = cmn._get_handle(path, 'w', compression=compression_only)
381+
with tm.assert_produces_warning(RuntimeWarning,
382+
check_stacklevel=False):
383+
with f:
384+
df.to_csv(f, compression=compression_only)

pandas/tests/test_common.py

Lines changed: 1 addition & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
# -*- coding: utf-8 -*-
22

33
import pytest
4-
import os
54
import collections
65
from functools import partial
76

87
import numpy as np
98

10-
import pandas
11-
from pandas import Series, DataFrame, Timestamp
9+
from pandas import Series, Timestamp
1210
import pandas.core.common as com
1311
from pandas.core import ops
14-
from pandas.io.common import (
15-
_compression_to_extension,
16-
_get_handle,
17-
)
18-
import pandas.util.testing as tm
1912

2013

2114
def test_get_callable_name():
@@ -115,99 +108,3 @@ def test_standardize_mapping():
115108

116109
dd = collections.defaultdict(list)
117110
assert isinstance(com.standardize_mapping(dd), partial)
118-
119-
120-
@pytest.mark.parametrize('obj', [
121-
DataFrame(100 * [[0.123456, 0.234567, 0.567567],
122-
[12.32112, 123123.2, 321321.2]],
123-
columns=['X', 'Y', 'Z']),
124-
Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
125-
@pytest.mark.parametrize('method', ['to_pickle', 'to_json', 'to_csv'])
126-
def test_compression_size(obj, method, compression_only):
127-
128-
with tm.ensure_clean() as filename:
129-
getattr(obj, method)(filename, compression=compression_only)
130-
compressed = os.path.getsize(filename)
131-
getattr(obj, method)(filename, compression=None)
132-
uncompressed = os.path.getsize(filename)
133-
assert uncompressed > compressed
134-
135-
136-
@pytest.mark.parametrize('obj', [
137-
DataFrame(100 * [[0.123456, 0.234567, 0.567567],
138-
[12.32112, 123123.2, 321321.2]],
139-
columns=['X', 'Y', 'Z']),
140-
Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
141-
@pytest.mark.parametrize('method', ['to_csv', 'to_json'])
142-
def test_compression_size_fh(obj, method, compression_only):
143-
144-
with tm.ensure_clean() as filename:
145-
f, _handles = _get_handle(filename, 'w', compression=compression_only)
146-
with f:
147-
getattr(obj, method)(f)
148-
assert not f.closed
149-
assert f.closed
150-
compressed = os.path.getsize(filename)
151-
with tm.ensure_clean() as filename:
152-
f, _handles = _get_handle(filename, 'w', compression=None)
153-
with f:
154-
getattr(obj, method)(f)
155-
assert not f.closed
156-
assert f.closed
157-
uncompressed = os.path.getsize(filename)
158-
assert uncompressed > compressed
159-
160-
161-
@pytest.mark.parametrize('write_method, write_kwargs, read_method', [
162-
('to_csv', {'index': False}, pandas.read_csv),
163-
('to_json', {}, pandas.read_json),
164-
('to_pickle', {}, pandas.read_pickle),
165-
])
166-
def test_dataframe_compression_defaults_to_infer(
167-
write_method, write_kwargs, read_method, compression_only):
168-
# Test that DataFrame.to_* methods default to inferring compression from
169-
# paths. GH 22004
170-
input = DataFrame([[1.0, 0, -4.4], [3.4, 5, 2.4]], columns=['X', 'Y', 'Z'])
171-
extension = _compression_to_extension[compression_only]
172-
with tm.ensure_clean('compressed' + extension) as path:
173-
getattr(input, write_method)(path, **write_kwargs)
174-
output = read_method(path, compression=compression_only)
175-
tm.assert_frame_equal(output, input)
176-
177-
178-
@pytest.mark.parametrize('write_method,write_kwargs,read_method,read_kwargs', [
179-
('to_csv', {'index': False, 'header': True},
180-
pandas.read_csv, {'squeeze': True}),
181-
('to_json', {}, pandas.read_json, {'typ': 'series'}),
182-
('to_pickle', {}, pandas.read_pickle, {}),
183-
])
184-
def test_series_compression_defaults_to_infer(
185-
write_method, write_kwargs, read_method, read_kwargs,
186-
compression_only):
187-
# Test that Series.to_* methods default to inferring compression from
188-
# paths. GH 22004
189-
input = Series([0, 5, -2, 10], name='X')
190-
extension = _compression_to_extension[compression_only]
191-
with tm.ensure_clean('compressed' + extension) as path:
192-
getattr(input, write_method)(path, **write_kwargs)
193-
output = read_method(path, compression=compression_only, **read_kwargs)
194-
tm.assert_series_equal(output, input, check_names=False)
195-
196-
197-
def test_compression_warning(compression_only):
198-
# Assert that passing a file object to to_csv while explicitly specifying a
199-
# compression protocol triggers a RuntimeWarning, as per GH 21227.
200-
# Note that pytest has an issue that causes assert_produces_warning to fail
201-
# in Python 2 if the warning has occurred in previous tests
202-
# (see https://git.io/fNEBm & https://git.io/fNEBC). Hence, should this
203-
# test fail in just Python 2 builds, it likely indicates that other tests
204-
# are producing RuntimeWarnings, thereby triggering the pytest bug.
205-
df = DataFrame(100 * [[0.123456, 0.234567, 0.567567],
206-
[12.32112, 123123.2, 321321.2]],
207-
columns=['X', 'Y', 'Z'])
208-
with tm.ensure_clean() as filename:
209-
f, _handles = _get_handle(filename, 'w', compression=compression_only)
210-
with tm.assert_produces_warning(RuntimeWarning,
211-
check_stacklevel=False):
212-
with f:
213-
df.to_csv(f, compression=compression_only)

0 commit comments

Comments
 (0)