1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as Vocabulary from './vocabulary';
7 describe('Vocabulary', () => {
8 let vocabulary: Vocabulary.Vocabulary;
28 {label: "Homo sapiens"}
34 {label: "Canis lupus familiaris"}
40 labels: [{label: "Sizes"}],
43 labels: [{label: "Small"}, {label: "S"}, {label: "Little"}]
46 labels: [{label: "Medium"}, {label: "M"}]
49 labels: [{label: "Large"}, {label: "L"}]
60 it('returns the list of tag keys', () => {
61 const tagKeys = Vocabulary.getTags(vocabulary);
62 // Alphabetically ordered by label
63 expect(tagKeys).toEqual([
64 {id: "IDKEYANIMALS", label: "Animal"},
65 {id: "IDKEYANIMALS", label: "Beast"},
66 {id: "IDKEYANIMALS", label: "Creature"},
67 {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT"},
68 {id: "IDKEYSIZES", label: "Sizes"},
72 it('returns the list of preferred tag keys', () => {
73 const preferredTagKeys = Vocabulary.getPreferredTags(vocabulary);
74 // Alphabetically ordered by label
75 expect(preferredTagKeys).toEqual([
76 {id: "IDKEYANIMALS", label: "Animal", synonyms: []},
77 {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT", synonyms: []},
78 {id: "IDKEYSIZES", label: "Sizes", synonyms: []},
82 it('returns the list of preferred tag keys with matching synonyms', () => {
83 const preferredTagKeys = Vocabulary.getPreferredTags(vocabulary, 'creat');
84 // Alphabetically ordered by label
85 expect(preferredTagKeys).toEqual([
86 {id: "IDKEYANIMALS", label: "Animal", synonyms: ["Creature"]},
87 {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT", synonyms: []},
88 {id: "IDKEYSIZES", label: "Sizes", synonyms: []},
92 it('returns the tag values for a given key', () => {
93 const tagValues = Vocabulary.getTagValues('IDKEYSIZES', vocabulary);
94 // Alphabetically ordered by label
95 expect(tagValues).toEqual([
96 {id: "IDVALSIZES4", label: "IDVALSIZES4"},
97 {id: "IDVALSIZES3", label: "L"},
98 {id: "IDVALSIZES3", label: "Large"},
99 {id: "IDVALSIZES1", label: "Little"},
100 {id: "IDVALSIZES2", label: "M"},
101 {id: "IDVALSIZES2", label: "Medium"},
102 {id: "IDVALSIZES1", label: "S"},
103 {id: "IDVALSIZES1", label: "Small"},
107 it('returns the preferred tag values for a given key', () => {
108 const preferredTagValues = Vocabulary.getPreferredTagValues('IDKEYSIZES', vocabulary);
109 // Alphabetically ordered by label
110 expect(preferredTagValues).toEqual([
111 {id: "IDVALSIZES4", label: "IDVALSIZES4", synonyms: []},
112 {id: "IDVALSIZES3", label: "Large", synonyms: []},
113 {id: "IDVALSIZES2", label: "Medium", synonyms: []},
114 {id: "IDVALSIZES1", label: "Small", synonyms: []},
118 it('returns the preferred tag values with matching synonyms for a given key', () => {
119 const preferredTagValues = Vocabulary.getPreferredTagValues('IDKEYSIZES', vocabulary, 'litt');
120 // Alphabetically ordered by label
121 expect(preferredTagValues).toEqual([
122 {id: "IDVALSIZES4", label: "IDVALSIZES4", synonyms: []},
123 {id: "IDVALSIZES3", label: "Large", synonyms: []},
124 {id: "IDVALSIZES2", label: "Medium", synonyms: []},
125 {id: "IDVALSIZES1", label: "Small", synonyms: ["Little"]},
129 it('returns an empty list of values for an non-existent key', () => {
130 const tagValues = Vocabulary.getTagValues('IDNONSENSE', vocabulary);
131 expect(tagValues).toEqual([]);
134 it('returns a key id for a given key label', () => {
136 // Two labels belonging to the same ID
137 {keyLabel: 'Animal', expected: 'IDKEYANIMALS'},
138 {keyLabel: 'Creature', expected: 'IDKEYANIMALS'},
139 // Non-existent label returns empty string
140 {keyLabel: 'ThisKeyLabelDoesntExist', expected: ''},
142 testCases.forEach(tc => {
143 const tagValueID = Vocabulary.getTagKeyID(tc.keyLabel, vocabulary);
144 expect(tagValueID).toEqual(tc.expected);
148 it('returns an key label for a given key id', () => {
150 // ID with many labels return the first one
151 {keyID: 'IDKEYANIMALS', expected: 'Animal'},
152 // Key IDs without any labels or unknown keys should return the literal
153 // key from the API's response (that is, the key 'id')
154 {keyID: 'IDKEYCOMMENT', expected: 'IDKEYCOMMENT'},
155 {keyID: 'FOO', expected: 'FOO'},
157 testCases.forEach(tc => {
158 const tagValueID = Vocabulary.getTagKeyLabel(tc.keyID, vocabulary);
159 expect(tagValueID).toEqual(tc.expected);
163 it('returns a value id for a given key id and value label', () => {
165 // Key ID and value label known
166 {keyID: 'IDKEYANIMALS', valueLabel: 'Human', expected: 'IDVALANIMALS1'},
167 {keyID: 'IDKEYANIMALS', valueLabel: 'Homo sapiens', expected: 'IDVALANIMALS1'},
168 // Key ID known, value label unknown
169 {keyID: 'IDKEYANIMALS', valueLabel: 'Dinosaur', expected: ''},
171 {keyID: 'IDNONSENSE', valueLabel: 'Does not matter', expected: ''},
173 testCases.forEach(tc => {
174 const tagValueID = Vocabulary.getTagValueID(tc.keyID, tc.valueLabel, vocabulary);
175 expect(tagValueID).toEqual(tc.expected);
179 it('returns a value label for a given key & value id pair', () => {
181 // Known key & value ids with multiple value labels: returns the first label
182 {keyId: 'IDKEYANIMALS', valueId: 'IDVALANIMALS1', expected: 'Human'},
183 // Values without label or unknown values should return the literal value from
184 // the API's response (that is, the value 'id')
185 {keyId: 'IDKEYSIZES', valueId: 'IDVALSIZES4', expected: 'IDVALSIZES4'},
186 {keyId: 'IDKEYCOMMENT', valueId: 'FOO', expected: 'FOO'},
187 {keyId: 'IDKEYANIMALS', valueId: 'BAR', expected: 'BAR'},
188 {keyId: 'IDKEYNONSENSE', valueId: 'FOOBAR', expected: 'FOOBAR'},
190 testCases.forEach(tc => {
191 const tagValueLabel = Vocabulary.getTagValueLabel(tc.keyId, tc.valueId, vocabulary);
192 expect(tagValueLabel).toEqual(tc.expected);