-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: Made s3 related tests mock boto #17388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ cython | |
pytest>=3.1.0 | ||
pytest-cov | ||
flake8 | ||
moto |
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,20 @@ | |
Tests parsers ability to read and parse non-local files | ||
and hence require a network connection to be read. | ||
""" | ||
|
||
import os | ||
|
||
import pytest | ||
import moto | ||
|
||
import pandas.util.testing as tm | ||
from pandas import DataFrame | ||
from pandas.io.parsers import read_csv, read_table | ||
from pandas.compat import BytesIO | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def tips_file(): | ||
return os.path.join(tm.get_data_path(), 'tips.csv') | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
|
@@ -19,6 +26,40 @@ def salaries_table(): | |
return read_table(path) | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def s3_resource(tips_file): | ||
pytest.importorskip('s3fs') | ||
moto.mock_s3().start() | ||
|
||
test_s3_files = [ | ||
('tips.csv', tips_file), | ||
('tips.csv.gz', tips_file + '.gz'), | ||
('tips.csv.bz2', tips_file + '.bz2'), | ||
] | ||
|
||
def add_tips_files(bucket_name): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this becomes inline when u use parametrize |
||
for s3_key, file_name in test_s3_files: | ||
with open(file_name, 'rb') as f: | ||
conn.Bucket(bucket_name).put_object( | ||
Key=s3_key, | ||
Body=f) | ||
|
||
boto3 = pytest.importorskip('boto3') | ||
# see gh-16135 | ||
bucket = 'pandas-test' | ||
|
||
conn = boto3.resource("s3", region_name="us-east-1") | ||
conn.create_bucket(Bucket=bucket) | ||
add_tips_files(bucket) | ||
|
||
conn.create_bucket(Bucket='cant_get_it', ACL='private') | ||
add_tips_files('cant_get_it') | ||
|
||
yield conn | ||
|
||
moto.mock_s3().stop() | ||
|
||
|
||
@pytest.mark.network | ||
@pytest.mark.parametrize( | ||
"compression,extension", | ||
|
@@ -51,15 +92,11 @@ def check_compressed_urls(salaries_table, compression, extension, mode, | |
|
||
|
||
class TestS3(object): | ||
|
||
def setup_method(self, method): | ||
try: | ||
import s3fs # noqa | ||
except ImportError: | ||
pytest.skip("s3fs not installed") | ||
|
||
@tm.network | ||
def test_parse_public_s3_bucket(self): | ||
pytest.importorskip('s3fs') | ||
# more of an integration test due to the not-public contents portion | ||
# can probably mock this though. | ||
for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: | ||
df = read_csv('s3://pandas-test/tips.csv' + | ||
ext, compression=comp) | ||
|
@@ -74,26 +111,24 @@ def test_parse_public_s3_bucket(self): | |
assert not df.empty | ||
tm.assert_frame_equal(read_csv(tm.get_data_path('tips.csv')), df) | ||
|
||
@tm.network | ||
def test_parse_public_s3n_bucket(self): | ||
def test_parse_public_s3n_bucket(self, s3_resource): | ||
|
||
# Read from AWS s3 as "s3n" URL | ||
df = read_csv('s3n://pandas-test/tips.csv', nrows=10) | ||
assert isinstance(df, DataFrame) | ||
assert not df.empty | ||
tm.assert_frame_equal(read_csv( | ||
tm.get_data_path('tips.csv')).iloc[:10], df) | ||
|
||
@tm.network | ||
def test_parse_public_s3a_bucket(self): | ||
def test_parse_public_s3a_bucket(self, s3_resource): | ||
# Read from AWS s3 as "s3a" URL | ||
df = read_csv('s3a://pandas-test/tips.csv', nrows=10) | ||
assert isinstance(df, DataFrame) | ||
assert not df.empty | ||
tm.assert_frame_equal(read_csv( | ||
tm.get_data_path('tips.csv')).iloc[:10], df) | ||
|
||
@tm.network | ||
def test_parse_public_s3_bucket_nrows(self): | ||
def test_parse_public_s3_bucket_nrows(self, s3_resource): | ||
for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: | ||
df = read_csv('s3://pandas-test/tips.csv' + | ||
ext, nrows=10, compression=comp) | ||
|
@@ -102,8 +137,7 @@ def test_parse_public_s3_bucket_nrows(self): | |
tm.assert_frame_equal(read_csv( | ||
tm.get_data_path('tips.csv')).iloc[:10], df) | ||
|
||
@tm.network | ||
def test_parse_public_s3_bucket_chunked(self): | ||
def test_parse_public_s3_bucket_chunked(self, s3_resource): | ||
# Read with a chunksize | ||
chunksize = 5 | ||
local_tips = read_csv(tm.get_data_path('tips.csv')) | ||
|
@@ -121,8 +155,7 @@ def test_parse_public_s3_bucket_chunked(self): | |
chunksize * i_chunk: chunksize * (i_chunk + 1)] | ||
tm.assert_frame_equal(true_df, df) | ||
|
||
@tm.network | ||
def test_parse_public_s3_bucket_chunked_python(self): | ||
def test_parse_public_s3_bucket_chunked_python(self, s3_resource): | ||
# Read with a chunksize using the Python parser | ||
chunksize = 5 | ||
local_tips = read_csv(tm.get_data_path('tips.csv')) | ||
|
@@ -140,8 +173,7 @@ def test_parse_public_s3_bucket_chunked_python(self): | |
chunksize * i_chunk: chunksize * (i_chunk + 1)] | ||
tm.assert_frame_equal(true_df, df) | ||
|
||
@tm.network | ||
def test_parse_public_s3_bucket_python(self): | ||
def test_parse_public_s3_bucket_python(self, s3_resource): | ||
for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: | ||
df = read_csv('s3://pandas-test/tips.csv' + ext, engine='python', | ||
compression=comp) | ||
|
@@ -150,8 +182,7 @@ def test_parse_public_s3_bucket_python(self): | |
tm.assert_frame_equal(read_csv( | ||
tm.get_data_path('tips.csv')), df) | ||
|
||
@tm.network | ||
def test_infer_s3_compression(self): | ||
def test_infer_s3_compression(self, s3_resource): | ||
for ext in ['', '.gz', '.bz2']: | ||
df = read_csv('s3://pandas-test/tips.csv' + ext, | ||
engine='python', compression='infer') | ||
|
@@ -160,8 +191,7 @@ def test_infer_s3_compression(self): | |
tm.assert_frame_equal(read_csv( | ||
tm.get_data_path('tips.csv')), df) | ||
|
||
@tm.network | ||
def test_parse_public_s3_bucket_nrows_python(self): | ||
def test_parse_public_s3_bucket_nrows_python(self, s3_resource): | ||
for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: | ||
df = read_csv('s3://pandas-test/tips.csv' + ext, engine='python', | ||
nrows=10, compression=comp) | ||
|
@@ -170,8 +200,7 @@ def test_parse_public_s3_bucket_nrows_python(self): | |
tm.assert_frame_equal(read_csv( | ||
tm.get_data_path('tips.csv')).iloc[:10], df) | ||
|
||
@tm.network | ||
def test_s3_fails(self): | ||
def test_s3_fails(self, s3_resource): | ||
with pytest.raises(IOError): | ||
read_csv('s3://nyqpug/asdf.csv') | ||
|
||
|
@@ -180,21 +209,18 @@ def test_s3_fails(self): | |
with pytest.raises(IOError): | ||
read_csv('s3://cant_get_it/') | ||
|
||
@tm.network | ||
def boto3_client_s3(self): | ||
def test_read_csv_handles_boto_s3_object(self, | ||
s3_resource, | ||
tips_file): | ||
# see gh-16135 | ||
|
||
# boto3 is a dependency of s3fs | ||
import boto3 | ||
client = boto3.client("s3") | ||
|
||
key = "/tips.csv" | ||
bucket = "pandas-test" | ||
s3_object = client.get_object(Bucket=bucket, Key=key) | ||
s3_object = s3_resource.meta.client.get_object( | ||
Bucket='pandas-test', | ||
Key='tips.csv') | ||
|
||
result = read_csv(s3_object["Body"]) | ||
result = read_csv(BytesIO(s3_object["Body"].read()), encoding='utf8') | ||
assert isinstance(result, DataFrame) | ||
assert not result.empty | ||
|
||
expected = read_csv(tm.get_data_path('tips.csv')) | ||
expected = read_csv(tips_file) | ||
tm.assert_frame_equal(result, expected) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the no change files e.g
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added those files when I was attempting to install moto via pip in those environments; they don't exist in master. Do we want to get rid of them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually these are empty, so ok