Commit de9086b9 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added API test for getting archive data.

parent 16cbf234
from flask import Flask
from flask import Flask, Response
from flask_restful import Resource, Api, abort
from datetime import datetime
from threading import Thread
......@@ -69,25 +69,22 @@ class Upload(Resource):
return Uploads._render(upload), 200
class Calc(Resource):
def get(self, upload_hash, calc_hash):
archive_id = '%s/%s' % (upload_hash, calc_hash)
logger = get_logger(__name__, archive_id=archive_id)
try:
file = files.open_archive_json(archive_id)
return file, 200
except KeyError:
abort(404, message='Archive %s does not exist.' % archive_id)
except Exception as e:
logger.error('Exception on reading archive', exc_info=e)
abort(500, message='Could not read the archive.')
finally:
file.close()
@app.route('/archive/<string:upload_hash>/<string:calc_hash>', methods=['GET'])
def get_calc(upload_hash, calc_hash):
archive_id = '%s/%s' % (upload_hash, calc_hash)
logger = get_logger(__name__, archive_id=archive_id)
try:
file = files.open_archive_json(archive_id)
return Response(file, mimetype='application/json', status=200)
except KeyError:
abort(404, message='Archive %s does not exist.' % archive_id)
except Exception as e:
logger.error('Exception on reading archive', exc_info=e)
abort(500, message='Could not read the archive.')
api.add_resource(Uploads, '/uploads')
api.add_resource(Upload, '/uploads/<string:upload_id>')
api.add_resource(Calc, '/archive/<string:upload_hash>/<string:calc_hash>')
def start_upload_handler(quit=False):
......
......@@ -302,4 +302,7 @@ def open_archive_json(archive_id) -> IO:
""" Returns a file-like to read the archive json. """
# The result already is a file-like and due to the Content-Encoding metadata is
# will automatically be un-gzipped.
return _client.get_object(config.files.archive_bucket, archive_id)
try:
return _client.get_object(config.files.archive_bucket, archive_id)
except minio.error.NoSuchKey:
raise KeyError()
......@@ -12,7 +12,7 @@ from nomad import config, api, files
from tests.test_processing import example_files
from tests.test_files import assert_exists
# import fixtures
from tests.test_files import clear_files # pylint: disable=unused-import
from tests.test_files import clear_files, archive_id # pylint: disable=unused-import
@pytest.fixture
......@@ -136,3 +136,13 @@ def test_processing(client, file):
assert upload['processing']['status'] == 'SUCCESS'
assert_exists(config.files.uploads_bucket, upload['id'])
def test_get_archive(client, archive_id):
rv = client.get('/archive/%s' % archive_id)
assert rv.status_code == 200
def test_get_non_existing_archive(client):
rv = client.get('/archive/%s' % 'doesnt/exist')
assert rv.status_code == 404
......@@ -77,7 +77,11 @@ def upload_id(clear_files) -> Generator[str, None, None]:
@pytest.fixture(scope='function')
def archive_id(clear_files) -> Generator[str, None, None]:
example_archive_id = '__test_archive_id'
example_archive_id = '__test_upload_hash/__test_calc_hash'
with files.write_archive_json(example_archive_id) as out:
json.dump({'test': 'value'}, out)
yield example_archive_id
......@@ -136,9 +140,6 @@ def test_hash(uploaded_id: str):
def test_archive(archive_id: str):
with files.write_archive_json(archive_id) as out:
json.dump({'test': 'value'}, out)
result = json.load(files.open_archive_json(archive_id))
assert 'test' in result
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment