|
1 | 1 | import unittest |
| 2 | +from unittest.mock import patch |
2 | 3 |
|
3 | | -from backend.common.census_cube.data.ontology_labels import gene_term_label, ontology_term_label |
| 4 | +from backend.common.census_cube.data.ontology_labels import ( |
| 5 | + gene_term_label, |
| 6 | + is_ontology_term_deprecated, |
| 7 | + ontology_term_description, |
| 8 | + ontology_term_label, |
| 9 | + ontology_term_synonyms, |
| 10 | +) |
4 | 11 |
|
5 | 12 |
|
6 | 13 | class OntologyLabelTests(unittest.TestCase): |
@@ -36,6 +43,97 @@ def test__gene_label(self): |
36 | 43 | with self.subTest(gene_id): |
37 | 44 | self.assertEqual(gene_term_label(gene_id), expected_gene_label) |
38 | 45 |
|
| 46 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 47 | + def test_ontology_term_label_handles_key_error(self, mock_ontology_parser): |
| 48 | + """Test that ontology_term_label handles KeyError for missing terms in ontology.""" |
| 49 | + # Simulate the error that occurred in production with CL:4033085 |
| 50 | + mock_ontology_parser.get_term_label.side_effect = KeyError("CL:4033085") |
| 51 | + |
| 52 | + result = ontology_term_label("CL:4033085") |
| 53 | + |
| 54 | + # Should return the term ID itself as a fallback instead of crashing |
| 55 | + self.assertEqual(result, "CL:4033085") |
| 56 | + |
| 57 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 58 | + def test_ontology_term_label_handles_value_error(self, mock_ontology_parser): |
| 59 | + """Test that ontology_term_label handles ValueError gracefully.""" |
| 60 | + mock_ontology_parser.get_term_label.side_effect = ValueError("Invalid term") |
| 61 | + |
| 62 | + result = ontology_term_label("CL:INVALID") |
| 63 | + |
| 64 | + # Should return the term ID itself as a fallback |
| 65 | + self.assertEqual(result, "CL:INVALID") |
| 66 | + |
| 67 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 68 | + def test_ontology_term_label_success_path(self, mock_ontology_parser): |
| 69 | + """Test that ontology_term_label returns the correct label when term exists.""" |
| 70 | + mock_ontology_parser.get_term_label.return_value = "native cell" |
| 71 | + |
| 72 | + result = ontology_term_label("CL:0000003") |
| 73 | + |
| 74 | + self.assertEqual(result, "native cell") |
| 75 | + mock_ontology_parser.get_term_label.assert_called_once_with("CL:0000003") |
| 76 | + |
| 77 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 78 | + def test_is_ontology_term_deprecated_handles_key_error(self, mock_ontology_parser): |
| 79 | + """Test that is_ontology_term_deprecated handles KeyError for missing terms.""" |
| 80 | + mock_ontology_parser.is_term_deprecated.side_effect = KeyError("CL:4033085") |
| 81 | + |
| 82 | + result = is_ontology_term_deprecated("CL:4033085") |
| 83 | + |
| 84 | + # Should return False for unknown terms (assume not deprecated) |
| 85 | + self.assertEqual(result, False) |
| 86 | + |
| 87 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 88 | + def test_is_ontology_term_deprecated_success_path(self, mock_ontology_parser): |
| 89 | + """Test that is_ontology_term_deprecated returns the correct value when term exists.""" |
| 90 | + mock_ontology_parser.is_term_deprecated.return_value = True |
| 91 | + |
| 92 | + result = is_ontology_term_deprecated("CL:0000001") |
| 93 | + |
| 94 | + self.assertEqual(result, True) |
| 95 | + mock_ontology_parser.is_term_deprecated.assert_called_once_with("CL:0000001") |
| 96 | + |
| 97 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 98 | + def test_ontology_term_description_handles_key_error(self, mock_ontology_parser): |
| 99 | + """Test that ontology_term_description handles KeyError for missing terms.""" |
| 100 | + mock_ontology_parser.get_term_description.side_effect = KeyError("CL:4033085") |
| 101 | + |
| 102 | + result = ontology_term_description("CL:4033085") |
| 103 | + |
| 104 | + # Should return None for unknown terms (no description available) |
| 105 | + self.assertIsNone(result) |
| 106 | + |
| 107 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 108 | + def test_ontology_term_description_success_path(self, mock_ontology_parser): |
| 109 | + """Test that ontology_term_description returns the correct description when term exists.""" |
| 110 | + mock_ontology_parser.get_term_description.return_value = "A cell description" |
| 111 | + |
| 112 | + result = ontology_term_description("CL:0000003") |
| 113 | + |
| 114 | + self.assertEqual(result, "A cell description") |
| 115 | + mock_ontology_parser.get_term_description.assert_called_once_with("CL:0000003") |
| 116 | + |
| 117 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 118 | + def test_ontology_term_synonyms_handles_key_error(self, mock_ontology_parser): |
| 119 | + """Test that ontology_term_synonyms handles KeyError for missing terms.""" |
| 120 | + mock_ontology_parser.get_term_synonyms.side_effect = KeyError("CL:4033085") |
| 121 | + |
| 122 | + result = ontology_term_synonyms("CL:4033085") |
| 123 | + |
| 124 | + # Should return empty list for unknown terms (no synonyms available) |
| 125 | + self.assertEqual(result, []) |
| 126 | + |
| 127 | + @patch("backend.common.census_cube.data.ontology_labels.ontology_parser") |
| 128 | + def test_ontology_term_synonyms_success_path(self, mock_ontology_parser): |
| 129 | + """Test that ontology_term_synonyms returns the correct synonyms when term exists.""" |
| 130 | + mock_ontology_parser.get_term_synonyms.return_value = ["synonym1", "synonym2"] |
| 131 | + |
| 132 | + result = ontology_term_synonyms("CL:0000003") |
| 133 | + |
| 134 | + self.assertEqual(result, ["synonym1", "synonym2"]) |
| 135 | + mock_ontology_parser.get_term_synonyms.assert_called_once_with("CL:0000003") |
| 136 | + |
39 | 137 |
|
40 | 138 | if __name__ == "__main__": |
41 | 139 | unittest.main() |
0 commit comments