Skip to content

Commit 6aaa545

Browse files
tomolopolisTom Searlemart-r
authored
Trainer upload export test (#151)
* CU-869an9w8y: medcat-trainer: fix: unit test --------- Co-authored-by: Tom Searle <[email protected]> Co-authored-by: mart-r <[email protected]>
1 parent b2b023b commit 6aaa545

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed

medcat-trainer/client/tests/test_mctclient.py

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,232 @@ def post_side_effect(url, *args, **kwargs):
122122
self.assertEqual(project.meta_tasks, [meta_task])
123123
self.assertEqual(project.rel_tasks, [rel_task])
124124

125+
@patch('mctclient.requests.post')
126+
@patch('mctclient.requests.get')
127+
def test_upload_projects_export_with_cdb_vocab_objects(self, mock_get, mock_post):
128+
"""Test upload_projects_export with cdb and vocab objects"""
129+
# Mock authentication and upload responses
130+
mock_upload_response = {"status": "success", "uploaded_projects": 2}
131+
132+
def post_side_effect(url, *args, **kwargs):
133+
if url.endswith('/api/api-token-auth/'):
134+
return MagicMock(status_code=200, text='{"token": "abc"}')
135+
elif url.endswith('/api/upload-deployment/'):
136+
return MagicMock(
137+
status_code=200,
138+
json=lambda: mock_upload_response
139+
)
140+
else:
141+
return MagicMock(status_code=404, text='')
142+
143+
mock_post.side_effect = post_side_effect
144+
145+
session = MedCATTrainerSession(server='http://localhost', username='u', password='p')
146+
cdb = MCTConceptDB(id='20', name='testCDB', conceptdb_file='cdb.dat')
147+
vocab = MCTVocab(id='30', name='testVocab', vocab_file='vocab.dat')
148+
149+
projects = [{"id": 1, "name": "Project 1"}, {"id": 2, "name": "Project 2"}]
150+
151+
result = session.upload_projects_export(projects, cdb=cdb, vocab=vocab)
152+
153+
# Verify the API call was made correctly
154+
mock_post.assert_called_with(
155+
f'{session.server}/api/upload-deployment/',
156+
headers=session.headers,
157+
json={
158+
'exported_projects': projects,
159+
'cdb_id': '20',
160+
'vocab_id': '30'
161+
}
162+
)
163+
self.assertEqual(result, mock_upload_response)
164+
165+
@patch('mctclient.requests.post')
166+
@patch('mctclient.requests.get')
167+
def test_upload_projects_export_with_cdb_vocab_strings(self, mock_get, mock_post):
168+
"""Test upload_projects_export with cdb and vocab as strings"""
169+
# Mock get_concept_dbs and get_vocabs responses
170+
def get_side_effect(url, *args, **kwargs):
171+
if url.endswith('/api/concept-dbs/'):
172+
return MagicMock(
173+
status_code=200,
174+
text=json.dumps({"results": [{"id": "20", "name": "testCDB", "cdb_file": "cdb.dat"}]})
175+
)
176+
elif url.endswith('/api/vocabs/'):
177+
return MagicMock(
178+
status_code=200,
179+
text=json.dumps({"results": [{"id": "30", "name": "testVocab", "vocab_file": "vocab.dat"}]})
180+
)
181+
else:
182+
return MagicMock(status_code=404, text='')
183+
184+
mock_get.side_effect = get_side_effect
185+
186+
# Mock authentication and upload responses
187+
mock_upload_response = {"status": "success", "uploaded_projects": 1}
188+
189+
def post_side_effect(url, *args, **kwargs):
190+
if url.endswith('/api/api-token-auth/'):
191+
return MagicMock(status_code=200, text='{"token": "abc"}')
192+
elif url.endswith('/api/upload-deployment/'):
193+
return MagicMock(
194+
status_code=200,
195+
json=lambda: mock_upload_response
196+
)
197+
else:
198+
return MagicMock(status_code=404, text='')
199+
200+
mock_post.side_effect = post_side_effect
201+
202+
session = MedCATTrainerSession(server='http://localhost', username='u', password='p')
203+
projects = [{"id": 1, "name": "Project 1"}]
204+
205+
result = session.upload_projects_export(projects, cdb="testCDB", vocab="testVocab")
206+
207+
# Verify the API call was made correctly
208+
mock_post.assert_called_with(
209+
f'{session.server}/api/upload-deployment/',
210+
headers=session.headers,
211+
json={
212+
'exported_projects': projects,
213+
'cdb_id': '20',
214+
'vocab_id': '30'
215+
}
216+
)
217+
self.assertEqual(result, mock_upload_response)
218+
219+
@patch('mctclient.requests.post')
220+
@patch('mctclient.requests.get')
221+
def test_upload_projects_export_with_modelpack_object(self, mock_get, mock_post):
222+
"""Test upload_projects_export with modelpack object"""
223+
# Mock authentication and upload responses
224+
mock_upload_response = {"status": "success", "uploaded_projects": 1}
225+
226+
def post_side_effect(url, *args, **kwargs):
227+
if url.endswith('/api/api-token-auth/'):
228+
return MagicMock(status_code=200, text='{"token": "abc"}')
229+
elif url.endswith('/api/upload-deployment/'):
230+
return MagicMock(
231+
status_code=200,
232+
json=lambda: mock_upload_response
233+
)
234+
else:
235+
return MagicMock(status_code=404, text='')
236+
237+
mock_post.side_effect = post_side_effect
238+
239+
session = MedCATTrainerSession(server='http://localhost', username='u', password='p')
240+
modelpack = MCTModelPack(id='40', name='testModelPack', model_pack_zip='model.zip')
241+
242+
projects = [{"id": 1, "name": "Project 1"}]
243+
244+
result = session.upload_projects_export(projects, modelpack=modelpack)
245+
246+
# Verify the API call was made correctly
247+
mock_post.assert_called_with(
248+
f'{session.server}/api/upload-deployment/',
249+
headers=session.headers,
250+
json={
251+
'exported_projects': projects,
252+
'modelpack_id': '40'
253+
}
254+
)
255+
self.assertEqual(result, mock_upload_response)
256+
257+
@patch('mctclient.requests.post')
258+
@patch('mctclient.requests.get')
259+
def test_upload_projects_export_with_modelpack_string(self, mock_get, mock_post):
260+
"""Test upload_projects_export with modelpack as string"""
261+
# Mock get_model_packs response
262+
def get_side_effect(url, *args, **kwargs):
263+
if url.endswith('/api/modelpacks/'):
264+
return MagicMock(
265+
status_code=200,
266+
text=json.dumps({"results": [{"id": "40", "name": "testModelPack", "model_pack": "model.zip"}]})
267+
)
268+
else:
269+
return MagicMock(status_code=404, text='')
270+
271+
mock_get.side_effect = get_side_effect
272+
273+
# Mock authentication and upload responses
274+
mock_upload_response = {"status": "success", "uploaded_projects": 1}
275+
276+
def post_side_effect(url, *args, **kwargs):
277+
if url.endswith('/api/api-token-auth/'):
278+
return MagicMock(status_code=200, text='{"token": "abc"}')
279+
elif url.endswith('/api/upload-deployment/'):
280+
return MagicMock(
281+
status_code=200,
282+
json=lambda: mock_upload_response
283+
)
284+
else:
285+
return MagicMock(status_code=404, text='')
286+
287+
mock_post.side_effect = post_side_effect
288+
289+
session = MedCATTrainerSession(server='http://localhost', username='u', password='p')
290+
projects = [{"id": 1, "name": "Project 1"}]
291+
292+
result = session.upload_projects_export(projects, modelpack="testModelPack")
293+
294+
# Verify the API call was made correctly
295+
mock_post.assert_called_with(
296+
f'{session.server}/api/upload-deployment/',
297+
headers=session.headers,
298+
json={
299+
'exported_projects': projects,
300+
'modelpack_id': '40'
301+
}
302+
)
303+
self.assertEqual(result, mock_upload_response)
304+
305+
@patch('mctclient.requests.post')
306+
def test_upload_projects_export_no_cdb_vocab_modelpack(self, mock_post):
307+
"""Test upload_projects_export raises exception when no cdb/vocab/modelpack provided"""
308+
# Mock authentication
309+
def post_side_effect(url, *args, **kwargs):
310+
if url.endswith('/api/api-token-auth/'):
311+
return MagicMock(status_code=200, text='{"token": "abc"}')
312+
else:
313+
return MagicMock(status_code=404, text='')
314+
315+
mock_post.side_effect = post_side_effect
316+
317+
session = MedCATTrainerSession(server='http://localhost', username='u', password='p')
318+
projects = [{"id": 1, "name": "Project 1"}]
319+
320+
with self.assertRaises(Exception) as context:
321+
session.upload_projects_export(projects)
322+
323+
self.assertIn('No cdb, vocab, or modelpack provided', str(context.exception))
324+
325+
@patch('mctclient.requests.post')
326+
def test_upload_projects_export_api_failure(self, mock_post):
327+
"""Test upload_projects_export handles API failure"""
328+
# Mock authentication and failed upload response
329+
def post_side_effect(url, *args, **kwargs):
330+
if url.endswith('/api/api-token-auth/'):
331+
return MagicMock(status_code=200, text='{"token": "abc"}')
332+
elif url.endswith('/api/upload-deployment/'):
333+
return MagicMock(
334+
status_code=400,
335+
text='{"error": "Bad request"}'
336+
)
337+
else:
338+
return MagicMock(status_code=404, text='')
339+
340+
mock_post.side_effect = post_side_effect
341+
342+
session = MedCATTrainerSession(server='http://localhost', username='u', password='p')
343+
cdb = MCTConceptDB(id='20', name='testCDB', conceptdb_file='cdb.dat')
344+
vocab = MCTVocab(id='30', name='testVocab', vocab_file='vocab.dat')
345+
projects = [{"id": 1, "name": "Project 1"}]
346+
347+
with self.assertRaises(Exception) as context:
348+
session.upload_projects_export(projects, cdb=cdb, vocab=vocab)
349+
350+
self.assertIn('Failed to upload projects export', str(context.exception))
351+
125352
if __name__ == '__main__':
126353
unittest.main()

0 commit comments

Comments
 (0)