@@ -35,59 +35,61 @@ def parser_and_data(all_parsers, csv1):
35
35
36
36
37
37
@pytest .mark .parametrize ("compression" , ["zip" , "infer" , "zip2" ])
38
- def test_zip (parser_and_data , compression ):
38
+ def test_zip (tmp_path , parser_and_data , compression ):
39
39
parser , data , expected = parser_and_data
40
40
41
- with tm . ensure_clean ( "test_file.zip" ) as path :
42
- with zipfile .ZipFile (path , mode = "w" ) as tmp :
43
- tmp .writestr ("test_file" , data )
41
+ path = tmp_path / "test_file.zip"
42
+ with zipfile .ZipFile (path , mode = "w" ) as tmp :
43
+ tmp .writestr ("test_file" , data )
44
44
45
- if compression == "zip2" :
46
- with open (path , "rb" ) as f :
47
- result = parser .read_csv (f , compression = "zip" )
48
- else :
49
- result = parser .read_csv (path , compression = compression )
45
+ if compression == "zip2" :
46
+ with open (path , "rb" ) as f :
47
+ result = parser .read_csv (f , compression = "zip" )
48
+ else :
49
+ result = parser .read_csv (path , compression = compression )
50
50
51
- tm .assert_frame_equal (result , expected )
51
+ tm .assert_frame_equal (result , expected )
52
52
53
53
54
54
@pytest .mark .parametrize ("compression" , ["zip" , "infer" ])
55
- def test_zip_error_multiple_files (parser_and_data , compression ):
55
+ def test_zip_error_multiple_files (tmp_path , parser_and_data , compression ):
56
56
parser , data , expected = parser_and_data
57
57
58
- with tm . ensure_clean ( "combined_zip.zip" ) as path :
59
- inner_file_names = ["test_file" , "second_file" ]
58
+ path = tmp_path / "combined_zip.zip"
59
+ inner_file_names = ["test_file" , "second_file" ]
60
60
61
- with zipfile .ZipFile (path , mode = "w" ) as tmp :
62
- for file_name in inner_file_names :
63
- tmp .writestr (file_name , data )
61
+ with zipfile .ZipFile (path , mode = "w" ) as tmp :
62
+ for file_name in inner_file_names :
63
+ tmp .writestr (file_name , data )
64
64
65
- with pytest .raises (ValueError , match = "Multiple files" ):
66
- parser .read_csv (path , compression = compression )
65
+ with pytest .raises (ValueError , match = "Multiple files" ):
66
+ parser .read_csv (path , compression = compression )
67
67
68
68
69
- def test_zip_error_no_files (parser_and_data ):
69
+ def test_zip_error_no_files (tmp_path , parser_and_data ):
70
70
parser , _ , _ = parser_and_data
71
71
72
- with tm . ensure_clean () as path :
73
- with zipfile .ZipFile (path , mode = "w" ):
74
- pass
72
+ path = tmp_path / "test_file.zip"
73
+ with zipfile .ZipFile (path , mode = "w" ):
74
+ pass
75
75
76
- with pytest .raises (ValueError , match = "Zero files" ):
77
- parser .read_csv (path , compression = "zip" )
76
+ with pytest .raises (ValueError , match = "Zero files" ):
77
+ parser .read_csv (path , compression = "zip" )
78
78
79
79
80
- def test_zip_error_invalid_zip (parser_and_data ):
80
+ def test_zip_error_invalid_zip (tmp_path , parser_and_data ):
81
81
parser , _ , _ = parser_and_data
82
82
83
- with tm .ensure_clean () as path :
84
- with open (path , "rb" ) as f :
85
- with pytest .raises (zipfile .BadZipFile , match = "File is not a zip file" ):
86
- parser .read_csv (f , compression = "zip" )
83
+ path = tmp_path / "invalid_file.zip"
84
+ path .touch ()
85
+ with open (path , "rb" ) as f :
86
+ with pytest .raises (zipfile .BadZipFile , match = "File is not a zip file" ):
87
+ parser .read_csv (f , compression = "zip" )
87
88
88
89
89
90
@pytest .mark .parametrize ("filename" , [None , "test.{ext}" ])
90
91
def test_compression (
92
+ tmp_path ,
91
93
request ,
92
94
parser_and_data ,
93
95
compression_only ,
@@ -108,17 +110,17 @@ def test_compression(
108
110
)
109
111
)
110
112
111
- with tm . ensure_clean ( filename = filename ) as path :
112
- tm .write_to_compressed (compress_type , path , data )
113
- compression = "infer" if filename else compress_type
113
+ path = tmp_path / filename if filename else tmp_path / "test_file"
114
+ tm .write_to_compressed (compress_type , path , data )
115
+ compression = "infer" if filename else compress_type
114
116
115
- if buffer :
116
- with open (path , "rb" ) as f :
117
- result = parser .read_csv (f , compression = compression )
118
- else :
119
- result = parser .read_csv (path , compression = compression )
117
+ if buffer :
118
+ with open (path , "rb" ) as f :
119
+ result = parser .read_csv (f , compression = compression )
120
+ else :
121
+ result = parser .read_csv (path , compression = compression )
120
122
121
- tm .assert_frame_equal (result , expected )
123
+ tm .assert_frame_equal (result , expected )
122
124
123
125
124
126
@pytest .mark .parametrize ("ext" , [None , "gz" , "bz2" ])
@@ -175,37 +177,38 @@ def test_compression_tar_archive(all_parsers, csv_dir_path):
175
177
assert list (df .columns ) == ["a" ]
176
178
177
179
178
- def test_ignore_compression_extension (all_parsers ):
180
+ def test_ignore_compression_extension (tmp_path , all_parsers ):
179
181
parser = all_parsers
180
182
df = DataFrame ({"a" : [0 , 1 ]})
181
- with tm .ensure_clean ("test.csv" ) as path_csv :
182
- with tm .ensure_clean ("test.csv.zip" ) as path_zip :
183
- # make sure to create un-compressed file with zip extension
184
- df .to_csv (path_csv , index = False )
185
- Path (path_zip ).write_text (
186
- Path (path_csv ).read_text (encoding = "utf-8" ), encoding = "utf-8"
187
- )
188
183
189
- tm .assert_frame_equal (parser .read_csv (path_zip , compression = None ), df )
184
+ path_csv = tmp_path / "test.csv"
185
+ path_zip = tmp_path / "test.csv.zip"
186
+ # make sure to create un-compressed file with zip extension
187
+ df .to_csv (path_csv , index = False )
188
+ Path (path_zip ).write_text (
189
+ Path (path_csv ).read_text (encoding = "utf-8" ), encoding = "utf-8"
190
+ )
191
+
192
+ tm .assert_frame_equal (parser .read_csv (path_zip , compression = None ), df )
190
193
191
194
192
- def test_writes_tar_gz (all_parsers ):
195
+ def test_writes_tar_gz (tmp_path , all_parsers ):
193
196
parser = all_parsers
194
197
data = DataFrame (
195
198
{
196
199
"Country" : ["Venezuela" , "Venezuela" ],
197
200
"Twitter" : ["Hugo Chávez Frías" , "Henrique Capriles R." ],
198
201
}
199
202
)
200
- with tm . ensure_clean ( "test.tar.gz" ) as tar_path :
201
- data .to_csv (tar_path , index = False )
203
+ tar_path = tmp_path / "test.tar.gz"
204
+ data .to_csv (tar_path , index = False )
202
205
203
- # test that read_csv infers .tar.gz to gzip:
204
- tm .assert_frame_equal (parser .read_csv (tar_path ), data )
206
+ # test that read_csv infers .tar.gz to gzip:
207
+ tm .assert_frame_equal (parser .read_csv (tar_path ), data )
205
208
206
- # test that file is indeed gzipped:
207
- with tarfile .open (tar_path , "r:gz" ) as tar :
208
- result = parser .read_csv (
209
- tar .extractfile (tar .getnames ()[0 ]), compression = "infer"
210
- )
211
- tm .assert_frame_equal (result , data )
209
+ # test that file is indeed gzipped:
210
+ with tarfile .open (tar_path , "r:gz" ) as tar :
211
+ result = parser .read_csv (
212
+ tar .extractfile (tar .getnames ()[0 ]), compression = "infer"
213
+ )
214
+ tm .assert_frame_equal (result , data )
0 commit comments