-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathquery-history.test.ts
More file actions
558 lines (473 loc) · 21.7 KB
/
query-history.test.ts
File metadata and controls
558 lines (473 loc) · 21.7 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import * as chai from 'chai';
import 'mocha';
import 'sinon-chai';
import * as vscode from 'vscode';
import * as sinon from 'sinon';
import * as chaiAsPromised from 'chai-as-promised';
import { logger } from '../../logging';
import { QueryHistoryManager, HistoryTreeDataProvider, SortOrder } from '../../query-history';
import { QueryEvaluationInfo, QueryWithResults } from '../../run-queries';
import { QueryHistoryConfigListener } from '../../config';
import * as messages from '../../pure/messages';
import { QueryServerClient } from '../../queryserver-client';
import { FullQueryInfo, InitialQueryInfo } from '../../query-results';
chai.use(chaiAsPromised);
const expect = chai.expect;
const assert = chai.assert;
describe('query-history', () => {
let configListener: QueryHistoryConfigListener;
let showTextDocumentSpy: sinon.SinonStub;
let showInformationMessageSpy: sinon.SinonStub;
let executeCommandSpy: sinon.SinonStub;
let showQuickPickSpy: sinon.SinonStub;
let queryHistoryManager: QueryHistoryManager | undefined;
let selectedCallback: sinon.SinonStub;
let doCompareCallback: sinon.SinonStub;
let tryOpenExternalFile: Function;
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
showTextDocumentSpy = sandbox.stub(vscode.window, 'showTextDocument');
showInformationMessageSpy = sandbox.stub(
vscode.window,
'showInformationMessage'
);
showQuickPickSpy = sandbox.stub(
vscode.window,
'showQuickPick'
);
executeCommandSpy = sandbox.stub(vscode.commands, 'executeCommand');
sandbox.stub(logger, 'log');
tryOpenExternalFile = (QueryHistoryManager.prototype as any).tryOpenExternalFile;
configListener = new QueryHistoryConfigListener();
selectedCallback = sandbox.stub();
doCompareCallback = sandbox.stub();
});
afterEach(async () => {
if (queryHistoryManager) {
queryHistoryManager.dispose();
queryHistoryManager = undefined;
}
sandbox.restore();
});
describe('tryOpenExternalFile', () => {
it('should open an external file', async () => {
await tryOpenExternalFile('xxx');
expect(showTextDocumentSpy).to.have.been.calledOnceWith(
vscode.Uri.file('xxx')
);
expect(executeCommandSpy).not.to.have.been.called;
});
[
'too large to open',
'Files above 50MB cannot be synchronized with extensions',
].forEach(msg => {
it(`should fail to open a file because "${msg}" and open externally`, async () => {
showTextDocumentSpy.throws(new Error(msg));
showInformationMessageSpy.returns({ title: 'Yes' });
await tryOpenExternalFile('xxx');
const uri = vscode.Uri.file('xxx');
expect(showTextDocumentSpy).to.have.been.calledOnceWith(
uri
);
expect(executeCommandSpy).to.have.been.calledOnceWith(
'revealFileInOS',
uri
);
});
it(`should fail to open a file because "${msg}" and NOT open externally`, async () => {
showTextDocumentSpy.throws(new Error(msg));
showInformationMessageSpy.returns({ title: 'No' });
await tryOpenExternalFile('xxx');
const uri = vscode.Uri.file('xxx');
expect(showTextDocumentSpy).to.have.been.calledOnceWith(uri);
expect(showInformationMessageSpy).to.have.been.called;
expect(executeCommandSpy).not.to.have.been.called;
});
});
});
let allHistory: FullQueryInfo[];
beforeEach(() => {
allHistory = [
createMockFullQueryInfo('a', createMockQueryWithResults(true)),
createMockFullQueryInfo('b', createMockQueryWithResults(true)),
createMockFullQueryInfo('a', createMockQueryWithResults(false)),
createMockFullQueryInfo('a', createMockQueryWithResults(true)),
];
});
describe('findOtherQueryToCompare', () => {
it('should find the second query to compare when one is selected', async () => {
const thisQuery = allHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
showQuickPickSpy.returns({ query: allHistory[0] });
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, []);
expect(otherQuery).to.eq(allHistory[0]);
// only called with first item, other items filtered out
expect(showQuickPickSpy.getCalls().length).to.eq(1);
expect(showQuickPickSpy.firstCall.args[0][0].query).to.eq(allHistory[0]);
});
it('should handle cancelling out of the quick select', async () => {
const thisQuery = allHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, []);
expect(otherQuery).to.be.undefined;
// only called with first item, other items filtered out
expect(showQuickPickSpy.getCalls().length).to.eq(1);
expect(showQuickPickSpy.firstCall.args[0][0].query).to.eq(allHistory[0]);
});
it('should compare against 2 queries', async () => {
const thisQuery = allHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
const otherQuery = await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0]]);
expect(otherQuery).to.eq(allHistory[0]);
expect(showQuickPickSpy).not.to.have.been.called;
});
it('should throw an error when a query is not successful', async () => {
const thisQuery = allHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
allHistory[0] = createMockFullQueryInfo('a', createMockQueryWithResults(false));
try {
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0]]);
assert(false, 'Should have thrown');
} catch (e) {
expect(e.message).to.eq('Please select a successful query.');
}
});
it('should throw an error when a databases are not the same', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
try {
// allHistory[0] is database a
// allHistory[1] is database b
await (queryHistoryManager as any).findOtherQueryToCompare(allHistory[0], [allHistory[0], allHistory[1]]);
assert(false, 'Should have thrown');
} catch (e) {
expect(e.message).to.eq('Query databases must be the same.');
}
});
it('should throw an error when more than 2 queries selected', async () => {
const thisQuery = allHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
try {
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0], allHistory[1]]);
assert(false, 'Should have thrown');
} catch (e) {
expect(e.message).to.eq('Please select no more than 2 queries.');
}
});
});
describe('handleItemClicked', () => {
it('should call the selectedCallback when an item is clicked', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.handleItemClicked(allHistory[0], [allHistory[0]]);
expect(selectedCallback).to.have.been.calledOnceWith(allHistory[0]);
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(allHistory[0]);
});
it('should do nothing if there is a multi-selection', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.handleItemClicked(allHistory[0], [allHistory[0], allHistory[1]]);
expect(selectedCallback).not.to.have.been.called;
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.be.undefined;
});
it('should throw if there is no selection', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
try {
await queryHistoryManager.handleItemClicked(undefined!, []);
expect(true).to.be.false;
} catch (e) {
expect(selectedCallback).not.to.have.been.called;
expect(e.message).to.contain('No query selected');
}
});
});
it('should remove an item and not select a new one', async function() {
queryHistoryManager = await createMockQueryHistory(allHistory);
// deleting the first item when a different item is selected
// will not change the selection
const toDelete = allHistory[1];
const selected = allHistory[3];
// select the item we want
await queryHistoryManager.treeView.reveal(selected, { select: true });
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [toDelete]);
expect(toDelete.completedQuery!.dispose).to.have.been.calledOnce;
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.deep.eq(selected);
expect(queryHistoryManager.treeDataProvider.allHistory).not.to.contain(toDelete);
// the current item should have been re-selected
expect(selectedCallback).to.have.been.calledOnceWith(selected);
});
it('should remove an item and select a new one', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
// deleting the selected item automatically selects next item
const toDelete = allHistory[1];
const newSelected = allHistory[2];
// avoid triggering the callback by setting the field directly
// select the item we want
await queryHistoryManager.treeView.reveal(toDelete, { select: true });
await queryHistoryManager.handleRemoveHistoryItem(toDelete, [toDelete]);
expect(toDelete.completedQuery!.dispose).to.have.been.calledOnce;
expect(queryHistoryManager.treeDataProvider.getCurrent()).to.eq(newSelected);
expect(queryHistoryManager.treeDataProvider.allHistory).not.to.contain(toDelete);
// the current item should have been selected
expect(selectedCallback).to.have.been.calledOnceWith(newSelected);
});
describe('Compare callback', () => {
it('should call the compare callback', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.handleCompareWith(allHistory[0], [allHistory[0], allHistory[3]]);
expect(doCompareCallback).to.have.been.calledOnceWith(allHistory[0], allHistory[3]);
});
it('should avoid calling the compare callback when only one item is selected', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.handleCompareWith(allHistory[0], [allHistory[0]]);
expect(doCompareCallback).not.to.have.been.called;
});
});
describe('updateCompareWith', () => {
it('should update compareWithItem when there is a single item', async () => {
queryHistoryManager = await createMockQueryHistory([]);
(queryHistoryManager as any).updateCompareWith(['a']);
expect(queryHistoryManager.compareWithItem).to.be.eq('a');
});
it('should delete compareWithItem when there are 0 items', async () => {
queryHistoryManager = await createMockQueryHistory([]);
queryHistoryManager.compareWithItem = allHistory[0];
(queryHistoryManager as any).updateCompareWith([]);
expect(queryHistoryManager.compareWithItem).to.be.undefined;
});
it('should delete compareWithItem when there are more than 2 items', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
queryHistoryManager.compareWithItem = allHistory[0];
(queryHistoryManager as any).updateCompareWith([allHistory[0], allHistory[1], allHistory[2]]);
expect(queryHistoryManager.compareWithItem).to.be.undefined;
});
it('should delete compareWithItem when there are 2 items and disjoint from compareWithItem', async () => {
queryHistoryManager = await createMockQueryHistory([]);
queryHistoryManager.compareWithItem = allHistory[0];
(queryHistoryManager as any).updateCompareWith([allHistory[1], allHistory[2]]);
expect(queryHistoryManager.compareWithItem).to.be.undefined;
});
it('should do nothing when compareWithItem exists and exactly 2 items', async () => {
queryHistoryManager = await createMockQueryHistory([]);
queryHistoryManager.compareWithItem = allHistory[0];
(queryHistoryManager as any).updateCompareWith([allHistory[0], allHistory[1]]);
expect(queryHistoryManager.compareWithItem).to.be.eq(allHistory[0]);
});
});
describe('HistoryTreeDataProvider', () => {
let historyTreeDataProvider: HistoryTreeDataProvider;
beforeEach(() => {
historyTreeDataProvider = new HistoryTreeDataProvider(vscode.Uri.file('/a/b/c').fsPath);
});
afterEach(() => {
historyTreeDataProvider.dispose();
});
it('should get a tree item with raw results', async () => {
const mockQuery = createMockFullQueryInfo('a', createMockQueryWithResults(true, /* raw results */ false));
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.command).to.deep.eq({
title: 'Query History Item',
command: 'codeQLQueryHistory.itemClicked',
arguments: [mockQuery],
});
expect(treeItem.label).to.contain('hucairz');
expect(treeItem.contextValue).to.eq('rawResultsItem');
expect(treeItem.iconPath).to.deep.eq(vscode.Uri.file('/a/b/c/media/drive.svg').fsPath);
});
it('should get a tree item with interpreted results', async () => {
const mockQuery = createMockFullQueryInfo('a', createMockQueryWithResults(true, /* interpreted results */ true));
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.contextValue).to.eq('interpretedResultsItem');
expect(treeItem.iconPath).to.deep.eq(vscode.Uri.file('/a/b/c/media/drive.svg').fsPath);
});
it('should get a tree item that did not complete successfully', async () => {
const mockQuery = createMockFullQueryInfo('a', createMockQueryWithResults(false), false);
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.iconPath).to.eq(vscode.Uri.file('/a/b/c/media/red-x.svg').fsPath);
});
it('should get a tree item that failed before creating any results', async () => {
const mockQuery = createMockFullQueryInfo('a', undefined, true);
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.iconPath).to.eq(vscode.Uri.file('/a/b/c/media/red-x.svg').fsPath);
});
it('should get a tree item that is in progress', async () => {
const mockQuery = createMockFullQueryInfo('a');
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
expect(treeItem.iconPath).to.deep.eq({
id: 'sync~spin', color: undefined
});
});
it('should get children', () => {
const mockQuery = createMockFullQueryInfo();
historyTreeDataProvider.allHistory.push(mockQuery);
expect(historyTreeDataProvider.getChildren()).to.deep.eq([mockQuery]);
expect(historyTreeDataProvider.getChildren(mockQuery)).to.deep.eq([]);
});
});
describe('determineSelection', () => {
const singleItem = 'a';
const multipleItems = ['b', 'c', 'd'];
it('should get the selection from parameters', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const selection = (queryHistoryManager as any).determineSelection(singleItem, multipleItems);
expect(selection).to.deep.eq({
finalSingleItem: singleItem,
finalMultiSelect: multipleItems
});
});
it('should get the selection when single selection is empty', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const selection = (queryHistoryManager as any).determineSelection(undefined, multipleItems);
expect(selection).to.deep.eq({
finalSingleItem: multipleItems[0],
finalMultiSelect: multipleItems
});
});
it('should get the selection when multi-selection is empty', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
const selection = (queryHistoryManager as any).determineSelection(singleItem, undefined);
expect(selection).to.deep.eq({
finalSingleItem: singleItem,
finalMultiSelect: [singleItem]
});
});
it('should get the selection from the treeView when both selections are empty', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.treeView.reveal(allHistory[1], { select: true });
const selection = (queryHistoryManager as any).determineSelection(undefined, undefined);
expect(selection).to.deep.eq({
finalSingleItem: allHistory[1],
finalMultiSelect: [allHistory[1]]
});
});
it('should get the selection from the treeDataProvider when both selections and the treeView are empty', async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
await queryHistoryManager.treeView.reveal(allHistory[1], { select: true });
const selection = (queryHistoryManager as any).determineSelection(undefined, undefined);
expect(selection).to.deep.eq({
finalSingleItem: allHistory[1],
finalMultiSelect: [allHistory[1]]
});
});
});
describe('getChildren', () => {
const history = [
item('a', 10, 20),
item('b', 5, 30),
item('c', 1, 25),
];
let treeDataProvider: HistoryTreeDataProvider;
beforeEach(async () => {
queryHistoryManager = await createMockQueryHistory(allHistory);
(queryHistoryManager.treeDataProvider as any).history = [...history];
treeDataProvider = queryHistoryManager.treeDataProvider;
});
it('should get children for name ascending', async () => {
const expected = [...history];
treeDataProvider.sortOrder = SortOrder.NameAsc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for name descending', async () => {
const expected = [...history].reverse();
treeDataProvider.sortOrder = SortOrder.NameDesc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for date ascending', async () => {
const expected = [history[2], history[1], history[0]];
treeDataProvider.sortOrder = SortOrder.DateAsc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for date descending', async () => {
const expected = [history[0], history[1], history[2]];
treeDataProvider.sortOrder = SortOrder.DateDesc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for result count ascending', async () => {
const expected = [history[0], history[2], history[1]];
treeDataProvider.sortOrder = SortOrder.CountAsc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for result count descending', async () => {
const expected = [history[1], history[2], history[0]];
treeDataProvider.sortOrder = SortOrder.CountDesc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for result count ascending when there are no results', async () => {
// fall back to name
const thisHistory = [item('a', 10), item('b', 50), item('c', 1)];
(queryHistoryManager!.treeDataProvider as any).history = [...thisHistory];
const expected = [...thisHistory];
treeDataProvider.sortOrder = SortOrder.CountAsc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
it('should get children for result count descending when there are no results', async () => {
// fall back to name
const thisHistory = [item('a', 10), item('b', 50), item('c', 1)];
(queryHistoryManager!.treeDataProvider as any).history = [...thisHistory];
const expected = [...thisHistory].reverse();
treeDataProvider.sortOrder = SortOrder.CountDesc;
const children = await treeDataProvider.getChildren();
expect(children).to.deep.eq(expected);
});
function item(label: string, start: number, resultCount?: number) {
return {
label,
initialInfo: {
start: new Date(start),
},
completedQuery: {
resultCount,
}
};
}
});
function createMockFullQueryInfo(dbName = 'a', queryWitbResults?: QueryWithResults, isFail = false): FullQueryInfo {
const fqi = new FullQueryInfo(
{
databaseInfo: { name: dbName },
start: new Date(),
queryPath: 'hucairz'
} as InitialQueryInfo,
configListener,
{} as vscode.CancellationTokenSource
);
if (queryWitbResults) {
fqi.completeThisQuery(queryWitbResults);
}
if (isFail) {
fqi.failureReason = 'failure reason';
}
return fqi;
}
function createMockQueryWithResults(didRunSuccessfully = true, hasInterpretedResults = true): QueryWithResults {
return {
query: {
hasInterpretedResults: () => Promise.resolve(hasInterpretedResults)
} as QueryEvaluationInfo,
result: {
resultType: didRunSuccessfully
? messages.QueryResultType.SUCCESS
: messages.QueryResultType.OTHER_ERROR
} as messages.EvaluationResult,
dispose: sandbox.spy(),
};
}
async function createMockQueryHistory(allHistory: FullQueryInfo[]) {
const qhm = new QueryHistoryManager(
{} as QueryServerClient,
'xxx',
configListener,
selectedCallback,
doCompareCallback
);
(qhm.treeDataProvider as any).history = [...allHistory];
await vscode.workspace.saveAll();
qhm.refreshTreeView();
return qhm;
}
});