Skip to content

Commit 95a6dbd

Browse files
committed
Add modality tests
1 parent 1603a89 commit 95a6dbd

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

api/dao/basecontainerstorage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,20 @@ def update_el(self, _id, payload, unset_payload=None, recursive=False, r_payload
160160
raise APIStorageException(e.message)
161161
if recursive and r_payload is not None:
162162
containerutil.propagate_changes(self.cont_name, _id, {}, {'$set': util.mongo_dict(r_payload)})
163+
164+
config.log.warning(update)
163165
return self.dbc.update_one({'_id': _id}, update)
164166

167+
def replace_el(self, _id, payload):
168+
if self.use_object_id:
169+
try:
170+
_id = bson.ObjectId(_id)
171+
except bson.errors.InvalidId as e:
172+
raise APIStorageException(e.message)
173+
payload['_id'] = _id
174+
return self.dbc.replace_one({'_id': _id}, payload)
175+
176+
165177
def delete_el(self, _id):
166178
if self.use_object_id:
167179
try:

api/handlers/modalityhandler.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ def post(self):
3030
#validate_data(payload, 'modality.json', 'input', 'POST', optional=True)
3131

3232
result = self.storage.create_el(payload)
33-
if result.acknowledged:
34-
return {'_id': result.inserted_id}
35-
else:
36-
self.abort(400, 'Modality not inserted')
33+
return {'_id': result.inserted_id}
3734

3835
@require_admin
3936
def put(self, modality_name):
@@ -42,18 +39,14 @@ def put(self, modality_name):
4239
# POST unnecessary, used to avoid run-time modification of schema
4340
#validate_data(payload, 'modality.json', 'input', 'POST', optional=True)
4441

45-
result = self.storage.update_el(modality_name, payload)
46-
if result.matched_count == 1:
47-
return {'modified': result.modified_count}
48-
else:
42+
result = self.storage.replace_el(modality_name, payload)
43+
if result.matched_count != 1:
4944
raise APINotFoundException('Modality with name {} not found, modality not updated'.format(modality_name))
5045

5146
@require_admin
5247
def delete(self, modality_name):
5348
result = self.storage.delete_el(modality_name)
54-
if result.deleted_count == 1:
55-
return {'deleted': result.deleted_count}
56-
else:
49+
if result.deleted_count != 1:
5750
raise APINotFoundException('Modality with name {} not found, modality not deleted'.format(modality_name))
5851

5952

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
def test_modalities(data_builder, as_admin, as_user):
2+
3+
payload = {
4+
'_id': 'MR',
5+
'classification': {
6+
'Intent': ["Structural", "Functional", "Localizer"],
7+
'Contrast': ["B0", "B1", "T1", "T2"]
8+
}
9+
}
10+
11+
# test adding new modality
12+
r = as_admin.post('/modalities', json=payload)
13+
assert r.ok
14+
assert r.json()['_id'] == payload['_id']
15+
modality1 = payload['_id']
16+
17+
# get specific modality
18+
r = as_user.get('/modalities/' + modality1)
19+
assert r.ok
20+
assert r.json() == payload
21+
22+
# try replacing existing modality via POST
23+
r = as_admin.post('/modalities', json=payload)
24+
assert r.status_code == 409
25+
26+
# list modalities as non-admin
27+
r = as_user.get('/modalities')
28+
assert r.ok
29+
modalities = r.json()
30+
assert len(modalities) == 1
31+
assert modalities[0]['_id'] == modality1
32+
33+
# replace existing modality
34+
update = {
35+
'classification': {
36+
'Intent': ["new", "stuff"]
37+
}
38+
}
39+
r = as_admin.put('/modalities/' + modality1, json=update)
40+
assert r.ok
41+
r = as_admin.get('/modalities/' + modality1)
42+
assert r.ok
43+
assert r.json()['classification'] == update['classification']
44+
45+
# try to replace missing modality
46+
r = as_admin.put('/modalities/' + 'madeup', json=update)
47+
assert r.status_code == 404
48+
49+
# delete modality
50+
r = as_admin.delete('/modalities/' + modality1)
51+
assert r.ok
52+
53+
# try to delete missing modality
54+
r = as_admin.delete('/modalities/' + modality1)
55+
assert r.status_code == 404
56+
57+
58+
59+
60+

tests/integration_tests/python/test_rules.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,5 +437,3 @@ def test_rules(randstr, data_builder, file_form, as_root, as_admin, with_user, a
437437
# delete rule
438438
r = as_admin.delete('/projects/' + project + '/rules/' + rule3)
439439
assert r.ok
440-
441-
# TODO add and test 'new-style' rules

0 commit comments

Comments
 (0)