18848: update the Dockerfile for package building to automatically pick
[arvados-workbench2.git] / src / models / vocabulary.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as Vocabulary from './vocabulary';
6
7 describe('Vocabulary', () => {
8     let vocabulary: Vocabulary.Vocabulary;
9
10     beforeEach(() => {
11         vocabulary = {
12             strict_tags: false,
13             tags: {
14                 IDKEYCOMMENT: {
15                     labels: []
16                 },
17                 IDKEYANIMALS: {
18                     strict: false,
19                     labels: [
20                         {label: "Animal" },
21                         {label: "Creature"},
22                         {label: "Beast"},
23                     ],
24                     values: {
25                         IDVALANIMALS1: {
26                             labels: [
27                                 {label: "Human"},
28                                 {label: "Homo sapiens"}
29                             ]
30                         },
31                         IDVALANIMALS2: {
32                             labels: [
33                                 {label: "Dog"},
34                                 {label: "Canis lupus familiaris"}
35                             ]
36                         },
37                     }
38                 },
39                 IDKEYSIZES: {
40                     labels: [{label: "Sizes"}],
41                     values: {
42                         IDVALSIZES1: {
43                             labels: [{label: "Small"}, {label: "S"}, {label: "Little"}]
44                         },
45                         IDVALSIZES2: {
46                             labels: [{label: "Medium"}, {label: "M"}]
47                         },
48                         IDVALSIZES3: {
49                             labels: [{label: "Large"}, {label: "L"}]
50                         },
51                         IDVALSIZES4: {
52                             labels: []
53                         }
54                     }
55                 }
56             }
57         }
58     });
59
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"},
69         ]);
70     });
71
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: []},
79         ]);
80     });
81
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: []},
89         ]);
90     });
91
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"},
104         ])
105     });
106
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: []},
115         ])
116     });
117
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"]},
126         ])
127     });
128
129     it('returns an empty list of values for an non-existent key', () => {
130         const tagValues = Vocabulary.getTagValues('IDNONSENSE', vocabulary);
131         expect(tagValues).toEqual([]);
132     });
133
134     it('returns a key id for a given key label', () => {
135         const testCases = [
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: ''},
141         ]
142         testCases.forEach(tc => {
143             const tagValueID = Vocabulary.getTagKeyID(tc.keyLabel, vocabulary);
144             expect(tagValueID).toEqual(tc.expected);
145         });
146     });
147
148     it('returns an key label for a given key id', () => {
149         const testCases = [
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'},
156         ]
157         testCases.forEach(tc => {
158             const tagValueID = Vocabulary.getTagKeyLabel(tc.keyID, vocabulary);
159             expect(tagValueID).toEqual(tc.expected);
160         });
161     });
162
163     it('returns a value id for a given key id and value label', () => {
164         const testCases = [
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: ''},
170             // Key ID unknown
171             {keyID: 'IDNONSENSE', valueLabel: 'Does not matter', expected: ''},
172         ]
173         testCases.forEach(tc => {
174             const tagValueID = Vocabulary.getTagValueID(tc.keyID, tc.valueLabel, vocabulary);
175             expect(tagValueID).toEqual(tc.expected);
176         });
177     });
178
179     it('returns a value label for a given key & value id pair', () => {
180         const testCases = [
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'},
189         ]
190         testCases.forEach(tc => {
191             const tagValueLabel = Vocabulary.getTagValueLabel(tc.keyId, tc.valueId, vocabulary);
192             expect(tagValueLabel).toEqual(tc.expected);
193         });
194     });
195 });