|
1 | 1 | import { describe, test, expect } from "bun:test" |
2 | | - |
3 | | -// Copy of findDownstream and formatImpactReport from |
4 | | -// src/altimate/tools/impact-analysis.ts (not exported, tested standalone) |
5 | | - |
6 | | -interface DownstreamModel { |
7 | | - name: string |
8 | | - depth: number |
9 | | - materialized?: string |
10 | | - path: string[] |
11 | | -} |
12 | | - |
13 | | -function findDownstream( |
14 | | - targetName: string, |
15 | | - models: Array<{ name: string; depends_on: string[]; materialized?: string }>, |
16 | | -): DownstreamModel[] { |
17 | | - const results: DownstreamModel[] = [] |
18 | | - const visited = new Set<string>() |
19 | | - |
20 | | - function walk(name: string, depth: number, path: string[]) { |
21 | | - for (const model of models) { |
22 | | - if (visited.has(model.name)) continue |
23 | | - const deps = model.depends_on.map((d) => d.split(".").pop()) |
24 | | - if (deps.includes(name)) { |
25 | | - visited.add(model.name) |
26 | | - const newPath = [...path, model.name] |
27 | | - results.push({ |
28 | | - name: model.name, |
29 | | - depth, |
30 | | - materialized: model.materialized, |
31 | | - path: newPath, |
32 | | - }) |
33 | | - walk(model.name, depth + 1, newPath) |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - walk(targetName, 1, [targetName]) |
39 | | - return results |
40 | | -} |
41 | | - |
42 | | -function formatImpactReport(data: { |
43 | | - model: string |
44 | | - column?: string |
45 | | - changeType: string |
46 | | - direct: DownstreamModel[] |
47 | | - transitive: DownstreamModel[] |
48 | | - affectedTestCount: number |
49 | | - columnImpact: string[] |
50 | | - totalModels: number |
51 | | -}): string { |
52 | | - const lines: string[] = [] |
53 | | - const target = data.column ? `${data.model}.${data.column}` : data.model |
54 | | - lines.push(`Impact Analysis: ${data.changeType.toUpperCase()} ${target}`) |
55 | | - lines.push("".padEnd(60, "=")) |
56 | | - |
57 | | - const totalAffected = data.direct.length + data.transitive.length |
58 | | - const pct = data.totalModels > 0 ? ((totalAffected / data.totalModels) * 100).toFixed(1) : "0" |
59 | | - lines.push(`Blast radius: ${totalAffected}/${data.totalModels} models (${pct}%)`) |
60 | | - lines.push("") |
61 | | - |
62 | | - if (data.changeType === "remove" && totalAffected > 0) { |
63 | | - lines.push("WARNING: This is a BREAKING change. All downstream models will fail.") |
64 | | - lines.push("") |
65 | | - } else if (data.changeType === "rename" && totalAffected > 0) { |
66 | | - lines.push("WARNING: Rename requires updating all downstream references.") |
67 | | - lines.push("") |
68 | | - } else if (data.changeType === "retype" && totalAffected > 0) { |
69 | | - lines.push("CAUTION: Type change may cause implicit casts or failures in downstream models.") |
70 | | - lines.push("") |
71 | | - } |
72 | | - |
73 | | - if (data.direct.length > 0) { |
74 | | - lines.push(`Direct Dependents (${data.direct.length})`) |
75 | | - lines.push("".padEnd(40, "-")) |
76 | | - for (const d of data.direct) { |
77 | | - const mat = d.materialized ? ` [${d.materialized}]` : "" |
78 | | - lines.push(` ${d.name}${mat}`) |
79 | | - } |
80 | | - lines.push("") |
81 | | - } |
82 | | - |
83 | | - if (data.transitive.length > 0) { |
84 | | - lines.push(`Transitive Dependents (${data.transitive.length})`) |
85 | | - lines.push("".padEnd(40, "-")) |
86 | | - for (const d of data.transitive) { |
87 | | - const mat = d.materialized ? ` [${d.materialized}]` : "" |
88 | | - const path = d.path.join(" \u2192 ") |
89 | | - lines.push(` ${d.name}${mat} (via: ${path})`) |
90 | | - } |
91 | | - lines.push("") |
92 | | - } |
93 | | - |
94 | | - if (data.column && data.columnImpact.length > 0) { |
95 | | - lines.push(`Affected Output Columns (${data.columnImpact.length})`) |
96 | | - lines.push("".padEnd(40, "-")) |
97 | | - for (const col of data.columnImpact) { |
98 | | - lines.push(` ${col}`) |
99 | | - } |
100 | | - lines.push("") |
101 | | - } |
102 | | - |
103 | | - if (data.affectedTestCount > 0) { |
104 | | - lines.push(`Tests in project: ${data.affectedTestCount}`) |
105 | | - lines.push("".padEnd(40, "-")) |
106 | | - lines.push(` Run \`dbt test\` to verify all ${data.affectedTestCount} tests still pass after this change.`) |
107 | | - lines.push("") |
108 | | - } |
109 | | - |
110 | | - if (totalAffected === 0) { |
111 | | - lines.push("No downstream models depend on this. Change is safe to make.") |
112 | | - } |
113 | | - |
114 | | - if (totalAffected > 0) { |
115 | | - lines.push("Recommended Actions") |
116 | | - lines.push("".padEnd(40, "-")) |
117 | | - if (data.changeType === "remove") { |
118 | | - lines.push("1. Update all downstream models to remove references") |
119 | | - lines.push("2. Run `dbt test` to verify no broken references") |
120 | | - lines.push("3. Consider deprecation period before removal") |
121 | | - } else if (data.changeType === "rename") { |
122 | | - lines.push("1. Update all downstream SQL references to new name") |
123 | | - lines.push("2. Run `dbt compile` to verify all models compile") |
124 | | - lines.push("3. Run `dbt test` to verify correctness") |
125 | | - } else if (data.changeType === "retype") { |
126 | | - lines.push("1. Check downstream models for implicit type casts") |
127 | | - lines.push("2. Verify aggregations and joins still work correctly") |
128 | | - lines.push("3. Run `dbt test` with data validation") |
129 | | - } else { |
130 | | - lines.push("1. Review downstream models for compatibility") |
131 | | - lines.push("2. Run `dbt compile` and `dbt test`") |
132 | | - } |
133 | | - } |
134 | | - |
135 | | - return lines.join("\n") |
136 | | -} |
| 2 | +import { findDownstream, formatImpactReport } from "../../../src/altimate/tools/impact-analysis" |
| 3 | +import type { DownstreamModel } from "../../../src/altimate/tools/impact-analysis" |
137 | 4 |
|
138 | 5 | describe("findDownstream: DAG traversal", () => { |
139 | 6 | test("returns empty for leaf model with no dependents", () => { |
|
0 commit comments