-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathdbt-helpers.test.ts
More file actions
360 lines (305 loc) · 12 KB
/
dbt-helpers.test.ts
File metadata and controls
360 lines (305 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* Direct unit tests for dbt native helper functions in
* src/altimate/native/dbt/helpers.ts.
*
* These pure functions power dbt-lineage, dbt-unit-test-gen, and
* dbt-manifest handlers. Previously only tested indirectly through
* dbtLineage() in dbt-lineage-helpers.test.ts. Direct tests catch
* regressions in isolation: a broken findModel or detectDialect
* silently degrades multiple downstream tools.
*/
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
import * as fs from "fs"
import * as path from "path"
import * as os from "os"
import {
loadRawManifest,
findModel,
getUniqueId,
detectDialect,
buildSchemaContext,
extractColumns,
listModelNames,
} from "../../src/altimate/native/dbt/helpers"
// ---------- findModel ----------
describe("findModel", () => {
const nodes: Record<string, any> = {
"model.project.orders": { resource_type: "model", name: "orders" },
"model.project.users": { resource_type: "model", name: "users" },
"source.project.raw.events": { resource_type: "source", name: "events" },
"test.project.not_null": { resource_type: "test", name: "not_null" },
}
test("finds model by exact unique_id key", () => {
expect(findModel(nodes, "model.project.orders")).toEqual(nodes["model.project.orders"])
})
test("finds model by name when unique_id does not match", () => {
expect(findModel(nodes, "users")).toEqual(nodes["model.project.users"])
})
test("returns null for source nodes (not resource_type=model)", () => {
expect(findModel(nodes, "events")).toBeNull()
})
test("returns null for nonexistent model", () => {
expect(findModel(nodes, "nonexistent")).toBeNull()
})
test("returns null for empty nodes", () => {
expect(findModel({}, "orders")).toBeNull()
})
test("returns a model when multiple models share the same name", () => {
const dupes: Record<string, any> = {
"model.a.orders": { resource_type: "model", name: "orders" },
"model.b.orders": { resource_type: "model", name: "orders" },
}
const result = findModel(dupes, "orders")
expect(result).not.toBeNull()
expect(result.resource_type).toBe("model")
})
})
// ---------- getUniqueId ----------
describe("getUniqueId", () => {
const nodes: Record<string, any> = {
"model.project.orders": { resource_type: "model", name: "orders" },
"source.project.raw.events": { resource_type: "source", name: "events" },
}
test("returns key when exact unique_id exists and is a model", () => {
expect(getUniqueId(nodes, "model.project.orders")).toBe("model.project.orders")
})
test("returns unique_id when looked up by name", () => {
expect(getUniqueId(nodes, "orders")).toBe("model.project.orders")
})
test("returns undefined for source node (not resource_type=model)", () => {
expect(getUniqueId(nodes, "events")).toBeUndefined()
})
test("returns undefined for nonexistent model", () => {
expect(getUniqueId(nodes, "nonexistent")).toBeUndefined()
})
test("does not match test nodes by name", () => {
const nodesWithTest: Record<string, any> = {
...nodes,
"test.project.not_null": { resource_type: "test", name: "not_null" },
}
expect(getUniqueId(nodesWithTest, "not_null")).toBeUndefined()
})
test("does not match seed nodes by name", () => {
const nodesWithSeed: Record<string, any> = {
...nodes,
"seed.project.country_codes": { resource_type: "seed", name: "country_codes" },
}
expect(getUniqueId(nodesWithSeed, "country_codes")).toBeUndefined()
})
test("does not match by unique_id if resource_type is not model", () => {
expect(getUniqueId(nodes, "source.project.raw.events")).toBeUndefined()
})
})
// ---------- detectDialect ----------
describe("detectDialect", () => {
test("maps known adapter types to dialect strings", () => {
const cases: Array<[string, string]> = [
["snowflake", "snowflake"],
["bigquery", "bigquery"],
["databricks", "databricks"],
["spark", "spark"],
["postgres", "postgres"],
["redshift", "redshift"],
["duckdb", "duckdb"],
["clickhouse", "clickhouse"],
["mysql", "mysql"],
["sqlserver", "tsql"],
["trino", "trino"],
]
for (const [adapter, expected] of cases) {
expect(detectDialect({ metadata: { adapter_type: adapter } })).toBe(expected)
}
})
test("returns unmapped adapter type verbatim (truthy passthrough)", () => {
expect(detectDialect({ metadata: { adapter_type: "athena" } })).toBe("athena")
})
test("defaults to 'snowflake' when no metadata", () => {
expect(detectDialect({})).toBe("snowflake")
})
test("defaults to 'snowflake' when adapter_type is empty string", () => {
expect(detectDialect({ metadata: { adapter_type: "" } })).toBe("snowflake")
})
test("defaults to 'snowflake' when metadata is null", () => {
expect(detectDialect({ metadata: null })).toBe("snowflake")
})
})
// ---------- buildSchemaContext ----------
describe("buildSchemaContext", () => {
const nodes: Record<string, any> = {
"model.project.upstream_a": {
resource_type: "model",
name: "upstream_a",
alias: "upstream_alias",
columns: {
id: { name: "id", data_type: "INTEGER" },
name: { name: "name", data_type: "VARCHAR" },
},
},
"model.project.upstream_b": {
resource_type: "model",
name: "upstream_b",
columns: {},
},
}
const sources: Record<string, any> = {
"source.project.raw.events": {
name: "events",
columns: {
event_id: { name: "event_id", data_type: "BIGINT" },
},
},
}
test("builds schema context using alias over name", () => {
const result = buildSchemaContext(nodes, sources, ["model.project.upstream_a"])
expect(result).not.toBeNull()
expect(result!.version).toBe("1")
// Alias takes precedence over name
expect(result!.tables["upstream_alias"]).toBeDefined()
expect(result!.tables["upstream_alias"].columns).toHaveLength(2)
// Name key must NOT exist when alias is present
expect(result!.tables["upstream_a"]).toBeUndefined()
})
test("skips upstream models with empty columns", () => {
const result = buildSchemaContext(nodes, sources, ["model.project.upstream_b"])
expect(result).toBeNull()
})
test("resolves upstream IDs from sources", () => {
const result = buildSchemaContext(nodes, sources, ["source.project.raw.events"])
expect(result).not.toBeNull()
expect(result!.tables["events"]).toBeDefined()
expect(result!.tables["events"].columns).toEqual([
{ name: "event_id", type: "BIGINT" },
])
})
test("returns null when no upstream IDs provided", () => {
expect(buildSchemaContext(nodes, sources, [])).toBeNull()
})
test("returns null when upstream IDs do not resolve", () => {
expect(buildSchemaContext(nodes, sources, ["model.project.ghost"])).toBeNull()
})
})
// ---------- extractColumns ----------
describe("extractColumns", () => {
test("extracts column with data_type and description", () => {
const dict = {
id: { name: "id", data_type: "INTEGER", description: "Primary key" },
}
const cols = extractColumns(dict)
expect(cols).toHaveLength(1)
expect(cols[0]).toEqual({ name: "id", data_type: "INTEGER", description: "Primary key" })
})
test("falls back to 'type' field when data_type is missing", () => {
const dict = {
name: { name: "name", type: "VARCHAR" },
}
const cols = extractColumns(dict)
expect(cols).toHaveLength(1)
expect(cols[0].name).toBe("name")
expect(cols[0].data_type).toBe("VARCHAR")
expect(cols[0].description).toBeUndefined()
})
test("uses dict key as column name when col.name is missing", () => {
const dict = { amount: { data_type: "DECIMAL" } }
const cols = extractColumns(dict)
expect(cols[0].name).toBe("amount")
})
test("returns empty array for empty dict", () => {
expect(extractColumns({})).toEqual([])
})
test("handles both name and type fallbacks simultaneously", () => {
const dict = {
my_col: { type: "TEXT" },
}
const result = extractColumns(dict)
expect(result[0].name).toBe("my_col")
expect(result[0].data_type).toBe("TEXT")
expect(result[0].description).toBeUndefined()
})
})
// ---------- listModelNames ----------
describe("listModelNames", () => {
test("returns only model names, excluding sources and tests", () => {
const nodes: Record<string, any> = {
"model.p.a": { resource_type: "model", name: "alpha" },
"source.p.b": { resource_type: "source", name: "beta" },
"model.p.c": { resource_type: "model", name: "gamma" },
"test.p.d": { resource_type: "test", name: "delta" },
}
const names = listModelNames(nodes)
expect(names).toEqual(["alpha", "gamma"])
})
test("returns empty array for no models", () => {
expect(listModelNames({})).toEqual([])
})
})
// ---------- loadRawManifest ----------
describe("loadRawManifest", () => {
let tmpDir: string
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "dbt-helpers-test-"))
})
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true })
})
test("returns null for non-existent file", () => {
expect(loadRawManifest(path.join(tmpDir, "nonexistent.json"))).toBeNull()
})
test("parses valid manifest file", () => {
const manifestPath = path.join(tmpDir, "manifest.json")
fs.writeFileSync(manifestPath, JSON.stringify({ nodes: {}, metadata: { adapter_type: "snowflake" } }))
const result = loadRawManifest(manifestPath)
expect(result).not.toBeNull()
expect(result.metadata.adapter_type).toBe("snowflake")
})
test("throws on invalid JSON", () => {
const manifestPath = path.join(tmpDir, "bad.json")
fs.writeFileSync(manifestPath, "not json {{{")
expect(() => loadRawManifest(manifestPath)).toThrow()
})
test("throws when manifest is a primitive (not an object)", () => {
// typeof 42 === "number", triggers the non-object guard
const manifestPath = path.join(tmpDir, "number.json")
fs.writeFileSync(manifestPath, "42")
expect(() => loadRawManifest(manifestPath)).toThrow("Manifest is not a JSON object")
})
test("caches by path+mtime (same reference returned)", () => {
const manifestPath = path.join(tmpDir, "cached.json")
fs.writeFileSync(manifestPath, JSON.stringify({ v: 1 }))
const first = loadRawManifest(manifestPath)
const second = loadRawManifest(manifestPath)
// Same object reference from cache
expect(first).toBe(second)
})
test("invalidates cache when file content is rewritten", () => {
const manifestPath = path.join(tmpDir, "updated.json")
fs.writeFileSync(manifestPath, JSON.stringify({ v: 1 }))
const first = loadRawManifest(manifestPath)
// Rewrite with bumped mtime to guarantee cache invalidation.
// Some filesystems have 1-second mtime granularity, so we
// explicitly set a future mtime.
fs.writeFileSync(manifestPath, JSON.stringify({ v: 2 }))
const futureMs = Date.now() / 1000 + 5
fs.utimesSync(manifestPath, futureMs, futureMs)
const second = loadRawManifest(manifestPath)
expect(second.v).toBe(2)
})
test("does not throw for JSON array (typeof [] is 'object')", () => {
// typeof [] === "object" in JS, so arrays pass the guard.
// Known edge case — callers handle gracefully since .nodes is undefined on arrays.
const manifestPath = path.join(tmpDir, "array-manifest.json")
fs.writeFileSync(manifestPath, "[1, 2, 3]")
const result = loadRawManifest(manifestPath)
expect(Array.isArray(result)).toBe(true)
})
test("resolves symlinks before caching", () => {
const realPath = path.join(tmpDir, "real-manifest.json")
const symPath = path.join(tmpDir, "link-manifest.json")
const data = { metadata: {}, nodes: { sym: true } }
fs.writeFileSync(realPath, JSON.stringify(data))
fs.symlinkSync(realPath, symPath)
const viaReal = loadRawManifest(realPath)
const viaSym = loadRawManifest(symPath)
expect(viaSym).toEqual(viaReal)
expect(viaSym.nodes.sym).toBe(true)
})
})