Skip to content

Commit 486180d

Browse files
committed
Fix and add tests.
1 parent 9362447 commit 486180d

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

extensions/ql-vscode/test/factories/query-history/local-query-history-item.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { CancellationTokenSource } from "vscode";
88
import { QueryResultType } from "../../../src/query-server/legacy-messages";
99
import { QueryMetadata } from "../../../src/common/interface-types";
10+
import { QueryLanguage } from "../../../src/common/query-language";
1011

1112
export function createMockLocalQueryInfo({
1213
startTime = new Date(),
@@ -16,6 +17,7 @@ export function createMockLocalQueryInfo({
1617
dbName = "db-name",
1718
hasMetadata = false,
1819
queryWithResults = undefined,
20+
language = undefined,
1921
}: {
2022
startTime?: Date;
2123
resultCount?: number;
@@ -24,6 +26,7 @@ export function createMockLocalQueryInfo({
2426
dbName?: string;
2527
hasMetadata?: boolean;
2628
queryWithResults?: QueryWithResults | undefined;
29+
language?: QueryLanguage;
2730
}): LocalQueryInfo {
2831
const cancellationToken = {
2932
dispose: () => {
@@ -40,6 +43,7 @@ export function createMockLocalQueryInfo({
4043
databaseInfo: {
4144
databaseUri: "databaseUri",
4245
name: dbName,
46+
language,
4347
},
4448
start: startTime,
4549
id: faker.number.int().toString(),

extensions/ql-vscode/test/factories/query-history/variant-analysis-history-item.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
VariantAnalysisStatus,
66
} from "../../../src/variant-analysis/shared/variant-analysis";
77
import { createMockVariantAnalysis } from "../variant-analysis/shared/variant-analysis";
8+
import { QueryLanguage } from "../../../src/common/query-language";
89

910
export function createMockVariantAnalysisHistoryItem({
1011
historyItemStatus = QueryStatus.InProgress,
@@ -14,6 +15,7 @@ export function createMockVariantAnalysisHistoryItem({
1415
userSpecifiedLabel = undefined,
1516
executionStartTime = undefined,
1617
variantAnalysis = undefined,
18+
language = QueryLanguage.Javascript,
1719
}: {
1820
historyItemStatus?: QueryStatus;
1921
variantAnalysisStatus?: VariantAnalysisStatus;
@@ -22,6 +24,7 @@ export function createMockVariantAnalysisHistoryItem({
2224
userSpecifiedLabel?: string;
2325
executionStartTime?: number;
2426
variantAnalysis?: VariantAnalysis;
27+
language?: QueryLanguage;
2528
}): VariantAnalysisHistoryItem {
2629
return {
2730
t: "variant-analysis",
@@ -34,6 +37,7 @@ export function createMockVariantAnalysisHistoryItem({
3437
createMockVariantAnalysis({
3538
status: variantAnalysisStatus,
3639
executionStartTime,
40+
language,
3741
}),
3842
userSpecifiedLabel,
3943
};

extensions/ql-vscode/test/factories/variant-analysis/shared/variant-analysis.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export function createMockVariantAnalysis({
1515
scannedRepos = createMockScannedRepos(),
1616
skippedRepos = createMockSkippedRepos(),
1717
executionStartTime = faker.number.int(),
18+
language = QueryLanguage.Javascript,
1819
}: {
1920
status?: VariantAnalysisStatus;
2021
scannedRepos?: VariantAnalysisScannedRepository[];
2122
skippedRepos?: VariantAnalysisSkippedRepositories;
2223
executionStartTime?: number | undefined;
24+
language?: QueryLanguage;
2325
}): VariantAnalysis {
2426
return {
2527
id: faker.number.int(),
@@ -32,7 +34,7 @@ export function createMockVariantAnalysis({
3234
query: {
3335
name: "a-query-name",
3436
filePath: "a-query-file-path",
35-
language: QueryLanguage.Javascript,
37+
language,
3638
text: "a-query-text",
3739
},
3840
databases: {

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/history-tree-data-provider.test.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ import {
2828
import { QueryHistoryManager } from "../../../../src/query-history/query-history-manager";
2929
import { createMockQueryHistoryDirs } from "../../../factories/query-history/query-history-dirs";
3030
import { createMockApp } from "../../../__mocks__/appMock";
31+
import { LanguageContextStore } from "../../../../src/language-context-store";
32+
import { App } from "../../../../src/common/app";
33+
import { QueryLanguage } from "../../../../src/common/query-language";
3134

3235
describe("HistoryTreeDataProvider", () => {
3336
const mockExtensionLocation = join(tmpDir.name, "mock-extension-location");
37+
let app: App;
3438
let configListener: QueryHistoryConfigListener;
3539
const doCompareCallback = jest.fn();
3640

@@ -45,10 +49,12 @@ describe("HistoryTreeDataProvider", () => {
4549

4650
let historyTreeDataProvider: HistoryTreeDataProvider;
4751
let labelProvider: HistoryItemLabelProvider;
52+
let languageContext: LanguageContextStore;
4853

4954
beforeEach(() => {
5055
jest.spyOn(extLogger, "log").mockResolvedValue(undefined);
5156

57+
app = createMockApp({});
5258
configListener = new QueryHistoryConfigListener();
5359
localQueriesResultsViewStub = {
5460
showResults: jest.fn(),
@@ -124,7 +130,11 @@ describe("HistoryTreeDataProvider", () => {
124130
ttlInMillis: 0,
125131
onDidChangeConfiguration: jest.fn(),
126132
});
127-
historyTreeDataProvider = new HistoryTreeDataProvider(labelProvider);
133+
languageContext = new LanguageContextStore(app);
134+
historyTreeDataProvider = new HistoryTreeDataProvider(
135+
labelProvider,
136+
languageContext,
137+
);
128138
});
129139

130140
afterEach(async () => {
@@ -418,11 +428,68 @@ describe("HistoryTreeDataProvider", () => {
418428
expect(children).toEqual(expected);
419429
});
420430
});
431+
432+
describe("filtering", () => {
433+
const history = [
434+
createMockLocalQueryInfo({
435+
userSpecifiedLabel: "a",
436+
// No language at all => unknown
437+
}),
438+
createMockVariantAnalysisHistoryItem({
439+
userSpecifiedLabel: "b",
440+
// No specified language => javascript
441+
}),
442+
createMockLocalQueryInfo({
443+
userSpecifiedLabel: "c",
444+
language: QueryLanguage.Python,
445+
}),
446+
createMockVariantAnalysisHistoryItem({
447+
userSpecifiedLabel: "d",
448+
language: QueryLanguage.Java,
449+
}),
450+
];
451+
452+
let treeDataProvider: HistoryTreeDataProvider;
453+
454+
beforeEach(async () => {
455+
queryHistoryManager = await createMockQueryHistory(allHistory);
456+
(queryHistoryManager.treeDataProvider as any).history = [...history];
457+
treeDataProvider = queryHistoryManager.treeDataProvider;
458+
});
459+
460+
it("should get all if no filter is provided", async () => {
461+
const expected = [history[0], history[1], history[2], history[3]];
462+
treeDataProvider.sortOrder = SortOrder.NameAsc;
463+
464+
const children = await treeDataProvider.getChildren();
465+
expect(children).toEqual(expected);
466+
});
467+
468+
it("should filter local runs by language", async () => {
469+
const expected = [history[3]];
470+
treeDataProvider.sortOrder = SortOrder.NameAsc;
471+
472+
await languageContext.setLanguageContext(QueryLanguage.Java);
473+
474+
const children = await treeDataProvider.getChildren();
475+
expect(children).toEqual(expected);
476+
});
477+
478+
it("should filter variant analysis runs by language", async () => {
479+
const expected = [history[2]];
480+
treeDataProvider.sortOrder = SortOrder.NameAsc;
481+
482+
await languageContext.setLanguageContext(QueryLanguage.Python);
483+
484+
const children = await treeDataProvider.getChildren();
485+
expect(children).toEqual(expected);
486+
});
487+
});
421488
});
422489

423490
async function createMockQueryHistory(allHistory: QueryHistoryInfo[]) {
424491
const qhm = new QueryHistoryManager(
425-
createMockApp({}),
492+
app,
426493
{} as QueryRunner,
427494
{} as DatabaseManager,
428495
localQueriesResultsViewStub,
@@ -439,6 +506,7 @@ describe("HistoryTreeDataProvider", () => {
439506
ttlInMillis: 0,
440507
onDidChangeConfiguration: jest.fn(),
441508
}),
509+
languageContext,
442510
doCompareCallback,
443511
);
444512
(qhm.treeDataProvider as any).history = [...allHistory];

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { createMockQueryHistoryDirs } from "../../../factories/query-history/que
2828
import { createMockApp } from "../../../__mocks__/appMock";
2929
import { App } from "../../../../src/common/app";
3030
import { createMockCommandManager } from "../../../__mocks__/commandsMock";
31+
import { LanguageContextStore } from "../../../../src/language-context-store";
3132

3233
describe("QueryHistoryManager", () => {
3334
const mockExtensionLocation = join(tmpDir.name, "mock-extension-location");
@@ -937,6 +938,7 @@ describe("QueryHistoryManager", () => {
937938
ttlInMillis: 0,
938939
onDidChangeConfiguration: jest.fn(),
939940
}),
941+
new LanguageContextStore(mockApp),
940942
doCompareCallback,
941943
);
942944
(qhm.treeDataProvider as any).history = [...allHistory];

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { QueryHistoryManager } from "../../../../src/query-history/query-history
2323
import { mockedObject } from "../../utils/mocking.helpers";
2424
import { createMockQueryHistoryDirs } from "../../../factories/query-history/query-history-dirs";
2525
import { createMockApp } from "../../../__mocks__/appMock";
26+
import { LanguageContextStore } from "../../../../src/language-context-store";
2627

2728
// set a higher timeout since recursive delete may take a while, expecially on Windows.
2829
jest.setTimeout(120000);
@@ -74,8 +75,10 @@ describe("Variant Analyses and QueryHistoryManager", () => {
7475
join(STORAGE_DIR, "workspace-query-history.json"),
7576
).queries;
7677

78+
const app = createMockApp({});
79+
7780
qhm = new QueryHistoryManager(
78-
createMockApp({}),
81+
app,
7982
{} as QueryRunner,
8083
{} as DatabaseManager,
8184
localQueriesResultsViewStub,
@@ -97,6 +100,7 @@ describe("Variant Analyses and QueryHistoryManager", () => {
97100
ttlInMillis: 0,
98101
onDidChangeConfiguration: jest.fn(),
99102
}),
103+
new LanguageContextStore(app),
100104
asyncNoop,
101105
);
102106
disposables.push(qhm);

0 commit comments

Comments
 (0)