Skip to content

Commit 028cc79

Browse files
committed
Combine createMockLocalQueryInfo and createMockLocalQuery
One factory method to rule them all! There were a number of problems with these methods: 1. We were previously using two different factory methods to generate a fake local queries. Ideally we'd just have one. 2. We weren't really creating a real LocalQueryInfo object, which blocked us [1] from being able to correctly understand which fields we need in our tests and how they interact together. 3. We stubbed a bunch of methods on the original object to get our tests to work. We can now use a real object with all the trimmings. [1]: #1697 (comment)
1 parent 0639c66 commit 028cc79

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

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

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,88 @@
1+
import { faker } from '@faker-js/faker';
12
import {
23
InitialQueryInfo,
3-
CompletedQueryInfo,
4-
CompletedLocalQueryInfo,
54
LocalQueryInfo,
65
} from '../../../query-results';
76
import { QueryEvaluationInfo, QueryWithResults } from '../../../run-queries-shared';
87
import { CancellationTokenSource } from 'vscode';
98
import { QueryResultType } from '../../../pure/legacy-messages';
9+
import { QueryMetadata } from '../../../pure/interface-types';
1010

11-
export function createMockLocalQueryInfo(
12-
startTime: string,
13-
userSpecifiedLabel?: string
14-
): LocalQueryInfo {
15-
return ({
16-
t: 'local',
17-
userSpecifiedLabel,
18-
startTime: startTime,
19-
getQueryFileName() {
20-
return 'query-file.ql';
21-
},
22-
getQueryName() {
23-
return 'query-name';
24-
},
25-
initialInfo: ({
26-
databaseInfo: {
27-
databaseUri: 'unused',
28-
name: 'db-name',
29-
},
30-
} as unknown) as InitialQueryInfo,
31-
completedQuery: ({
32-
resultCount: 456,
33-
statusString: 'in progress',
34-
} as unknown) as CompletedQueryInfo,
35-
} as unknown) as CompletedLocalQueryInfo;
36-
}
11+
export function createMockLocalQueryInfo({
12+
startTime = new Date(),
13+
resultCount = 0,
14+
userSpecifiedLabel = undefined,
15+
failureReason = undefined,
16+
dbName = 'db-name',
17+
hasMetadata = false,
18+
queryWithResults = undefined,
19+
}: {
20+
startTime?: Date,
21+
resultCount?: number,
22+
userSpecifiedLabel?: string,
23+
failureReason?: string,
24+
dbName?: string,
25+
hasMetadata?: boolean,
26+
queryWithResults?: QueryWithResults | undefined,
27+
}): LocalQueryInfo {
28+
const cancellationToken = {
29+
dispose: () => { /**/ },
30+
} as CancellationTokenSource;
3731

38-
export function createMockLocalQuery(
39-
dbName = 'a',
40-
queryWithResults?: QueryWithResults,
41-
isFail = false
42-
): LocalQueryInfo {
4332
const initialQueryInfo = {
44-
databaseInfo: { name: dbName },
45-
start: new Date(),
46-
queryPath: 'hucairz'
33+
queryText: 'select 1',
34+
isQuickQuery: false,
35+
isQuickEval: false,
36+
queryName: 'query-name',
37+
queryPath: 'query-file.ql',
38+
databaseInfo: {
39+
databaseUri: 'databaseUri',
40+
name: dbName,
41+
},
42+
start: startTime,
43+
id: faker.datatype.number().toString(),
44+
userSpecifiedLabel
4745
} as InitialQueryInfo;
4846

49-
const cancellationToken = {
50-
dispose: () => { /**/ },
51-
} as CancellationTokenSource;
47+
const localQuery = new LocalQueryInfo(initialQueryInfo, cancellationToken);
5248

53-
const fqi = new LocalQueryInfo(
54-
initialQueryInfo,
55-
cancellationToken,
56-
);
49+
localQuery.failureReason = failureReason;
5750

5851
if (queryWithResults) {
59-
fqi.completeThisQuery(queryWithResults);
52+
localQuery.completeThisQuery(queryWithResults);
53+
localQuery.completedQuery?.setResultCount(1);
54+
} else if (resultCount > 0) {
55+
const queryWithResults = createMockQueryWithResults({ hasMetadata });
56+
localQuery.completeThisQuery(queryWithResults);
57+
localQuery.completedQuery?.setResultCount(resultCount);
6058
}
6159

62-
if (isFail) {
63-
fqi.failureReason = 'failure reason';
64-
}
65-
66-
return fqi;
60+
return localQuery;
6761
}
6862

69-
export function createMockQueryWithResults(
70-
sandbox: sinon.SinonSandbox,
63+
export function createMockQueryWithResults({
64+
sandbox = undefined,
7165
didRunSuccessfully = true,
72-
hasInterpretedResults = true
73-
): QueryWithResults {
66+
hasInterpretedResults = true,
67+
hasMetadata = undefined
68+
}: {
69+
sandbox?: sinon.SinonSandbox,
70+
didRunSuccessfully?: boolean,
71+
hasInterpretedResults?: boolean,
72+
hasMetadata?: boolean,
73+
}): QueryWithResults {
74+
const dispose = sandbox ? sandbox.spy() : () => { /**/ };
75+
const deleteQuery = sandbox ? sandbox.stub() : () => { /**/ };
76+
const metadata = hasMetadata ? { name: 'query-name' } as QueryMetadata : undefined;
77+
7478
return {
7579
query: {
7680
hasInterpretedResults: () => Promise.resolve(hasInterpretedResults),
77-
deleteQuery: sandbox.stub(),
81+
deleteQuery,
82+
metadata,
7883
} as unknown as QueryEvaluationInfo,
7984
successful: didRunSuccessfully,
80-
message: 'foo',
81-
dispose: sandbox.spy(),
85+
dispose,
8286
result: {
8387
evaluationTime: 1,
8488
queryId: 0,

0 commit comments

Comments
 (0)