import { assert } from "chai" import { Schema, Column } from "@mwater/expressions" import { CustomRow } from "../src/CustomRow" // Create schema with one join const schema = new Schema({ tables: [ { id: "entities.a", name: { _base: "en", en: "A" }, contents: [{ id: "x", type: "text", name: { _base: "en", en: "X" } }] as Column[] }, { id: "custom.ts1.t1", name: { _base: "en", en: "T1" }, contents: [ { id: "x", type: "text", name: { _base: "en", en: "X" } }, { id: "y", type: "join", join: { type: "n-1", toTable: "entities.a" }, name: { _base: "en", en: "Y" } }, { id: "z", type: "id", name: { _base: "en", en: "Z" }, idTable: "custom.ts1.t2" } ] }, { id: "custom.ts1.t2", name: { _base: "en", en: "T2" }, contents: [ { id: "x", type: "text", name: { _base: "en", en: "X" } }, ] } ] }) describe("CustomRow", () => { it("gets plain value", async () => { const row = new CustomRow({ schema: schema, row: { x: "abc" }, tableId: "custom.ts1.t1", getEntityById: () => null, getCustomTableRow: () => Promise.resolve(null) }) const value = await row.getField("x") assert.equal(value, "abc") }) it("follows joins", async () => { const row = new CustomRow({ schema: schema, row: { x: "abc", y: "id1", z: "id2" }, tableId: "custom.ts1.t1", getEntityById: (entityType, entityId, cb) => { assert.equal(entityType, "a") assert.equal(entityId, "id1") cb({ x: "xyz" }) }, getCustomTableRow: async (tableId, rowId) => { assert.equal(tableId, "custom.ts1.t2") assert.equal(rowId, "id2") return Promise.resolve({ x: "xyz2" }) } }) const innerRow = await row.followJoin("y") const value = await innerRow!.getField("x") assert.equal(value, "xyz") const innerRow2 = await row.followJoin("z") const value2 = await innerRow2!.getField("x") assert.equal(value2, "xyz2") }) })