Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/reporters/github/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { test, expect, describe } from "vitest";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import n from "nunjucks";
import { formatCost, queryPreview, buildViewModel } from "./github.ts";
import type { ReportContext } from "../reporter.ts";
import { isQueryLong, renderExplain, type ReportContext } from "../reporter.ts";
import type { RunComparison } from "../site-api.ts";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const successTemplate = readFileSync(join(__dirname, "success.md.j2"), "utf-8");

n.configure({ autoescape: false, trimBlocks: true, lstripBlocks: true });

function renderTemplate(ctx: ReportContext) {
const viewModel = buildViewModel(ctx);
return n.renderString(successTemplate, {
...ctx,
...viewModel,
isQueryLong,
renderExplain,
formatCost,
});
}

describe("formatCost", () => {
test("formats small numbers without commas", () => {
expect(formatCost(9)).toBe("9");
Expand Down Expand Up @@ -300,3 +321,22 @@ describe("buildViewModel", () => {
});

});

describe("template rendering", () => {
test("renders queryStats.total as the query count", () => {
const ctx = makeContext({
queryStats: { total: 5, matched: 3, optimized: 1, errored: 0 },
comparison: makeComparison(),
});
const output = renderTemplate(ctx);
expect(output).toContain("5 queries analyzed");
});

test("renders queryStats.total in no-comparison mode", () => {
const ctx = makeContext({
queryStats: { total: 3, matched: 1, optimized: 0, errored: 0 },
});
const output = renderTemplate(ctx);
expect(output).toContain("3 queries analyzed");
});
});
2 changes: 1 addition & 1 deletion src/reporters/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type ReportMetadata = {
declare const s: unique symbol;

export interface ReportStatistics {
/** Total number of queries seen in the log */
/** Number of unique, non-filtered queries analyzed */
total: number;
/** Number of queries that matched the query pattern */
matched: number;
Expand Down
95 changes: 95 additions & 0 deletions src/reporters/site-api.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { test, expect, describe } from "vitest";
import {
buildQueries,
compareRuns,
type CiQueryPayload,
type PreviousRun,
} from "./site-api.ts";
import type { QueryProcessResult } from "../runner.ts";

function makeQuery(hash: string, cost: number = 100): CiQueryPayload {
return {
Expand Down Expand Up @@ -31,6 +33,99 @@ function makePreviousRun(queries: CiQueryPayload[]): PreviousRun {
};
}

describe("buildQueries", () => {
test("filters out invalid results", () => {
const results: QueryProcessResult[] = [
{
kind: "no_improvement",
fingerprint: "hash-a",
rawQuery: "SELECT 1",
formattedQuery: "SELECT 1",
cost: 10,
existingIndexes: [],
nudges: [],
tags: [],
referencedTables: [],
},
{ kind: "invalid" },
{
kind: "no_improvement",
fingerprint: "hash-b",
rawQuery: "SELECT 2",
formattedQuery: "SELECT 2",
cost: 20,
existingIndexes: [],
nudges: [],
tags: [],
referencedTables: [],
},
];

const queries = buildQueries(results);
expect(queries).toHaveLength(2);
expect(queries.map((q) => q.hash)).toEqual(["hash-a", "hash-b"]);
});

test("filters out ignored query hashes", () => {
const results: QueryProcessResult[] = [
{
kind: "no_improvement",
fingerprint: "hash-a",
rawQuery: "SELECT 1",
formattedQuery: "SELECT 1",
cost: 10,
existingIndexes: [],
nudges: [],
tags: [],
referencedTables: [],
},
{
kind: "no_improvement",
fingerprint: "hash-b",
rawQuery: "SELECT 2",
formattedQuery: "SELECT 2",
cost: 20,
existingIndexes: [],
nudges: [],
tags: [],
referencedTables: [],
},
];

const queries = buildQueries(results, {
ignoredQueryHashes: ["hash-a"],
acknowledgedQueryHashes: [],
regressionThreshold: 10,
minimumCost: 0,
});
expect(queries).toHaveLength(1);
expect(queries[0].hash).toBe("hash-b");
});

test("count reflects deduplicated output, not raw input length", () => {
const results: QueryProcessResult[] = [
{
kind: "no_improvement",
fingerprint: "hash-a",
rawQuery: "SELECT 1",
formattedQuery: "SELECT 1",
cost: 10,
existingIndexes: [],
nudges: [],
tags: [],
referencedTables: [],
},
{ kind: "invalid" },
{ kind: "invalid" },
{ kind: "invalid" },
];

const queries = buildQueries(results);
// 4 results in, but only 1 valid query out
expect(queries).toHaveLength(1);
});
});

describe("compareRuns", () => {
describe("new query detection via previousRun", () => {
test("when previousRun has no queries, all current queries are new", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ export class Runner {
}

async processQuery(log: ExplainedLog): Promise<QueryProcessResult> {
this.queryStats.total++;
const { query } = log;
const queryFingerprint = await fingerprint(query);
if (this.ignoredQueryHashes.has(queryFingerprint)) {
Expand Down Expand Up @@ -257,6 +256,7 @@ export class Runner {
}
return { kind: "invalid" };
}
this.queryStats.total++;
const indexCandidates = analyzer.deriveIndexes(
this.stats.ownMetadata,
indexesToCheck,
Expand Down
Loading