-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSearchQueryBuilder.test.unit.ts
More file actions
124 lines (103 loc) · 4.56 KB
/
Copy pathSearchQueryBuilder.test.unit.ts
File metadata and controls
124 lines (103 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// For a more comprehensive set of test cases, see the tests
// in test_cases/search_*
import { SearchOptions } from "./BasicSearchManager.js"
import { SearchRequestType, SearchRequest, SearchRequestTypeId } from "./SearchRequest.js";
import { SearchQueryBuilder } from './SearchQueryBuilder.js';
describe(`SearchQueryBuilder`, () => {
const kSimpleSearchString = 'office'
const kSimpleUnsupportedSearchString = '127.0.0.*'
// ----- constructor
it(`constructor(simpleString) correctly sets fields with proper defaults for options`, async () => {
const builder = new SearchQueryBuilder(kSimpleSearchString)
expect(builder._searchText).toBe(kSimpleSearchString)
const options = builder._searchOptions;
expect(options.useCache).toBeTruthy();
expect(options.track_total_hits).toBeTruthy();
expect(options.default_operator).toBe('AND');
expect(options.metadataOnly).toBeFalsy();
expect(options.fields.length).toBe(0);
expect(options.sort.length).toBe(1);
expect(options.sort).toMatchSnapshot();
expect(options.from).toBe(0);
expect(options.size).toBe(SearchQueryBuilder.kDefaultNumResults)
});
it(`constructor(simpleString,<options>) correctly sets fields with specified options`, async () => {
let builder;
// track_total_hits
builder = new SearchQueryBuilder("", { track_total_hits: true })
expect(builder._searchOptions.track_total_hits).toBeTruthy()
builder = new SearchQueryBuilder("", { track_total_hits: false });
expect(builder._searchOptions.track_total_hits).toBeFalsy();
// metadataOnly
builder = new SearchQueryBuilder("", { metadataOnly: true });
expect(builder._searchOptions.metadataOnly).toBeTruthy();
builder = new SearchQueryBuilder("", { metadataOnly: false });
expect(builder._searchOptions.metadataOnly).toBeFalsy()
// fileds
builder = new SearchQueryBuilder("", { fields: [] });
expect(builder._searchOptions.fields.length).toBe(0);
expect(builder._searchOptions.fields).toMatchSnapshot();
builder = new SearchQueryBuilder("", { fields: ["containers.cna"] });
expect(builder._searchOptions.fields).toMatchSnapshot();
builder = new SearchQueryBuilder("", { fields: ["containers.adp", "containers.cna", "z", "xy", "ab"] }); // order should be preserved
expect(builder._searchOptions.fields).toMatchSnapshot();
// sort
builder = new SearchQueryBuilder("", { sort: [] });
expect(builder._searchOptions.sort).toMatchSnapshot();
builder = new SearchQueryBuilder("", {
sort: [{
"cveMetadata.cveId.keyword": { "order": "asc" }
}]
});
expect(builder._searchOptions.sort).toMatchSnapshot();
builder = new SearchQueryBuilder("", {
sort: [
{
"cveMetadata.dateUpdated": { "order": "desc" }
},
{
"cveMetadata.cveId.keyword": { "order": "desc" }
}
]
}); // order should be preserved
expect(builder._searchOptions.sort).toMatchSnapshot()
});
it(`constructor with options for paging correctly returns the number requested`, async () => {
const builder = new SearchQueryBuilder(kSimpleSearchString,
{
track_total_hits: true,
from: 200,
size: 50
});
const req = builder._searchRequest
const result = req.processSearchText();
// console.log(`result: ${JSON.stringify(result, null, 2)}`);
expect(result.data['searchTextType']).toBe("SEARCH_GENERAL_TEXT");
expect(result.data['processedSearchText']).toBe(kSimpleSearchString);
expect(result).toMatchSnapshot();
expect(req._searchText).toBe(result.data['processedSearchText']);
expect(req).toMatchSnapshot();
});
// ----- spock+snapshot testing constructor+buildRequest()
const testCases: [string, Partial<SearchOptions>][] = [
[`office`, { track_total_hits: true }],
[`"office"`, { track_total_hits: true }],
[`microsoft office`, { track_total_hits: false }],
[`"https://pastebin.com/kpzHKKJu"`, { track_total_hits: true }],
[`"microsoft office"`, { track_total_hits: false }],
[`CVE-2020-5422`, { useCache: false }],
[`CVE-2000`, { metadataOnly: false }],
[`CWE-123`, {
track_total_hits: false,
default_operator: 'OR',
metadataOnly: true
}],
[`CAPEC-64`, { default_operator: 'OR' }],
]
testCases.forEach((test: [string, Partial<SearchOptions>]) => {
it(`(${test[0]},${JSON.stringify(test[1])})..buildQuery() correctly returns the expected query`, async () => {
const builder = new SearchQueryBuilder(test[0], test[1])
expect(builder.buildQuery()).toMatchSnapshot();
});
});
});