|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pandas import DataFrame, date_range, read_csv |
| 5 | +from pandas.compat import StringIO |
| 6 | +from pandas.io.common import is_gcs_url |
| 7 | +from pandas.util import _test_decorators as td |
| 8 | +from pandas.util.testing import assert_frame_equal |
| 9 | + |
| 10 | + |
| 11 | +def test_is_gcs_url(): |
| 12 | + assert is_gcs_url("gcs://pandas/somethingelse.com") |
| 13 | + assert is_gcs_url("gs://pandas/somethingelse.com") |
| 14 | + assert not is_gcs_url("s3://pandas/somethingelse.com") |
| 15 | + |
| 16 | + |
| 17 | +@td.skip_if_no('gcsfs') |
| 18 | +def test_read_csv_gcs(mock): |
| 19 | + df1 = DataFrame({'int': [1, 3], 'float': [2.0, np.nan], 'str': ['t', 's'], |
| 20 | + 'dt': date_range('2018-06-18', periods=2)}) |
| 21 | + with mock.patch('gcsfs.GCSFileSystem') as MockFileSystem: |
| 22 | + instance = MockFileSystem.return_value |
| 23 | + instance.open.return_value = StringIO(df1.to_csv(index=False)) |
| 24 | + df2 = read_csv('gs://test/test.csv', parse_dates=['dt']) |
| 25 | + |
| 26 | + assert_frame_equal(df1, df2) |
| 27 | + |
| 28 | + |
| 29 | +@td.skip_if_no('gcsfs') |
| 30 | +def test_gcs_get_filepath_or_buffer(mock): |
| 31 | + df1 = DataFrame({'int': [1, 3], 'float': [2.0, np.nan], 'str': ['t', 's'], |
| 32 | + 'dt': date_range('2018-06-18', periods=2)}) |
| 33 | + with mock.patch('pandas.io.gcs.get_filepath_or_buffer') as MockGetFilepath: |
| 34 | + MockGetFilepath.return_value = (StringIO(df1.to_csv(index=False)), |
| 35 | + None, None, False) |
| 36 | + df2 = read_csv('gs://test/test.csv', parse_dates=['dt']) |
| 37 | + |
| 38 | + assert_frame_equal(df1, df2) |
| 39 | + assert MockGetFilepath.called |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.skipif(td.safe_import('gcsfs'), |
| 43 | + reason='Only check when gcsfs not installed') |
| 44 | +def test_gcs_not_present_exception(): |
| 45 | + with pytest.raises(ImportError) as e: |
| 46 | + read_csv('gs://test/test.csv') |
| 47 | + assert 'gcsfs library is required' in str(e.value) |
0 commit comments