Skip to content

Commit 6979fb8

Browse files
committed
REF: Move s3 mocking to conftest
1 parent b21401b commit 6979fb8

File tree

3 files changed

+74
-55
lines changed

3 files changed

+74
-55
lines changed

pandas/tests/io/conftest.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import os
2+
3+
import moto
4+
import pytest
5+
from pandas.io.parsers import read_table
6+
7+
HERE = os.path.dirname(__file__)
8+
9+
10+
@pytest.fixture(scope='module')
11+
def tips_file():
12+
"""Path to the tips dataset"""
13+
return os.path.join(HERE, 'parser', 'data', 'tips.csv')
14+
15+
16+
@pytest.fixture(scope='module')
17+
def jsonl_file():
18+
"""Path a JSONL dataset"""
19+
return os.path.join(HERE, 'parser', 'data', 'items.jsonl')
20+
21+
22+
@pytest.fixture(scope='module')
23+
def salaries_table():
24+
"""DataFrame with the salaries dataset"""
25+
path = os.path.join(HERE, 'parser', 'data', 'salaries.csv')
26+
return read_table(path)
27+
28+
29+
@pytest.fixture(scope='module')
30+
def s3_resource(tips_file, jsonl_file):
31+
"""Fixture for mocking S3 interaction.
32+
33+
The primary bucket name is "pandas-test". The following datasets
34+
are loaded.
35+
36+
- tips.csv
37+
- tips.csv.gz
38+
- tips.csv.bz2
39+
- items.jsonl
40+
41+
A private bucket "cant_get_it" is also created. The boto3 s3 resource
42+
is yielded by the fixture.
43+
"""
44+
pytest.importorskip('s3fs')
45+
moto.mock_s3().start()
46+
47+
test_s3_files = [
48+
('tips.csv', tips_file),
49+
('tips.csv.gz', tips_file + '.gz'),
50+
('tips.csv.bz2', tips_file + '.bz2'),
51+
('items.jsonl', jsonl_file),
52+
]
53+
54+
def add_tips_files(bucket_name):
55+
for s3_key, file_name in test_s3_files:
56+
with open(file_name, 'rb') as f:
57+
conn.Bucket(bucket_name).put_object(
58+
Key=s3_key,
59+
Body=f)
60+
61+
boto3 = pytest.importorskip('boto3')
62+
# see gh-16135
63+
bucket = 'pandas-test'
64+
65+
conn = boto3.resource("s3", region_name="us-east-1")
66+
conn.create_bucket(Bucket=bucket)
67+
add_tips_files(bucket)
68+
69+
conn.create_bucket(Bucket='cant_get_it', ACL='private')
70+
add_tips_files('cant_get_it')
71+
72+
yield conn
73+
74+
moto.mock_s3().stop()

pandas/tests/io/json/test_pandas.py

-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
from pandas.compat import (range, lrange, StringIO,
55
OrderedDict, is_platform_32bit)
66
import os
7-
import moto
87
import numpy as np
9-
from pandas.tests.io.parser.test_network import s3_resource
10-
from pandas.tests.io.parser.test_network import tips_file, jsonl_file
118
from pandas import (Series, DataFrame, DatetimeIndex, Timestamp,
129
read_json, compat)
1310
from datetime import timedelta

pandas/tests/io/parser/test_network.py

-52
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,14 @@
44
Tests parsers ability to read and parse non-local files
55
and hence require a network connection to be read.
66
"""
7-
import os
8-
97
import pytest
10-
import moto
118

129
import pandas.util.testing as tm
1310
from pandas import DataFrame
1411
from pandas.io.parsers import read_csv, read_table
1512
from pandas.compat import BytesIO
1613

1714

18-
@pytest.fixture(scope='module')
19-
def tips_file():
20-
return os.path.join(tm.get_data_path(), 'tips.csv')
21-
22-
@pytest.fixture(scope='module')
23-
def jsonl_file():
24-
return os.path.join(tm.get_data_path(), 'items.jsonl')
25-
26-
@pytest.fixture(scope='module')
27-
def salaries_table():
28-
path = os.path.join(tm.get_data_path(), 'salaries.csv')
29-
return read_table(path)
30-
31-
32-
@pytest.fixture(scope='module')
33-
def s3_resource(tips_file, jsonl_file):
34-
pytest.importorskip('s3fs')
35-
moto.mock_s3().start()
36-
37-
test_s3_files = [
38-
('tips.csv', tips_file),
39-
('tips.csv.gz', tips_file + '.gz'),
40-
('tips.csv.bz2', tips_file + '.bz2'),
41-
('items.jsonl', jsonl_file),
42-
]
43-
44-
def add_tips_files(bucket_name):
45-
for s3_key, file_name in test_s3_files:
46-
with open(file_name, 'rb') as f:
47-
conn.Bucket(bucket_name).put_object(
48-
Key=s3_key,
49-
Body=f)
50-
51-
boto3 = pytest.importorskip('boto3')
52-
# see gh-16135
53-
bucket = 'pandas-test'
54-
55-
conn = boto3.resource("s3", region_name="us-east-1")
56-
conn.create_bucket(Bucket=bucket)
57-
add_tips_files(bucket)
58-
59-
conn.create_bucket(Bucket='cant_get_it', ACL='private')
60-
add_tips_files('cant_get_it')
61-
62-
yield conn
63-
64-
moto.mock_s3().stop()
65-
66-
6715
@pytest.mark.network
6816
@pytest.mark.parametrize(
6917
"compression,extension",

0 commit comments

Comments
 (0)