|
| 1 | +"""Unit tests for cnb_tools.modules.annotation""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, Mock, mock_open, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from synapseclient.core.exceptions import SynapseHTTPError |
| 7 | + |
| 8 | +from cnb_tools.modules import annotation |
| 9 | +from cnb_tools.modules.base import UnknownSynapseID |
| 10 | + |
| 11 | + |
| 12 | +class TestGetSubmissionStatus: |
| 13 | + """Tests for get_submission_status function""" |
| 14 | + |
| 15 | + @patch("cnb_tools.modules.annotation.get_synapse_client") |
| 16 | + def test_get_submission_status_success( |
| 17 | + self, mock_get_client, mock_syn, mock_submission_status |
| 18 | + ): |
| 19 | + """Test successfully getting submission status""" |
| 20 | + mock_get_client.return_value = mock_syn |
| 21 | + mock_syn.getSubmissionStatus.return_value = mock_submission_status |
| 22 | + |
| 23 | + result = annotation.get_submission_status(12345) |
| 24 | + |
| 25 | + mock_syn.getSubmissionStatus.assert_called_once_with(12345) |
| 26 | + assert result == mock_submission_status |
| 27 | + |
| 28 | + @patch("cnb_tools.modules.annotation.get_synapse_client") |
| 29 | + def test_get_submission_status_invalid_id(self, mock_get_client, mock_syn): |
| 30 | + """Test error handling for invalid submission ID""" |
| 31 | + mock_get_client.return_value = mock_syn |
| 32 | + mock_response = Mock() |
| 33 | + mock_response.json.return_value = {"reason": "Submission not found"} |
| 34 | + mock_syn.getSubmissionStatus.side_effect = SynapseHTTPError( |
| 35 | + response=mock_response |
| 36 | + ) |
| 37 | + |
| 38 | + with pytest.raises(UnknownSynapseID) as exc_info: |
| 39 | + annotation.get_submission_status(99999) |
| 40 | + assert "Submission not found" in str(exc_info.value) |
| 41 | + |
| 42 | + |
| 43 | +class TestUpdateAnnotations: |
| 44 | + """Tests for update_annotations function""" |
| 45 | + |
| 46 | + @patch("cnb_tools.modules.annotation.get_synapse_client") |
| 47 | + @patch("cnb_tools.modules.annotation.get_submission_status") |
| 48 | + def test_update_annotations_success( |
| 49 | + self, mock_get_status, mock_get_client, mock_syn, mock_submission_status, capsys |
| 50 | + ): |
| 51 | + """Test successfully updating annotations""" |
| 52 | + mock_get_client.return_value = mock_syn |
| 53 | + mock_get_status.return_value = mock_submission_status |
| 54 | + mock_syn.store.return_value = mock_submission_status |
| 55 | + |
| 56 | + # Mock the submissionAnnotations attribute as a dictionary |
| 57 | + mock_submission_status.submissionAnnotations = MagicMock() |
| 58 | + |
| 59 | + new_annots = {"new_field": "value"} |
| 60 | + result = annotation.update_annotations(12345, new_annots, verbose=False) |
| 61 | + |
| 62 | + mock_submission_status.submissionAnnotations.update.assert_called_once_with( |
| 63 | + new_annots |
| 64 | + ) |
| 65 | + mock_syn.store.assert_called_once_with(mock_submission_status) |
| 66 | + |
| 67 | + captured = capsys.readouterr() |
| 68 | + assert "Submission ID 12345 annotations updated" in captured.out |
| 69 | + assert result == mock_submission_status |
| 70 | + |
| 71 | + @patch("cnb_tools.modules.annotation.get_synapse_client") |
| 72 | + @patch("cnb_tools.modules.annotation.get_submission_status") |
| 73 | + def test_update_annotations_verbose( |
| 74 | + self, mock_get_status, mock_get_client, mock_syn, mock_submission_status, capsys |
| 75 | + ): |
| 76 | + """Test updating annotations with verbose output""" |
| 77 | + mock_get_client.return_value = mock_syn |
| 78 | + mock_get_status.return_value = mock_submission_status |
| 79 | + mock_syn.store.return_value = mock_submission_status |
| 80 | + |
| 81 | + # Mock submissionAnnotations as a dict for JSON serialization |
| 82 | + mock_submission_status.submissionAnnotations = {"score": 0.95, "passed": True} |
| 83 | + |
| 84 | + annotation.update_annotations(12345, {"test": "value"}, verbose=True) |
| 85 | + |
| 86 | + captured = capsys.readouterr() |
| 87 | + assert "Annotations:" in captured.out |
| 88 | + assert "score" in captured.out |
| 89 | + |
| 90 | + |
| 91 | +class TestUpdateAnnotationsFromFile: |
| 92 | + """Tests for update_annotations_from_file function""" |
| 93 | + |
| 94 | + @patch("cnb_tools.modules.annotation.with_retry") |
| 95 | + @patch( |
| 96 | + "builtins.open", |
| 97 | + new_callable=mock_open, |
| 98 | + read_data='{"score": 0.85, "status": "pass", "null_field": null, "empty_list": []}', |
| 99 | + ) |
| 100 | + def test_update_annotations_from_file(self, mock_file, mock_retry): |
| 101 | + """Test updating annotations from JSON file""" |
| 102 | + mock_retry.return_value = MagicMock() |
| 103 | + |
| 104 | + annotation.update_annotations_from_file(12345, "test.json", verbose=False) |
| 105 | + |
| 106 | + mock_file.assert_called_once_with("test.json", encoding="utf-8") |
| 107 | + |
| 108 | + # Verify with_retry was called |
| 109 | + assert mock_retry.called |
| 110 | + |
| 111 | + # Verify null and empty list values were filtered |
| 112 | + call_args = mock_retry.call_args |
| 113 | + assert call_args[1]["wait"] == 3 |
| 114 | + assert call_args[1]["retries"] == 10 |
| 115 | + |
| 116 | + |
| 117 | +class TestUpdateSubmissionStatus: |
| 118 | + """Tests for update_submission_status function""" |
| 119 | + |
| 120 | + @patch("cnb_tools.modules.annotation.get_synapse_client") |
| 121 | + @patch("cnb_tools.modules.annotation.get_submission_status") |
| 122 | + def test_update_submission_status( |
| 123 | + self, mock_get_status, mock_get_client, mock_syn, mock_submission_status, capsys |
| 124 | + ): |
| 125 | + """Test updating submission status""" |
| 126 | + mock_get_client.return_value = mock_syn |
| 127 | + mock_get_status.return_value = mock_submission_status |
| 128 | + mock_syn.store.return_value = mock_submission_status |
| 129 | + |
| 130 | + result = annotation.update_submission_status(12345, "ACCEPTED") |
| 131 | + |
| 132 | + assert mock_submission_status.status == "ACCEPTED" |
| 133 | + mock_syn.store.assert_called_once_with(mock_submission_status) |
| 134 | + |
| 135 | + captured = capsys.readouterr() |
| 136 | + assert "Updated submission ID 12345 to status: ACCEPTED" in captured.out |
| 137 | + assert result == mock_submission_status |
0 commit comments