|
22 | 22 | import pytest
|
23 | 23 |
|
24 | 24 | from pandas.compat import _get_lzma_file, _import_lzma, is_platform_little_endian
|
| 25 | +import pandas.util._test_decorators as td |
25 | 26 |
|
26 | 27 | import pandas as pd
|
27 | 28 | from pandas import Index
|
@@ -390,3 +391,92 @@ def test_unicode_decode_error():
|
390 | 391 | # just test the columns are correct since the values are random
|
391 | 392 | excols = pd.Index(["a", "b", "c"])
|
392 | 393 | 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