Add Connection String Enhancement for SQL Bindings (#18011)

* sql database work to prompt users to choose connections

* add test
This commit is contained in:
Vasu Bhog
2022-01-13 17:54:46 -08:00
committed by GitHub
parent 050d2cc98f
commit d4534f875f
4 changed files with 305 additions and 27 deletions

View File

@@ -11,7 +11,7 @@ import * as utils from '../../common/utils';
import * as constants from '../../common/constants';
import * as azureFunctionUtils from '../../common/azureFunctionsUtils';
import { createContext, TestContext } from '../testContext';
import { createContext, TestContext, createTestCredentials } from '../testContext';
import { launchAddSqlBindingQuickpick } from '../../dialogs/addSqlBindingQuickpick';
import { PackageHelper } from '../../tools/packageHelper';
@@ -29,6 +29,7 @@ describe('Add SQL Binding quick pick', () => {
it('Should show error if the file contains no Azure Functions', async function (): Promise<void> {
sinon.stub(utils, 'getAzureFunctionService').resolves(testContext.azureFunctionService.object);
sinon.stub(utils, 'getVscodeMssqlApi').resolves(testContext.vscodeMssqlIExtension.object);
const spy = sinon.spy(vscode.window, 'showErrorMessage');
testContext.azureFunctionService.setup(x => x.getAzureFunctions(TypeMoq.It.isAny())).returns(async () => {
return Promise.resolve({
@@ -46,6 +47,7 @@ describe('Add SQL Binding quick pick', () => {
it('Should show error if adding SQL binding was not successful', async function (): Promise<void> {
sinon.stub(utils, 'getAzureFunctionService').resolves(testContext.azureFunctionService.object);
sinon.stub(utils, 'getVscodeMssqlApi').resolves(testContext.vscodeMssqlIExtension.object);
const spy = sinon.spy(vscode.window, 'showErrorMessage');
testContext.azureFunctionService.setup(x => x.getAzureFunctions(TypeMoq.It.isAny())).returns(async () => {
return Promise.resolve({
@@ -54,6 +56,7 @@ describe('Add SQL Binding quick pick', () => {
azureFunctions: ['af1', 'af2']
});
});
//failure since no AFs are found in the project
sinon.stub(azureFunctionUtils, 'getAFProjectContainingFile').resolves(undefined);
const errormsg = 'Error inserting binding';
testContext.azureFunctionService.setup(x => x.addSqlBinding(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(async () => {
@@ -77,5 +80,55 @@ describe('Add SQL Binding quick pick', () => {
should(spy.calledOnce).be.true('showErrorMessage should have been called exactly once');
should(spy.calledWith(errormsg)).be.true(`showErrorMessage not called with expected message '${errormsg}' Actual '${spy.getCall(0).args[0]}'`);
});
});
it('Should show error connection profile does not connect', async function (): Promise<void> {
sinon.stub(utils, 'getAzureFunctionService').resolves(testContext.azureFunctionService.object);
sinon.stub(utils, 'getVscodeMssqlApi').resolves(testContext.vscodeMssqlIExtension.object);
let connectionCreds = createTestCredentials();
sinon.stub(azureFunctionUtils, 'getAFProjectContainingFile').resolves(vscode.Uri.file('testUri'));
testContext.azureFunctionService.setup(x => x.getAzureFunctions(TypeMoq.It.isAny())).returns(async () => {
return Promise.resolve({
success: true,
errorMessage: '',
azureFunctions: ['af1']
});
});
// Mocks connect call to mssql
let error = new Error('Connection Request Failed');
testContext.vscodeMssqlIExtension.setup(x => x.connect(TypeMoq.It.isAny(), undefined)).throws(error);
// Mocks promptForConnection
testContext.vscodeMssqlIExtension.setup(x => x.promptForConnection(true)).returns(() => Promise.resolve(connectionCreds));
let quickpickStub = sinon.stub(vscode.window, 'showQuickPick');
// select Azure function
quickpickStub.onFirstCall().resolves({ label: 'af1' });
// select input or output binding
quickpickStub.onSecondCall().resolves({ label: constants.input });
// give object name
let inputBoxStub = sinon.stub(vscode.window, 'showInputBox').onFirstCall().resolves('dbo.table1');
// select connection profile
quickpickStub.onThirdCall().resolves({ label: constants.createNewLocalAppSettingWithIcon });
// give connection string setting name
inputBoxStub.onSecondCall().resolves('SqlConnectionString');
// select connection profile method
quickpickStub.onCall(3).resolves({ label: constants.connectionProfile });
await launchAddSqlBindingQuickpick(vscode.Uri.file('testUri'), packageHelper);
// should go back to the select connection string methods
should(quickpickStub.callCount === 5);
should(quickpickStub.getCall(4).args).deepEqual([
[constants.connectionProfile, constants.userConnectionString],
{
canPickMany: false,
ignoreFocusOut: true,
title: constants.selectConnectionString
}]);
});
});