refs #14280 Merge branch 'origin/14280-query-language'
[arvados-workbench2.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, parseSearchQuery } from "~/store/search-bar/search-bar-actions";
6 import { ResourceKind } from "~/models/resource";
7 import { ClusterObjectType } from "~/models/search-bar";
8
9 describe('search-bar-actions', () => {
10     describe('parseSearchQuery', () => {
11         it('should correctly parse query #1', () => {
12             const q = 'val0 is:trashed val1';
13             const r = parseSearchQuery(q);
14             expect(r.hasKeywords).toBeTruthy();
15             expect(r.values).toEqual(['val0', 'val1']);
16             expect(r.properties).toEqual({
17                 is: ['trashed']
18             });
19         });
20
21         it('should correctly parse query #2 (value with keyword should be ignored)', () => {
22             const q = 'val0 is:from:trashed val1';
23             const r = parseSearchQuery(q);
24             expect(r.hasKeywords).toBeTruthy();
25             expect(r.values).toEqual(['val0', 'val1']);
26             expect(r.properties).toEqual({
27                 from: ['trashed']
28             });
29         });
30
31         it('should correctly parse query #3 (many keywords)', () => {
32             const q = 'val0 is:trashed val2 from:2017-04-01 val1';
33             const r = parseSearchQuery(q);
34             expect(r.hasKeywords).toBeTruthy();
35             expect(r.values).toEqual(['val0', 'val2', 'val1']);
36             expect(r.properties).toEqual({
37                 is: ['trashed'],
38                 from: ['2017-04-01']
39             });
40         });
41
42         it('should correctly parse query #4 (no duplicated values)', () => {
43             const q = 'val0 is:trashed val2 val2 val0';
44             const r = parseSearchQuery(q);
45             expect(r.hasKeywords).toBeTruthy();
46             expect(r.values).toEqual(['val0', 'val2']);
47             expect(r.properties).toEqual({
48                 is: ['trashed']
49             });
50         });
51
52         it('should correctly parse query #5 (properties)', () => {
53             const q = 'val0 has:filesize:100mb val2 val2 val0';
54             const r = parseSearchQuery(q);
55             expect(r.hasKeywords).toBeTruthy();
56             expect(r.values).toEqual(['val0', 'val2']);
57             expect(r.properties).toEqual({
58                 'has': ['filesize:100mb']
59             });
60         });
61
62         it('should correctly parse query #6 (multiple properties, multiple is)', () => {
63             const q = 'val0 has:filesize:100mb val2 has:user:daniel is:starred val2 val0 is:trashed';
64             const r = parseSearchQuery(q);
65             expect(r.hasKeywords).toBeTruthy();
66             expect(r.values).toEqual(['val0', 'val2']);
67             expect(r.properties).toEqual({
68                 'has': ['filesize:100mb', 'user:daniel'],
69                 'is': ['starred', 'trashed']
70             });
71         });
72     });
73
74     describe('getAdvancedDataFromQuery', () => {
75         it('should correctly build advanced data record from query #1', () => {
76             const r = getAdvancedDataFromQuery('val0 has:filesize:100mb val2 has:user:daniel is:starred val2 val0 is:trashed');
77             expect(r).toEqual({
78                 searchValue: 'val0 val2',
79                 type: undefined,
80                 cluster: undefined,
81                 projectUuid: undefined,
82                 inTrash: true,
83                 dateFrom: undefined,
84                 dateTo: undefined,
85                 properties: [{
86                     key: 'filesize',
87                     value: '100mb'
88                 }, {
89                     key: 'user',
90                     value: 'daniel'
91                 }],
92                 saveQuery: false,
93                 queryName: ''
94             });
95         });
96
97         it('should correctly build advanced data record from query #2', () => {
98             const r = getAdvancedDataFromQuery('document from:2017-08-01 pdf has:filesize:101mb is:trashed type:arvados#collection cluster:indianapolis');
99             expect(r).toEqual({
100                 searchValue: 'document pdf',
101                 type: ResourceKind.COLLECTION,
102                 cluster: ClusterObjectType.INDIANAPOLIS,
103                 projectUuid: undefined,
104                 inTrash: true,
105                 dateFrom: '2017-08-01',
106                 dateTo: undefined,
107                 properties: [{
108                     key: 'filesize',
109                     value: '101mb'
110                 }],
111                 saveQuery: false,
112                 queryName: ''
113             });
114         });
115     });
116
117     describe('getQueryFromAdvancedData', () => {
118         it('should build query from advanced data', () => {
119             const q = getQueryFromAdvancedData({
120                 searchValue: 'document pdf',
121                 type: ResourceKind.COLLECTION,
122                 cluster: ClusterObjectType.INDIANAPOLIS,
123                 projectUuid: undefined,
124                 inTrash: true,
125                 dateFrom: '2017-08-01',
126                 dateTo: '',
127                 properties: [{
128                     key: 'filesize',
129                     value: '101mb'
130                 }],
131                 saveQuery: false,
132                 queryName: ''
133             });
134             expect(q).toBe('document pdf type:arvados#collection cluster:indianapolis is:trashed from:2017-08-01 has:filesize:101mb');
135         });
136     });
137 });