Skip to content

Commit

Permalink
increase coverage of api/handlers/containerhandler.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrussimon committed Mar 27, 2017
1 parent bb8342f commit 37fc2f3
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 9 deletions.
3 changes: 0 additions & 3 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ def prefix(path, routes):
route('/<cid:{cid}>/subject', ContainerHandler, h='get_subject', m=['GET']),
]),

route('/sessions/<cid:{cid}>/jobs', ContainerHandler, h='get_jobs', m=['GET']),

This comment has been minimized.

Copy link
@ambrussimon

ambrussimon Mar 27, 2017

Author Contributor

Please confirm, but I think the route is (and should only be) covered by line 202.

This comment has been minimized.

Copy link
@kofalt

kofalt Mar 27, 2017

Contributor

That looks right to me.

route('/sessions/<cid:{cid}>/jobs', ContainerHandler, h='get_jobs', m=['GET']),


# Collections

Expand Down
10 changes: 5 additions & 5 deletions api/handlers/containerhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get(self, cont_name, **kwargs):
_id = kwargs.get('cid')
self.config = self.container_handler_configurations[cont_name]
self.storage = self.config['storage']
container= self._get_container(_id)
container = self._get_container(_id)

permchecker = self._get_permchecker(container)
try:
Expand Down Expand Up @@ -234,9 +234,9 @@ def get_jobs(self, cid):
if join_cont:
# create a map of analyses and acquisitions by _id
containers = dict((str(c['_id']), c) for c in analyses+acquisitions)
for c in containers:
for container in containers.itervalues():
# No need to return perm arrays
c.pop('permissions', None)
container.pop('permissions', None)
response['containers'] = containers

return response
Expand All @@ -245,7 +245,7 @@ def get_all(self, cont_name, par_cont_name=None, par_id=None):
self.config = self.container_handler_configurations[cont_name]
self.storage = self.config['storage']

projection = self.config['list_projection']
projection = self.config['list_projection'].copy()
if self.is_true('info'):
projection.pop('info')
if not projection:
Expand Down Expand Up @@ -307,7 +307,7 @@ def _filter_all_permissions(self, results, uid, site):
def _add_results_counts(self, results, cont_name):
dbc_name = self.config.get('children_cont')
el_cont_name = cont_name[:-1]
dbc = config.db.get(dbc_name)
dbc = config.db[dbc_name]
counts = dbc.aggregate([
{'$match': {el_cont_name: {'$in': [res['_id'] for res in results]}}},
{'$group': {'_id': '$' + el_cont_name, 'count': {"$sum": 1}}}
Expand Down
140 changes: 140 additions & 0 deletions test/integration_tests/python/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,143 @@ def test_project_template(with_hierarchy, data_builder, as_user):
assert r.json()['satisfies_template'] == False

data_builder.delete_acquisition(acquisition_id)


def test_get_all_containers(with_hierarchy, as_public):
data = with_hierarchy

# get all projects w/ info=true
r = as_public.get('/projects', params={'info': 'true'})
assert r.ok

# get all projects w/ counts=true
r = as_public.get('/projects', params={'counts': 'true'})
assert r.ok
assert all('session_count' in proj for proj in r.json())

# get all sessions for project w/ measurements=true and stats=true
r = as_public.get('/projects/' + data.project + '/sessions', params={
'measurements': 'true',
'stats': 'true'
})
assert r.ok


def test_get_all_for_user(as_user, as_public):
r = as_user.get('/users/self')
user_id = r.json()['_id']

# try to get containers for user w/o logging in
r = as_public.get('/users/' + user_id + '/sessions')
assert r.status_code == 403

# get containers for user
r = as_user.get('/users/' + user_id + '/sessions')
assert r.ok


def test_get_container(with_hierarchy, as_user, as_public):
data = with_hierarchy

# NOTE cannot reach APIStorageException - wanted to cover 400 error w/ invalid oid
# but then realized that api.py's cid regex makes this an invalid route resulting in 404

# try to get container w/ invalid object id
# r = as_user.get('/projects/test')
# assert r.status_code == 400

# try to get container w/ nonexistent object id
r = as_public.get('/projects/000000000000000000000000')
assert r.status_code == 404

# get container
r = as_public.get('/projects/' + data.project)
assert r.ok

# get container w/ ?paths=true
r = as_user.post('/projects/' + data.project + '/files', files={
'file': ('test.csv', 'header\nrow1\n'),
'metadata': ('', json.dumps({'name': 'test.csv', 'type': 'csv'}))
})
assert r.ok

r = as_public.get('/projects/' + data.project, params={'paths': 'true'})
assert r.ok
assert all('path' in f for f in r.json()['files'])

# get container w/ ?join=origin
r = as_public.get('/projects/' + data.project, params={'join': 'origin'})
assert r.ok
assert 'join-origin' in r.json()


def test_get_session_jobs(with_hierarchy, with_gear, as_user, db):
data = with_hierarchy
gear = with_gear

# get session jobs w/ analysis and job
r = as_user.post('/sessions/' + data.session + '/analyses', params={'job': 'true'}, json={
'analysis': {'label': 'test analysis'},
'job': {
'gear_id': gear,
'inputs': {
'dicom': {
'type': 'acquisition',
'id': data.acquisition,
'name': 'test.dcm'
}
}
}
})
assert r.ok

r = as_user.get('/sessions/' + data.session + '/jobs', params={'join': 'containers'})
assert r.ok

# TODO figure out better/generalized way to handle state cleanup
# make sure other tests start clean w/o existing jobs (eg for /jobs/next to work as expected)
db.jobs.remove()


def test_post_container(with_hierarchy, as_user):
data = with_hierarchy

# create project w/ param inherit=true
r = as_user.post('/projects', params={'inherit': 'true'}, json={
'group': data.group,
'label': 'test-inheritance-project'
})
assert r.ok
as_user.delete('/projects/' + r.json()['_id'])

# create session w/ timestamp
r = as_user.post('/sessions', json={
'project': data.project,
'label': 'test-timestamp-session',
'timestamp': '1979-01-01T00:00:00+00:00'
})
assert r.ok
as_user.delete('/sessions/' + r.json()['_id'])


def test_put_container(with_hierarchy, as_user):
data = with_hierarchy

# update session w/ timestamp
r = as_user.put('/sessions/' + data.session, json={
'timestamp': '1979-01-01T00:00:00+00:00'
})
assert r.ok

# update subject w/ oid
r = as_user.put('/sessions/' + data.session, json={
'subject': {'_id': '000000000000000000000000'}
})
assert r.ok


def test_delete_project_template(with_hierarchy, as_user):
data = with_hierarchy

r = as_user.delete('/projects/' + data.project + '/template')
assert r.ok
4 changes: 3 additions & 1 deletion test/integration_tests/python/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_jobs_access(as_user):
assert r.status_code == 403


def test_jobs(with_hierarchy, with_gear, with_invalid_gear, as_user, as_admin):
def test_jobs(with_hierarchy, with_gear, with_invalid_gear, as_user, as_admin, db):
data = with_hierarchy
gear = with_gear
invalid_gear = with_invalid_gear
Expand Down Expand Up @@ -94,3 +94,5 @@ def test_jobs(with_hierarchy, with_gear, with_invalid_gear, as_user, as_admin):

r = as_user.post('/jobs/' + job1_id + '/retry')
assert r.ok

db.jobs.remove()

0 comments on commit 37fc2f3

Please sign in to comment.