-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathquery-results.test.ts
More file actions
289 lines (248 loc) · 9.19 KB
/
query-results.test.ts
File metadata and controls
289 lines (248 loc) · 9.19 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import * as chai from 'chai';
import * as path from 'path';
import * as fs from 'fs-extra';
import 'mocha';
import 'sinon-chai';
import * as sinon from 'sinon';
import * as chaiAsPromised from 'chai-as-promised';
import { FullQueryInfo, InitialQueryInfo, interpretResults } from '../../query-results';
import { QueryEvaluationInfo, QueryWithResults, tmpDir } from '../../run-queries';
import { QueryHistoryConfig } from '../../config';
import { EvaluationResult, QueryResultType } from '../../pure/messages';
import { SortDirection, SortedResultSetInfo } from '../../pure/interface-types';
import { CodeQLCliServer, SourceInfo } from '../../cli';
import { env } from 'process';
import { CancellationTokenSource } from 'vscode';
chai.use(chaiAsPromised);
const expect = chai.expect;
describe('query-results', () => {
let disposeSpy: sinon.SinonSpy;
let onDidChangeQueryHistoryConfigurationSpy: sinon.SinonSpy;
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
disposeSpy = sandbox.spy();
onDidChangeQueryHistoryConfigurationSpy = sandbox.spy();
});
afterEach(() => {
sandbox.restore();
});
describe('FullQueryInfo', () => {
it('should interpolate', () => {
const fqi = createMockFullQueryInfo();
const date = new Date('2022-01-01T00:00:00.000Z');
const dateStr = date.toLocaleString(env.language);
(fqi.initialInfo as any).start = date;
expect(fqi.interpolate('xxx')).to.eq('xxx');
expect(fqi.interpolate('%t %q %d %s %%')).to.eq(`${dateStr} hucairz a in progress %`);
expect(fqi.interpolate('%t %q %d %s %%::%t %q %d %s %%')).to.eq(`${dateStr} hucairz a in progress %::${dateStr} hucairz a in progress %`);
});
it('should get the query name', () => {
const fqi = createMockFullQueryInfo();
// from the query path
expect(fqi.getQueryName()).to.eq('hucairz');
fqi.completeThisQuery(createMockQueryWithResults());
// from the metadata
expect(fqi.getQueryName()).to.eq('vwx');
// from quick eval position
(fqi.initialInfo as any).quickEvalPosition = {
line: 1,
endLine: 2,
fileName: '/home/users/yz'
};
expect(fqi.getQueryName()).to.eq('Quick evaluation of yz:1-2');
(fqi.initialInfo as any).quickEvalPosition.endLine = 1;
expect(fqi.getQueryName()).to.eq('Quick evaluation of yz:1');
});
it('should get the query file name', () => {
const fqi = createMockFullQueryInfo();
// from the query path
expect(fqi.getQueryFileName()).to.eq('hucairz');
// from quick eval position
(fqi.initialInfo as any).quickEvalPosition = {
line: 1,
endLine: 2,
fileName: '/home/users/yz'
};
expect(fqi.getQueryFileName()).to.eq('yz:1-2');
(fqi.initialInfo as any).quickEvalPosition.endLine = 1;
expect(fqi.getQueryFileName()).to.eq('yz:1');
});
it('should get the label', () => {
const fqi = createMockFullQueryInfo('db-name');
// the %q from the config is now replaced by the file name of the query
expect(fqi.label).to.eq('from config hucairz');
// the %q from the config is now replaced by the name of the query
// in the metadata
fqi.completeThisQuery(createMockQueryWithResults());
expect(fqi.label).to.eq('from config vwx');
// replace the config with a user specified label
// must be interpolated
fqi.initialInfo.userSpecifiedLabel = 'user specified label %d';
expect(fqi.label).to.eq('user specified label db-name');
});
it('should get the getResultsPath', () => {
const fqi = createMockFullQueryInfo('a', createMockQueryWithResults());
const completedQuery = fqi.completedQuery!;
// from results path
expect(completedQuery.getResultsPath('zxa', false)).to.eq('/a/b/c');
completedQuery.sortedResultsInfo.set('zxa', {
resultsPath: 'bxa'
} as SortedResultSetInfo);
// still from results path
expect(completedQuery.getResultsPath('zxa', false)).to.eq('/a/b/c');
// from sortedResultsInfo
expect(completedQuery.getResultsPath('zxa')).to.eq('bxa');
});
it('should get the statusString', () => {
const fqi = createMockFullQueryInfo('a', createMockQueryWithResults(false));
const completedQuery = fqi.completedQuery!;
completedQuery.result.message = 'Tremendously';
expect(completedQuery.statusString).to.eq('failed: Tremendously');
completedQuery.result.resultType = QueryResultType.OTHER_ERROR;
expect(completedQuery.statusString).to.eq('failed: Tremendously');
completedQuery.result.resultType = QueryResultType.CANCELLATION;
completedQuery.result.evaluationTime = 2345;
expect(completedQuery.statusString).to.eq('cancelled after 2 seconds');
completedQuery.result.resultType = QueryResultType.OOM;
expect(completedQuery.statusString).to.eq('out of memory');
completedQuery.result.resultType = QueryResultType.SUCCESS;
expect(completedQuery.statusString).to.eq('finished in 2 seconds');
completedQuery.result.resultType = QueryResultType.TIMEOUT;
expect(completedQuery.statusString).to.eq('timed out after 2 seconds');
});
it('should updateSortState', async () => {
const fqi = createMockFullQueryInfo('a', createMockQueryWithResults());
const completedQuery = fqi.completedQuery!;
const spy = sandbox.spy();
const mockServer = {
sortBqrs: spy
} as unknown as CodeQLCliServer;
const sortState = {
columnIndex: 1,
sortDirection: SortDirection.desc
};
await completedQuery.updateSortState(mockServer, 'result-name', sortState);
const expectedPath = path.join(tmpDir.name, 'sortedResults6789-result-name.bqrs');
expect(spy).to.have.been.calledWith(
'/a/b/c',
expectedPath,
'result-name',
[sortState.columnIndex],
[sortState.sortDirection],
);
expect(completedQuery.sortedResultsInfo.get('result-name')).to.deep.equal({
resultsPath: expectedPath,
sortState
});
// delete the sort stae
await completedQuery.updateSortState(mockServer, 'result-name');
expect(completedQuery.sortedResultsInfo.size).to.eq(0);
});
});
it('should interpretResults', async () => {
const spy = sandbox.mock();
spy.returns('1234');
const mockServer = {
interpretBqrs: spy
} as unknown as CodeQLCliServer;
const interpretedResultsPath = path.join(tmpDir.name, 'interpreted.json');
const resultsPath = '123';
const sourceInfo = {};
const metadata = {
kind: 'my-kind',
id: 'my-id' as string | undefined,
scored: undefined
};
const results1 = await interpretResults(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
expect(results1).to.eq('1234');
expect(spy).to.have.been.calledWith(
metadata,
resultsPath, interpretedResultsPath, sourceInfo
);
// Try again, but with no id
spy.reset();
spy.returns('1234');
delete metadata.id;
const results2 = await interpretResults(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
expect(results2).to.eq('1234');
expect(spy).to.have.been.calledWith(
{ kind: 'my-kind', id: 'dummy-id', scored: undefined },
resultsPath, interpretedResultsPath, sourceInfo
);
// try a third time, but this time we get from file
spy.reset();
fs.writeFileSync(interpretedResultsPath, JSON.stringify({
a: 6
}), 'utf8');
const results3 = await interpretResults(
mockServer,
metadata,
{
resultsPath, interpretedResultsPath
},
sourceInfo as SourceInfo
);
expect(results3).to.deep.eq({ a: 6 });
});
function createMockQueryWithResults(didRunSuccessfully = true, hasInterpretedResults = true): QueryWithResults {
return {
query: {
hasInterpretedResults: () => Promise.resolve(hasInterpretedResults),
queryID: 6789,
metadata: {
name: 'vwx'
},
resultsPaths: {
resultsPath: '/a/b/c',
interpretedResultsPath: '/d/e/f'
}
} as QueryEvaluationInfo,
result: {
evaluationTime: 12340,
resultType: didRunSuccessfully
? QueryResultType.SUCCESS
: QueryResultType.OTHER_ERROR
} as EvaluationResult,
dispose: disposeSpy,
};
}
function createMockFullQueryInfo(dbName = 'a', queryWitbResults?: QueryWithResults, isFail = false): FullQueryInfo {
const fqi = new FullQueryInfo(
{
databaseInfo: { name: dbName },
start: new Date(),
queryPath: 'path/to/hucairz'
} as InitialQueryInfo,
mockQueryHistoryConfig(),
{} as CancellationTokenSource
);
if (queryWitbResults) {
fqi.completeThisQuery(queryWitbResults);
}
if (isFail) {
fqi.failureReason = 'failure reason';
}
return fqi;
}
function mockQueryHistoryConfig(): QueryHistoryConfig {
return {
onDidChangeConfiguration: onDidChangeQueryHistoryConfigurationSpy,
format: 'from config %q'
};
}
});