Skip to content

Commit

Permalink
Fix and test exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed May 28, 2018
1 parent 499077e commit 0b5341b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
4 changes: 2 additions & 2 deletions explicates/api/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _get_zip_compression(self):
import zlib
assert zlib
return zipfile.ZIP_DEFLATED
except Exception as ex:
except Exception as ex: # pragma: no cover
return zipfile.ZIP_STORED

def _ascii_encode(self, collection_id):
Expand All @@ -42,7 +42,7 @@ def _zip_response(self, collection_id, generator):
json_fn = safe_name + '.json'
zip_fn = safe_name + '.zip'
z.write_iter(json_fn, generator)
response = Response(z, mimetype='application/zip')
response = Response(stream_with_context(z), mimetype='application/zip')
content_disposition = 'attachment; filename={}'.format(zip_fn)
response.headers['Content-Disposition'] = content_disposition
return response
Expand Down
2 changes: 1 addition & 1 deletion explicates/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _stream_annotation_data(self, collection):
table = Annotation.__table__
where_clauses = [
table.c.collection_key == collection.key,
not table.c.deleted
table.c.deleted != True
]

query = table.select().where(and_(*where_clauses))
Expand Down
59 changes: 59 additions & 0 deletions test/test_api/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf8 -*-

import json
from nose.tools import *
from freezegun import freeze_time
from base import Test, with_context
from factories import AnnotationFactory
from flask import current_app, url_for


class TestExportAPI(Test):

def setUp(self):
super(TestExportAPI, self).setUp()
assert_dict_equal.__self__.maxDiff = None

@with_context
@freeze_time("1984-11-19")
def test_404_exporting_unknown_collection(self):
"""Test 404 exporting unknown Collection."""
endpoint = '/export/foo/'
res = self.app_get_json_ld(endpoint)
assert_equal(res.status_code, 404, res.data)

@with_context
@freeze_time("1984-11-19")
def test_collection_exported(self):
"""Test Collection exported."""
annotation = AnnotationFactory()
endpoint = u'/export/{}/'.format(annotation.collection.id)
res = self.app_get_json_ld(endpoint)
assert_equal(res.status_code, 200, res.data)
assert_equal(json.loads(res.data), [
{
'id': url_for('api.annotations',
collection_id=annotation.collection.id,
annotation_id=annotation.id),
'type': 'Annotation',
'body': annotation.data['body'],
'target': annotation.data['target'],
'created': '1984-11-19T00:00:00Z',
'generated': '1984-11-19T00:00:00Z',
'generator': current_app.config.get('GENERATOR')
}
])

@with_context
@freeze_time("1984-11-19")
def test_collection_exported_as_zip(self):
"""Test Collection exported as zip.
Testing of this really ought to be improved!
"""
annotation = AnnotationFactory()
endpoint = u'/export/{}/?zip=1'.format(annotation.collection.id)
res = self.app.get(endpoint)
assert_equal(res.headers['Content-Type'], 'application/zip')
content_disposition = 'attachment; filename=collection1.zip'
assert_equal(res.headers['Content-Disposition'], content_disposition)

0 comments on commit 0b5341b

Please sign in to comment.