18574: Fixes value list edge cases (with tests). Also avoids duplicated code.
[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             },
69         },
70     }
71
72     def setUp(self):
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)
76
77     def test_vocabulary_keys(self):
78         self.assertEqual(self.voc.strict_keys, False)
79         self.assertEqual(
80             self.voc.key_aliases.keys(),
81             set(['idtaganimals', 'creature', 'animal',
82                 'idtagimportances', 'importance', 'priority',
83                 'idtagcomments', 'comment', 'notes'])
84         )
85
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')
91         self.assertEqual(
92             vk.value_aliases.keys(),
93             set(['idvalanimal1', 'human', 'homo sapiens',
94                 'idvalanimal2', 'elephant', 'loxodonta'])
95         )
96
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')
103
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']
110
111     def test_empty_vocabulary(self):
112         voc = vocabulary.Vocabulary({})
113         self.assertEqual(voc.strict_keys, False)
114         self.assertEqual(voc.key_aliases, {})
115
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')
121
122     def test_convert_to_identifiers(self):
123         cases = [
124             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
125             {'IDTAGIMPORTANCES': 'High'},
126             {'importance': 'IDVALIMPORTANCE1'},
127             {'priority': 'high priority'},
128         ]
129         for case in cases:
130             self.assertEqual(
131                 self.voc.convert_to_identifiers(case),
132                 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
133                 "failing test case: {}".format(case)
134             )
135
136     def test_convert_to_identifiers_multiple_pairs(self):
137         cases = [
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'},
142         ]
143         for case in cases:
144             self.assertEqual(
145                 self.voc.convert_to_identifiers(case),
146                 {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
147                 "failing test case: {}".format(case)
148             )
149
150     def test_convert_to_identifiers_value_lists(self):
151         cases = [
152             {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
153             {'IDTAGIMPORTANCES': ['High', 'Medium']},
154             {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
155             {'priority': ['high', 'medium']},
156         ]
157         for case in cases:
158             self.assertEqual(
159                 self.voc.convert_to_identifiers(case),
160                 {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
161                 "failing test case: {}".format(case)
162             )
163
164     def test_convert_to_identifiers_unknown_key(self):
165         # Non-strict vocabulary
166         self.assertEqual(self.voc.strict_keys, False)
167         self.assertEqual(self.voc.convert_to_identifiers({'foo': 'bar'}), {'foo': 'bar'})
168         # Strict vocabulary
169         strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
170         strict_voc.strict_keys = True
171         with self.assertRaises(KeyError):
172             strict_voc.convert_to_identifiers({'foo': 'bar'})
173
174     def test_convert_to_identifiers_unknown_value(self):
175         # Non-strict key
176         self.assertEqual(self.voc['animal'].strict, False)
177         self.assertEqual(self.voc.convert_to_identifiers({'Animal': 'foo'}), {'IDTAGANIMALS': 'foo'})
178         # Strict key
179         self.assertEqual(self.voc['priority'].strict, True)
180         with self.assertRaises(ValueError):
181             self.voc.convert_to_identifiers({'Priority': 'foo'})
182
183     def test_convert_to_identifiers_unknown_value_list(self):
184         # Non-strict key
185         self.assertEqual(self.voc['animal'].strict, False)
186         self.assertEqual(
187             self.voc.convert_to_identifiers({'Animal': ['foo', 'loxodonta']}),
188             {'IDTAGANIMALS': ['foo', 'IDVALANIMAL2']}
189         )
190         # Strict key
191         self.assertEqual(self.voc['priority'].strict, True)
192         with self.assertRaises(ValueError):
193             self.voc.convert_to_identifiers({'Priority': ['foo', 'bar']})
194
195     def test_convert_to_labels(self):
196         cases = [
197             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1'},
198             {'IDTAGIMPORTANCES': 'High'},
199             {'importance': 'IDVALIMPORTANCE1'},
200             {'priority': 'high priority'},
201         ]
202         for case in cases:
203             self.assertEqual(
204                 self.voc.convert_to_labels(case),
205                 {'Importance': 'High'},
206                 "failing test case: {}".format(case)
207             )
208
209     def test_convert_to_labels_multiple_pairs(self):
210         cases = [
211             {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'},
212             {'IDTAGIMPORTANCES': 'High', 'IDTAGANIMALS': 'IDVALANIMAL1', 'comment': 'Very important person'},
213             {'importance': 'IDVALIMPORTANCE1', 'animal': 'IDVALANIMAL1', 'notes': 'Very important person'},
214             {'priority': 'high priority', 'animal': 'IDVALANIMAL1', 'NOTES': 'Very important person'},
215         ]
216         for case in cases:
217             self.assertEqual(
218                 self.voc.convert_to_labels(case),
219                 {'Importance': 'High', 'Animal': 'Human', 'Comment': 'Very important person'},
220                 "failing test case: {}".format(case)
221             )
222
223     def test_convert_to_labels_value_lists(self):
224         cases = [
225             {'IDTAGIMPORTANCES': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
226             {'IDTAGIMPORTANCES': ['High', 'Medium']},
227             {'importance': ['IDVALIMPORTANCE1', 'IDVALIMPORTANCE2']},
228             {'priority': ['high', 'medium']},
229         ]
230         for case in cases:
231             self.assertEqual(
232                 self.voc.convert_to_labels(case),
233                 {'Importance': ['High', 'Medium']},
234                 "failing test case: {}".format(case)
235             )
236
237     def test_convert_to_labels_unknown_key(self):
238         # Non-strict vocabulary
239         self.assertEqual(self.voc.strict_keys, False)
240         self.assertEqual(self.voc.convert_to_labels({'foo': 'bar'}), {'foo': 'bar'})
241         # Strict vocabulary
242         strict_voc = arvados.vocabulary.Vocabulary(self.EXAMPLE_VOC)
243         strict_voc.strict_keys = True
244         with self.assertRaises(KeyError):
245             strict_voc.convert_to_labels({'foo': 'bar'})
246
247     def test_convert_to_labels_unknown_value(self):
248         # Non-strict key
249         self.assertEqual(self.voc['animal'].strict, False)
250         self.assertEqual(self.voc.convert_to_labels({'IDTAGANIMALS': 'foo'}), {'Animal': 'foo'})
251         # Strict key
252         self.assertEqual(self.voc['priority'].strict, True)
253         with self.assertRaises(ValueError):
254             self.voc.convert_to_labels({'IDTAGIMPORTANCES': 'foo'})
255
256     def test_convert_to_labels_unknown_value_list(self):
257         # Non-strict key
258         self.assertEqual(self.voc['animal'].strict, False)
259         self.assertEqual(
260             self.voc.convert_to_labels({'IDTAGANIMALS': ['foo', 'IDVALANIMAL1']}),
261             {'Animal': ['foo', 'Human']}
262         )
263         # Strict key
264         self.assertEqual(self.voc['priority'].strict, True)
265         with self.assertRaises(ValueError):
266             self.voc.convert_to_labels({'IDTAGIMPORTANCES': ['foo', 'bar']})
267
268     def test_convert_roundtrip(self):
269         initial = {'IDTAGIMPORTANCES': 'IDVALIMPORTANCE1', 'IDTAGANIMALS': 'IDVALANIMAL1', 'IDTAGCOMMENTS': 'Very important person'}
270         converted = self.voc.convert_to_labels(initial)
271         self.assertNotEqual(converted, initial)
272         self.assertEqual(self.voc.convert_to_identifiers(converted), initial)