Skip to content

Commit 3de08a1

Browse files
Add tests for Buffer and URL I/O
1 parent e5b8edd commit 3de08a1

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

pandas/tests/io/test_pickle.py

+90
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import pytest
2323

2424
from pandas.compat import _get_lzma_file, _import_lzma, is_platform_little_endian
25+
import pandas.util._test_decorators as td
2526

2627
import pandas as pd
2728
from pandas import Index
@@ -390,3 +391,92 @@ def test_unicode_decode_error():
390391
# just test the columns are correct since the values are random
391392
excols = pd.Index(["a", "b", "c"])
392393
tm.assert_index_equal(df.columns, excols)
394+
395+
396+
# ---------------------
397+
# tests for buffer I/O
398+
# ---------------------
399+
400+
401+
def test_pickle_buffer_roundtrip():
402+
with tm.ensure_clean() as path:
403+
df = tm.makeDataFrame()
404+
df.to_pickle(open(path, "wb"))
405+
result = pd.read_pickle(open(path, "rb"))
406+
tm.assert_frame_equal(df, result)
407+
408+
409+
# ---------------------
410+
# tests for URL I/O
411+
# ---------------------
412+
413+
414+
@pytest.mark.parametrize("mockurl", ["http://url.com", "ftp://test.com"])
415+
def test_pickle_generalurl_read(monkeypatch, mockurl):
416+
def python_pickler(obj, path):
417+
with open(path, "wb") as fh:
418+
pickle.dump(obj, fh, protocol=-1)
419+
420+
class MockReadResponse:
421+
def __init__(self, path):
422+
self.file = open(path, "rb")
423+
self.headers = {"Content-Encoding": None}
424+
425+
def read(self):
426+
return self.file.read()
427+
428+
def close(self):
429+
return self.file.close()
430+
431+
with tm.ensure_clean() as path:
432+
433+
def mock_urlopen_read(*args, **kwargs):
434+
return MockReadResponse(path)
435+
436+
df = tm.makeDataFrame()
437+
python_pickler(df, path)
438+
monkeypatch.setattr("urllib.request.urlopen", mock_urlopen_read)
439+
result = pd.read_pickle(mockurl)
440+
tm.assert_frame_equal(df, result)
441+
442+
443+
@td.skip_if_no("gcsfs")
444+
@pytest.mark.parametrize("mockurl", ["gs://gcs.com", "gcs://gcs.com"])
445+
def test_pickle_gcsurl_roundtrip(monkeypatch, mockurl):
446+
with tm.ensure_clean() as path:
447+
448+
class MockGCSFileSystem:
449+
def __init__(self, *args, **kwargs):
450+
pass
451+
452+
def open(self, *args):
453+
mode = args[1] or None
454+
f = open(path, mode)
455+
return f
456+
457+
monkeypatch.setattr("gcsfs.GCSFileSystem", MockGCSFileSystem)
458+
df = tm.makeDataFrame()
459+
df.to_pickle(mockurl)
460+
result = pd.read_pickle(mockurl)
461+
tm.assert_frame_equal(df, result)
462+
463+
464+
@td.skip_if_no("s3fs")
465+
@pytest.mark.parametrize("mockurl", ["s3://s3.com", "s3n://s3.com", "s3a://s3.com"])
466+
def test_pickle_s3url_roundtrip(monkeypatch, mockurl):
467+
with tm.ensure_clean() as path:
468+
469+
class MockS3FileSystem:
470+
def __init__(self, *args, **kwargs):
471+
pass
472+
473+
def open(self, *args):
474+
mode = args[1] or None
475+
f = open(path, mode)
476+
return f
477+
478+
monkeypatch.setattr("s3fs.S3FileSystem", MockS3FileSystem)
479+
df = tm.makeDataFrame()
480+
df.to_pickle(mockurl)
481+
result = pd.read_pickle(mockurl)
482+
tm.assert_frame_equal(df, result)

0 commit comments

Comments
 (0)