Skip to content

Commit

Permalink
Merge pull request #3290 from github/robertbrignull/history-tests-dtos
Browse files Browse the repository at this point in the history
Don't accidentally mix DTO and non-DTO types in query-history-store.test.ts
  • Loading branch information
robertbrignull authored Jan 29, 2024
2 parents 978e48f + 98764a1 commit 5367bde
Showing 1 changed file with 64 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
writeQueryHistoryToFile,
} from "../../../../../src/query-history/store/query-history-store";
import { join } from "path";
import { writeFileSync, mkdirpSync, writeFile } from "fs-extra";
import { writeFileSync, mkdirpSync } from "fs-extra";
import type { InitialQueryInfo } from "../../../../../src/query-results";
import { LocalQueryInfo } from "../../../../../src/query-results";
import type { QueryWithResults } from "../../../../../src/run-queries-shared";
Expand All @@ -13,46 +13,42 @@ import type { DatabaseInfo } from "../../../../../src/common/interface-types";
import type { CancellationTokenSource } from "vscode";
import { Uri } from "vscode";
import { tmpDir } from "../../../../../src/tmp-dir";
import type { VariantAnalysisHistoryItem } from "../../../../../src/query-history/variant-analysis-history-item";
import type { QueryHistoryInfo } from "../../../../../src/query-history/query-history-info";
import { createMockVariantAnalysisHistoryItem } from "../../../../factories/query-history/variant-analysis-history-item";
import { nanoid } from "nanoid";
import type {
QueryHistoryDto,
QueryHistoryItemDto,
} from "../../../../../src/query-history/store/query-history-dto";
import { mapQueryHistoryToDto } from "../../../../../src/query-history/store/query-history-domain-mapper";

describe("write and read", () => {
let infoSuccessRaw: LocalQueryInfo;
let infoSuccessInterpreted: LocalQueryInfo;
let infoEarlyFailure: LocalQueryInfo;
let infoLateFailure: LocalQueryInfo;
let infoInProgress: LocalQueryInfo;

let variantAnalysis1: VariantAnalysisHistoryItem;
let variantAnalysis2: VariantAnalysisHistoryItem;

let allHistory: QueryHistoryInfo[];
let allHistoryDtos: QueryHistoryItemDto[];
let expectedHistory: QueryHistoryInfo[];
let queryPath: string;
let cnt = 0;

beforeEach(() => {
queryPath = join(Uri.file(tmpDir.name).fsPath, `query-${cnt++}`);

infoSuccessRaw = createMockFullQueryInfo(
const infoSuccessRaw = createMockFullQueryInfo(
"a",
createMockQueryWithResults(`${queryPath}-a`, false, "/a/b/c/a"),
);
infoSuccessInterpreted = createMockFullQueryInfo(
const infoSuccessInterpreted = createMockFullQueryInfo(
"b",
createMockQueryWithResults(`${queryPath}-b`, true, "/a/b/c/b"),
);
infoEarlyFailure = createMockFullQueryInfo("c", undefined, true);
infoLateFailure = createMockFullQueryInfo(
const infoEarlyFailure = createMockFullQueryInfo("c", undefined, true);
const infoLateFailure = createMockFullQueryInfo(
"d",
createMockQueryWithResults(`${queryPath}-c`, false, "/a/b/c/d"),
);
infoInProgress = createMockFullQueryInfo("e");
const infoInProgress = createMockFullQueryInfo("e");

variantAnalysis1 = createMockVariantAnalysisHistoryItem({});
variantAnalysis2 = createMockVariantAnalysisHistoryItem({});
const variantAnalysis1 = createMockVariantAnalysisHistoryItem({});
const variantAnalysis2 = createMockVariantAnalysisHistoryItem({});

allHistory = [
infoSuccessRaw,
Expand All @@ -64,6 +60,8 @@ describe("write and read", () => {
variantAnalysis2,
];

allHistoryDtos = mapQueryHistoryToDto(allHistory);

// the expected results only contains the history with completed queries
expectedHistory = [
infoSuccessRaw,
Expand Down Expand Up @@ -139,69 +137,61 @@ describe("write and read", () => {

it("should remove remote queries from the history", async () => {
const path = join(tmpDir.name, "query-history-with-remote.json");
await writeFile(
path,
JSON.stringify({
version: 2,
queries: [
...allHistory,
{
t: "remote",
status: "InProgress",
completed: false,
queryId: nanoid(),
remoteQuery: {
queryName: "query-name",
queryFilePath: "query-file.ql",
queryText: "select 1",
language: "javascript",
controllerRepository: {
owner: "github",
name: "vscode-codeql-integration-tests",
},
executionStartTime: Date.now(),
actionsWorkflowRunId: 1,
repositoryCount: 0,
writeRawQueryHistory(path, {
version: 2,
queries: [
...allHistoryDtos,
{
t: "remote",
status: "InProgress",
completed: false,
queryId: nanoid(),
remoteQuery: {
queryName: "query-name",
queryFilePath: "query-file.ql",
queryText: "select 1",
language: "javascript",
controllerRepository: {
owner: "github",
name: "vscode-codeql-integration-tests",
},
executionStartTime: Date.now(),
actionsWorkflowRunId: 1,
repositoryCount: 0,
},
{
t: "remote",
status: "Completed",
completed: true,
queryId: nanoid(),
remoteQuery: {
queryName: "query-name",
queryFilePath: "query-file.ql",
queryText: "select 1",
language: "javascript",
controllerRepository: {
owner: "github",
name: "vscode-codeql-integration-tests",
},
executionStartTime: Date.now(),
actionsWorkflowRunId: 1,
repositoryCount: 0,
} as unknown as QueryHistoryItemDto,
{
t: "remote",
status: "Completed",
completed: true,
queryId: nanoid(),
remoteQuery: {
queryName: "query-name",
queryFilePath: "query-file.ql",
queryText: "select 1",
language: "javascript",
controllerRepository: {
owner: "github",
name: "vscode-codeql-integration-tests",
},
executionStartTime: Date.now(),
actionsWorkflowRunId: 1,
repositoryCount: 0,
},
],
}),
"utf8",
);
} as unknown as QueryHistoryItemDto,
],
});

const actual = await readQueryHistoryFromFile(path);
expect(actual.length).toEqual(expectedHistory.length);
});

it("should handle an invalid query history version", async () => {
const badPath = join(tmpDir.name, "bad-query-history.json");
writeFileSync(
badPath,
JSON.stringify({
version: 3,
queries: allHistory,
}),
"utf8",
);
writeRawQueryHistory(badPath, {
version: 3,
queries: allHistoryDtos,
});

const allHistoryActual = await readQueryHistoryFromFile(badPath);
// version number is invalid. Should return an empty array.
Expand Down Expand Up @@ -274,4 +264,8 @@ describe("write and read", () => {

return result;
}

function writeRawQueryHistory(path: string, queryHistory: QueryHistoryDto) {
writeFileSync(path, JSON.stringify(queryHistory), "utf8");
}
});

0 comments on commit 5367bde

Please sign in to comment.