Files
2026-07-13 12:25:07 +08:00

249 lines
9.1 KiB
TypeScript

// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import { test, expect } from "@perspective-dev/test";
import perspective from "../perspective_client.ts";
((perspective) => {
test.describe("Inner joins, indexed tables", function () {
test("inner joins two tables on a shared key", async function () {
const left = await perspective.table(
[
{ id: 1, x: 10 },
{ id: 2, x: 20 },
{ id: 3, x: 30 },
],
{ index: "id" },
);
const right = await perspective.table(
[
{ id: 1, y: "a" },
{ id: 2, y: "b" },
{ id: 4, y: "d" },
],
{ index: "id" },
);
const joined = await perspective.join(left, right, "id");
const view = await joined.view();
const json = await view.to_json();
expect(json).toHaveLength(2);
await view.delete();
await joined.delete();
await right.delete();
await left.delete();
});
test("joined table has correct schema", async function () {
const left = await perspective.table(
{ id: "integer", x: "float" },
{ index: "id" },
);
const right = await perspective.table(
{ id: "integer", y: "string" },
{ index: "id" },
);
const joined = await perspective.join(left, right, "id");
const schema = await joined.schema();
expect(schema).toEqual({
id: "integer",
x: "float",
y: "string",
});
await joined.delete();
await right.delete();
await left.delete();
});
test("joined table reacts to left table updates", async function () {
const left = await perspective.table(
[
{ id: 1, x: 10 },
{ id: 2, x: 20 },
],
{ index: "id" },
);
const right = await perspective.table(
[
{ id: 1, y: "a" },
{ id: 2, y: "b" },
],
{ index: "id" },
);
const joined = await perspective.join(left, right, "id");
const view = await joined.view();
let json = await view.to_json();
expect(json).toHaveLength(2);
await left.update([{ id: 1, x: 99 }]);
json = await view.to_json();
expect(json).toEqual([
{ id: 1, x: 99, y: "a" },
{ id: 2, x: 20, y: "b" },
]);
await view.delete();
await joined.delete();
await right.delete();
await left.delete();
});
test("joined table reacts to right table updates", async function () {
const left = await perspective.table(
[
{ id: 1, x: 10 },
{ id: 2, x: 20 },
],
{ index: "id" },
);
const right = await perspective.table(
[
{ id: 1, y: "a" },
{ id: 2, y: "b" },
],
{ index: "id" },
);
const joined = await perspective.join(left, right, "id");
const view = await joined.view();
await right.update([{ id: 3, y: "c" }]);
const json = await view.to_json();
// id=3 only exists in right, so inner join should not include it
expect(json).toHaveLength(2);
expect(json).toEqual([
{ id: 1, x: 10, y: "a" },
{ id: 2, x: 20, y: "b" },
]);
await view.delete();
await joined.delete();
await right.delete();
await left.delete();
});
test("joined table reacts to new matching rows", async function () {
const left = await perspective.table([{ id: 1, x: 10 }], {
index: "id",
});
const right = await perspective.table([{ id: 2, y: "b" }], {
index: "id",
});
const joined = await perspective.join(left, right, "id");
const view = await joined.view();
let json = await view.to_json();
expect(json).toHaveLength(0);
// Add matching row to right
await right.update([{ id: 1, y: "a" }]);
json = await view.to_json();
expect(json).toHaveLength(1);
expect(json).toEqual([{ id: 1, x: 10, y: "a" }]);
await view.delete();
await joined.delete();
await right.delete();
await left.delete();
});
test("joined table supports views with group_by", async function () {
const left = await perspective.table(
[
{ id: 1, category: "A", x: 10 },
{ id: 2, category: "A", x: 20 },
{ id: 3, category: "B", x: 30 },
],
{ index: "id" },
);
const right = await perspective.table(
[
{ id: 1, y: 100 },
{ id: 2, y: 200 },
{ id: 3, y: 300 },
],
{ index: "id" },
);
const joined = await perspective.join(left, right, "id");
const view = await joined.view({
group_by: ["category"],
columns: ["x", "y"],
});
const json = await view.to_columns();
expect(json["x"]).toEqual([60, 30, 30]);
expect(json["y"]).toEqual([600, 300, 300]);
await view.delete();
await joined.delete();
await right.delete();
await left.delete();
});
test("rejects column name conflicts", async function () {
const left = await perspective.table([{ id: 1, value: 10 }]);
const right = await perspective.table([{ id: 1, value: 20 }]);
let error;
try {
await perspective.join(left, right, "id");
} catch (e) {
error = e;
}
expect(error).toBeDefined();
await right.delete();
await left.delete();
});
test("rejects updates on joined table", async function () {
const left = await perspective.table([{ id: 1, x: 10 }], {
index: "id",
});
const right = await perspective.table([{ id: 1, y: "a" }], {
index: "id",
});
const joined = await perspective.join(left, right, "id");
let error;
try {
await joined.update([{ id: 1, x: 99, y: "z" }]);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
await joined.delete();
await right.delete();
await left.delete();
});
});
})(perspective);