mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-18 09:35:39 -05:00
Remove ApiWrapper from sql-database-projects (#11345)
* Remove ApiWrapper * fix compile error * Use .resolves * Check error messages * Check for not called * FIx global beforeEach/afterEach
This commit is contained in:
@@ -6,15 +6,14 @@
|
||||
import * as should from 'should';
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import * as vscode from 'vscode';
|
||||
import * as sinon from 'sinon';
|
||||
import * as baselines from './baselines/baselines';
|
||||
import * as templates from '../templates/templates';
|
||||
import * as constants from '../common/constants';
|
||||
|
||||
import { createContext, TestContext } from './testContext';
|
||||
import MainController from '../controllers/mainController';
|
||||
import { shouldThrowSpecificError, generateTestFolderPath, createTestProject } from './testUtils';
|
||||
import { generateTestFolderPath, createTestProject } from './testUtils';
|
||||
|
||||
let testContext: TestContext;
|
||||
|
||||
@@ -25,22 +24,18 @@ describe('MainController: main controller operations', function (): void {
|
||||
await baselines.loadBaselines();
|
||||
});
|
||||
|
||||
beforeEach(function (): void {
|
||||
testContext.apiWrapper.reset();
|
||||
afterEach(function (): void {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it('Should create new project through MainController', async function (): Promise<void> {
|
||||
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
|
||||
|
||||
testContext.apiWrapper.setup(x => x.showInputBox(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('MyProjectName'));
|
||||
testContext.apiWrapper.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).returns(() => Promise.resolve([vscode.Uri.file(projFileDir)]));
|
||||
testContext.apiWrapper.setup(x => x.workspaceFolders()).returns(() => undefined);
|
||||
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => {
|
||||
console.log(s);
|
||||
return Promise.resolve(s);
|
||||
});
|
||||
sinon.stub(vscode.window, 'showInputBox').resolves('MyProjectName');
|
||||
sinon.stub(vscode.window, 'showOpenDialog').resolves([vscode.Uri.file(projFileDir)]);
|
||||
sinon.replaceGetter(vscode.workspace, 'workspaceFolders', () => undefined);
|
||||
|
||||
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
|
||||
const controller = new MainController(testContext.context);
|
||||
const proj = await controller.createNewProject();
|
||||
|
||||
should(proj).not.equal(undefined);
|
||||
@@ -48,30 +43,33 @@ describe('MainController: main controller operations', function (): void {
|
||||
|
||||
it('Should show error when no project name', async function (): Promise<void> {
|
||||
for (const name of ['', ' ', undefined]) {
|
||||
testContext.apiWrapper.reset();
|
||||
testContext.apiWrapper.setup(x => x.showInputBox(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(name));
|
||||
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
|
||||
|
||||
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
|
||||
await shouldThrowSpecificError(async () => await controller.createNewProject(), constants.projectNameRequired, `case: '${name}'`);
|
||||
const stub = sinon.stub(vscode.window, 'showInputBox').resolves(name);
|
||||
const spy = sinon.spy(vscode.window, 'showErrorMessage');
|
||||
const controller = new MainController(testContext.context);
|
||||
await controller.createNewProject();
|
||||
should(spy.calledOnce).be.true('showErrorMessage should have been called exactly once');
|
||||
should(spy.calledWith(constants.projectNameRequired)).be.true(`showErrorMessage not called with expected message '${constants.projectNameRequired}' Actual '${spy.getCall(0).args[0]}'`);
|
||||
stub.restore();
|
||||
spy.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it('Should show error when no location name', async function (): Promise<void> {
|
||||
testContext.apiWrapper.setup(x => x.showInputBox(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('MyProjectName'));
|
||||
testContext.apiWrapper.setup(x => x.showOpenDialog(TypeMoq.It.isAny())).returns(() => Promise.resolve(undefined));
|
||||
testContext.apiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny())).returns((s) => { throw new Error(s); });
|
||||
|
||||
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
|
||||
await shouldThrowSpecificError(async () => await controller.createNewProject(), constants.projectLocationRequired);
|
||||
sinon.stub(vscode.window, 'showInputBox').resolves('MyProjectName');
|
||||
sinon.stub(vscode.window, 'showOpenDialog').resolves(undefined);
|
||||
const spy = sinon.spy(vscode.window, 'showErrorMessage');
|
||||
const controller = new MainController(testContext.context);
|
||||
await controller.createNewProject();
|
||||
should(spy.calledOnce).be.true('showErrorMessage should be called exactly once');
|
||||
should(spy.calledWith(constants.projectLocationRequired)).be.true(`showErrorMessage not called with expected message '${constants.projectLocationRequired}' Actual '${spy.getCall(0).args[0]}'`);
|
||||
});
|
||||
|
||||
it('Should create new instance without error', async function (): Promise<void> {
|
||||
should.doesNotThrow(() => new MainController(testContext.context, testContext.apiWrapper.object), 'Creating controller should not throw an error');
|
||||
should.doesNotThrow(() => new MainController(testContext.context), 'Creating controller should not throw an error');
|
||||
});
|
||||
|
||||
it('Should activate and deactivate without error', async function (): Promise<void> {
|
||||
let controller = new MainController(testContext.context, testContext.apiWrapper.object);
|
||||
let controller = new MainController(testContext.context);
|
||||
should.notEqual(controller.extensionContext, undefined);
|
||||
|
||||
should.doesNotThrow(() => controller.activate(), 'activate() should not throw an error');
|
||||
@@ -84,14 +82,14 @@ describe('MainController: main controller operations', function (): void {
|
||||
const nestedFolder = path.join(rootFolderPath, 'nestedProject');
|
||||
const nestedProject = await createTestProject(baselines.openProjectFileBaseline, nestedFolder);
|
||||
|
||||
testContext.apiWrapper.setup(x => x.workspaceFolders()).returns(() => [workspaceFolder]);
|
||||
const workspaceFolder: vscode.WorkspaceFolder = {
|
||||
uri: vscode.Uri.file(rootFolderPath),
|
||||
name: '',
|
||||
index: 0
|
||||
};
|
||||
sinon.replaceGetter(vscode.workspace, 'workspaceFolders', () => [workspaceFolder]);
|
||||
|
||||
const controller = new MainController(testContext.context, testContext.apiWrapper.object);
|
||||
const controller = new MainController(testContext.context);
|
||||
should(controller.projController.projects.length).equal(0);
|
||||
|
||||
await controller.loadProjectsInWorkspace();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,48 +16,40 @@ import { PublishDatabaseDialog } from '../dialogs/publishDatabaseDialog';
|
||||
import { Project } from '../models/project';
|
||||
import { SqlDatabaseProjectTreeViewProvider } from '../controllers/databaseProjectTreeViewProvider';
|
||||
import { ProjectsController } from '../controllers/projectController';
|
||||
import { createContext, TestContext } from './testContext';
|
||||
import { IPublishSettings, IGenerateScriptSettings } from '../models/IPublishSettings';
|
||||
|
||||
|
||||
let testContext: TestContext;
|
||||
|
||||
describe.skip('Publish Database Dialog', () => {
|
||||
before(async function (): Promise<void> {
|
||||
await templates.loadTemplates(path.join(__dirname, '..', '..', 'resources', 'templates'));
|
||||
await baselines.loadBaselines();
|
||||
});
|
||||
|
||||
beforeEach(function (): void {
|
||||
testContext = createContext();
|
||||
});
|
||||
|
||||
it('Should open dialog successfully ', async function (): Promise<void> {
|
||||
const projController = new ProjectsController(testContext.apiWrapper.object, new SqlDatabaseProjectTreeViewProvider());
|
||||
const projController = new ProjectsController(new SqlDatabaseProjectTreeViewProvider());
|
||||
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
|
||||
|
||||
const projFilePath = await projController.createNewProject('TestProjectName', vscode.Uri.file(projFileDir), true, 'BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575');
|
||||
const project = new Project(projFilePath);
|
||||
const publishDatabaseDialog = new PublishDatabaseDialog(testContext.apiWrapper.object, project);
|
||||
const publishDatabaseDialog = new PublishDatabaseDialog(project);
|
||||
publishDatabaseDialog.openDialog();
|
||||
should.notEqual(publishDatabaseDialog.publishTab, undefined);
|
||||
});
|
||||
|
||||
it('Should create default database name correctly ', async function (): Promise<void> {
|
||||
const projController = new ProjectsController(testContext.apiWrapper.object, new SqlDatabaseProjectTreeViewProvider());
|
||||
const projController = new ProjectsController(new SqlDatabaseProjectTreeViewProvider());
|
||||
const projFolder = `TestProject_${new Date().getTime()}`;
|
||||
const projFileDir = path.join(os.tmpdir(), projFolder);
|
||||
|
||||
const projFilePath = await projController.createNewProject('TestProjectName', vscode.Uri.file(projFileDir), true, 'BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575');
|
||||
const project = new Project(projFilePath);
|
||||
|
||||
const publishDatabaseDialog = new PublishDatabaseDialog(testContext.apiWrapper.object, project);
|
||||
const publishDatabaseDialog = new PublishDatabaseDialog(project);
|
||||
should.equal(publishDatabaseDialog.getDefaultDatabaseName(), project.projectFileName);
|
||||
});
|
||||
|
||||
it('Should include all info in publish profile', async function (): Promise<void> {
|
||||
const proj = await testUtils.createTestProject(baselines.openProjectFileBaseline);
|
||||
const dialog = TypeMoq.Mock.ofType(PublishDatabaseDialog, undefined, undefined, testContext.apiWrapper.object, proj);
|
||||
const dialog = TypeMoq.Mock.ofType(PublishDatabaseDialog, undefined, undefined, proj);
|
||||
dialog.setup(x => x.getConnectionUri()).returns(() => { return Promise.resolve('Mock|Connection|Uri'); });
|
||||
dialog.setup(x => x.getTargetDatabaseName()).returns(() => 'MockDatabaseName');
|
||||
dialog.callBase = true;
|
||||
|
||||
@@ -8,10 +8,8 @@ import * as azdata from 'azdata';
|
||||
import * as path from 'path';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import * as mssql from '../../../mssql/src/mssql';
|
||||
import { ApiWrapper } from '../common/apiWrapper';
|
||||
|
||||
export interface TestContext {
|
||||
apiWrapper: TypeMoq.IMock<ApiWrapper>;
|
||||
context: vscode.ExtensionContext;
|
||||
dacFxService: TypeMoq.IMock<mssql.IDacFxService>;
|
||||
}
|
||||
@@ -37,7 +35,6 @@ export function createContext(): TestContext {
|
||||
let extensionPath = path.join(__dirname, '..', '..');
|
||||
|
||||
return {
|
||||
apiWrapper: TypeMoq.Mock.ofType(ApiWrapper),
|
||||
context: {
|
||||
subscriptions: [],
|
||||
workspaceState: {
|
||||
|
||||
Reference in New Issue
Block a user