-
Notifications
You must be signed in to change notification settings - Fork 10
fix(medcat-trainer): Fix remote model service errors on cache_project… #363
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
medcat-trainer/webapp/api/api/tests/test_remote_model_service_e2e.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| """End-to-end happy-path test for projects using a remote MedCAT model service. | ||
|
|
||
| This test mirrors the UI flow when a user works with a remote-model project: | ||
| 1. Set up a project configured with use_model_service=True and a dataset containing one document. | ||
| 2. Call GET /api/cache-project-model/<id>/ and assert 200 (no local model to load; endpoint returns success). | ||
| 3. Call POST /api/prepare-documents/ with project_id and document_ids; the view calls the remote | ||
| MedCAT service to get annotations. Only the HTTP call to the remote service is stubbed (requests.post); | ||
| the rest of the stack (auth, DB, add_annotations, prepared_documents) runs for real. | ||
|
|
||
| Assertions include: both endpoints return 200, the stub was called with the expected URL and document | ||
| text, and the document is added to the project's prepared_documents. | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from django.contrib.auth.models import User | ||
| from django.core.files.uploadedfile import SimpleUploadedFile | ||
| from django.test import TestCase | ||
| from rest_framework.test import APIClient | ||
|
|
||
| from ..models import Dataset, Document, ProjectAnnotateEntities | ||
|
|
||
|
|
||
| class RemoteModelServiceE2ETestCase(TestCase): | ||
| """Single test: create remote project + dataset with one document, then call cache and prepare-documents.""" | ||
|
|
||
| def setUp(self): | ||
| self.user = User.objects.create_user(username="testuser", password="testpass") | ||
| csv_content = b"name,text\ndoc1,Patient had acute kidney failure." | ||
| self.dataset = Dataset( | ||
| name="Test Remote Dataset", | ||
| original_file=SimpleUploadedFile("test.csv", csv_content, content_type="text/csv"), | ||
| ) | ||
| self.dataset.save() | ||
| self.document = Document.objects.create( | ||
| dataset=self.dataset, | ||
| name="doc1", | ||
| text="Patient had acute kidney failure.", | ||
| ) | ||
| self.project = ProjectAnnotateEntities.objects.create( | ||
| name="Test Remote Project", | ||
| dataset=self.dataset, | ||
| use_model_service=True, | ||
| model_service_url="http://medcat-service:8000", | ||
| cuis="", | ||
| ) | ||
| self.project.members.add(self.user) | ||
|
|
||
| def _run_cache_and_prepare_then_assert_annotated_entities( | ||
| self, mock_json_return_value, expected_annotated_entities_str | ||
| ): | ||
| """Shared flow: stub medcat-service with given response, call cache + prepare-documents + annotated-entities, assert response matches expected JSON string.""" | ||
| mock_response = MagicMock() | ||
| mock_response.raise_for_status = MagicMock() | ||
| mock_response.json.return_value = mock_json_return_value | ||
|
|
||
| with patch.dict(os.environ, {"REMOTE_MODEL_SERVICE_TYPE": "medcat"}): | ||
| with patch("api.utils.requests.post", return_value=mock_response) as mock_post: | ||
| client = APIClient() | ||
| client.force_authenticate(user=self.user) | ||
|
|
||
| cache_resp = client.get(f"/api/cache-project-model/{self.project.id}/") | ||
| self.assertEqual(cache_resp.status_code, 200) | ||
|
|
||
| prepare_resp = client.post( | ||
| "/api/prepare-documents/", | ||
| data={"project_id": self.project.id, "document_ids": [self.document.id]}, | ||
| format="json", | ||
| ) | ||
| self.assertEqual(prepare_resp.status_code, 200) | ||
| self.assertEqual(prepare_resp.json().get("message"), "Documents prepared successfully") | ||
|
|
||
| mock_post.assert_called_once() | ||
| call_args, call_kwargs = mock_post.call_args | ||
| self.assertEqual(call_args[0], f"{self.project.model_service_url.rstrip('/')}/api/process") | ||
| self.assertEqual(call_kwargs["json"], {"content": {"text": self.document.text}}) | ||
| self.assertIn("timeout", call_kwargs) | ||
|
|
||
| self.project.refresh_from_db() | ||
| self.assertIn(self.document, self.project.prepared_documents.all()) | ||
|
|
||
| ann_resp = client.get( | ||
| "/api/annotated-entities/", | ||
| data={"project": self.project.id, "document": self.document.id}, | ||
| ) | ||
| self.assertEqual(ann_resp.status_code, 200) | ||
| expected = json.loads(expected_annotated_entities_str) | ||
| actual = ann_resp.json() | ||
| self.assertEqual(actual["count"], expected["count"]) | ||
| self.assertEqual(actual["next"], expected["next"]) | ||
| self.assertEqual(actual["previous"], expected["previous"]) | ||
| self.assertEqual(len(actual["results"]), len(expected["results"])) | ||
| for i, exp_result in enumerate(expected["results"]): | ||
| for key in exp_result: | ||
| self.assertEqual( | ||
| actual["results"][i].get(key), exp_result[key], f"results[{i}].{key}" | ||
| ) | ||
|
|
||
| def test_cache_and_prepare_documents_remote_project_empty_annotations(self): | ||
| """GET cache-project-model returns 200; POST prepare-documents with stubbed medcat-service returns 200.""" | ||
| mock_json = { | ||
| "result": { | ||
| "text": self.document.text, | ||
| "annotations": [], | ||
| "success": True, | ||
| "timestamp": "", | ||
| "elapsed_time": 0, | ||
| "footer": None, | ||
| } | ||
| } | ||
| expected_str = """ | ||
| { | ||
| "count": 0, | ||
| "next": null, | ||
| "previous": null, | ||
| "results": [] | ||
| } | ||
| """ | ||
| self._run_cache_and_prepare_then_assert_annotated_entities(mock_json, expected_str) | ||
|
|
||
| def test_cache_and_prepare_documents_remote_project_with_annotations(self): | ||
| """Same flow but mock returns one annotation; assert annotated-entities list includes it.""" | ||
| mock_json = { | ||
| "result": { | ||
| "text": self.document.text, | ||
| "annotations": [ | ||
| { | ||
| "0": { | ||
| "cui": "C0022660", | ||
| "start": 10, | ||
| "end": 30, | ||
| "source_value": "acute kidney failure", | ||
| "detected_name": "acute~kidney~failure", | ||
| "acc": 0.99, | ||
| "context_similarity": 0.99, | ||
| "meta_anns": {}, | ||
| } | ||
| } | ||
| ], | ||
| "success": True, | ||
| "timestamp": "", | ||
| "elapsed_time": 0, | ||
| "footer": None, | ||
| } | ||
| } | ||
| expected_str = """ | ||
| { | ||
| "count": 1, | ||
| "next": null, | ||
| "previous": null, | ||
| "results": [ | ||
| { | ||
| "value": "acute~kidney~failure", | ||
| "start_ind": 10, | ||
| "end_ind": 30, | ||
| "acc": 0.99, | ||
| "comment": null, | ||
| "validated": false, | ||
| "correct": false, | ||
| "alternative": false, | ||
| "manually_created": false, | ||
| "deleted": false, | ||
| "killed": false, | ||
| "irrelevant": false | ||
| } | ||
| ] | ||
| } | ||
| """ | ||
| self._run_cache_and_prepare_then_assert_annotated_entities(mock_json, expected_str) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very similar to the
call_remote_model_service_spacymethod above. The only difference in the setup is the payload and then when reading output the entities are read differently. So there's a bunch of duplicate code other than that.I suppose your purpose was to keep both and (probably) just remove the old / incorrect implementation later down the line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I 100% planned to just delete one of them later.
As a side note - the original payload it tried to use is much closer to the one I'd also want medcat-service to have, so I'm open to there being some other service api going around... eg I think cohort has built its own.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cheers - I've added a test for this too, it hits the same APIs the UI will. As I've used cursor to fix cursor it feels like an infinite loop / infinite revenue for them . This is until the planet runs out of resources anyway.