Skip to content

Commit 285a8f1

Browse files
committed
Fix more tests
1 parent dd3dcbf commit 285a8f1

File tree

5 files changed

+44
-62
lines changed

5 files changed

+44
-62
lines changed

pandas/conftest.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
utc,
4444
)
4545

46+
try:
47+
import zstandard as zstd
48+
except ImportError:
49+
zstd = None
50+
4651
import pandas.util._test_decorators as td
4752

4853
from pandas.core.dtypes.dtypes import (
@@ -266,15 +271,15 @@ def other_closed(request):
266271
return request.param
267272

268273

269-
@pytest.fixture(params=[None, "gzip", "bz2", "zip", "xz", "zstd"])
274+
@pytest.fixture(params=[None, "gzip", "bz2", "zip", "xz"] + (["zstd"] if zstd else []))
270275
def compression(request):
271276
"""
272277
Fixture for trying common compression types in compression tests.
273278
"""
274279
return request.param
275280

276281

277-
@pytest.fixture(params=["gzip", "bz2", "zip", "xz", "zstd"])
282+
@pytest.fixture(params=["gzip", "bz2", "zip", "xz"] + (["zstd"] if zstd else []))
278283
def compression_only(request):
279284
"""
280285
Fixture for trying common compression types in compression tests excluding

pandas/tests/io/parser/test_network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def check_compressed_urls(salaries_table, compression, extension, mode, engine):
4141
"pandas/tests/io/parser/data/salaries.csv"
4242
)
4343

44-
url = f"{base_url}.{extension}"
44+
url = base_url + extension
4545

4646
if mode != "explicit":
4747
compression = mode

pandas/tests/io/test_pickle.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ def test_write_explicit_bad(self, compression, get_random_path):
354354
"ext", ["", ".gz", ".bz2", ".zip", ".no_compress", ".xz", ".zst"]
355355
)
356356
def test_write_infer(self, ext, get_random_path):
357+
if ext == ".zst":
358+
pytest.importorskip("zstandard")
359+
357360
base = get_random_path
358361
path1 = base + ext
359362
path2 = base + ".raw"

pandas/tests/io/xml/test_to_xml.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
import pandas._testing as tm
1919

20+
import pandas.io.common as icom
2021
from pandas.io.common import get_handle
2122
from pandas.io.xml import read_xml
2223

@@ -1273,15 +1274,14 @@ def test_style_to_json():
12731274
</data>"""
12741275

12751276

1276-
@pytest.mark.parametrize("comp", ["bz2", "gzip", "xz", "zip", "zstd"])
1277-
def test_compression_output(parser, comp):
1277+
def test_compression_output(parser, compression_only):
12781278
with tm.ensure_clean() as path:
1279-
geom_df.to_xml(path, parser=parser, compression=comp)
1279+
geom_df.to_xml(path, parser=parser, compression=compression_only)
12801280

12811281
with get_handle(
12821282
path,
12831283
"r",
1284-
compression=comp,
1284+
compression=compression_only,
12851285
) as handle_obj:
12861286
output = handle_obj.handle.read()
12871287

@@ -1290,18 +1290,15 @@ def test_compression_output(parser, comp):
12901290
assert geom_xml == output.strip()
12911291

12921292

1293-
@pytest.mark.parametrize("comp", ["bz2", "gzip", "xz", "zip", "zstd"])
1294-
@pytest.mark.parametrize(
1295-
"compfile", ["xml.bz2", "xml.gz", "xml.xz", "xml.zip", "xml.zst"]
1296-
)
1297-
def test_filename_and_suffix_comp(parser, comp, compfile):
1293+
def test_filename_and_suffix_comp(parser, compression_only):
1294+
compfile = "xml." + icom._compression_to_extension[compression_only]
12981295
with tm.ensure_clean(filename=compfile) as path:
1299-
geom_df.to_xml(path, parser=parser, compression=comp)
1296+
geom_df.to_xml(path, parser=parser, compression=compression_only)
13001297

13011298
with get_handle(
13021299
path,
13031300
"r",
1304-
compression=comp,
1301+
compression=compression_only,
13051302
) as handle_obj:
13061303
output = handle_obj.handle.read()
13071304

pandas/tests/io/xml/test_xml.py

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
BytesIO,
55
StringIO,
66
)
7+
from lzma import LZMAError
78
import os
89
from urllib.error import HTTPError
10+
from zipfile import BadZipFile
911

1012
import numpy as np
1113
import pytest
1214

15+
try:
16+
from zstandard import ZstdError
17+
except ImportError:
18+
ZstdError = None
19+
1320
import pandas.util._test_decorators as td
1421

1522
from pandas import DataFrame
@@ -1014,65 +1021,35 @@ def test_online_stylesheet():
10141021
# COMPRESSION
10151022

10161023

1017-
@pytest.mark.parametrize("comp", ["bz2", "gzip", "xz", "zip", "zstd"])
1018-
def test_compression_read(parser, comp):
1024+
def test_compression_read(parser, compression_only):
10191025
with tm.ensure_clean() as path:
1020-
geom_df.to_xml(path, index=False, parser=parser, compression=comp)
1026+
geom_df.to_xml(path, index=False, parser=parser, compression=compression_only)
10211027

1022-
xml_df = read_xml(path, parser=parser, compression=comp)
1028+
xml_df = read_xml(path, parser=parser, compression=compression_only)
10231029

10241030
tm.assert_frame_equal(xml_df, geom_df)
10251031

10261032

1027-
@pytest.mark.parametrize("comp", ["gzip", "xz", "zip", "zstd"])
1028-
def test_wrong_compression_bz2(parser, comp):
1029-
with tm.ensure_clean() as path:
1030-
geom_df.to_xml(path, parser=parser, compression=comp)
1031-
1032-
with pytest.raises(OSError, match="Invalid data stream"):
1033-
read_xml(path, parser=parser, compression="bz2")
1034-
1035-
1036-
@pytest.mark.parametrize("comp", ["bz2", "xz", "zip", "zstd"])
1037-
def test_wrong_compression_gz(parser, comp):
1038-
with tm.ensure_clean() as path:
1039-
geom_df.to_xml(path, parser=parser, compression=comp)
1040-
1041-
with pytest.raises(OSError, match="Not a gzipped file"):
1042-
read_xml(path, parser=parser, compression="gzip")
1043-
1044-
1045-
@pytest.mark.parametrize("comp", ["bz2", "gzip", "zip", "zstd"])
1046-
def test_wrong_compression_xz(parser, comp):
1047-
from lzma import LZMAError
1048-
1049-
with tm.ensure_clean() as path:
1050-
geom_df.to_xml(path, parser=parser, compression=comp)
1051-
1052-
with pytest.raises(LZMAError, match="Input format not supported by decoder"):
1053-
read_xml(path, parser=parser, compression="xz")
1054-
1055-
1056-
@pytest.mark.parametrize("comp", ["bz2", "gzip", "xz", "zstd"])
1057-
def test_wrong_compression_zip(parser, comp):
1058-
from zipfile import BadZipFile
1059-
1060-
with tm.ensure_clean() as path:
1061-
geom_df.to_xml(path, parser=parser, compression=comp)
1062-
1063-
with pytest.raises(BadZipFile, match="File is not a zip file"):
1064-
read_xml(path, parser=parser, compression="zip")
1033+
def test_wrong_compression(parser, compression, compression_only):
1034+
actual_compression = compression
1035+
attempted_compression = compression_only
10651036

1037+
if actual_compression == attempted_compression:
1038+
return
10661039

1067-
@pytest.mark.parametrize("comp", ["bz2", "gzip", "xz", "zip"])
1068-
def test_wrong_compression_zstandard(parser, comp):
1069-
from zipfile import BadZipFile
1040+
error_cls, error_str = {
1041+
"bz2": (OSError, "Invalid data stream"),
1042+
"gzip": (OSError, "Not a gzipped file"),
1043+
"xz": (LZMAError, "Input format not supported by decoder"),
1044+
"zip": (BadZipFile, "File is not a zip file"),
1045+
"zstd": (ZstdError, "Unknown frame descriptor"),
1046+
}[attempted_compression]
10701047

10711048
with tm.ensure_clean() as path:
1072-
geom_df.to_xml(path, parser=parser, compression=comp)
1049+
geom_df.to_xml(path, parser=parser, compression=actual_compression)
10731050

1074-
with pytest.raises(BadZipFile, match="File is not a zip file"):
1075-
read_xml(path, parser=parser, compression="zstd")
1051+
with pytest.raises(error_cls, match=error_str):
1052+
read_xml(path, parser=parser, compression=attempted_compression)
10761053

10771054

10781055
def test_unsuported_compression(datapath, parser):

0 commit comments

Comments
 (0)