|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import { DBTCoreProjectIntegration } from "../../dbt_client/dbtCoreIntegration"; |
| 4 | +import { Container } from "inversify"; |
| 5 | +import { DBTTerminal } from "../../dbt_client/dbtTerminal"; |
| 6 | +import { |
| 7 | + DBTCommandExecutionInfrastructure, |
| 8 | + PythonDBTCommandExecutionStrategy, |
| 9 | +} from "../../dbt_client/dbtIntegration"; |
| 10 | +import { PythonBridge } from "python-bridge"; |
| 11 | +import { PythonEnvironment } from "../../manifest/pythonEnvironment"; |
| 12 | +import { TelemetryService } from "../../telemetry"; |
| 13 | +import { DBTProjectContainer } from "../../manifest/dbtProjectContainer"; |
| 14 | +import { AltimateRequest } from "../../altimate"; |
| 15 | +import { ValidationProvider } from "../../validation_provider"; |
| 16 | +import { DeferToProdService } from "../../services/deferToProdService"; |
| 17 | +import { Uri, languages, EventEmitter } from "vscode"; |
| 18 | + |
| 19 | +suite("DBTCoreProjectIntegration Tests", () => { |
| 20 | + let sandbox: sinon.SinonSandbox; |
| 21 | + let dbtCoreProjectIntegration: DBTCoreProjectIntegration; |
| 22 | + let mockPythonBridge: any; |
| 23 | + |
| 24 | + setup(() => { |
| 25 | + sandbox = sinon.createSandbox(); |
| 26 | + // Create mock dependencies |
| 27 | + const container = new Container(); |
| 28 | + |
| 29 | + // Mock all required dependencies |
| 30 | + mockPythonBridge = { |
| 31 | + lock: sandbox.stub(), |
| 32 | + }; |
| 33 | + |
| 34 | + const mockExecutionInfrastructure = { |
| 35 | + createPythonBridge: () => mockPythonBridge, |
| 36 | + createQueue: () => {}, |
| 37 | + }; |
| 38 | + |
| 39 | + const mockUri = Uri.file("/test/project/root"); |
| 40 | + const mockDiagnosticCollection = |
| 41 | + languages.createDiagnosticCollection("dbt-project-config"); |
| 42 | + |
| 43 | + // Create mock event emitter for Python environment changes |
| 44 | + const mockPythonEnvChangeEmitter = new EventEmitter<Uri | undefined>(); |
| 45 | + |
| 46 | + // Create the instance directly with constructor injection |
| 47 | + dbtCoreProjectIntegration = new DBTCoreProjectIntegration( |
| 48 | + mockExecutionInfrastructure as any, |
| 49 | + { |
| 50 | + onPythonEnvironmentChanged: mockPythonEnvChangeEmitter.event, |
| 51 | + pythonPath: "/usr/bin/python3", |
| 52 | + environmentVariables: {}, |
| 53 | + } as any, |
| 54 | + {} as TelemetryService, |
| 55 | + {} as PythonDBTCommandExecutionStrategy, |
| 56 | + {} as DBTProjectContainer, |
| 57 | + {} as AltimateRequest, |
| 58 | + { |
| 59 | + debug: () => {}, |
| 60 | + error: () => {}, |
| 61 | + log: () => {}, |
| 62 | + } as any, |
| 63 | + {} as ValidationProvider, |
| 64 | + {} as DeferToProdService, |
| 65 | + mockUri, |
| 66 | + mockDiagnosticCollection, |
| 67 | + ); |
| 68 | + |
| 69 | + (dbtCoreProjectIntegration as any).python = mockPythonBridge; |
| 70 | + }); |
| 71 | + |
| 72 | + teardown(() => { |
| 73 | + sandbox.restore(); |
| 74 | + }); |
| 75 | + |
| 76 | + test("validateSql should return validation result for valid SQL", async () => { |
| 77 | + // Arrange |
| 78 | + const query = "SELECT * FROM my_table"; |
| 79 | + const dialect = "postgres"; |
| 80 | + const models = {}; |
| 81 | + const expectedResult = { |
| 82 | + valid: true, |
| 83 | + errors: [], |
| 84 | + }; |
| 85 | + |
| 86 | + mockPythonBridge.lock.resolves(expectedResult); |
| 87 | + |
| 88 | + // Act |
| 89 | + const result = await dbtCoreProjectIntegration.validateSql( |
| 90 | + query, |
| 91 | + dialect, |
| 92 | + models, |
| 93 | + ); |
| 94 | + |
| 95 | + // Assert |
| 96 | + assert.deepStrictEqual(result, expectedResult); |
| 97 | + assert.ok(mockPythonBridge.lock.calledOnce); |
| 98 | + const lockCall = mockPythonBridge.lock.getCall(0); |
| 99 | + assert.ok(lockCall.args[0].toString().includes("validate_sql")); |
| 100 | + }); |
| 101 | + |
| 102 | + test("validateSql should handle invalid SQL", async () => { |
| 103 | + const query = "SELECT * FREM my_table"; // Intentional typo |
| 104 | + const dialect = "postgres"; |
| 105 | + const models = {}; |
| 106 | + const expectedResult = { |
| 107 | + valid: false, |
| 108 | + errors: [ |
| 109 | + { |
| 110 | + message: 'syntax error at or near "FREM"', |
| 111 | + line: 1, |
| 112 | + column: 8, |
| 113 | + }, |
| 114 | + ], |
| 115 | + }; |
| 116 | + |
| 117 | + mockPythonBridge.lock.resolves(expectedResult); |
| 118 | + |
| 119 | + // Act |
| 120 | + const result = await dbtCoreProjectIntegration.validateSql( |
| 121 | + query, |
| 122 | + dialect, |
| 123 | + models, |
| 124 | + ); |
| 125 | + |
| 126 | + // Assert |
| 127 | + assert.deepStrictEqual(result, expectedResult); |
| 128 | + assert.ok(mockPythonBridge.lock.calledOnce); |
| 129 | + }); |
| 130 | +}); |
0 commit comments