Skip to content

Commit a04fdfd

Browse files
committed
adding tests
1 parent 872364d commit a04fdfd

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { IUiSettingsClient } from 'kibana/public';
8+
import { IIndexPattern } from '../../../../../../../../src/plugins/data/common/index_patterns';
9+
import { SavedSearchSavedObject } from '../../../../../common/types/kibana';
10+
import { createSearchItems } from './new_job_utils';
11+
12+
describe('createSearchItems', () => {
13+
const savedSearch = ({
14+
client: {
15+
http: {
16+
basePath: {
17+
basePath: '/abc',
18+
serverBasePath: '/abc',
19+
},
20+
anonymousPaths: {},
21+
},
22+
batchQueue: [],
23+
},
24+
attributes: {
25+
title: 'not test',
26+
description: '',
27+
hits: 0,
28+
columns: ['_source'],
29+
sort: [],
30+
version: 1,
31+
kibanaSavedObjectMeta: {
32+
searchSourceJSON: '',
33+
},
34+
},
35+
_version: 'WzI0OSw0XQ==',
36+
id: '4b9b1010-c678-11ea-b6e6-e942978fa29c',
37+
type: 'search',
38+
migrationVersion: {
39+
search: '7.4.0',
40+
},
41+
references: [
42+
{
43+
name: 'kibanaSavedObjectMeta.searchSourceJSON.index',
44+
type: 'index-pattern',
45+
id: '7e252840-bd27-11ea-8a6c-75d1a0bd08ab',
46+
},
47+
],
48+
} as unknown) as SavedSearchSavedObject;
49+
50+
const kibanaConfig = {} as IUiSettingsClient;
51+
const indexPattern = ({
52+
fields: [],
53+
} as unknown) as IIndexPattern;
54+
55+
test('should match index pattern', () => {
56+
const resp = createSearchItems(kibanaConfig, indexPattern, null);
57+
expect(resp).toStrictEqual({
58+
combinedQuery: { bool: { must: [{ match_all: {} }] } },
59+
query: { query: '', language: 'lucene' },
60+
});
61+
});
62+
63+
test('should match saved search with kuery and condition', () => {
64+
const searchSource = {
65+
highlightAll: true,
66+
version: true,
67+
query: { query: 'airline : "AAL" ', language: 'kuery' },
68+
filter: [],
69+
indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index',
70+
};
71+
savedSearch.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource);
72+
73+
const resp = createSearchItems(kibanaConfig, indexPattern, savedSearch);
74+
expect(resp).toStrictEqual({
75+
combinedQuery: {
76+
bool: {
77+
should: [{ match_phrase: { airline: 'AAL' } }],
78+
minimum_should_match: 1,
79+
filter: [],
80+
must_not: [],
81+
},
82+
},
83+
query: {
84+
language: 'kuery',
85+
query: 'airline : "AAL" ',
86+
},
87+
});
88+
});
89+
90+
test('should match saved search with kuery and not condition', () => {
91+
const searchSource = {
92+
highlightAll: true,
93+
version: true,
94+
query: { query: 'NOT airline : "AAL" ', language: 'kuery' },
95+
filter: [],
96+
indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index',
97+
};
98+
savedSearch.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource);
99+
100+
const resp = createSearchItems(kibanaConfig, indexPattern, savedSearch);
101+
expect(resp).toStrictEqual({
102+
combinedQuery: {
103+
bool: {
104+
filter: [],
105+
must_not: [
106+
{
107+
bool: {
108+
minimum_should_match: 1,
109+
should: [
110+
{
111+
match_phrase: {
112+
airline: 'AAL',
113+
},
114+
},
115+
],
116+
},
117+
},
118+
],
119+
},
120+
},
121+
query: {
122+
language: 'kuery',
123+
query: 'NOT airline : "AAL" ',
124+
},
125+
});
126+
});
127+
128+
test('should match saved search with kuery and condition and not condition', () => {
129+
const searchSource = {
130+
highlightAll: true,
131+
version: true,
132+
query: { query: 'airline : "AAL" and NOT airline : "AWE" ', language: 'kuery' },
133+
filter: [],
134+
indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index',
135+
};
136+
savedSearch.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource);
137+
138+
const resp = createSearchItems(kibanaConfig, indexPattern, savedSearch);
139+
expect(resp).toStrictEqual({
140+
combinedQuery: {
141+
bool: {
142+
filter: [
143+
{ bool: { should: [{ match_phrase: { airline: 'AAL' } }], minimum_should_match: 1 } },
144+
{
145+
bool: {
146+
must_not: {
147+
bool: { should: [{ match_phrase: { airline: 'AWE' } }], minimum_should_match: 1 },
148+
},
149+
},
150+
},
151+
],
152+
must_not: [],
153+
},
154+
},
155+
query: { query: 'airline : "AAL" and NOT airline : "AWE" ', language: 'kuery' },
156+
});
157+
});
158+
159+
test('should match saved search with kuery and filter', () => {
160+
const searchSource = {
161+
highlightAll: true,
162+
version: true,
163+
query: {
164+
language: 'kuery',
165+
query: '',
166+
},
167+
filter: [
168+
{
169+
meta: {
170+
alias: null,
171+
negate: false,
172+
disabled: false,
173+
type: 'phrase',
174+
key: 'airline',
175+
params: {
176+
query: 'AAL',
177+
},
178+
indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index',
179+
},
180+
query: {
181+
match_phrase: {
182+
airline: 'AAL',
183+
},
184+
},
185+
$state: {
186+
store: 'appState',
187+
},
188+
},
189+
],
190+
indexRefName: 'kibanaSavedObjectMeta.searchSourceJSON.index',
191+
};
192+
savedSearch.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(searchSource);
193+
194+
const resp = createSearchItems(kibanaConfig, indexPattern, savedSearch);
195+
expect(resp).toStrictEqual({
196+
combinedQuery: {
197+
bool: {
198+
must: [{ match_all: {} }],
199+
filter: [{ match_phrase: { airline: 'AAL' } }],
200+
must_not: [],
201+
},
202+
},
203+
query: { language: 'kuery', query: '' },
204+
});
205+
});
206+
});

0 commit comments

Comments
 (0)