Skip to content

Commit 4dc1b12

Browse files
Merge pull request #1984 from github/elena/move-tests
Move tests for serialization/deserialization in their own file
2 parents c2dcfe9 + e97a7c4 commit 4dc1b12

2 files changed

Lines changed: 206 additions & 115 deletions

File tree

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

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import {
2121
import { CodeQLCliServer, SourceInfo } from "../../../src/cli";
2222
import { CancellationTokenSource, Uri } from "vscode";
2323
import { tmpDir } from "../../../src/helpers";
24-
import {
25-
deserializeQueryHistory,
26-
serializeQueryHistory,
27-
} from "../../../src/query-serialization";
2824
import {
2925
formatLegacyMessage,
3026
QueryInProgress,
@@ -438,117 +434,6 @@ describe("query-results", () => {
438434
);
439435
});
440436

441-
describe("serialize and deserialize", () => {
442-
let infoSuccessRaw: LocalQueryInfo;
443-
let infoSuccessInterpreted: LocalQueryInfo;
444-
let infoEarlyFailure: LocalQueryInfo;
445-
let infoLateFailure: LocalQueryInfo;
446-
let infoInprogress: LocalQueryInfo;
447-
let allHistory: LocalQueryInfo[];
448-
449-
beforeEach(() => {
450-
infoSuccessRaw = createMockFullQueryInfo(
451-
"a",
452-
createMockQueryWithResults(
453-
`${queryPath}-a`,
454-
false,
455-
false,
456-
"/a/b/c/a",
457-
false,
458-
),
459-
);
460-
infoSuccessInterpreted = createMockFullQueryInfo(
461-
"b",
462-
createMockQueryWithResults(
463-
`${queryPath}-b`,
464-
true,
465-
true,
466-
"/a/b/c/b",
467-
false,
468-
),
469-
);
470-
infoEarlyFailure = createMockFullQueryInfo("c", undefined, true);
471-
infoLateFailure = createMockFullQueryInfo(
472-
"d",
473-
createMockQueryWithResults(
474-
`${queryPath}-c`,
475-
false,
476-
false,
477-
"/a/b/c/d",
478-
false,
479-
),
480-
);
481-
infoInprogress = createMockFullQueryInfo("e");
482-
allHistory = [
483-
infoSuccessRaw,
484-
infoSuccessInterpreted,
485-
infoEarlyFailure,
486-
infoLateFailure,
487-
infoInprogress,
488-
];
489-
});
490-
491-
it("should serialize and deserialize query history", async () => {
492-
// the expected results only contains the history with completed queries
493-
const expectedHistory = [
494-
infoSuccessRaw,
495-
infoSuccessInterpreted,
496-
infoLateFailure,
497-
];
498-
499-
const allHistoryPath = join(tmpDir.name, "workspace-query-history.json");
500-
501-
// serialize and deserialize
502-
await serializeQueryHistory(allHistory, allHistoryPath);
503-
const allHistoryActual = await deserializeQueryHistory(allHistoryPath);
504-
505-
// the dispose methods will be different. Ignore them.
506-
allHistoryActual.forEach((info) => {
507-
if (info.t === "local" && info.completedQuery) {
508-
const completedQuery = info.completedQuery;
509-
(completedQuery as any).dispose = undefined;
510-
511-
// these fields should be missing on the deserialized value
512-
// but they are undefined on the original value
513-
if (!("logFileLocation" in completedQuery)) {
514-
(completedQuery as any).logFileLocation = undefined;
515-
}
516-
const query = completedQuery.query;
517-
if (!("quickEvalPosition" in query)) {
518-
(query as any).quickEvalPosition = undefined;
519-
}
520-
}
521-
});
522-
expectedHistory.forEach((info) => {
523-
if (info.completedQuery) {
524-
(info.completedQuery as any).dispose = undefined;
525-
}
526-
});
527-
528-
// make the diffs somewhat sane by comparing each element directly
529-
for (let i = 0; i < allHistoryActual.length; i++) {
530-
expect(allHistoryActual[i]).toEqual(expectedHistory[i]);
531-
}
532-
expect(allHistoryActual.length).toEqual(expectedHistory.length);
533-
});
534-
535-
it("should handle an invalid query history version", async () => {
536-
const badPath = join(tmpDir.name, "bad-query-history.json");
537-
writeFileSync(
538-
badPath,
539-
JSON.stringify({
540-
version: 3,
541-
queries: allHistory,
542-
}),
543-
"utf8",
544-
);
545-
546-
const allHistoryActual = await deserializeQueryHistory(badPath);
547-
// version number is invalid. Should return an empty array.
548-
expect(allHistoryActual).toEqual([]);
549-
});
550-
});
551-
552437
function safeDel(file: string) {
553438
try {
554439
unlinkSync(file);
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import {
2+
deserializeQueryHistory,
3+
serializeQueryHistory,
4+
} from "../../../src/query-serialization";
5+
import { join } from "path";
6+
import { writeFileSync, mkdirpSync } from "fs-extra";
7+
import { LocalQueryInfo, InitialQueryInfo } from "../../../src/query-results";
8+
import { QueryWithResults } from "../../../src/run-queries-shared";
9+
import { DatabaseInfo } from "../../../src/pure/interface-types";
10+
import { CancellationTokenSource, Uri } from "vscode";
11+
import { tmpDir } from "../../../src/helpers";
12+
import { QueryResultType } from "../../../src/pure/legacy-messages";
13+
import { QueryInProgress } from "../../../src/legacy-query-server/run-queries";
14+
15+
describe("serialize and deserialize", () => {
16+
let infoSuccessRaw: LocalQueryInfo;
17+
let infoSuccessInterpreted: LocalQueryInfo;
18+
let infoEarlyFailure: LocalQueryInfo;
19+
let infoLateFailure: LocalQueryInfo;
20+
let infoInprogress: LocalQueryInfo;
21+
let allHistory: LocalQueryInfo[];
22+
let queryPath: string;
23+
let cnt = 0;
24+
25+
beforeEach(() => {
26+
queryPath = join(Uri.file(tmpDir.name).fsPath, `query-${cnt++}`);
27+
28+
infoSuccessRaw = createMockFullQueryInfo(
29+
"a",
30+
createMockQueryWithResults(
31+
`${queryPath}-a`,
32+
false,
33+
false,
34+
"/a/b/c/a",
35+
false,
36+
),
37+
);
38+
infoSuccessInterpreted = createMockFullQueryInfo(
39+
"b",
40+
createMockQueryWithResults(
41+
`${queryPath}-b`,
42+
true,
43+
true,
44+
"/a/b/c/b",
45+
false,
46+
),
47+
);
48+
infoEarlyFailure = createMockFullQueryInfo("c", undefined, true);
49+
infoLateFailure = createMockFullQueryInfo(
50+
"d",
51+
createMockQueryWithResults(
52+
`${queryPath}-c`,
53+
false,
54+
false,
55+
"/a/b/c/d",
56+
false,
57+
),
58+
);
59+
infoInprogress = createMockFullQueryInfo("e");
60+
allHistory = [
61+
infoSuccessRaw,
62+
infoSuccessInterpreted,
63+
infoEarlyFailure,
64+
infoLateFailure,
65+
infoInprogress,
66+
];
67+
});
68+
69+
it("should serialize and deserialize query history", async () => {
70+
// the expected results only contains the history with completed queries
71+
const expectedHistory = [
72+
infoSuccessRaw,
73+
infoSuccessInterpreted,
74+
infoLateFailure,
75+
];
76+
77+
const allHistoryPath = join(tmpDir.name, "workspace-query-history.json");
78+
79+
// serialize and deserialize
80+
await serializeQueryHistory(allHistory, allHistoryPath);
81+
const allHistoryActual = await deserializeQueryHistory(allHistoryPath);
82+
83+
// the dispose methods will be different. Ignore them.
84+
allHistoryActual.forEach((info) => {
85+
if (info.t === "local" && info.completedQuery) {
86+
const completedQuery = info.completedQuery;
87+
(completedQuery as any).dispose = undefined;
88+
89+
// these fields should be missing on the deserialized value
90+
// but they are undefined on the original value
91+
if (!("logFileLocation" in completedQuery)) {
92+
(completedQuery as any).logFileLocation = undefined;
93+
}
94+
const query = completedQuery.query;
95+
if (!("quickEvalPosition" in query)) {
96+
(query as any).quickEvalPosition = undefined;
97+
}
98+
}
99+
});
100+
expectedHistory.forEach((info) => {
101+
if (info.completedQuery) {
102+
(info.completedQuery as any).dispose = undefined;
103+
}
104+
});
105+
106+
// make the diffs somewhat sane by comparing each element directly
107+
for (let i = 0; i < allHistoryActual.length; i++) {
108+
expect(allHistoryActual[i]).toEqual(expectedHistory[i]);
109+
}
110+
expect(allHistoryActual.length).toEqual(expectedHistory.length);
111+
});
112+
113+
it("should handle an invalid query history version", async () => {
114+
const badPath = join(tmpDir.name, "bad-query-history.json");
115+
writeFileSync(
116+
badPath,
117+
JSON.stringify({
118+
version: 3,
119+
queries: allHistory,
120+
}),
121+
"utf8",
122+
);
123+
124+
const allHistoryActual = await deserializeQueryHistory(badPath);
125+
// version number is invalid. Should return an empty array.
126+
expect(allHistoryActual).toEqual([]);
127+
});
128+
129+
function createMockFullQueryInfo(
130+
dbName = "a",
131+
queryWithResults?: QueryWithResults,
132+
isFail = false,
133+
): LocalQueryInfo {
134+
const fqi = new LocalQueryInfo(
135+
{
136+
databaseInfo: {
137+
name: dbName,
138+
databaseUri: Uri.parse(`/a/b/c/${dbName}`).fsPath,
139+
} as unknown as DatabaseInfo,
140+
start: new Date(),
141+
queryPath: "path/to/hucairz",
142+
queryText: "some query",
143+
isQuickQuery: false,
144+
isQuickEval: false,
145+
id: `some-id-${dbName}`,
146+
} as InitialQueryInfo,
147+
{
148+
dispose: () => {
149+
/**/
150+
},
151+
} as CancellationTokenSource,
152+
);
153+
154+
if (queryWithResults) {
155+
fqi.completeThisQuery(queryWithResults);
156+
}
157+
if (isFail) {
158+
fqi.failureReason = "failure reason";
159+
}
160+
return fqi;
161+
}
162+
163+
function createMockQueryWithResults(
164+
queryPath: string,
165+
didRunSuccessfully = true,
166+
hasInterpretedResults = true,
167+
dbPath = "/a/b/c",
168+
includeSpies = true,
169+
): QueryWithResults {
170+
// pretend that the results path exists
171+
const resultsPath = join(queryPath, "results.bqrs");
172+
mkdirpSync(queryPath);
173+
writeFileSync(resultsPath, "", "utf8");
174+
175+
const query = new QueryInProgress(
176+
queryPath,
177+
Uri.file(dbPath).fsPath,
178+
true,
179+
"queryDbscheme",
180+
undefined,
181+
{
182+
name: "vwx",
183+
},
184+
);
185+
186+
const result: QueryWithResults = {
187+
query: query.queryEvalInfo,
188+
successful: didRunSuccessfully,
189+
message: "foo",
190+
dispose: jest.fn(),
191+
result: {
192+
evaluationTime: 1,
193+
queryId: 0,
194+
runId: 0,
195+
resultType: QueryResultType.SUCCESS,
196+
},
197+
};
198+
199+
if (includeSpies) {
200+
(query as any).hasInterpretedResults = () =>
201+
Promise.resolve(hasInterpretedResults);
202+
}
203+
204+
return result;
205+
}
206+
});

0 commit comments

Comments
 (0)