-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from 4Catalyzer/boto3
Switch to boto3 for S3
- Loading branch information
Showing
13 changed files
with
276 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[run] | ||
source = flask_annex | ||
|
||
[report] | ||
exclude_lines = | ||
pragma: no cover | ||
raise NotImplementedError |
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,14 @@ | ||
# Flask-Annex [![PyPI][pypi-badge]][pypi] | ||
# Flask-Annex [![Travis][build-badge]][build] [![PyPI][pypi-badge]][pypi] | ||
Efficient integration of external storage services for | ||
[Flask](http://flask.pocoo.org/). | ||
|
||
[![Codecov][codecov-badge]][codecov] | ||
|
||
[build-badge]: https://img.shields.io/travis/4Catalyzer/flask-annex/master.svg | ||
[build]: https://travis-ci.org/4Catalyzer/flask-annex | ||
|
||
[pypi-badge]: https://img.shields.io/pypi/v/Flask-Annex.svg | ||
[pypi]: https://pypi.python.org/pypi/Flask-Annex | ||
|
||
[codecov-badge]: https://img.shields.io/codecov/c/github/4Catalyzer/flask-annex/master.svg | ||
[codecov]: https://codecov.io/gh/4Catalyzer/flask-annex |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
comment: off |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from flask import Flask | ||
import pytest | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.fixture | ||
def app(): | ||
app = Flask(__name__) | ||
app.config['TESTING'] = True | ||
|
||
return app |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from io import BytesIO | ||
|
||
import pytest | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def assert_key_value(annex, key, value): | ||
out_file = BytesIO() | ||
annex.get_file(key, out_file) | ||
|
||
out_file.seek(0) | ||
assert out_file.read() == value | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class AbstractTestAnnex(object): | ||
@pytest.fixture | ||
def annex(self, annex_base): | ||
annex_base.save_file('foo/bar.txt', BytesIO(b'1\n')) | ||
annex_base.save_file('foo/baz.json', BytesIO(b'2\n')) | ||
return annex_base | ||
|
||
def test_get_file(self, annex): | ||
assert_key_value(annex, 'foo/bar.txt', b'1\n') | ||
|
||
def test_get_filename(self, tmpdir, annex): | ||
out_filename = tmpdir.join('out').strpath | ||
annex.get_file('foo/bar.txt', out_filename) | ||
assert open(out_filename).read() == '1\n' | ||
|
||
def test_list_keys(self, annex): | ||
assert sorted(annex.list_keys('foo/')) == [ | ||
'foo/bar.txt', | ||
'foo/baz.json', | ||
] | ||
|
||
def test_save_file(self, annex): | ||
annex.save_file('qux/foo.txt', BytesIO(b'3\n')) | ||
assert_key_value(annex, 'qux/foo.txt', b'3\n') | ||
|
||
def test_save_filename(self, tmpdir, annex): | ||
in_file = tmpdir.join('in') | ||
in_file.write('4\n') | ||
|
||
annex.save_file('qux/bar.txt', in_file.strpath) | ||
assert_key_value(annex, 'qux/bar.txt', b'4\n') | ||
|
||
def test_replace_file(self, annex): | ||
assert_key_value(annex, 'foo/bar.txt', b'1\n') | ||
annex.save_file('foo/bar.txt', BytesIO(b'5\n')) | ||
assert_key_value(annex, 'foo/bar.txt', b'5\n') | ||
|
||
def test_delete(self, annex): | ||
assert annex.list_keys('foo/bar.txt') | ||
annex.delete('foo/bar.txt') | ||
assert not annex.list_keys('foo/bar.txt') | ||
|
||
def test_delete_many(self, annex): | ||
assert annex.list_keys('') | ||
annex.delete_many(('foo/bar.txt', 'foo/baz.json')) | ||
assert not annex.list_keys('') |
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from io import BytesIO | ||
|
||
import pytest | ||
|
||
from flask_annex import Annex | ||
|
||
from helpers import AbstractTestAnnex, assert_key_value | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.fixture | ||
def file_annex_path(tmpdir): | ||
return tmpdir.join('annex').strpath | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class TestFileAnnex(AbstractTestAnnex): | ||
@pytest.fixture | ||
def annex_base(self, file_annex_path): | ||
return Annex('file', root_path=file_annex_path) | ||
|
||
def test_save_file_existing_dir(self, annex): | ||
annex.save_file('foo/qux.txt', BytesIO(b'6\n')) | ||
assert_key_value(annex, 'foo/qux.txt', b'6\n') | ||
|
||
def test_send_file(self, app, annex): | ||
with app.test_request_context(): | ||
response = annex.send_file('foo/baz.json') | ||
|
||
assert response.status_code == 200 | ||
assert response.mimetype == 'application/json' | ||
|
||
|
||
class TestFileAnnexFromEnv(TestFileAnnex): | ||
@pytest.fixture | ||
def annex_base(self, monkeypatch, file_annex_path): | ||
monkeypatch.setenv('FLASK_ANNEX_STORAGE', 'file') | ||
monkeypatch.setenv('FLASK_ANNEX_FILE_ROOT_PATH', file_annex_path) | ||
|
||
return Annex.from_env('FLASK_ANNEX') |
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
from flask_annex import Annex | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
def test_unknown_annex(): | ||
with pytest.raises(ValueError): | ||
Annex('unknown') | ||
|
||
|
||
def test_unknown_annex_from_env(monkeypatch): | ||
monkeypatch.setenv('FLASK_ANNEX_STORAGE', 'unknown') | ||
|
||
with pytest.raises(ValueError): | ||
Annex.from_env('FLASK_ANNEX') |
This file contains 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from io import BytesIO | ||
|
||
import pytest | ||
|
||
from flask_annex import Annex | ||
|
||
from helpers import AbstractTestAnnex, assert_key_value | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
try: | ||
import boto3 | ||
from moto import mock_s3 | ||
except ImportError: | ||
pytestmark = pytest.mark.skipif(True, reason="S3 support not installed") | ||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.yield_fixture | ||
def bucket_name(): | ||
with mock_s3(): | ||
bucket = boto3.resource('s3').Bucket('flask-annex') | ||
bucket.create() | ||
|
||
yield bucket.name | ||
|
||
|
||
# ----------------------------------------------------------------------------- | ||
|
||
|
||
class TestS3Annex(AbstractTestAnnex): | ||
@pytest.fixture | ||
def annex_base(self, bucket_name): | ||
return Annex('s3', bucket_name=bucket_name) | ||
|
||
def test_save_file_unknown_type(self, annex): | ||
annex.save_file('foo/qux', BytesIO(b'6\n')) | ||
assert_key_value(annex, 'foo/qux', b'6\n') | ||
|
||
def test_send_file(self, app, annex): | ||
with app.test_request_context(): | ||
response = annex.send_file('foo/baz.json') | ||
|
||
assert response.status_code == 302 | ||
|
||
|
||
class TestS3AnnexFromEnv(TestS3Annex): | ||
@pytest.fixture | ||
def annex_base(self, monkeypatch, bucket_name): | ||
monkeypatch.setenv('FLASK_ANNEX_STORAGE', 's3') | ||
monkeypatch.setenv('FLASK_ANNEX_S3_BUCKET_NAME', bucket_name) | ||
monkeypatch.setenv('FLASK_ANNEX_S3_REGION', 'us-east-1') | ||
|
||
return Annex.from_env('FLASK_ANNEX') |
Oops, something went wrong.