18574: Adds 'preferred_label' virtual attribute. Avoids storing the voc twice.
[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         self.assertEqual(vk.preferred_label, 'Animal')
78
79         vv = vk.value_aliases['human']
80         self.assertEqual(vv.identifier, 'IDVALANIMAL1')
81         self.assertEqual(vv.aliases, ['Human', 'Homo sapiens'])
82         self.assertEqual(vv.preferred_label, 'Human')
83
84         self.assertEqual(voc['creature']['human'].identifier, vv.identifier)
85         self.assertEqual(voc['Creature']['Human'].identifier, vv.identifier)
86         self.assertEqual(voc['CREATURE']['HUMAN'].identifier, vv.identifier)
87         with self.assertRaises(KeyError):
88             inexistant = voc['foo']
89
90     def test_empty_vocabulary(self):
91         voc = vocabulary.Vocabulary()
92         self.assertEqual(voc.strict_keys, False)
93         self.assertEqual(voc.key_aliases, {})
94
95     def test_vocabulary_explicit_instantiation(self):
96         voc = vocabulary.Vocabulary(self.EXAMPLE_VOC)
97         self.perform_vocabulary_tests(voc)
98
99     @mock.patch('arvados.api')
100     def test_load_vocabulary_with_api(self, api_mock):
101         api_mock.return_value = mock.MagicMock()
102         api_mock.return_value.vocabulary.return_value = self.EXAMPLE_VOC
103
104         voc = vocabulary.load_vocabulary(arvados.api('v1'))
105         self.perform_vocabulary_tests(voc)