18574: Fixes key_aliases/value_aliases indexing. Adds dict-style indexing.
[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             'IDTAGIMPORTANCE': {
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         },
63     }
64
65     def perform_vocabulary_tests(self, voc):
66         self.assertEqual(voc.strict_keys, False)
67         self.assertEqual(
68             voc.key_aliases.keys(),
69             set(['IDTAGANIMALS', 'creature', 'animal',
70                 'IDTAGIMPORTANCE', 'importance', 'priority'])
71         )
72
73         vk = voc.key_aliases['creature']
74         self.assertEqual(vk.strict, False)
75         self.assertEqual(vk.identifier, 'IDTAGANIMALS')
76         self.assertEqual(vk.aliases, ['Animal', 'Creature'])
77
78         vv = vk.value_aliases['human']
79         self.assertEqual(vv.identifier, 'IDVALANIMAL1')
80         self.assertEqual(vv.aliases, ['Human', 'Homo sapiens'])
81
82         self.assertEqual(voc['creature']['human'].identifier, vv.identifier)
83         self.assertEqual(voc['Creature']['Human'].identifier, vv.identifier)
84         self.assertEqual(voc['CREATURE']['HUMAN'].identifier, vv.identifier)
85
86     def test_empty_vocabulary(self):
87         voc = vocabulary.Vocabulary()
88         self.assertEqual(voc.strict_keys, False)
89         self.assertEqual(voc.key_aliases, {})
90
91     def test_vocabulary_explicit_instantiation(self):
92         voc = vocabulary.Vocabulary(self.EXAMPLE_VOC)
93         self.perform_vocabulary_tests(voc)
94
95     @mock.patch('arvados.api')
96     def test_load_vocabulary_with_api(self, api_mock):
97         api_mock.return_value = mock.MagicMock()
98         api_mock.return_value.vocabulary.return_value = self.EXAMPLE_VOC
99
100         voc = vocabulary.load_vocabulary(arvados.api('v1'))
101         self.perform_vocabulary_tests(voc)