Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
nomad-FAIR
Commits
de9086b9
Commit
de9086b9
authored
Aug 21, 2018
by
Markus Scheidgen
Browse files
Added API test for getting archive data.
parent
16cbf234
Changes
4
Hide whitespace changes
Inline
Side-by-side
nomad/api.py
View file @
de9086b9
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
):
...
...
nomad/files.py
View file @
de9086b9
...
...
@@ -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
()
tests/test_api.py
View file @
de9086b9
...
...
@@ -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
tests/test_files.py
View file @
de9086b9
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment