X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8dd0176878ad1ae38e14e09149bf9dcd812c5ce7..c68a6e131b0e2938d181467febedf2a50a1aa816:/sdk/python/tests/test_vocabulary.py diff --git a/sdk/python/tests/test_vocabulary.py b/sdk/python/tests/test_vocabulary.py index 49fd601262..7cca66a1c6 100644 --- a/sdk/python/tests/test_vocabulary.py +++ b/sdk/python/tests/test_vocabulary.py @@ -62,40 +62,79 @@ class VocabularyTest(unittest.TestCase): }, } - def perform_vocabulary_tests(self, voc): - self.assertEqual(voc.strict_keys, False) + def setUp(self): + self.api = arvados.api('v1') + self.voc = vocabulary.Vocabulary(self.EXAMPLE_VOC) + self.api.vocabulary = mock.MagicMock(return_value=self.EXAMPLE_VOC) + + def test_vocabulary_keys(self): + self.assertEqual(self.voc.strict_keys, False) self.assertEqual( - voc.key_aliases.keys(), - set(['IDTAGANIMALS', 'creature', 'animal', - 'IDTAGIMPORTANCE', 'importance', 'priority']) + self.voc.key_aliases.keys(), + set(['idtaganimals', 'creature', 'animal', + 'idtagimportance', 'importance', 'priority']) ) - vk = voc.key_aliases['creature'] + vk = self.voc.key_aliases['creature'] self.assertEqual(vk.strict, False) self.assertEqual(vk.identifier, 'IDTAGANIMALS') self.assertEqual(vk.aliases, ['Animal', 'Creature']) + self.assertEqual(vk.preferred_label, 'Animal') + self.assertEqual( + vk.value_aliases.keys(), + set(['idvalanimal1', 'human', 'homo sapiens', + 'idvalanimal2', 'elephant', 'loxodonta']) + ) + def test_vocabulary_values(self): + vk = self.voc.key_aliases['creature'] vv = vk.value_aliases['human'] self.assertEqual(vv.identifier, 'IDVALANIMAL1') self.assertEqual(vv.aliases, ['Human', 'Homo sapiens']) + self.assertEqual(vv.preferred_label, 'Human') - self.assertEqual(voc['creature']['human'].identifier, vv.identifier) - self.assertEqual(voc['Creature']['Human'].identifier, vv.identifier) - self.assertEqual(voc['CREATURE']['HUMAN'].identifier, vv.identifier) + def test_vocabulary_indexing(self): + self.assertEqual(self.voc['creature']['human'].identifier, 'IDVALANIMAL1') + self.assertEqual(self.voc['Creature']['Human'].identifier, 'IDVALANIMAL1') + self.assertEqual(self.voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1') + with self.assertRaises(KeyError): + inexistant = self.voc['foo'] def test_empty_vocabulary(self): - voc = vocabulary.Vocabulary() + voc = vocabulary.Vocabulary({}) self.assertEqual(voc.strict_keys, False) self.assertEqual(voc.key_aliases, {}) - def test_vocabulary_explicit_instantiation(self): - voc = vocabulary.Vocabulary(self.EXAMPLE_VOC) - self.perform_vocabulary_tests(voc) + def test_load_vocabulary_with_api(self): + voc = vocabulary.load_vocabulary(self.api) + self.assertEqual(voc['creature']['human'].identifier, 'IDVALANIMAL1') + self.assertEqual(voc['Creature']['Human'].identifier, 'IDVALANIMAL1') + self.assertEqual(voc['CREATURE']['HUMAN'].identifier, 'IDVALANIMAL1') - @mock.patch('arvados.api') - def test_load_vocabulary_with_api(self, api_mock): - api_mock.return_value = mock.MagicMock() - api_mock.return_value.vocabulary.return_value = self.EXAMPLE_VOC + def test_convert_to_identifiers(self): + cases = [ + {'IDTAGIMPORTANCE': 'IDVALIMPORTANCE1'}, + {'IDTAGIMPORTANCE': 'High'}, + {'importance': 'IDVALIMPORTANCE1'}, + {'priority': 'high priority'}, + ] + for case in cases: + self.assertEqual( + self.voc.convert_to_identifiers(case), + {'IDTAGIMPORTANCE': 'IDVALIMPORTANCE1'}, + "failing test case: {}".format(case) + ) - voc = vocabulary.load_vocabulary(arvados.api('v1')) - self.perform_vocabulary_tests(voc) + def test_convert_to_labels(self): + cases = [ + {'IDTAGIMPORTANCE': 'IDVALIMPORTANCE1'}, + {'IDTAGIMPORTANCE': 'High'}, + {'importance': 'IDVALIMPORTANCE1'}, + {'priority': 'high priority'}, + ] + for case in cases: + self.assertEqual( + self.voc.convert_to_labels(case), + {'Importance': 'High'}, + "failing test case: {}".format(case) + ) \ No newline at end of file