Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export class Runner {
if (loglevel !== "LOG" || !queryString.startsWith("plan:")) {
continue;
}
total++;
const planString: string = queryString.split("plan:")[1].trim();
const json = preprocessEncodedJson(planString);
if (!json) {
Expand All @@ -135,6 +134,7 @@ export class Runner {
continue;
}

total++;
const recentQuery = await RecentQuery.fromLogEntry(query, hash);
recentQueries.push(recentQuery)
}
Expand Down
Loading