Skip to content

Commit 63e6591

Browse files
committed
Consolidate testing to a single parametrized test
1 parent 6be808d commit 63e6591

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

pandas/tests/io/formats/test_to_csv.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
import gzip
43
import sys
54

65
import pytest
@@ -352,15 +351,3 @@ def test_to_csv_compression(self, compression_only,
352351
result = pd.read_csv(path, index_col=0,
353352
compression=read_compression)
354353
tm.assert_frame_equal(result, df)
355-
356-
def test_compression_defaults_to_infer():
357-
"""
358-
Test that to_csv defaults to inferring compression from paths.
359-
https://github.com/pandas-dev/pandas/pull/22011
360-
"""
361-
df = DataFrame({"A": [1]})
362-
with tm.ensure_clean('compressed.csv.gz') as path:
363-
df.to_csv(path, index=False)
364-
with gzip.open(path, 'rt') as read_file:
365-
lines = read_file.read().splitlines()
366-
assert lines == ['A', '1']

pandas/tests/test_common.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import numpy as np
99

10+
import pandas
1011
from pandas import Series, DataFrame, Timestamp
1112
from pandas.compat import range, lmap
1213
import pandas.core.common as com
@@ -222,19 +223,12 @@ def test_standardize_mapping():
222223
def test_compression_size(obj, method, compression_only):
223224
# Tests that compression is occurring by comparing to the bytes on disk of
224225
# the uncompressed file.
225-
extension = _compression_to_extension[compression_only]
226-
to_method = getattr(obj, method)
227-
with tm.ensure_clean('no-compression') as path:
228-
to_method(path, compression=None)
229-
no_compression_size = os.path.getsize(path)
230-
with tm.ensure_clean('explicit-compression' + extension) as path:
231-
to_method(path, compression=compression_only)
232-
explicit_compression_size = os.path.getsize(path)
233-
with tm.ensure_clean('inferred-compression' + extension) as path:
234-
to_method(path) # assumes that compression='infer' is the default
235-
inferred_compression_size = os.path.getsize(path)
236-
assert (no_compression_size > explicit_compression_size ==
237-
inferred_compression_size)
226+
with tm.ensure_clean() as filename:
227+
getattr(obj, method)(filename, compression=compression_only)
228+
compressed = os.path.getsize(filename)
229+
getattr(obj, method)(filename, compression=None)
230+
uncompressed = os.path.getsize(filename)
231+
assert uncompressed > compressed
238232

239233

240234
@pytest.mark.parametrize('obj', [
@@ -244,6 +238,7 @@ def test_compression_size(obj, method, compression_only):
244238
Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
245239
@pytest.mark.parametrize('method', ['to_csv', 'to_json'])
246240
def test_compression_size_fh(obj, method, compression_only):
241+
247242
with tm.ensure_clean() as filename:
248243
f, _handles = _get_handle(filename, 'w', compression=compression_only)
249244
with f:
@@ -261,6 +256,30 @@ def test_compression_size_fh(obj, method, compression_only):
261256
assert uncompressed > compressed
262257

263258

259+
@pytest.mark.parametrize('input', [
260+
DataFrame([[1.0, 0, -4.4],
261+
[3.4, 5, 2.4]], columns=['X', 'Y', 'Z']),
262+
Series([0, 1, 2, 4], name='X'),
263+
])
264+
@pytest.mark.parametrize('methods', [
265+
('to_csv', pandas.read_csv),
266+
('to_json', pandas.read_json),
267+
('to_pickle', pandas.read_pickle),
268+
])
269+
def test_compression_defaults_to_infer(input, methods, compression_only):
270+
# Test that to_* methods default to inferring compression from paths.
271+
# https://github.com/pandas-dev/pandas/pull/22011
272+
write_method, read_method = methods
273+
extension = _compression_to_extension[compression_only]
274+
with tm.ensure_clean('compressed' + extension) as path:
275+
# assumes that compression='infer' is the default
276+
getattr(input, write_method)(path)
277+
output = read_method(path, compression=compression_only)
278+
assert_equals = (tm.assert_frame_equal if isinstance(input, DataFrame)
279+
else tm.assert_series_equal)
280+
assert_equals(output, input)
281+
282+
264283
def test_compression_warning(compression_only):
265284
# Assert that passing a file object to to_csv while explicitly specifying a
266285
# compression protocol triggers a RuntimeWarning, as per

0 commit comments

Comments
 (0)