Skip to content

Commit b0ba1e0

Browse files
committed
Move factory for creating local query into shared folder
There's a lot of clean-up in these tests so I'm making one change per commit. Let's move out the utility methods so we can focus on just our tests.
1 parent 718a6d6 commit b0ba1e0

2 files changed

Lines changed: 46 additions & 33 deletions

File tree

extensions/ql-vscode/src/vscode-tests/factories/local-queries/local-query-history-item.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
CompletedLocalQueryInfo,
55
LocalQueryInfo,
66
} from '../../../query-results';
7+
import { QueryWithResults } from '../../../run-queries-shared';
8+
import { CancellationTokenSource } from 'vscode';
79

810
export function createMockLocalQueryInfo(
911
startTime: string,
@@ -31,3 +33,34 @@ export function createMockLocalQueryInfo(
3133
} as unknown) as CompletedQueryInfo,
3234
} as unknown) as CompletedLocalQueryInfo;
3335
}
36+
37+
export function createMockLocalQuery(
38+
dbName = 'a',
39+
queryWithResults?: QueryWithResults,
40+
isFail = false
41+
): LocalQueryInfo {
42+
const initialQueryInfo = {
43+
databaseInfo: { name: dbName },
44+
start: new Date(),
45+
queryPath: 'hucairz'
46+
} as InitialQueryInfo;
47+
48+
const cancellationToken = {
49+
dispose: () => { /**/ },
50+
} as CancellationTokenSource;
51+
52+
const fqi = new LocalQueryInfo(
53+
initialQueryInfo,
54+
cancellationToken,
55+
);
56+
57+
if (queryWithResults) {
58+
fqi.completeThisQuery(queryWithResults);
59+
}
60+
61+
if (isFail) {
62+
fqi.failureReason = 'failure reason';
63+
}
64+
65+
return fqi;
66+
}

extensions/ql-vscode/src/vscode-tests/no-workspace/query-history.test.ts

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { registerQueryHistoryScrubber } from '../../query-history-scrubber';
99
import { QueryHistoryManager, HistoryTreeDataProvider, SortOrder } from '../../query-history';
1010
import { QueryEvaluationInfo, QueryWithResults } from '../../run-queries-shared';
1111
import { QueryHistoryConfig, QueryHistoryConfigListener } from '../../config';
12-
import { LocalQueryInfo, InitialQueryInfo } from '../../query-results';
12+
import { LocalQueryInfo } from '../../query-results';
1313
import { DatabaseManager } from '../../databases';
1414
import * as tmp from 'tmp-promise';
1515
import { ONE_DAY_IN_MS, ONE_HOUR_IN_MS, TWO_HOURS_IN_MS, THREE_HOURS_IN_MS } from '../../pure/time';
@@ -23,6 +23,7 @@ import { QueryRunner } from '../../queryRunner';
2323
import { QueryResultType } from '../../pure/legacy-messages';
2424
import { VariantAnalysisManager } from '../../remote-queries/variant-analysis-manager';
2525
import { QueryHistoryInfo } from '../../query-history-info';
26+
import { createMockLocalQuery } from '../factories/local-queries/local-query-history-item';
2627

2728
describe('query-history', () => {
2829
const mockExtensionLocation = path.join(tmpDir.name, 'mock-extension-location');
@@ -128,10 +129,10 @@ describe('query-history', () => {
128129

129130
beforeEach(() => {
130131
localQueryHistory = [
131-
createMockFullQueryInfo('a', createMockQueryWithResults(true)),
132-
createMockFullQueryInfo('b', createMockQueryWithResults(true)),
133-
createMockFullQueryInfo('a', createMockQueryWithResults(false)),
134-
createMockFullQueryInfo('a', createMockQueryWithResults(true)),
132+
createMockLocalQuery('a', createMockQueryWithResults(true)),
133+
createMockLocalQuery('b', createMockQueryWithResults(true)),
134+
createMockLocalQuery('a', createMockQueryWithResults(false)),
135+
createMockLocalQuery('a', createMockQueryWithResults(true)),
135136
];
136137
allHistory = [...localQueryHistory];
137138
});
@@ -174,7 +175,7 @@ describe('query-history', () => {
174175
it('should throw an error when a query is not successful', async () => {
175176
const thisQuery = allHistory[3];
176177
queryHistoryManager = await createMockQueryHistory(allHistory);
177-
allHistory[0] = createMockFullQueryInfo('a', createMockQueryWithResults(false));
178+
allHistory[0] = createMockLocalQuery('a', createMockQueryWithResults(false));
178179

179180
try {
180181
await (queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [thisQuery, allHistory[0]]);
@@ -356,7 +357,7 @@ describe('query-history', () => {
356357

357358

358359
it('should get a tree item with raw results', async () => {
359-
const mockQuery = createMockFullQueryInfo('a', createMockQueryWithResults(true, /* raw results */ false));
360+
const mockQuery = createMockLocalQuery('a', createMockQueryWithResults(true, /* raw results */ false));
360361
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
361362
expect(treeItem.command).to.deep.eq({
362363
title: 'Query History Item',
@@ -370,34 +371,34 @@ describe('query-history', () => {
370371
});
371372

372373
it('should get a tree item with interpreted results', async () => {
373-
const mockQuery = createMockFullQueryInfo('a', createMockQueryWithResults(true, /* interpreted results */ true));
374+
const mockQuery = createMockLocalQuery('a', createMockQueryWithResults(true, /* interpreted results */ true));
374375
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
375376
expect(treeItem.contextValue).to.eq('interpretedResultsItem');
376377
expect(treeItem.iconPath).to.deep.eq(vscode.Uri.file(mockExtensionLocation + '/media/drive.svg').fsPath);
377378
});
378379

379380
it('should get a tree item that did not complete successfully', async () => {
380-
const mockQuery = createMockFullQueryInfo('a', createMockQueryWithResults(false), false);
381+
const mockQuery = createMockLocalQuery('a', createMockQueryWithResults(false), false);
381382
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
382383
expect(treeItem.iconPath).to.eq(vscode.Uri.file(mockExtensionLocation + '/media/red-x.svg').fsPath);
383384
});
384385

385386
it('should get a tree item that failed before creating any results', async () => {
386-
const mockQuery = createMockFullQueryInfo('a', undefined, true);
387+
const mockQuery = createMockLocalQuery('a', undefined, true);
387388
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
388389
expect(treeItem.iconPath).to.eq(vscode.Uri.file(mockExtensionLocation + '/media/red-x.svg').fsPath);
389390
});
390391

391392
it('should get a tree item that is in progress', async () => {
392-
const mockQuery = createMockFullQueryInfo('a');
393+
const mockQuery = createMockLocalQuery('a');
393394
const treeItem = await historyTreeDataProvider.getTreeItem(mockQuery);
394395
expect(treeItem.iconPath).to.deep.eq({
395396
id: 'sync~spin', color: undefined
396397
});
397398
});
398399

399400
it('should get children', () => {
400-
const mockQuery = createMockFullQueryInfo();
401+
const mockQuery = createMockLocalQuery();
401402
historyTreeDataProvider.allHistory.push(mockQuery);
402403
expect(historyTreeDataProvider.getChildren()).to.deep.eq([mockQuery]);
403404
expect(historyTreeDataProvider.getChildren(mockQuery)).to.deep.eq([]);
@@ -596,27 +597,6 @@ describe('query-history', () => {
596597
}
597598
});
598599

599-
function createMockFullQueryInfo(dbName = 'a', queryWithResults?: QueryWithResults, isFail = false): LocalQueryInfo {
600-
const fqi = new LocalQueryInfo(
601-
{
602-
databaseInfo: { name: dbName },
603-
start: new Date(),
604-
queryPath: 'hucairz'
605-
} as InitialQueryInfo,
606-
{
607-
dispose: () => { /**/ },
608-
} as vscode.CancellationTokenSource
609-
);
610-
611-
if (queryWithResults) {
612-
fqi.completeThisQuery(queryWithResults);
613-
}
614-
if (isFail) {
615-
fqi.failureReason = 'failure reason';
616-
}
617-
return fqi;
618-
}
619-
620600
describe('query history scrubber', () => {
621601
let clock: sinon.SinonFakeTimers;
622602
let deregister: vscode.Disposable | undefined;

0 commit comments

Comments
 (0)