@@ -73,17 +73,19 @@ def filepath_or_buffer(filepath_or_buffer_id, tmp_path):
73
73
74
74
75
75
@pytest .fixture
76
- def assert_filepath_or_buffer_equals (filepath_or_buffer , filepath_or_buffer_id ):
76
+ def assert_filepath_or_buffer_equals (
77
+ filepath_or_buffer , filepath_or_buffer_id , encoding
78
+ ):
77
79
"""
78
80
Assertion helper for checking filepath_or_buffer.
79
81
"""
80
82
81
83
def _assert_filepath_or_buffer_equals (expected ):
82
84
if filepath_or_buffer_id == "string" :
83
- with open (filepath_or_buffer ) as f :
85
+ with open (filepath_or_buffer , encoding = encoding ) as f :
84
86
result = f .read ()
85
87
elif filepath_or_buffer_id == "pathlike" :
86
- result = filepath_or_buffer .read_text ()
88
+ result = filepath_or_buffer .read_text (encoding = encoding )
87
89
elif filepath_or_buffer_id == "buffer" :
88
90
result = filepath_or_buffer .getvalue ()
89
91
assert result == expected
@@ -3240,14 +3242,32 @@ def test_repr_html_ipython_config(ip):
3240
3242
3241
3243
3242
3244
@pytest .mark .parametrize ("method" , ["to_string" , "to_html" , "to_latex" ])
3245
+ @pytest .mark .parametrize (
3246
+ "encoding, data" ,
3247
+ [(None , "abc" ), ("utf-8" , "abc" ), ("gbk" , "造成输出中文显示乱码" ), ("foo" , "abc" )],
3248
+ )
3243
3249
def test_filepath_or_buffer_arg (
3244
- float_frame , method , filepath_or_buffer , assert_filepath_or_buffer_equals
3250
+ method ,
3251
+ filepath_or_buffer ,
3252
+ assert_filepath_or_buffer_equals ,
3253
+ encoding ,
3254
+ data ,
3255
+ filepath_or_buffer_id ,
3245
3256
):
3246
- df = float_frame
3247
- expected = getattr (df , method )()
3257
+ df = DataFrame ([data ])
3248
3258
3249
- getattr (df , method )(buf = filepath_or_buffer )
3250
- assert_filepath_or_buffer_equals (expected )
3259
+ if filepath_or_buffer_id not in ["string" , "pathlike" ] and encoding is not None :
3260
+ with pytest .raises (
3261
+ ValueError , match = "buf is not a file name and encoding is specified."
3262
+ ):
3263
+ getattr (df , method )(buf = filepath_or_buffer , encoding = encoding )
3264
+ elif encoding == "foo" :
3265
+ with pytest .raises (LookupError , match = "unknown encoding" ):
3266
+ getattr (df , method )(buf = filepath_or_buffer , encoding = encoding )
3267
+ else :
3268
+ expected = getattr (df , method )()
3269
+ getattr (df , method )(buf = filepath_or_buffer , encoding = encoding )
3270
+ assert_filepath_or_buffer_equals (expected )
3251
3271
3252
3272
3253
3273
@pytest .mark .parametrize ("method" , ["to_string" , "to_html" , "to_latex" ])
0 commit comments