22159: converted other middlewares back to string pattern
[arvados.git] / services / workbench2 / src / models / vocabulary.cy.js
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 import { isEqual } from 'lodash';
7
8 describe('Vocabulary', () => {
9     let vocabulary;
10
11     beforeEach(() => {
12         vocabulary = {
13             strict_tags: false,
14             tags: {
15                 IDKEYCOMMENT: {
16                     labels: []
17                 },
18                 IDKEYANIMALS: {
19                     strict: false,
20                     labels: [
21                         {label: "Animal" },
22                         {label: "Creature"},
23                         {label: "Beast"},
24                     ],
25                     values: {
26                         IDVALANIMALS1: {
27                             labels: [
28                                 {label: "Human"},
29                                 {label: "Homo sapiens"}
30                             ]
31                         },
32                         IDVALANIMALS2: {
33                             labels: [
34                                 {label: "Dog"},
35                                 {label: "Canis lupus familiaris"}
36                             ]
37                         },
38                     }
39                 },
40                 IDKEYSIZES: {
41                     labels: [{label: "Sizes"}],
42                     values: {
43                         IDVALSIZES1: {
44                             labels: [{label: "Small"}, {label: "S"}, {label: "Little"}]
45                         },
46                         IDVALSIZES2: {
47                             labels: [{label: "Medium"}, {label: "M"}]
48                         },
49                         IDVALSIZES3: {
50                             labels: [{label: "Large"}, {label: "L"}]
51                         },
52                         IDVALSIZES4: {
53                             labels: []
54                         }
55                     }
56                 },
57                 automation: {
58                     strict: true,
59                     labels: [],
60                     values: {
61                         upload: { labels: [] },
62                         results: { labels: [] },
63                     }
64                 }
65             }
66         }
67     });
68
69     it('returns the list of tag keys', () => {
70         const tagKeys = Vocabulary.getTags(vocabulary);
71         // Alphabetically ordered by label
72         expect(isEqual(tagKeys, [
73             {id: "IDKEYANIMALS", label: "Animal"},
74             {id: "IDKEYANIMALS", label: "Beast"},
75             {id: "IDKEYANIMALS", label: "Creature"},
76             {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT"},
77             {id: "IDKEYSIZES", label: "Sizes"},
78             {id: "automation", label: "automation"},
79         ])).to.equal(true);
80     });
81
82     it('returns the list of preferred tag keys', () => {
83         const preferredTagKeys = Vocabulary.getPreferredTags(vocabulary);
84         // Alphabetically ordered by label
85         expect(isEqual(preferredTagKeys, [
86             {id: "IDKEYANIMALS", label: "Animal", synonyms: []},
87             {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT", synonyms: []},
88             {id: "IDKEYSIZES", label: "Sizes", synonyms: []},
89             {id: "automation", label: "automation", synonyms: []},
90         ])).to.equal(true);
91     });
92
93     it('returns the list of preferred tag keys with matching synonyms', () => {
94         const preferredTagKeys = Vocabulary.getPreferredTags(vocabulary, 'creat');
95         // Alphabetically ordered by label
96         expect(isEqual(preferredTagKeys, [
97             {id: "IDKEYANIMALS", label: "Animal", synonyms: ["Creature"]},
98             {id: "IDKEYCOMMENT", label: "IDKEYCOMMENT", synonyms: []},
99             {id: "IDKEYSIZES", label: "Sizes", synonyms: []},
100             {id: "automation", label: "automation", synonyms: []},
101         ])).to.equal(true);
102     });
103
104     it('returns the tag values for a given key', () => {
105         const tagValues = Vocabulary.getTagValues('IDKEYSIZES', vocabulary);
106         // Alphabetically ordered by label
107         expect(isEqual(tagValues, [
108             {id: "IDVALSIZES4", label: "IDVALSIZES4"},
109             {id: "IDVALSIZES3", label: "L"},
110             {id: "IDVALSIZES3", label: "Large"},
111             {id: "IDVALSIZES1", label: "Little"},
112             {id: "IDVALSIZES2", label: "M"},
113             {id: "IDVALSIZES2", label: "Medium"},
114             {id: "IDVALSIZES1", label: "S"},
115             {id: "IDVALSIZES1", label: "Small"},
116         ])).to.equal(true);
117         // Let's try a key that doesn't have any labels
118         const tagValues2 = Vocabulary.getTagValues('automation', vocabulary);
119         expect(isEqual(tagValues2, [
120             {id: "results", label: "results"},
121             {id: "upload", label: "upload"},
122         ])).to.equal(true);
123     });
124
125     it('returns the preferred tag values for a given key', () => {
126         const preferredTagValues = Vocabulary.getPreferredTagValues('IDKEYSIZES', vocabulary);
127         // Alphabetically ordered by label
128         expect(isEqual(preferredTagValues, [
129             {id: "IDVALSIZES4", label: "IDVALSIZES4", synonyms: []},
130             {id: "IDVALSIZES3", label: "Large", synonyms: []},
131             {id: "IDVALSIZES2", label: "Medium", synonyms: []},
132             {id: "IDVALSIZES1", label: "Small", synonyms: []},
133         ])).to.equal(true);
134         // Let's try a key that doesn't have any labels
135         const preferredTagValues2 = Vocabulary.getPreferredTagValues('automation', vocabulary);
136         expect(isEqual(preferredTagValues2, [
137             {id: "results", label: "results", synonyms: []},
138             {id: "upload", label: "upload", synonyms: []},
139         ])).to.equal(true);
140     });
141
142     it('returns the preferred tag values with matching synonyms for a given key', () => {
143         const preferredTagValues = Vocabulary.getPreferredTagValues('IDKEYSIZES', vocabulary, 'litt');
144         // Alphabetically ordered by label
145         expect(isEqual(preferredTagValues, [
146             {id: "IDVALSIZES4", label: "IDVALSIZES4", synonyms: []},
147             {id: "IDVALSIZES3", label: "Large", synonyms: []},
148             {id: "IDVALSIZES2", label: "Medium", synonyms: []},
149             {id: "IDVALSIZES1", label: "Small", synonyms: ["Little"]},
150         ])).to.equal(true);
151     });
152
153     it('returns an empty list of values for an non-existent key', () => {
154         const tagValues = Vocabulary.getTagValues('IDNONSENSE', vocabulary);
155         expect(isEqual(tagValues, [])).to.equal(true);
156     });
157
158     it('returns a key id for a given key label', () => {
159         const testCases = [
160             // Two labels belonging to the same ID
161             {keyLabel: 'Animal', expected: 'IDKEYANIMALS'},
162             {keyLabel: 'Creature', expected: 'IDKEYANIMALS'},
163             // Non-existent label returns empty string
164             {keyLabel: 'ThisKeyLabelDoesntExist', expected: ''},
165             // Key with no labels still returns the key ID
166             {keyLabel: 'automation', expected: 'automation'},
167         ]
168         testCases.forEach(tc => {
169             const tagValueID = Vocabulary.getTagKeyID(tc.keyLabel, vocabulary);
170             expect(tagValueID).to.equal(tc.expected);
171         });
172     });
173
174     it('returns an key label for a given key id', () => {
175         const testCases = [
176             // ID with many labels return the first one
177             {keyID: 'IDKEYANIMALS', expected: 'Animal'},
178             // Key IDs without any labels or unknown keys should return the literal
179             // key from the API's response (that is, the key 'id')
180             {keyID: 'IDKEYCOMMENT', expected: 'IDKEYCOMMENT'},
181             {keyID: 'FOO', expected: 'FOO'},
182         ]
183         testCases.forEach(tc => {
184             const tagValueID = Vocabulary.getTagKeyLabel(tc.keyID, vocabulary);
185             expect(tagValueID).to.equal(tc.expected);
186         });
187     });
188
189     it('returns a value id for a given key id and value label', () => {
190         const testCases = [
191             // Key ID and value label known
192             {keyID: 'IDKEYANIMALS', valueLabel: 'Human', expected: 'IDVALANIMALS1'},
193             {keyID: 'IDKEYANIMALS', valueLabel: 'Homo sapiens', expected: 'IDVALANIMALS1'},
194             // Key ID known, value label unknown
195             {keyID: 'IDKEYANIMALS', valueLabel: 'Dinosaur', expected: ''},
196             // Key ID unknown
197             {keyID: 'IDNONSENSE', valueLabel: 'Does not matter', expected: ''},
198             // Value with no labels still returns the value ID
199             {keyID: 'automation', valueLabel: 'results', expected: 'results'},
200         ]
201         testCases.forEach(tc => {
202             const tagValueID = Vocabulary.getTagValueID(tc.keyID, tc.valueLabel, vocabulary);
203             expect(tagValueID).to.equal(tc.expected);
204         });
205     });
206
207     it('returns a value label for a given key & value id pair', () => {
208         const testCases = [
209             // Known key & value ids with multiple value labels: returns the first label
210             {keyId: 'IDKEYANIMALS', valueId: 'IDVALANIMALS1', expected: 'Human'},
211             // Values without label or unknown values should return the literal value from
212             // the API's response (that is, the value 'id')
213             {keyId: 'IDKEYSIZES', valueId: 'IDVALSIZES4', expected: 'IDVALSIZES4'},
214             {keyId: 'IDKEYCOMMENT', valueId: 'FOO', expected: 'FOO'},
215             {keyId: 'IDKEYANIMALS', valueId: 'BAR', expected: 'BAR'},
216             {keyId: 'IDKEYNONSENSE', valueId: 'FOOBAR', expected: 'FOOBAR'},
217         ]
218         testCases.forEach(tc => {
219             const tagValueLabel = Vocabulary.getTagValueLabel(tc.keyId, tc.valueId, vocabulary);
220             expect(tagValueLabel).to.equal(tc.expected);
221         });
222     });
223 });