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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ This repository documents what is specific to the MySQL backend:

<!-- antelopejs-docs-compat:start -->

| Package | Supported | Tested with |
| ---------------------------------------- | ---------------- | ----------- |
| `@antelopejs/interface-core` | `>=0.5.1 <0.6.0` | `0.5.1` |
| `@antelopejs/interface-database` | `>=0.4.1 <0.5.0` | `0.4.1` |
| `@antelopejs/interface-database-catalog` | `>=0.1.0 <0.2.0` | `0.1.0` |
| Package | Supported | Tested with |
| ---------------------------------------- | ------------------------ | ------------------------ |
| `@antelopejs/interface-core` | `>=0.5.1 <0.6.0` | `0.5.1` |
| `@antelopejs/interface-database` | `>=0.4.1 <0.5.0` | `0.4.1` |
| `@antelopejs/interface-database-catalog` | `0.1.1-next.44.gee21aa3` | `0.1.1-next.44.gee21aa3` |

<!-- antelopejs-docs-compat:end -->

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"dependencies": {
"@antelopejs/interface-core": ">=0.5.1 <0.6.0",
"@antelopejs/interface-database": ">=0.4.1 <0.5.0",
"@antelopejs/interface-database-catalog": ">=0.1.0 <0.2.0",
"@antelopejs/interface-database-catalog": "0.1.1-next.44.gee21aa3",
"mysql2": "^3.24.6"
},
"devDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 60 additions & 24 deletions src/test-external/ranges.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,87 @@
import { expect } from "chai";
import {
AssertNoRareValue,
type FixtureCount,
type ResultValue,
} from "@antelopejs/interface-database-catalog/testing";
import {
type ColumnProfile,
type RelationProfile,
Profile,
} from "@antelopejs/interface-database-catalog";

import { Received } from "./received";

/** The type of `sales.grades.grade` as the catalog lists it, which the contract allows to name every label. */
const DECLARED_GRADES = "enum('bronze','silver','gold','platinum')";
/** The rows of `sales.grades` and `sales.snowflakes` that hold each value, as the fixture inserts them. */
const FIXTURE_COUNTS = new Map<string, FixtureCount[]>(
Object.entries({
grade: [
{ value: "bronze", rows: 4 },
{ value: "silver", rows: 5 },
{ value: "gold", rows: 8 },
{ value: "platinum", rows: 1 },
],
level: [
{ value: 7001, rows: 4 },
{ value: 7002, rows: 5 },
{ value: 7008, rows: 5 },
{ value: 7009, rows: 4 },
],
ref: Array.from({ length: 5 }, (_, index) => ({
value: 1500000000000000001n + BigInt(index) + "",
rows: 1,
})),
}),
);

async function ProfiledColumn(
column: string,
relation = "sales.grades",
): Promise<{ profile: ColumnProfile | undefined; results: string }> {
const { answer, results } = await Received(() =>
): Promise<{
profile: ColumnProfile | undefined;
whole: RelationProfile;
sets: ResultValue[];
}> {
const { answer, sets } = await Received(() =>
Profile({ relation, columns: [column] }),
);
return {
profile: answer.columns.find((profiled) => profiled.name === column),
results,
whole: answer,
sets,
};
}

function AssertNoRare(
column: string,
{ whole, sets }: { whole: RelationProfile; sets: ResultValue[] },
): void {
AssertNoRareValue({
profile: whole,
results: sets,
counts: { [column]: FIXTURE_COUNTS.get(column) ?? [] },
});
}

describe("the range of an external column", function () {
this.timeout(120_000);

it("ends an enum column at labels seen at least 5 times, so no result set holds a rarer one but the declared labels", async () => {
const { profile, results } = await ProfiledColumn("grade");
expect(profile?.range).to.deep.equal({
it("ends an enum column at labels seen at least 5 times, so no result set holds a rarer one beside the declared type", async () => {
const profiled = await ProfiledColumn("grade");
expect(profiled.profile?.range).to.deep.equal({
status: "ok",
value: { min: "gold", max: "silver" },
});
const measured = results.replaceAll(DECLARED_GRADES, "");
expect(measured).to.include('"silver"');
expect(measured).to.not.include("bronze");
expect(measured).to.not.include("platinum");
AssertNoRare("grade", profiled);
});

it("ends an integer column at values seen 5 times, past values seen 4 times", async () => {
const { profile, results } = await ProfiledColumn("level");
expect(profile?.range).to.deep.equal({
const profiled = await ProfiledColumn("level");
expect(profiled.profile?.range).to.deep.equal({
status: "ok",
value: { min: 7002, max: 7008 },
});
expect(results).to.include("7002");
expect(results).to.not.include("7001");
expect(results).to.not.include("7009");
AssertNoRare("level", profiled);
});

it("answers empty for an integer column whose every value is seen once", async () => {
Expand All @@ -54,15 +90,15 @@ describe("the range of an external column", function () {
});

it("counts bigint values one double cannot tell apart as distinct values, so five seen once each stay unnamed", async () => {
const { profile, results } = await ProfiledColumn(
"ref",
"sales.snowflakes",
);
expect(profile?.range).to.deep.equal({ status: "error", code: "empty" });
expect(profile?.categories).to.deep.equal({
const profiled = await ProfiledColumn("ref", "sales.snowflakes");
expect(profiled.profile?.range).to.deep.equal({
status: "error",
code: "empty",
});
expect(profiled.profile?.categories).to.deep.equal({
status: "ok",
value: { values: [], other: 5 },
});
expect(results).to.not.match(/15000000000000000\d\d/);
AssertNoRare("ref", profiled);
});
});
44 changes: 40 additions & 4 deletions src/test-external/received.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,69 @@
import type { ResultValue } from "@antelopejs/interface-database-catalog/testing";
import {
IsBoolean,
IsDocument,
IsNumber,
IsString,
} from "../implementations/database/guards";
import { Connection, PoolConnection } from "mysql2/promise";

/** Whether a parsed JSON value is one `ResultValue` spells, every nested element checked. */
function IsResultValue(value: unknown): value is ResultValue {
if (
value === null ||
IsString(value) ||
IsNumber(value) ||
IsBoolean(value)
) {
return true;
}
if (Array.isArray(value)) {
return value.every(IsResultValue);
}
return IsDocument(value) && Object.values(value).every(IsResultValue);
}

function ParseResult(json: string): ResultValue {
const parsed: unknown = JSON.parse(json);
if (!IsResultValue(parsed)) {
throw new TypeError("A result set holds a value JSON cannot spell");
}
return parsed;
}

/**
* Every result set the module's pooled connections receive while `work` runs, as JSON: each
* statement they run goes through the `execute` or `query` mysql2's promise `PoolConnection`
* inherits from its `Connection`, which a method of its own shadows for the while.
*/
export async function Received<T>(
work: () => Promise<T>,
): Promise<{ answer: T; results: string }> {
): Promise<{ answer: T; results: string; sets: ResultValue[] }> {
const results: string[] = [];
const sets: ResultValue[] = [];
const tapped = PoolConnection.prototype;
Object.defineProperty(tapped, "execute", {
configurable: true,
async value(this: Connection, ...args: Parameters<Connection["execute"]>) {
const answer = await Connection.prototype.execute.call(this, ...args);
results.push(JSON.stringify(answer[0]));
const json = JSON.stringify(answer[0]);
results.push(json);
sets.push(ParseResult(json));
return answer;
},
});
Object.defineProperty(tapped, "query", {
configurable: true,
async value(this: Connection, ...args: Parameters<Connection["query"]>) {
const answer = await Connection.prototype.query.call(this, ...args);
results.push(JSON.stringify(answer[0]));
const json = JSON.stringify(answer[0]);
results.push(json);
sets.push(ParseResult(json));
return answer;
},
});
try {
return { answer: await work(), results: results.join("\n") };
return { answer: await work(), results: results.join("\n"), sets };
} finally {
Reflect.deleteProperty(tapped, "execute");
Reflect.deleteProperty(tapped, "query");
Expand Down
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"noEmitOnError": true,
"sourceMap": true,
"useDefineForClassFields": true,
"paths": {
"@antelopejs/interface-database-catalog/testing": [
"./node_modules/@antelopejs/interface-database-catalog/dist/testing.d.ts"
]
},
"types": ["chai", "mocha", "node"]
},
"include": ["src/**/*.ts", "src/**/*.js"]
Expand Down