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'},
73 self.api = arvados.api('v1')
74 self.voc = vocabulary.Vocabulary(self.EXAMPLE_VOC)
75 self.api.vocabulary = mock.MagicMock(return_value=self.EXAMPLE_VOC)
77 def test_vocabulary_keys(self):
78 self.assertEqual(self.voc.strict_keys, False)
80 self.voc.key_aliases.keys(),
81 set(['idtaganimals', 'creature', 'animal',
82 'idtagimportances', 'importance', 'priority',
83 'idtagcomments', 'comment', 'notes'])
86 vk = self.voc.key_aliases['creature']
87 self.assertEqual(vk.strict, False)
88 self.assertEqual(vk.identifier, 'IDTAGANIMALS')
89 self.assertEqual(vk.aliases, ['Animal', 'Creature'])
90 self.assertEqual(vk.preferred_label, 'Animal')
92 vk.value_aliases.keys(),
93 set(['idvalanimal1', 'human', 'homo sapiens',
94 'idvalanimal2', 'elephant', 'loxodonta'])
97 def test_vocabulary_values(self):
98 vk = self.voc.key_aliases['creature']
99 vv = vk.value_aliases['human']
100 self.assertEqual(vv.identifier, 'IDVALANIMAL1')
101 self.assertEqual(vv.aliases, ['Human', 'Homo sapiens'])
102 self.assertEqual(vv.preferred_label, 'Human')
104 def test_vocabulary_indexing(self):
105 self.assertEqual(self.voc['creature']['human'].identifier, 'IDVALANIMAL1')
106 self.assertEqual(self.voc['Creature']['Human'].identifier, 'IDVALANIMAL1')
107 self.assertEqual(self.voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1')
108 with self.assertRaises(KeyError):
109 inexistant = self.voc['foo']
111 def test_empty_vocabulary(self):
112 voc = vocabulary.Vocabulary({})
113 self.assertEqual(voc.strict_keys, False)
114 self.assertEqual(voc.key_aliases, {})
116 def test_load_vocabulary_with_api(self):
117 voc = vocabulary.load_vocabulary(self.api)
118 self.assertEqual(voc['creature']['human'].identifier, 'IDVALANIMAL1')
119 self.assertEqual(voc['Creature']['Human'].identifier, 'IDVALANIMAL1')
120 self.assertEqual(voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1')
122 def test_convert_to_identifiers(self):
124 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
125 {'IDTAGIMPORTANCES': 'High'},
126 {'importance': 'IDVALIMPORTANCE1'},
127 {'priority': 'high priority'},
131 self.voc.convert_to_identifiers(case),
132 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
133 "failing test case: {}".format(case)
136 def test_convert_to_identifiers_multiple_pairs(self):
138 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
139 {'IDTAGIMPORTANCES': 'High', 'IDTAGANIMALS': 'IDVALANIMAL1', 'comment': 'Very important person'},
140 {'importance': 'IDVALIMPORTANCE1', 'animal': 'IDVALANIMAL1', 'notes': 'Very important person'},
141 {'priority': 'high priority', 'animal': 'IDVALANIMAL1', 'NOTES': 'Very important person'},
145 self.voc.convert_to_identifiers(case),
146 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
147 "failing test case: {}".format(case)
150 def test_convert_to_identifiers_unknown_key(self):
151 # Non-strict vocabulary
152 self.assertEqual(self.voc.strict_keys, False)
153 self.assertEqual(self.voc.convert_to_identifiers({'foo': 'bar'}), {'foo': 'bar'})
155 strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
156 strict_voc.strict_keys = True
157 with self.assertRaises(KeyError):
158 strict_voc.convert_to_identifiers({'foo': 'bar'})
160 def test_convert_to_identifiers_unknown_value(self):
162 self.assertEqual(self.voc['animal'].strict, False)
163 self.assertEqual(self.voc.convert_to_identifiers({'Animal': 'foo'}), {'IDTAGANIMALS': 'foo'})
165 self.assertEqual(self.voc['priority'].strict, True)
166 with self.assertRaises(ValueError):
167 self.voc.convert_to_identifiers({'Priority': 'foo'})
169 def test_convert_to_labels(self):
171 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
172 {'IDTAGIMPORTANCES': 'High'},
173 {'importance': 'IDVALIMPORTANCE1'},
174 {'priority': 'high priority'},
178 self.voc.convert_to_labels(case),
179 {'Importance': 'High'},
180 "failing test case: {}".format(case)
183 def test_convert_to_labels_multiple_pairs(self):
185 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
186 {'IDTAGIMPORTANCES': 'High', 'IDTAGANIMALS': 'IDVALANIMAL1', 'comment': 'Very important person'},
187 {'importance': 'IDVALIMPORTANCE1', 'animal': 'IDVALANIMAL1', 'notes': 'Very important person'},
188 {'priority': 'high priority', 'animal': 'IDVALANIMAL1', 'NOTES': 'Very important person'},
192 self.voc.convert_to_labels(case),
193 {'Importance': 'High', 'Animal': 'Human', 'Comment': 'Very important person'},
194 "failing test case: {}".format(case)
197 def test_convert_roundtrip(self):
198 initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
199 converted = self.voc.convert_to_labels(initial)
200 self.assertNotEqual(converted, initial)
201 self.assertEqual(self.voc.convert_to_identifiers(converted), initial)
203 def test_convert_to_labels_unknown_key(self):
204 # Non-strict vocabulary
205 self.assertEqual(self.voc.strict_keys, False)
206 self.assertEqual(self.voc.convert_to_labels({'foo': 'bar'}), {'foo': 'bar'})
208 strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
209 strict_voc.strict_keys = True
210 with self.assertRaises(KeyError):
211 strict_voc.convert_to_labels({'foo': 'bar'})
213 def test_convert_to_labels_unknown_value(self):
215 self.assertEqual(self.voc['animal'].strict, False)
216 self.assertEqual(self.voc.convert_to_labels({'IDTAGANIMALS': 'foo'}), {'Animal': 'foo'})
218 self.assertEqual(self.voc['priority'].strict, True)
219 with self.assertRaises(ValueError):
220 self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})