1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
9 from arvados import api, vocabulary
11 class VocabularyTest(unittest.TestCase):
18 {'label': 'Creature'},
24 {'label': 'Homo sapiens'},
29 {'label': 'Elephant'},
30 {'label': 'Loxodonta'},
38 {'label': 'Importance'},
39 {'label': 'Priority'},
45 {'label': 'High priority'},
51 {'label': 'Medium priority'},
57 {'label': 'Low priority'},
74 self.api = arvados.api('v1')
75 self.voc = vocabulary.Vocabulary(self.EXAMPLE_VOC)
76 self.api.vocabulary = mock.MagicMock(return_value=self.EXAMPLE_VOC)
78 def test_vocabulary_keys(self):
79 self.assertEqual(self.voc.strict_keys, False)
81 self.voc.key_aliases.keys(),
82 set(['idtaganimals', 'creature', 'animal',
83 'idtagimportances', 'importance', 'priority',
84 'idtagcomments', 'comment', 'notes'])
87 vk = self.voc.key_aliases['creature']
88 self.assertEqual(vk.strict, False)
89 self.assertEqual(vk.identifier, 'IDTAGANIMALS')
90 self.assertEqual(vk.aliases, ['Animal', 'Creature'])
91 self.assertEqual(vk.preferred_label, 'Animal')
93 vk.value_aliases.keys(),
94 set(['idvalanimal1', 'human', 'homo sapiens',
95 'idvalanimal2', 'elephant', 'loxodonta'])
98 def test_vocabulary_values(self):
99 vk = self.voc.key_aliases['creature']
100 vv = vk.value_aliases['human']
101 self.assertEqual(vv.identifier, 'IDVALANIMAL1')
102 self.assertEqual(vv.aliases, ['Human', 'Homo sapiens'])
103 self.assertEqual(vv.preferred_label, 'Human')
105 def test_vocabulary_indexing(self):
106 self.assertEqual(self.voc['creature']['human'].identifier, 'IDVALANIMAL1')
107 self.assertEqual(self.voc['Creature']['Human'].identifier, 'IDVALANIMAL1')
108 self.assertEqual(self.voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1')
109 with self.assertRaises(KeyError):
110 inexistant = self.voc['foo']
112 def test_empty_vocabulary(self):
113 voc = vocabulary.Vocabulary({})
114 self.assertEqual(voc.strict_keys, False)
115 self.assertEqual(voc.key_aliases, {})
117 def test_load_vocabulary_with_api(self):
118 voc = vocabulary.load_vocabulary(self.api)
119 self.assertEqual(voc['creature']['human'].identifier, 'IDVALANIMAL1')
120 self.assertEqual(voc['Creature']['Human'].identifier, 'IDVALANIMAL1')
121 self.assertEqual(voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1')
123 def test_convert_to_identifiers(self):
125 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
126 {'IDTAGIMPORTANCES': 'High'},
127 {'importance': 'IDVALIMPORTANCE1'},
128 {'priority': 'high priority'},
132 self.voc.convert_to_identifiers(case),
133 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
134 "failing test case: {}".format(case)
137 def test_convert_to_identifiers_multiple_pairs(self):
139 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
140 {'IDTAGIMPORTANCES': 'High', 'IDTAGANIMALS': 'IDVALANIMAL1', 'comment': 'Very important person'},
141 {'importance': 'IDVALIMPORTANCE1', 'animal': 'IDVALANIMAL1', 'notes': 'Very important person'},
142 {'priority': 'high priority', 'animal': 'IDVALANIMAL1', 'NOTES': 'Very important person'},
146 self.voc.convert_to_identifiers(case),
147 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
148 "failing test case: {}".format(case)
151 def test_convert_to_identifiers_value_lists(self):
153 {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
154 {'IDTAGIMPORTANCES': ['High', 'Medium']},
155 {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
156 {'priority': ['high', 'medium']},
160 self.voc.convert_to_identifiers(case),
161 {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
162 "failing test case: {}".format(case)
165 def test_convert_to_identifiers_unknown_key(self):
166 # Non-strict vocabulary
167 self.assertEqual(self.voc.strict_keys, False)
168 self.assertEqual(self.voc.convert_to_identifiers({'foo': 'bar'}), {'foo': 'bar'})
170 strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
171 strict_voc.strict_keys = True
172 with self.assertRaises(vocabulary.VocabularyKeyError):
173 strict_voc.convert_to_identifiers({'foo': 'bar'})
175 def test_convert_to_identifiers_invalid_key(self):
176 with self.assertRaises(vocabulary.VocabularyKeyError):
177 self.voc.convert_to_identifiers({42: 'bar'})
178 with self.assertRaises(vocabulary.VocabularyKeyError):
179 self.voc.convert_to_identifiers({None: 'bar'})
180 with self.assertRaises(vocabulary.VocabularyKeyError):
181 self.voc.convert_to_identifiers({('f', 'o', 'o'): 'bar'})
183 def test_convert_to_identifiers_unknown_value(self):
185 self.assertEqual(self.voc['animal'].strict, False)
186 self.assertEqual(self.voc.convert_to_identifiers({'Animal': 'foo'}), {'IDTAGANIMALS': 'foo'})
188 self.assertEqual(self.voc['priority'].strict, True)
189 with self.assertRaises(vocabulary.VocabularyValueError):
190 self.voc.convert_to_identifiers({'Priority': 'foo'})
192 def test_convert_to_identifiers_invalid_value(self):
193 with self.assertRaises(vocabulary.VocabularyValueError):
194 self.voc.convert_to_identifiers({'Animal': 42})
195 with self.assertRaises(vocabulary.VocabularyValueError):
196 self.voc.convert_to_identifiers({'Animal': None})
197 with self.assertRaises(vocabulary.VocabularyValueError):
198 self.voc.convert_to_identifiers({'Animal': {'hello': 'world'}})
199 with self.assertRaises(vocabulary.VocabularyValueError):
200 self.voc.convert_to_identifiers({'Animal': [42]})
201 with self.assertRaises(vocabulary.VocabularyValueError):
202 self.voc.convert_to_identifiers({'Animal': [None]})
203 with self.assertRaises(vocabulary.VocabularyValueError):
204 self.voc.convert_to_identifiers({'Animal': [{'hello': 'world'}]})
206 def test_convert_to_identifiers_unknown_value_list(self):
208 self.assertEqual(self.voc['animal'].strict, False)
210 self.voc.convert_to_identifiers({'Animal': ['foo', 'loxodonta']}),
211 {'IDTAGANIMALS': ['foo', 'IDVALANIMAL2']}
214 self.assertEqual(self.voc['priority'].strict, True)
215 with self.assertRaises(vocabulary.VocabularyValueError):
216 self.voc.convert_to_identifiers({'Priority': ['foo', 'bar']})
218 def test_convert_to_labels(self):
220 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
221 {'IDTAGIMPORTANCES': 'High'},
222 {'importance': 'IDVALIMPORTANCE1'},
223 {'priority': 'high priority'},
227 self.voc.convert_to_labels(case),
228 {'Importance': 'High'},
229 "failing test case: {}".format(case)
232 def test_convert_to_labels_multiple_pairs(self):
234 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
235 {'IDTAGIMPORTANCES': 'High', 'IDTAGANIMALS': 'IDVALANIMAL1', 'comment': 'Very important person'},
236 {'importance': 'IDVALIMPORTANCE1', 'animal': 'IDVALANIMAL1', 'notes': 'Very important person'},
237 {'priority': 'high priority', 'animal': 'IDVALANIMAL1', 'NOTES': 'Very important person'},
241 self.voc.convert_to_labels(case),
242 {'Importance': 'High', 'Animal': 'Human', 'Comment': 'Very important person'},
243 "failing test case: {}".format(case)
246 def test_convert_to_labels_value_lists(self):
248 {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
249 {'IDTAGIMPORTANCES': ['High', 'Medium']},
250 {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
251 {'priority': ['high', 'medium']},
255 self.voc.convert_to_labels(case),
256 {'Importance': ['High', 'Medium']},
257 "failing test case: {}".format(case)
260 def test_convert_to_labels_unknown_key(self):
261 # Non-strict vocabulary
262 self.assertEqual(self.voc.strict_keys, False)
263 self.assertEqual(self.voc.convert_to_labels({'foo': 'bar'}), {'foo': 'bar'})
265 strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
266 strict_voc.strict_keys = True
267 with self.assertRaises(vocabulary.VocabularyKeyError):
268 strict_voc.convert_to_labels({'foo': 'bar'})
270 def test_convert_to_labels_invalid_key(self):
271 with self.assertRaises(vocabulary.VocabularyKeyError):
272 self.voc.convert_to_labels({42: 'bar'})
273 with self.assertRaises(vocabulary.VocabularyKeyError):
274 self.voc.convert_to_labels({None: 'bar'})
275 with self.assertRaises(vocabulary.VocabularyKeyError):
276 self.voc.convert_to_labels({('f', 'o', 'o'): 'bar'})
278 def test_convert_to_labels_unknown_value(self):
280 self.assertEqual(self.voc['animal'].strict, False)
281 self.assertEqual(self.voc.convert_to_labels({'IDTAGANIMALS': 'foo'}), {'Animal': 'foo'})
283 self.assertEqual(self.voc['priority'].strict, True)
284 with self.assertRaises(vocabulary.VocabularyValueError):
285 self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})
287 def test_convert_to_labels_invalid_value(self):
288 with self.assertRaises(vocabulary.VocabularyValueError):
289 self.voc.convert_to_labels({'IDTAGIMPORTANCES': {'high': True}})
290 with self.assertRaises(vocabulary.VocabularyValueError):
291 self.voc.convert_to_labels({'IDTAGIMPORTANCES': None})
292 with self.assertRaises(vocabulary.VocabularyValueError):
293 self.voc.convert_to_labels({'IDTAGIMPORTANCES': 42})
294 with self.assertRaises(vocabulary.VocabularyValueError):
295 self.voc.convert_to_labels({'IDTAGIMPORTANCES': False})
296 with self.assertRaises(vocabulary.VocabularyValueError):
297 self.voc.convert_to_labels({'IDTAGIMPORTANCES': [42]})
298 with self.assertRaises(vocabulary.VocabularyValueError):
299 self.voc.convert_to_labels({'IDTAGIMPORTANCES': [None]})
300 with self.assertRaises(vocabulary.VocabularyValueError):
301 self.voc.convert_to_labels({'IDTAGIMPORTANCES': [{'high': True}]})
303 def test_convert_to_labels_unknown_value_list(self):
305 self.assertEqual(self.voc['animal'].strict, False)
307 self.voc.convert_to_labels({'IDTAGANIMALS': ['foo', 'IDVALANIMAL1']}),
308 {'Animal': ['foo', 'Human']}
311 self.assertEqual(self.voc['priority'].strict, True)
312 with self.assertRaises(vocabulary.VocabularyValueError):
313 self.voc.convert_to_labels({'IDTAGIMPORTANCES': ['foo', 'bar']})
315 def test_convert_roundtrip(self):
316 initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
317 converted = self.voc.convert_to_labels(initial)
318 self.assertNotEqual(converted, initial)
319 self.assertEqual(self.voc.convert_to_identifiers(converted), initial)