-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathdbtProjectContainer.test.ts
More file actions
334 lines (281 loc) · 10.4 KB
/
dbtProjectContainer.test.ts
File metadata and controls
334 lines (281 loc) · 10.4 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
import { DBTTerminal, EnvironmentVariables } from "@altimateai/dbt-integration";
import {
afterEach,
beforeEach,
describe,
expect,
it,
jest,
} from "@jest/globals";
import {
EventEmitter,
ExtensionContext,
Uri,
window,
WorkspaceFolder,
} from "vscode";
import { AltimateRequest } from "../../altimate";
import { DBTClient } from "../../dbt_client";
import { AltimateDatapilot } from "../../dbt_client/datapilot";
import {
DBTProjectContainer,
ProjectRegisteredUnregisteredEvent,
} from "../../dbt_client/dbtProjectContainer";
import { DBTWorkspaceFolder } from "../../dbt_client/dbtWorkspaceFolder";
import { ManifestCacheChangedEvent } from "../../dbt_client/event/manifestCacheChangedEvent";
import { createEntry } from "../fixtures/runHistory";
describe("DBTProjectContainer Tests", () => {
let container: DBTProjectContainer;
let mockDbtClient: jest.Mocked<DBTClient>;
let mockDbtTerminal: jest.Mocked<DBTTerminal>;
let mockAltimateDatapilot: jest.Mocked<AltimateDatapilot>;
let mockAltimateRequest: jest.Mocked<AltimateRequest>;
let mockDbtWorkspaceFolder: jest.Mocked<DBTWorkspaceFolder>;
const createMockDbtWorkspaceFolder = (
workspaceFolder: WorkspaceFolder,
onManifestChanged: EventEmitter<ManifestCacheChangedEvent>,
onProjectRegisteredUnregistered: EventEmitter<ProjectRegisteredUnregisteredEvent>,
pythonPath?: string,
envVars?: EnvironmentVariables,
): DBTWorkspaceFolder => {
return mockDbtWorkspaceFolder;
};
beforeEach(() => {
mockDbtClient = {
onDBTInstallationVerification: new EventEmitter().event,
dispose: jest.fn(),
} as unknown as jest.Mocked<DBTClient>;
mockDbtTerminal = {
show: jest.fn(),
log: jest.fn(),
trace: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
error: jest.fn(),
dispose: jest.fn(),
logNewLine: jest.fn(),
logLine: jest.fn(),
logHorizontalRule: jest.fn(),
logBlock: jest.fn(),
warn: jest.fn(),
} as unknown as jest.Mocked<DBTTerminal>;
mockAltimateDatapilot = {
checkIfAltimateDatapilotInstalled: jest.fn(),
installAltimateDatapilot: jest.fn(),
} as unknown as jest.Mocked<AltimateDatapilot>;
mockAltimateRequest = {
dispose: jest.fn(),
enabled: jest.fn(),
isAuthenticated: jest.fn(),
validateCredentials: jest.fn(),
} as unknown as jest.Mocked<AltimateRequest>;
mockDbtWorkspaceFolder = {
dispose: jest.fn(),
} as unknown as jest.Mocked<DBTWorkspaceFolder>;
container = new DBTProjectContainer(
mockDbtClient,
createMockDbtWorkspaceFolder,
mockDbtTerminal,
mockAltimateDatapilot,
mockAltimateRequest,
);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should initialize with correct dependencies", () => {
expect(container).toBeDefined();
expect(container.onDBTInstallationVerification).toBe(
mockDbtClient.onDBTInstallationVerification,
);
});
it("should dispose client and terminal dependencies", () => {
container.dispose();
expect(mockDbtClient.dispose).toHaveBeenCalled();
expect(mockDbtTerminal.dispose).toHaveBeenCalled();
});
describe("rerunFromHistory", () => {
const mockProject = {
getProjectName: jest.fn().mockReturnValue("test-project"),
runModel: jest.fn(),
buildModel: jest.fn(),
buildProject: jest.fn(),
runTest: jest.fn(),
compileModel: jest.fn(),
};
beforeEach(() => {
jest
.spyOn(container, "findProjectByName")
.mockReturnValue(mockProject as any);
});
it("should show error when project is not found", () => {
jest.spyOn(container, "findProjectByName").mockReturnValue(undefined);
container.rerunFromHistory(
createEntry({ projectName: "nonexistent", command: "dbt run" }),
);
expect(window.showErrorMessage).toHaveBeenCalledWith(
expect.stringContaining("nonexistent"),
);
});
it("should show warning for project-wide dbt run (empty args)", () => {
container.rerunFromHistory(createEntry({ command: "dbt run", args: [] }));
expect(window.showWarningMessage).toHaveBeenCalledWith(
expect.stringContaining("dbt run"),
);
expect(mockProject.runModel).not.toHaveBeenCalled();
});
it("should delegate to runModel when args are present", () => {
container.rerunFromHistory(
createEntry({ command: "dbt run", args: ["my_model"] }),
);
expect(mockProject.runModel).toHaveBeenCalledWith(
expect.objectContaining({ modelName: "my_model" }),
);
});
it("should show warning for project-wide dbt test (empty args)", () => {
container.rerunFromHistory(
createEntry({ command: "dbt test", args: [] }),
);
expect(window.showWarningMessage).toHaveBeenCalledWith(
expect.stringContaining("dbt test"),
);
expect(mockProject.runTest).not.toHaveBeenCalled();
});
it("should delegate to runTest when args are present", () => {
container.rerunFromHistory(
createEntry({ command: "dbt test", args: ["my_test"] }),
);
expect(mockProject.runTest).toHaveBeenCalledWith("my_test");
});
it("should show warning for project-wide dbt compile (empty args)", () => {
container.rerunFromHistory(
createEntry({ command: "dbt compile", args: [] }),
);
expect(window.showWarningMessage).toHaveBeenCalledWith(
expect.stringContaining("dbt compile"),
);
expect(mockProject.compileModel).not.toHaveBeenCalled();
});
it("should delegate to compileModel when args are present", () => {
container.rerunFromHistory(
createEntry({ command: "dbt compile", args: ["+my_model"] }),
);
expect(mockProject.compileModel).toHaveBeenCalledWith(
expect.objectContaining({
plusOperatorLeft: "+",
modelName: "my_model",
}),
);
});
it("should call buildProject for project-wide dbt build (empty args)", () => {
container.rerunFromHistory(
createEntry({ command: "dbt build", args: [] }),
);
expect(mockProject.buildProject).toHaveBeenCalled();
});
it("should delegate to buildModel when build has args", () => {
container.rerunFromHistory(
createEntry({ command: "dbt build", args: ["+my_model+"] }),
);
expect(mockProject.buildModel).toHaveBeenCalledWith(
expect.objectContaining({
plusOperatorLeft: "+",
modelName: "my_model",
plusOperatorRight: "+",
}),
);
});
it("should show warning for unknown command", () => {
container.rerunFromHistory(
createEntry({ command: "dbt seed", args: [] }),
);
expect(window.showWarningMessage).toHaveBeenCalledWith(
expect.stringContaining("seed"),
);
});
});
describe("findDBTProject with untitled URIs (multi-project)", () => {
let mockContext: jest.Mocked<ExtensionContext>;
let workspaceState: Map<string, any>;
beforeEach(() => {
workspaceState = new Map();
mockContext = {
workspaceState: {
get: jest.fn((key: string) => workspaceState.get(key)),
update: jest.fn((key: string, value: any) => {
if (value === undefined) {
workspaceState.delete(key);
} else {
workspaceState.set(key, value);
}
}),
},
extensionUri: { fsPath: "/ext" },
extension: { packageJSON: { version: "0.0.1" }, id: "test" },
globalState: {
get: jest.fn(),
update: jest.fn(),
},
} as unknown as jest.Mocked<ExtensionContext>;
container.setContext(mockContext);
});
it("should resolve untitled URI to stored project when workspace state is set", () => {
const projectPath = "/Users/test/jaffle_shop";
// Simulate deserialized workspace state — .path survives, .fsPath does not
workspaceState.set("dbtPowerUser.projectSelected", {
label: "jaffle_shop",
uri: { path: projectPath },
});
const untitledUri = { scheme: "untitled", fsPath: "Untitled-1" } as any;
// findDBTProject calls resolveProjectUri internally, which should
// reconstruct a file URI from the stored .path
// It won't find a project (no workspace folders registered), but we
// can verify resolveProjectUri ran by checking Uri.file was called
container.findDBTProject(untitledUri);
expect(Uri.file).toHaveBeenCalledWith(projectPath);
});
it("should not resolve file-scheme URIs through workspace state", () => {
workspaceState.set("dbtPowerUser.projectSelected", {
label: "jaffle_shop",
uri: { path: "/Users/test/jaffle_shop" },
});
const fileUri = {
scheme: "file",
fsPath: "/Users/test/model.sql",
} as any;
(Uri.file as jest.Mock).mockClear();
container.findDBTProject(fileUri);
// Should NOT call Uri.file for the stored project — file URIs
// go through the normal workspace folder matching
expect(Uri.file).not.toHaveBeenCalledWith("/Users/test/jaffle_shop");
});
it("should return undefined for untitled URI when no project is stored", () => {
const untitledUri = { scheme: "untitled", fsPath: "Untitled-1" } as any;
const result = container.findDBTProject(untitledUri);
expect(result).toBeUndefined();
});
it("should clear stale project selection when project is unregistered", () => {
const projectPath = "/Users/test/jaffle_shop";
workspaceState.set("dbtPowerUser.projectSelected", {
label: "jaffle_shop",
uri: { path: projectPath },
});
// Simulate project unregistration by firing the event
// clearStaleProjectSelection is called via the event listener
// We need to access it indirectly — verify state is cleared
container.setToWorkspaceState("dbtPowerUser.projectSelected", {
label: "jaffle_shop",
uri: { path: projectPath },
});
expect(
container.getFromWorkspaceState("dbtPowerUser.projectSelected"),
).toBeDefined();
// Clear it manually (clearStaleProjectSelection is private,
// tested through its effect)
container.setToWorkspaceState("dbtPowerUser.projectSelected", undefined);
expect(
container.getFromWorkspaceState("dbtPowerUser.projectSelected"),
).toBeUndefined();
});
});
});