17637: Fixes edge case while simplifying code.
[arvados.git] / src / store / search-bar / search-bar-actions.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { getAdvancedDataFromQuery, getQueryFromAdvancedData } from "~/store/search-bar/search-bar-actions";
6 import { ResourceKind } from "~/models/resource";
7
8 describe('search-bar-actions', () => {
9     describe('getAdvancedDataFromQuery', () => {
10         it('should correctly build advanced data record from query #1', () => {
11             const r = getAdvancedDataFromQuery('val0 has:"file size":"100mb" val2 has:"user":"daniel" is:starred val2 val0');
12             expect(r).toEqual({
13                 searchValue: 'val0 val2',
14                 type: undefined,
15                 cluster: undefined,
16                 projectUuid: undefined,
17                 inTrash: false,
18                 pastVersions: false,
19                 dateFrom: '',
20                 dateTo: '',
21                 properties: [{
22                     key: 'file size',
23                     value: '100mb'
24                 }, {
25                     key: 'user',
26                     value: 'daniel'
27                 }],
28                 saveQuery: false,
29                 queryName: ''
30             });
31         });
32
33         it('should correctly build advanced data record from query #2', () => {
34             const r = getAdvancedDataFromQuery('document from:2017-08-01 pdf has:"filesize":"101mb" is:trashed type:arvados#collection cluster:c97qx is:pastVersion');
35             expect(r).toEqual({
36                 searchValue: 'document pdf',
37                 type: ResourceKind.COLLECTION,
38                 cluster: 'c97qx',
39                 projectUuid: undefined,
40                 inTrash: true,
41                 pastVersions: true,
42                 dateFrom: '2017-08-01',
43                 dateTo: '',
44                 properties: [{
45                     key: 'filesize',
46                     value: '101mb'
47                 }],
48                 saveQuery: false,
49                 queryName: ''
50             });
51         });
52     });
53
54     describe('getQueryFromAdvancedData', () => {
55         it('should build query from advanced data', () => {
56             const q = getQueryFromAdvancedData({
57                 searchValue: 'document pdf',
58                 type: ResourceKind.COLLECTION,
59                 cluster: 'c97qx',
60                 projectUuid: undefined,
61                 inTrash: true,
62                 pastVersions: false,
63                 dateFrom: '2017-08-01',
64                 dateTo: '',
65                 properties: [
66                     { key: 'file size', value: '101mb' },
67                     { key: 'Species', value: 'Human' },
68                     { key: 'Species', value: 'Canine' },
69                 ],
70                 saveQuery: false,
71                 queryName: ''
72             });
73             expect(q).toBe('document pdf type:arvados#collection cluster:c97qx is:trashed from:2017-08-01 has:"file size":"101mb" has:"Species":"Human" has:"Species":"Canine"');
74         });
75
76         it('should build query from advanced data #2', () => {
77             const q = getQueryFromAdvancedData({
78                 searchValue: 'document pdf',
79                 type: ResourceKind.COLLECTION,
80                 cluster: 'c97qx',
81                 projectUuid: undefined,
82                 inTrash: false,
83                 pastVersions: true,
84                 dateFrom: '2017-08-01',
85                 dateTo: '',
86                 properties: [
87                     { key: 'file size', value: '101mb' },
88                     { key: 'Species', value: 'Human' },
89                     { key: 'Species', value: 'Canine' },
90                 ],
91                 saveQuery: false,
92                 queryName: ''
93             });
94             expect(q).toBe('document pdf type:arvados#collection cluster:c97qx is:pastVersion from:2017-08-01 has:"file size":"101mb" has:"Species":"Human" has:"Species":"Canine"');
95         });
96
97         it('should add has:"key":"value" expression to query from same property key', () => {
98             const searchValue = 'document pdf has:"file size":"101mb" has:"Species":"Canine"';
99             const prevData = {
100                 searchValue,
101                 type: undefined,
102                 cluster: undefined,
103                 projectUuid: undefined,
104                 inTrash: false,
105                 pastVersions: false,
106                 dateFrom: '',
107                 dateTo: '',
108                 properties: [
109                     { key: 'file size', value: '101mb' },
110                     { key: 'Species', value: 'Canine' },
111                 ],
112                 saveQuery: false,
113                 queryName: ''
114             };
115             const currData = {
116                 ...prevData,
117                 properties: [
118                     { key: 'file size', value: '101mb' },
119                     { key: 'Species', value: 'Canine' },
120                     { key: 'Species', value: 'Human' },
121                 ],
122             };
123             const q = getQueryFromAdvancedData(currData, prevData);
124             expect(q).toBe('document pdf has:"file size":"101mb" has:"Species":"Canine" has:"Species":"Human"');
125         });
126
127         it('should add has:"keyID":"valueID" expression to query when necessary', () => {
128             const searchValue = 'document pdf has:"file size":"101mb"';
129             const prevData = {
130                 searchValue,
131                 type: undefined,
132                 cluster: undefined,
133                 projectUuid: undefined,
134                 inTrash: false,
135                 pastVersions: false,
136                 dateFrom: '',
137                 dateTo: '',
138                 properties: [
139                     { key: 'file size', value: '101mb' },
140                 ],
141                 saveQuery: false,
142                 queryName: ''
143             };
144             const currData = {
145                 ...prevData,
146                 properties: [
147                     { key: 'file size', value: '101mb' },
148                     { key: 'Species', keyID: 'IDTAGSPECIES', value: 'Human', valueID: 'IDVALHUMAN'},
149                 ],
150             };
151             const q = getQueryFromAdvancedData(currData, prevData);
152             expect(q).toBe('document pdf has:"file size":"101mb" has:"IDTAGSPECIES":"IDVALHUMAN"');
153         });
154
155         it('should remove has:"key":"value" expression from query', () => {
156             const searchValue = 'document pdf has:"file size":"101mb" has:"Species":"Human" has:"Species":"Canine"';
157             const prevData = {
158                 searchValue,
159                 type: undefined,
160                 cluster: undefined,
161                 projectUuid: undefined,
162                 inTrash: false,
163                 pastVersions: false,
164                 dateFrom: '',
165                 dateTo: '',
166                 properties: [
167                     { key: 'file size', value: '101mb' },
168                     { key: 'Species', value: 'Canine' },
169                     { key: 'Species', value: 'Human' },
170                 ],
171                 saveQuery: false,
172                 queryName: ''
173             };
174             const currData = {
175                 ...prevData,
176                 properties: [
177                     { key: 'file size', value: '101mb' },
178                     { key: 'Species', value: 'Canine' },
179                 ],
180             };
181             const q = getQueryFromAdvancedData(currData, prevData);
182             expect(q).toBe('document pdf has:"file size":"101mb" has:"Species":"Canine"');
183         });
184     });
185 });