Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add artifact creation test handler #1884

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions qiita_db/handlers/artifact.py
Expand Up @@ -8,6 +8,7 @@

from tornado.web import HTTPError
from collections import defaultdict
from json import loads

import qiita_db as qdb
from .oauth2 import OauthBaseHandler, authenticate_oauth
Expand Down Expand Up @@ -136,3 +137,43 @@ def patch(self, artifact_id):
'supported operations: add' % req_op)

self.finish()


class ArtifactAPItestHandler(OauthBaseHandler):
@authenticate_oauth
def post(self):
"""Creates a new artifact

Parameters
----------
filepaths : str
Json string with a list of filepaths and its types
type : str
The artifact type
prep_template: int
The id of the template that the new artifact belongs to
name : str, optional
The artifact name

Returns
-------
dict
'artifact': the id of the new artifact

See Also
--------
qiita_db.artifact.Artifact.create
"""
filepaths = loads(self.get_argument('filepaths'))
artifact_type = self.get_argument('type')
prep_template = self.get_argument('prep')
name = self.get_argument('name', None)

if prep_template:
prep_template = qdb.metadata_template.prep_template.PrepTemplate(
prep_template)

a = qdb.artifact.Artifact.create(
filepaths, artifact_type, name=name, prep_template=prep_template)

self.write({'artifact': a.id})
6 changes: 5 additions & 1 deletion qiita_db/handlers/oauth2.py
Expand Up @@ -148,7 +148,11 @@ def write_error(self, status_code, **kwargs):
'ERROR:\n%s\nTRACE:\n%s\nHTTP INFO:\n%s\n' %
(error, trace_info, request_info))

self.finish(exc_info[1].log_message)
message = exc_info[1].message
if hasattr(exc_info[1], 'log_message'):
message = exc_info[1].log_message

self.finish(message)

def head(self):
"""Adds proper response for head requests"""
Expand Down
75 changes: 75 additions & 0 deletions qiita_db/handlers/tests/test_artifact.py
Expand Up @@ -12,8 +12,10 @@
from os.path import join, exists
from os import close, remove
from tempfile import mkstemp
from json import dumps

from tornado.web import HTTPError
import pandas as pd

from qiita_db.handlers.tests.oauthbase import OauthTestingBase
import qiita_db as qdb
Expand Down Expand Up @@ -122,5 +124,78 @@ def test_patch(self):
self.assertIn('No such file or directory', obs.body)


class ArtifactAPItestHandlerTests(OauthTestingBase):
database = True

def setUp(self):
super(ArtifactAPItestHandlerTests, self).setUp()

metadata_dict = {
'SKB8.640193': {'center_name': 'ANL',
'primer': 'GTGCCAGCMGCCGCGGTAA',
'barcode': 'GTCCGCAAGTTA',
'run_prefix': "s_G1_L001_sequences",
'platform': 'ILLUMINA',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol': 'AAAA',
'experiment_design_description': 'BBBB'}}
metadata = pd.DataFrame.from_dict(metadata_dict, orient='index')
self.prep_template = \
qdb.metadata_template.prep_template.PrepTemplate.create(
metadata, qdb.study.Study(1), "16S")

self._clean_up_files = []

def tearDown(self):
super(ArtifactAPItestHandlerTests, self).tearDown()

for f in self._clean_up_files:
if exists(f):
remove(f)

def test_post(self):
fd, fp1 = mkstemp(suffix='_seqs.fastq')
close(fd)
self._clean_up_files.append(fp1)
with open(fp1, 'w') as f:
f.write("@HWI-ST753:189:D1385ACXX:1:1101:1214:1906 1:N:0:\n"
"NACGTAGGGTGCAAGCGTTGTCCGGAATNA\n"
"+\n"
"#1=DDFFFHHHHHJJJJJJJJJJJJGII#0\n")

fd, fp2 = mkstemp(suffix='_barcodes.fastq')
close(fd)
self._clean_up_files.append(fp2)
with open(fp2, 'w') as f:
f.write("@HWI-ST753:189:D1385ACXX:1:1101:1214:1906 2:N:0:\n"
"NNNCNNNNNNNNN\n"
"+\n"
"#############\n")

data = {'filepaths': dumps([(fp1, 'raw_forward_seqs'),
(fp2, 'raw_barcodes')]),
'type': "FASTQ",
'name': "New test artifact",
'prep': self.prep_template.id}
obs = self.post('/apitest/artifact/', headers=self.header, data=data)
self.assertEqual(obs.code, 200)
obs = loads(obs.body)
self.assertEqual(obs.keys(), ['artifact'])

a = qdb.artifact.Artifact(obs['artifact'])
self._clean_up_files.extend([fp for _, fp, _ in a.filepaths])
self.assertEqual(a.name, "New test artifact")

def test_post_error(self):
data = {'filepaths': dumps([('Do not exist', 'raw_forward_seqs')]),
'type': "FASTQ",
'name': "New test artifact",
'prep': 1}
obs = self.post('/apitest/artifact/', headers=self.header, data=data)
self.assertEqual(obs.code, 500)
self.assertIn("Prep template 1 already has an artifact associated",
obs.body)


if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions qiita_pet/webserver.py
Expand Up @@ -44,7 +44,7 @@
from qiita_db.handlers.processing_job import (
JobHandler, HeartbeatHandler, ActiveStepHandler, CompleteHandler,
ProcessingJobAPItestHandler)
from qiita_db.handlers.artifact import ArtifactHandler
from qiita_db.handlers.artifact import ArtifactHandler, ArtifactAPItestHandler
from qiita_db.handlers.prep_template import (
PrepTemplateDataHandler, PrepTemplateAPItestHandler,
PrepTemplateDBHandler)
Expand Down Expand Up @@ -168,7 +168,8 @@ def __init__(self):
test_handlers = [
(r"/apitest/processing_job/", ProcessingJobAPItestHandler),
(r"/apitest/reset/", ResetAPItestHandler),
(r"/apitest/prep_template/", PrepTemplateAPItestHandler)
(r"/apitest/prep_template/", PrepTemplateAPItestHandler),
(r"/apitest/artifact/", ArtifactAPItestHandler)
]
handlers.extend(test_handlers)

Expand Down