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'},
66 self.api = arvados.api('v1')
67 self.voc = vocabulary.Vocabulary(self.EXAMPLE_VOC)
68 self.api.vocabulary = mock.MagicMock(return_value=self.EXAMPLE_VOC)
70 def test_vocabulary_keys(self):
71 self.assertEqual(self.voc.strict_keys, False)
73 self.voc.key_aliases.keys(),
74 set(['idtaganimals', 'creature', 'animal',
75 'idtagimportance', 'importance', 'priority'])
78 vk = self.voc.key_aliases['creature']
79 self.assertEqual(vk.strict, False)
80 self.assertEqual(vk.identifier, 'IDTAGANIMALS')
81 self.assertEqual(vk.aliases, ['Animal', 'Creature'])
82 self.assertEqual(vk.preferred_label, 'Animal')
84 vk.value_aliases.keys(),
85 set(['idvalanimal1', 'human', 'homo sapiens',
86 'idvalanimal2', 'elephant', 'loxodonta'])
89 def test_vocabulary_values(self):
90 vk = self.voc.key_aliases['creature']
91 vv = vk.value_aliases['human']
92 self.assertEqual(vv.identifier, 'IDVALANIMAL1')
93 self.assertEqual(vv.aliases, ['Human', 'Homo sapiens'])
94 self.assertEqual(vv.preferred_label, 'Human')
96 def test_vocabulary_indexing(self):
97 self.assertEqual(self.voc['creature']['human'].identifier, 'IDVALANIMAL1')
98 self.assertEqual(self.voc['Creature']['Human'].identifier, 'IDVALANIMAL1')
99 self.assertEqual(self.voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1')
100 with self.assertRaises(KeyError):
101 inexistant = self.voc['foo']
103 def test_empty_vocabulary(self):
104 voc = vocabulary.Vocabulary({})
105 self.assertEqual(voc.strict_keys, False)
106 self.assertEqual(voc.key_aliases, {})
108 def test_load_vocabulary_with_api(self):
109 voc = vocabulary.load_vocabulary(self.api)
110 self.assertEqual(voc['creature']['human'].identifier, 'IDVALANIMAL1')
111 self.assertEqual(voc['Creature']['Human'].identifier, 'IDVALANIMAL1')
112 self.assertEqual(voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1')
114 def test_convert_to_identifiers(self):
116 {'IDTAGIMPORTANCE': 'IDVALIMPORTANCE1'},
117 {'IDTAGIMPORTANCE': 'High'},
118 {'importance': 'IDVALIMPORTANCE1'},
119 {'priority': 'high priority'},
123 self.voc.convert_to_identifiers(case),
124 {'IDTAGIMPORTANCE': 'IDVALIMPORTANCE1'},
125 "failing test case: {}".format(case)
128 def test_convert_to_labels(self):
130 {'IDTAGIMPORTANCE': 'IDVALIMPORTANCE1'},
131 {'IDTAGIMPORTANCE': 'High'},
132 {'importance': 'IDVALIMPORTANCE1'},
133 {'priority': 'high priority'},
137 self.voc.convert_to_labels(case),
138 {'Importance': 'High'},
139 "failing test case: {}".format(case)