|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pandas import DataFrame, read_csv |
| 4 | +from pandas.compat import BytesIO |
| 5 | +from pandas.io.common import is_gcs_url |
| 6 | +from pandas.util import _test_decorators as td |
| 7 | + |
| 8 | + |
| 9 | +def test_is_gcs_url(): |
| 10 | + assert is_gcs_url("gcs://pandas/somethingelse.com") |
| 11 | + assert is_gcs_url("gs://pandas/somethingelse.com") |
| 12 | + assert not is_gcs_url("s3://pandas/somethingelse.com") |
| 13 | + |
| 14 | + |
| 15 | +@td.skip_if_no('gcsfs') |
| 16 | +def test_read_csv_gcs(): |
| 17 | + try: |
| 18 | + from unittest.mock import patch |
| 19 | + except ImportError: |
| 20 | + from mock import patch |
| 21 | + |
| 22 | + with patch('gcsfs.GCSFileSystem') as MockFileSystem: |
| 23 | + instance = MockFileSystem.return_value |
| 24 | + instance.open.return_value = BytesIO(b'a,b\n1,2\n3,4') |
| 25 | + df = read_csv('gs://test/test.csv') |
| 26 | + |
| 27 | + assert isinstance(df, DataFrame) |
| 28 | + assert len(df == 2) |
| 29 | + assert all(df.columns == ['a', 'b']) |
| 30 | + |
| 31 | + |
| 32 | +@td.skip_if_no('gcsfs') |
| 33 | +def test_gcs_get_filepath_or_buffer(): |
| 34 | + try: |
| 35 | + from unittest.mock import patch |
| 36 | + except ImportError: |
| 37 | + from mock import patch |
| 38 | + |
| 39 | + with patch('pandas.io.gcs.get_filepath_or_buffer') as MockGetFilepath: |
| 40 | + MockGetFilepath.return_value = (BytesIO(b'a,b\n1,2\n3,4'), None, None, |
| 41 | + False) |
| 42 | + df = read_csv('gs://test/test.csv') |
| 43 | + |
| 44 | + assert isinstance(df, DataFrame) |
| 45 | + assert len(df == 2) |
| 46 | + assert all(df.columns == ['a', 'b']) |
| 47 | + assert MockGetFilepath.called |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.skipif(td.safe_import('gcsfs'), |
| 51 | + reason='Only check when gcsfs not installed') |
| 52 | +def test_gcs_not_present_exception(): |
| 53 | + with pytest.raises(ImportError) as e: |
| 54 | + read_csv('gs://test/test.csv') |
| 55 | + assert 'gcsfs library is required' in str(e.value) |
0 commit comments