Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-Carneiro committed Feb 20, 2025
1 parent 65adcd9 commit b40ff0a
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/commands/dataSourceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ export async function runDataSource(
} else if (!success) {
res = res.errorMsg ? res.errorMsg : res.error;
}
const connVersion = selectedConnection.insightsVersion ?? 0;
await writeQueryResultsToView(
res,
query,
Expand All @@ -229,11 +228,10 @@ export async function runDataSource(
`[DATASOURCE] Results is a string with length: ${res.length}`,
"INFO",
);
} else {
if (res.errorMsg) {
res = res.errorMsg;
}
} else if (res.errorMsg) {
res = res.errorMsg;
}

await writeQueryResultsToConsole(
res,
query,
Expand Down Expand Up @@ -315,7 +313,7 @@ export async function runApiDataSource(
const results = handleWSResults(apiCall.arrayBuffer);
return handleScratchpadTableRes(results);
} else {
return { error: "API call failed" };
return { error: "Datasource API call failed" };
}
}

Expand Down Expand Up @@ -426,7 +424,7 @@ export async function runQsqlDataSource(
const results = handleWSResults(qsqlCall.arrayBuffer);
return handleScratchpadTableRes(results);
} else {
return { error: "API call failed" };
return { error: "Datasource QSQL call failed" };
}
}

Expand All @@ -448,7 +446,7 @@ export async function runSqlDataSource(
const results = handleWSResults(sqlCall.arrayBuffer);
return handleScratchpadTableRes(results);
} else {
return { error: "API call failed" };
return { error: "Datasource SQL call failed" };
}
}

Expand Down
176 changes: 175 additions & 1 deletion test/suite/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
ServerDetails,
ServerType,
} from "../../src/models/connectionsModels";
import { InsightsApiConfig } from "../../src/models/config";

describe("dataSourceCommand", () => {
afterEach(() => {
Expand Down Expand Up @@ -147,6 +148,11 @@ describe("dataSourceCommand2", () => {
sql: {
query: "dummy SQL query",
},
uda: {
name: "test query",
description: "test description",
params: [],
},
},
};
resultsPanel = new KdbResultsViewProvider(uriTest);
Expand Down Expand Up @@ -520,6 +526,99 @@ describe("dataSourceCommand2", () => {
});
});

describe("runUDADataSource", () => {
let getDataInsightsStub,
handleWSResultsStub,
handleScratchpadTableRes,
parseErrorStub: sinon.SinonStub;

beforeEach(() => {
getDataInsightsStub = sinon.stub(insightsConn, "getDatasourceQuery");
handleWSResultsStub = sinon.stub(queryUtils, "handleWSResults");
parseErrorStub = sinon.stub(dataSourceCommand, "parseError");
handleScratchpadTableRes = sinon.stub(
queryUtils,
"handleScratchpadTableRes",
);
});

afterEach(() => {
sinon.restore();
});

it("should call the API and handle the results", async () => {
getDataInsightsStub.resolves({ arrayBuffer: true });
handleWSResultsStub.resolves([
{ a: "2", b: "3" },
{ a: "4", b: "6" },
{ a: "6", b: "9" },
]);
handleScratchpadTableRes.resolves([
{ a: "2", b: "3" },
{ a: "4", b: "6" },
{ a: "6", b: "9" },
]);

const result = await dataSourceCommand.runUDADataSource(
dummyDataSourceFiles,
insightsConn,
);

sinon.assert.calledOnce(getDataInsightsStub);
sinon.assert.calledOnce(handleWSResultsStub);
assert.deepStrictEqual(result, [
{ a: "2", b: "3" },
{ a: "4", b: "6" },
{ a: "6", b: "9" },
]);
});

it("should call the API and handle the error results", async () => {
getDataInsightsStub.resolves({ error: "error test" });
parseErrorStub.resolves({ error: "error test" });

const result = await dataSourceCommand.runUDADataSource(
dummyDataSourceFiles,
insightsConn,
);

assert.deepStrictEqual(result, { error: "error test" });
});

it("should call the API and handle undefined response ", async () => {
getDataInsightsStub.resolves(undefined);

const result = await dataSourceCommand.runUDADataSource(
dummyDataSourceFiles,
insightsConn,
);

assert.deepStrictEqual(result, { error: "UDA call failed" });
});

it("should handle undefined UDA ", async () => {
dummyDataSourceFiles.dataSource.uda = undefined;

const result = await dataSourceCommand.runUDADataSource(
dummyDataSourceFiles,
insightsConn,
);

assert.deepStrictEqual(result, { error: "UDA not found" });
});

it("should handle UDA without name", async () => {
dummyDataSourceFiles.dataSource.uda.name = "";

const result = await dataSourceCommand.runUDADataSource(
dummyDataSourceFiles,
insightsConn,
);

assert.deepStrictEqual(result, { error: "UDA name not found" });
});
});

describe("runDataSource", () => {
const dummyMeta: MetaObject = {
header: {
Expand Down Expand Up @@ -601,6 +700,11 @@ describe("dataSourceCommand2", () => {
selectedTarget: "dummy-target",
},
sql: { query: "test query" },
uda: {
name: "test query",
description: "test description",
params: [],
},
},
insightsNode: "dummyNode",
};
Expand All @@ -610,6 +714,7 @@ describe("dataSourceCommand2", () => {
const connMngService = new ConnectionManagementService();
const uriTest: vscode.Uri = vscode.Uri.parse("test");
const ab = new ArrayBuffer(26);

ext.resultsViewProvider = new KdbResultsViewProvider(uriTest);
let isVisibleStub,
getMetaStub,
Expand All @@ -618,7 +723,8 @@ describe("dataSourceCommand2", () => {
retrieveConnStub,
getDataInsightsStub,
writeQueryResultsToViewStub,
writeQueryResultsToConsoleStub: sinon.SinonStub;
writeQueryResultsToConsoleStub,
qeStatusStub: sinon.SinonStub;
let windowMock: sinon.SinonMock;

ext.outputChannel = vscode.window.createOutputChannel("kdb");
Expand Down Expand Up @@ -752,6 +858,30 @@ describe("dataSourceCommand2", () => {
ext.connectedConnectionList.length = 0;
});

it("should fail run QSQL if qe is off", async () => {
const apiConfig: InsightsApiConfig = {
encryptionDatabase: false,
encryptionInTransit: false,
queryEnvironmentsEnabled: false,
version: "1.0",
};
insightsConn.apiConfig = apiConfig;
ext.connectedConnectionList.push(insightsConn);
retrieveConnStub.resolves(insightsConn);
insightsConn.meta = dummyMeta;
getMetaStub.resolves(dummyMeta);
ext.isResultsTabVisible = true;
await dataSourceCommand.runDataSource(
dummyFileContent as DataSourceFiles,
insightsConn.connLabel,
"test-file.kdb.json",
);
sinon.assert.neverCalledWith(writeQueryResultsToConsoleStub);
sinon.assert.neverCalledWith(writeQueryResultsToViewStub);
insightsConn.apiConfig.queryEnvironmentsEnabled = true;
ext.connectedConnectionList.length = 0;
});

it("should return API results", async () => {
ext.connectedConnectionList.push(insightsConn);
retrieveConnStub.resolves(insightsConn);
Expand Down Expand Up @@ -790,6 +920,25 @@ describe("dataSourceCommand2", () => {
ext.connectedConnectionList.length = 0;
});

it("should return UDA results", async () => {
ext.connectedConnectionList.push(insightsConn);
retrieveConnStub.resolves(insightsConn);
insightsConn.meta = dummyMeta;
dummyFileContent.dataSource.selectedType = DataSourceTypes.UDA;
getMetaStub.resolves(dummyMeta);
getDataInsightsStub.resolves({ arrayBuffer: ab, error: "" });
ext.isResultsTabVisible = false;
await dataSourceCommand.runDataSource(
dummyFileContent as DataSourceFiles,
insightsConn.connLabel,
"test-file.kdb.json",
);
sinon.assert.neverCalledWith(writeQueryResultsToViewStub);
sinon.assert.calledOnce(writeQueryResultsToConsoleStub);

ext.connectedConnectionList.length = 0;
});

it("should return error message QSQL", async () => {
dummyFileContent.dataSource.selectedType = DataSourceTypes.QSQL;
getMetaStub.resolves(dummyMeta);
Expand Down Expand Up @@ -910,13 +1059,26 @@ describe("dataSourceCommand2", () => {
selectedTarget: "dummy-target",
},
sql: { query: "test query" },
uda: {
name: "test query",
description: "test description",
params: [],
},
},
insightsNode: "dummyNode",
};
let windowMock: sinon.SinonMock;
let connMngService: ConnectionManagementService;
let qeStatusStub: sinon.SinonStub;

beforeEach(() => {
ext.activeConnection = insightsConn;
windowMock = sinon.mock(vscode.window);
connMngService = new ConnectionManagementService();
qeStatusStub = sinon.stub(
connMngService,
"retrieveInsightsConnQEEnabled",
);
});
afterEach(() => {
ext.activeConnection = undefined;
Expand All @@ -932,6 +1094,18 @@ describe("dataSourceCommand2", () => {
.once()
.withArgs("Please connect to an Insights server");
});

it("should show error msg if qe is off", async () => {
qeStatusStub.returns("disabled");
await dataSourceCommand.populateScratchpad(
dummyFileContent,
insightsConn.connLabel,
);
windowMock
.expects("showErrorMessage")
.once()
.withArgs("The query enviroment(s) are disabled for this connection");
});
});

describe("parseError", () => {
Expand Down
Loading

0 comments on commit b40ff0a

Please sign in to comment.