18574: Adds key & value type checking. Improves code layout for readability.
[arvados.git] / sdk / python / tests / test_vocabulary.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import arvados
6 import unittest
7 import mock
8
9 from arvados import api, vocabulary
10
11 class VocabularyTest(unittest.TestCase):
12     EXAMPLE_VOC = {
13         'tags': {
14             'IDTAGANIMALS': {
15                 'strict': False,
16                 'labels': [
17                     {'label': 'Animal'},
18                     {'label': 'Creature'},
19                 ],
20                 'values': {
21                     'IDVALANIMAL1': {
22                         'labels': [
23                             {'label': 'Human'},
24                             {'label': 'Homo sapiens'},
25                         ],
26                     },
27                     'IDVALANIMAL2': {
28                         'labels': [
29                             {'label': 'Elephant'},
30                             {'label': 'Loxodonta'},
31                         ],
32                     },
33                 },
34             },
35             'IDTAGIMPORTANCES': {
36                 'strict': True,
37                 'labels': [
38                     {'label': 'Importance'},
39                     {'label': 'Priority'},
40                 ],
41                 'values': {
42                     'IDVALIMPORTANCE1': {
43                         'labels': [
44                             {'label': 'High'},
45                             {'label': 'High priority'},
46                         ],
47                     },
48                     'IDVALIMPORTANCE2': {
49                         'labels': [
50                             {'label': 'Medium'},
51                             {'label': 'Medium priority'},
52                         ],
53                     },
54                     'IDVALIMPORTANCE3': {
55                         'labels': [
56                             {'label': 'Low'},
57                             {'label': 'Low priority'},
58                         ],
59                     },
60                 },
61             },
62             'IDTAGCOMMENTS': {
63                 'strict': False,
64                 'labels': [
65                     {'label': 'Comment'},
66                     {'label': 'Notes'},
67                 ],
68                 'values': None,
69             },
70         },
71     }
72
73     def setUp(self):
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)
77
78     def test_vocabulary_keys(self):
79         self.assertEqual(self.voc.strict_keys, False)
80         self.assertEqual(
81             self.voc.key_aliases.keys(),
82             set(['idtaganimals', 'creature', 'animal',
83                 'idtagimportances', 'importance', 'priority',
84                 'idtagcomments', 'comment', 'notes'])
85         )
86
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')
92         self.assertEqual(
93             vk.value_aliases.keys(),
94             set(['idvalanimal1', 'human', 'homo sapiens',
95                 'idvalanimal2', 'elephant', 'loxodonta'])
96         )
97
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')
104
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']
111
112     def test_empty_vocabulary(self):
113         voc = vocabulary.Vocabulary({})
114         self.assertEqual(voc.strict_keys, False)
115         self.assertEqual(voc.key_aliases, {})
116
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')
122
123     def test_convert_to_identifiers(self):
124         cases = [
125             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
126             {'IDTAGIMPORTANCES': 'High'},
127             {'importance': 'IDVALIMPORTANCE1'},
128             {'priority': 'high priority'},
129         ]
130         for case in cases:
131             self.assertEqual(
132                 self.voc.convert_to_identifiers(case),
133                 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
134                 "failing test case: {}".format(case)
135             )
136
137     def test_convert_to_identifiers_multiple_pairs(self):
138         cases = [
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'},
143         ]
144         for case in cases:
145             self.assertEqual(
146                 self.voc.convert_to_identifiers(case),
147                 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
148                 "failing test case: {}".format(case)
149             )
150
151     def test_convert_to_identifiers_value_lists(self):
152         cases = [
153             {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
154             {'IDTAGIMPORTANCES': ['High', 'Medium']},
155             {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
156             {'priority': ['high', 'medium']},
157         ]
158         for case in cases:
159             self.assertEqual(
160                 self.voc.convert_to_identifiers(case),
161                 {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
162                 "failing test case: {}".format(case)
163             )
164
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'})
169         # Strict vocabulary
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'})
174
175     def test_convert_to_identifiers_invalid_key(self):
176         with self.assertRaises(vocabulary.VocabularyKeyError):
177             self.voc.convert_to_identifiers({('f', 'o', 'o'): 'bar'})
178
179     def test_convert_to_identifiers_unknown_value(self):
180         # Non-strict key
181         self.assertEqual(self.voc['animal'].strict, False)
182         self.assertEqual(self.voc.convert_to_identifiers({'Animal': 'foo'}), {'IDTAGANIMALS': 'foo'})
183         # Strict key
184         self.assertEqual(self.voc['priority'].strict, True)
185         with self.assertRaises(vocabulary.VocabularyValueError):
186             self.voc.convert_to_identifiers({'Priority': 'foo'})
187
188     def test_convert_to_identifiers_invalid_value(self):
189         with self.assertRaises(vocabulary.VocabularyValueError):
190             self.voc.convert_to_identifiers({'Animal': 42})
191
192     def test_convert_to_identifiers_unknown_value_list(self):
193         # Non-strict key
194         self.assertEqual(self.voc['animal'].strict, False)
195         self.assertEqual(
196             self.voc.convert_to_identifiers({'Animal': ['foo', 'loxodonta']}),
197             {'IDTAGANIMALS': ['foo', 'IDVALANIMAL2']}
198         )
199         # Strict key
200         self.assertEqual(self.voc['priority'].strict, True)
201         with self.assertRaises(vocabulary.VocabularyValueError):
202             self.voc.convert_to_identifiers({'Priority': ['foo', 'bar']})
203
204     def test_convert_to_labels(self):
205         cases = [
206             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
207             {'IDTAGIMPORTANCES': 'High'},
208             {'importance': 'IDVALIMPORTANCE1'},
209             {'priority': 'high priority'},
210         ]
211         for case in cases:
212             self.assertEqual(
213                 self.voc.convert_to_labels(case),
214                 {'Importance': 'High'},
215                 "failing test case: {}".format(case)
216             )
217
218     def test_convert_to_labels_multiple_pairs(self):
219         cases = [
220             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
221             {'IDTAGIMPORTANCES': 'High', 'IDTAGANIMALS': 'IDVALANIMAL1', 'comment': 'Very important person'},
222             {'importance': 'IDVALIMPORTANCE1', 'animal': 'IDVALANIMAL1', 'notes': 'Very important person'},
223             {'priority': 'high priority', 'animal': 'IDVALANIMAL1', 'NOTES': 'Very important person'},
224         ]
225         for case in cases:
226             self.assertEqual(
227                 self.voc.convert_to_labels(case),
228                 {'Importance': 'High', 'Animal': 'Human', 'Comment': 'Very important person'},
229                 "failing test case: {}".format(case)
230             )
231
232     def test_convert_to_labels_value_lists(self):
233         cases = [
234             {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
235             {'IDTAGIMPORTANCES': ['High', 'Medium']},
236             {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
237             {'priority': ['high', 'medium']},
238         ]
239         for case in cases:
240             self.assertEqual(
241                 self.voc.convert_to_labels(case),
242                 {'Importance': ['High', 'Medium']},
243                 "failing test case: {}".format(case)
244             )
245
246     def test_convert_to_labels_unknown_key(self):
247         # Non-strict vocabulary
248         self.assertEqual(self.voc.strict_keys, False)
249         self.assertEqual(self.voc.convert_to_labels({'foo': 'bar'}), {'foo': 'bar'})
250         # Strict vocabulary
251         strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
252         strict_voc.strict_keys = True
253         with self.assertRaises(vocabulary.VocabularyKeyError):
254             strict_voc.convert_to_labels({'foo': 'bar'})
255
256     def test_convert_to_labels_invalid_key(self):
257         with self.assertRaises(vocabulary.VocabularyKeyError):
258             self.voc.convert_to_labels({42: 'bar'})
259
260     def test_convert_to_labels_unknown_value(self):
261         # Non-strict key
262         self.assertEqual(self.voc['animal'].strict, False)
263         self.assertEqual(self.voc.convert_to_labels({'IDTAGANIMALS': 'foo'}), {'Animal': 'foo'})
264         # Strict key
265         self.assertEqual(self.voc['priority'].strict, True)
266         with self.assertRaises(vocabulary.VocabularyValueError):
267             self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})
268
269     def test_convert_to_labels_invalid_value(self):
270         with self.assertRaises(vocabulary.VocabularyValueError):
271             self.voc.convert_to_labels({'IDTAGIMPORTANCES': {'high': True}})
272
273     def test_convert_to_labels_unknown_value_list(self):
274         # Non-strict key
275         self.assertEqual(self.voc['animal'].strict, False)
276         self.assertEqual(
277             self.voc.convert_to_labels({'IDTAGANIMALS': ['foo', 'IDVALANIMAL1']}),
278             {'Animal': ['foo', 'Human']}
279         )
280         # Strict key
281         self.assertEqual(self.voc['priority'].strict, True)
282         with self.assertRaises(vocabulary.VocabularyValueError):
283             self.voc.convert_to_labels({'IDTAGIMPORTANCES': ['foo', 'bar']})
284
285     def test_convert_roundtrip(self):
286         initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
287         converted = self.voc.convert_to_labels(initial)
288         self.assertNotEqual(converted, initial)
289         self.assertEqual(self.voc.convert_to_identifiers(converted), initial)