|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import { MetaDataLoader, InMemoryStringSource } from "@metaobjectsdev/metadata"; |
| 3 | +import { extractViewSpec } from "../../src/projection/extract-view-spec.js"; |
| 4 | +import { emitViewDdl } from "../../src/projection/view-ddl-emit.js"; |
| 5 | + |
| 6 | +/** |
| 7 | + * A projection field may RENAME what it exposes: |
| 8 | + * |
| 9 | + * { field.string: { name: bookingRef, extends: "Shipment.reference" } } |
| 10 | + * |
| 11 | + * The view's OUTPUT column is the projection field's (`booking_ref`); the column it |
| 12 | + * SELECTS is the extends TARGET's (`reference`). Deriving both from the projection |
| 13 | + * field emits `SELECT s.booking_ref` against a table whose column is `reference` — |
| 14 | + * a view SQLite accepts at CREATE and fails at first SELECT, Postgres rejects outright. |
| 15 | + * |
| 16 | + * Renaming base columns is one of only two things the authoring guidance says |
| 17 | + * genuinely forces a projection, so this is the construct's headline case. |
| 18 | + */ |
| 19 | +async function load(children: unknown[]) { |
| 20 | + const json = JSON.stringify({ "metadata.root": { package: "test", children } }); |
| 21 | + const result = await new MetaDataLoader().load([new InMemoryStringSource(json)]); |
| 22 | + if (result.errors.length > 0) { |
| 23 | + throw new Error(`Loader errors:\n${result.errors.map((e) => e.message).join("\n")}`); |
| 24 | + } |
| 25 | + return result.root; |
| 26 | +} |
| 27 | + |
| 28 | +const shipment = { |
| 29 | + "object.entity": { |
| 30 | + name: "Shipment", |
| 31 | + children: [ |
| 32 | + { "source.rdb": { "@table": "shipment" } }, |
| 33 | + { "field.int": { name: "id" } }, |
| 34 | + { "field.string": { name: "reference" } }, |
| 35 | + { "field.string": { name: "originCity" } }, |
| 36 | + { "identity.primary": { name: "pk", "@fields": "id" } }, |
| 37 | + ], |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +describe("extractViewSpec — extends-bound passthrough that RENAMES the base field", () => { |
| 42 | + test("sourceColumn comes from the extends target, dbColAlias from the projection field", async () => { |
| 43 | + const root = await load([ |
| 44 | + shipment, |
| 45 | + { |
| 46 | + "object.projection": { |
| 47 | + name: "ShipmentSummary", |
| 48 | + children: [ |
| 49 | + { "source.rdb": { "@kind": "view", "@view": "v_shipment_summary" } }, |
| 50 | + { "field.int": { name: "id", extends: "Shipment.id" } }, |
| 51 | + // THE RENAME: exposed as bookingRef, sourced from Shipment.reference. |
| 52 | + { "field.string": { name: "bookingRef", extends: "Shipment.reference" } }, |
| 53 | + { "identity.primary": { name: "pk", extends: "Shipment.pk" } }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }, |
| 57 | + ]); |
| 58 | + |
| 59 | + const projection = root.objects().find((o) => o.name === "ShipmentSummary")!; |
| 60 | + const spec = extractViewSpec(projection, root, { columnNamingStrategy: "snake_case" }); |
| 61 | + |
| 62 | + const booking = spec.selectSpec.columns.find((c) => c.fieldName === "bookingRef")!; |
| 63 | + expect(booking.kind).toBe("passthrough"); |
| 64 | + if (booking.kind === "passthrough") { |
| 65 | + // The view exposes it under the projection's own name… |
| 66 | + expect(booking.dbColAlias).toBe("booking_ref"); |
| 67 | + // …but SELECTs the base entity's real column. |
| 68 | + expect(booking.sourceColumn).toBe("reference"); |
| 69 | + } |
| 70 | + |
| 71 | + // An un-renamed field is unaffected. |
| 72 | + const id = spec.selectSpec.columns.find((c) => c.fieldName === "id")!; |
| 73 | + if (id.kind === "passthrough") { |
| 74 | + expect(id.dbColAlias).toBe("id"); |
| 75 | + expect(id.sourceColumn).toBe("id"); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + test("an explicit @column on the projection field names the VIEW column, not the base's", async () => { |
| 80 | + const root = await load([ |
| 81 | + shipment, |
| 82 | + { |
| 83 | + "object.projection": { |
| 84 | + name: "ShipmentSummary", |
| 85 | + children: [ |
| 86 | + { "source.rdb": { "@kind": "view", "@view": "v_shipment_summary" } }, |
| 87 | + { "field.int": { name: "id", extends: "Shipment.id" } }, |
| 88 | + { |
| 89 | + "field.string": { |
| 90 | + name: "bookingRef", |
| 91 | + extends: "Shipment.reference", |
| 92 | + "@column": "booking_code", |
| 93 | + }, |
| 94 | + }, |
| 95 | + { "identity.primary": { name: "pk", extends: "Shipment.pk" } }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + }, |
| 99 | + ]); |
| 100 | + |
| 101 | + const projection = root.objects().find((o) => o.name === "ShipmentSummary")!; |
| 102 | + const spec = extractViewSpec(projection, root, { columnNamingStrategy: "snake_case" }); |
| 103 | + const booking = spec.selectSpec.columns.find((c) => c.fieldName === "bookingRef")!; |
| 104 | + if (booking.kind === "passthrough") { |
| 105 | + expect(booking.dbColAlias).toBe("booking_code"); |
| 106 | + expect(booking.sourceColumn).toBe("reference"); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + test("a field-level extends onto an ABSTRACT (shape reuse) does not hijack the source column", async () => { |
| 111 | + const root = await load([ |
| 112 | + // A package-level abstract field reused for its shape, NOT a base column. |
| 113 | + { "field.string": { name: "Code24", abstract: true, "@maxLength": 24 } }, |
| 114 | + { |
| 115 | + "object.entity": { |
| 116 | + name: "Shipment", |
| 117 | + children: [ |
| 118 | + { "source.rdb": { "@table": "shipment" } }, |
| 119 | + { "field.int": { name: "id" } }, |
| 120 | + { "field.string": { name: "reference" } }, |
| 121 | + { "identity.primary": { name: "pk", "@fields": "id" } }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + "object.projection": { |
| 127 | + name: "ShipmentSummary", |
| 128 | + children: [ |
| 129 | + { "source.rdb": { "@kind": "view", "@view": "v_shipment_summary" } }, |
| 130 | + { "field.int": { name: "id", extends: "Shipment.id" } }, |
| 131 | + // Extends an abstract for SHAPE — the base column is its own name. |
| 132 | + { "field.string": { name: "reference", extends: "Code24" } }, |
| 133 | + { "identity.primary": { name: "pk", extends: "Shipment.pk" } }, |
| 134 | + ], |
| 135 | + }, |
| 136 | + }, |
| 137 | + ]); |
| 138 | + |
| 139 | + const projection = root.objects().find((o) => o.name === "ShipmentSummary")!; |
| 140 | + const spec = extractViewSpec(projection, root, { columnNamingStrategy: "snake_case" }); |
| 141 | + const ref = spec.selectSpec.columns.find((c) => c.fieldName === "reference")!; |
| 142 | + if (ref.kind === "passthrough") { |
| 143 | + expect(ref.dbColAlias).toBe("reference"); |
| 144 | + // NOT "code24" — the abstract is shape, not a base column. |
| 145 | + expect(ref.sourceColumn).toBe("reference"); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + test("end to end: the emitted CREATE VIEW selects the base column under the new alias", async () => { |
| 150 | + const root = await load([ |
| 151 | + shipment, |
| 152 | + { |
| 153 | + "object.projection": { |
| 154 | + name: "ShipmentSummary", |
| 155 | + children: [ |
| 156 | + { "source.rdb": { "@kind": "view", "@view": "v_shipment_summary" } }, |
| 157 | + { "field.int": { name: "id", extends: "Shipment.id" } }, |
| 158 | + { "field.string": { name: "bookingRef", extends: "Shipment.reference" } }, |
| 159 | + { "field.string": { name: "originCity", extends: "Shipment.originCity" } }, |
| 160 | + { "identity.primary": { name: "pk", extends: "Shipment.pk" } }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + }, |
| 164 | + ]); |
| 165 | + |
| 166 | + const projection = root.objects().find((o) => o.name === "ShipmentSummary")!; |
| 167 | + const spec = extractViewSpec(projection, root, { columnNamingStrategy: "snake_case" }); |
| 168 | + const sql = emitViewDdl(spec, { |
| 169 | + dialect: "sqlite", |
| 170 | + baseTableName: "shipment", |
| 171 | + joinTables: {}, |
| 172 | + }); |
| 173 | + |
| 174 | + // The rename is an ALIAS over the base column, not a second column name. |
| 175 | + expect(sql).toContain("s.reference AS booking_ref"); |
| 176 | + // And never the pre-fix form, which named a column `shipment` does not have. |
| 177 | + expect(sql).not.toContain("s.booking_ref"); |
| 178 | + expect(sql).toContain("s.origin_city AS origin_city"); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe("read-view HOST — the rename rule must NOT apply", () => { |
| 183 | + /** |
| 184 | + * A write-through entity's read-view reads its OWN table, so a passthrough always sources |
| 185 | + * from the field's own column. A host field may legally `extends` a SIBLING field of the |
| 186 | + * same entity for shape reuse — a dotted ref whose owner IS the base. Treating that as a |
| 187 | + * rename makes the view serve the sibling's data under this field's name: valid SQL, no |
| 188 | + * error, wrong rows. |
| 189 | + */ |
| 190 | + test("a host field extending a SIBLING field still selects its own column", async () => { |
| 191 | + const root = await load([ |
| 192 | + { |
| 193 | + "object.entity": { |
| 194 | + name: "Shipment", |
| 195 | + children: [ |
| 196 | + { "source.rdb": { "@kind": "table", "@table": "shipment", "@role": "primary" } }, |
| 197 | + { "source.rdb": { "@kind": "view", "@view": "v_shipment", "@role": "replica" } }, |
| 198 | + { "field.int": { name: "id" } }, |
| 199 | + { "field.string": { name: "reference" } }, |
| 200 | + // Shape reuse off a sibling — NOT a rename of it. |
| 201 | + { "field.string": { name: "altReference", extends: "Shipment.reference" } }, |
| 202 | + { "identity.primary": { name: "pk", "@fields": "id" } }, |
| 203 | + ], |
| 204 | + }, |
| 205 | + }, |
| 206 | + ]); |
| 207 | + |
| 208 | + const host = root.objects().find((o) => o.name === "Shipment")!; |
| 209 | + const spec = extractViewSpec(host, root, { columnNamingStrategy: "snake_case" }); |
| 210 | + |
| 211 | + const alt = spec.selectSpec.columns.find((c) => c.fieldName === "altReference")!; |
| 212 | + expect(alt.kind).toBe("passthrough"); |
| 213 | + if (alt.kind === "passthrough") { |
| 214 | + expect(alt.dbColAlias).toBe("alt_reference"); |
| 215 | + // Its OWN column — not the sibling's, which is a real separate column on the table. |
| 216 | + expect(alt.sourceColumn).toBe("alt_reference"); |
| 217 | + } |
| 218 | + |
| 219 | + const sql = emitViewDdl(spec, { |
| 220 | + dialect: "sqlite", |
| 221 | + baseTableName: "shipment", |
| 222 | + joinTables: {}, |
| 223 | + }); |
| 224 | + expect(sql).toContain("s.alt_reference AS alt_reference"); |
| 225 | + expect(sql).toContain("s.reference AS reference"); |
| 226 | + }); |
| 227 | +}); |
0 commit comments