|
| 1 | +// test/validator-check/e2e.test.ts |
| 2 | +import { describe, test, expect } from "bun:test"; |
| 3 | +import { MetaDataLoader, InMemoryStringSource } from "@metaobjectsdev/metadata"; |
| 4 | +import type { MetaData } from "@metaobjectsdev/metadata"; |
| 5 | +import { buildExpectedSchema } from "../../src/expected-schema.js"; |
| 6 | +import { diff } from "../../src/diff/index.js"; |
| 7 | +import { emit } from "../../src/emit/index.js"; |
| 8 | + |
| 9 | +async function load(json: string): Promise<MetaData> { |
| 10 | + return (await new MetaDataLoader().load([new InMemoryStringSource(json)])).root; |
| 11 | +} |
| 12 | +const ORDER = JSON.stringify({ |
| 13 | + "metadata.root": { children: [{ |
| 14 | + "object.entity": { name: "Order", children: [ |
| 15 | + { "field.long": { name: "id" } }, |
| 16 | + { "field.int": { name: "qty", children: [{ "validator.numeric": { name: "r", "@min": 1 } }] } }, |
| 17 | + { "field.enum": { name: "status", "@values": ["OPEN", "CLOSED"] } }, |
| 18 | + { "source.rdb": { name: "src", "@table": "orders" } }, |
| 19 | + { "identity.primary": { name: "pk", "@fields": ["id"], "@generation": "increment" } }, |
| 20 | + ] }, |
| 21 | + }] }, |
| 22 | +}); |
| 23 | + |
| 24 | +describe("e2e: validator-derived checks in CREATE TABLE", () => { |
| 25 | + test("postgres CREATE TABLE inlines the numeric check exactly once", async () => { |
| 26 | + const expected = buildExpectedSchema(await load(ORDER), { dialect: "postgres" }); |
| 27 | + const { up } = emit(await collectChanges(expected), { dialect: "postgres" }); |
| 28 | + expect(up).toContain(`CONSTRAINT "orders_qty_numeric_chk" CHECK (qty >= 1)`); |
| 29 | + expect(up.split(`CHECK (qty >= 1)`).length - 1).toBe(1); |
| 30 | + }); |
| 31 | + test("enum + validator coexist with distinct names", async () => { |
| 32 | + const t = buildExpectedSchema(await load(ORDER), { dialect: "postgres" }).tables[0]!; |
| 33 | + const names = t.checks.map((c) => c.name).sort(); |
| 34 | + expect(names).toEqual(["orders_qty_numeric_chk", "orders_status_chk"]); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +// create-table against an empty actual yields a create-table carrying the checks |
| 39 | +async function collectChanges(expected: ReturnType<typeof buildExpectedSchema>) { |
| 40 | + const r = await diff({ expected, actual: { tables: [], views: [] } }); |
| 41 | + return r.changes; |
| 42 | +} |
0 commit comments