|
11 | 11 | from pandas.compat import range, lmap |
12 | 12 | import pandas.core.common as com |
13 | 13 | from pandas.core import ops |
| 14 | +from pandas.io.common import _get_handle |
14 | 15 | import pandas.util.testing as tm |
15 | 16 |
|
16 | 17 |
|
@@ -248,19 +249,34 @@ def test_compression_size(obj, method, compression): |
248 | 249 | [12.32112, 123123.2, 321321.2]], |
249 | 250 | columns=['X', 'Y', 'Z']), |
250 | 251 | Series(100 * [0.123456, 0.234567, 0.567567], name='X')]) |
251 | | -@pytest.mark.parametrize('method', ['to_csv']) |
| 252 | +@pytest.mark.parametrize('method', ['to_csv', 'to_json']) |
252 | 253 | def test_compression_size_fh(obj, method, compression_only): |
253 | 254 |
|
254 | 255 | with tm.ensure_clean() as filename: |
255 | | - with open(filename, 'w') as fh: |
256 | | - getattr(obj, method)(fh, compression=compression_only) |
257 | | - assert not fh.closed |
258 | | - assert fh.closed |
| 256 | + f, _handles = _get_handle(filename, 'w', compression=compression_only) |
| 257 | + with f: |
| 258 | + getattr(obj, method)(f) |
| 259 | + assert not f.closed |
| 260 | + assert f.closed |
259 | 261 | compressed = os.path.getsize(filename) |
260 | 262 | with tm.ensure_clean() as filename: |
261 | | - with open(filename, 'w') as fh: |
262 | | - getattr(obj, method)(fh, compression=None) |
263 | | - assert not fh.closed |
264 | | - assert fh.closed |
| 263 | + f, _handles = _get_handle(filename, 'w', compression=None) |
| 264 | + with f: |
| 265 | + getattr(obj, method)(f) |
| 266 | + assert not f.closed |
| 267 | + assert f.closed |
265 | 268 | uncompressed = os.path.getsize(filename) |
266 | 269 | assert uncompressed > compressed |
| 270 | + |
| 271 | + |
| 272 | +# GH 21227 |
| 273 | +def test_compression_warning(compression_only): |
| 274 | + df = DataFrame(100 * [[0.123456, 0.234567, 0.567567], |
| 275 | + [12.32112, 123123.2, 321321.2]], |
| 276 | + columns=['X', 'Y', 'Z']) |
| 277 | + with tm.ensure_clean() as filename: |
| 278 | + f, _handles = _get_handle(filename, 'w', compression=compression_only) |
| 279 | + with tm.assert_produces_warning(RuntimeWarning, |
| 280 | + check_stacklevel=False): |
| 281 | + with f: |
| 282 | + df.to_csv(f, compression=compression_only) |
0 commit comments