From d268b9948be48a3e4e08e3f8ac0f9107a5067ad3 Mon Sep 17 00:00:00 2001 From: Kim Santiago <31145923+kisantia@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:02:53 -0700 Subject: [PATCH] Add a few tests for projectController (#21083) --- .../src/test/projectController.test.ts | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/extensions/sql-database-projects/src/test/projectController.test.ts b/extensions/sql-database-projects/src/test/projectController.test.ts index 2da42eefcc..5899cac664 100644 --- a/extensions/sql-database-projects/src/test/projectController.test.ts +++ b/extensions/sql-database-projects/src/test/projectController.test.ts @@ -48,7 +48,7 @@ describe('ProjectsController', function (): void { sinon.restore(); }); - after(async function(): Promise { + after(async function (): Promise { await testUtils.deleteGeneratedTestFolder(); }); @@ -74,7 +74,7 @@ describe('ProjectsController', function (): void { it('Should create new sqlproj file with correct specified target platform', async function (): Promise { const projController = new ProjectsController(testContext.outputChannel); const projFileDir = path.join(testUtils.generateBaseFolderName(), `TestProject_${new Date().getTime()}`); - const projTargetPlatform = SqlTargetPlatform.sqlAzure; // default is SQL Server 2019 + const projTargetPlatform = SqlTargetPlatform.sqlAzure; // default is SQL Server 2022 const projFilePath = await projController.createNewProject({ newProjName: 'TestProjectName', @@ -90,6 +90,21 @@ describe('ProjectsController', function (): void { should(constants.getTargetPlatformFromVersion(projTargetVersion)).equal(projTargetPlatform); }); + it('Should create new edge project with expected template files', async function (): Promise { + const projController = new ProjectsController(testContext.outputChannel); + const projFileDir = path.join(testUtils.generateBaseFolderName(), `TestProject_${new Date().getTime()}`); + + const projFilePath = await projController.createNewProject({ + newProjName: 'TestProjectName', + folderUri: vscode.Uri.file(projFileDir), + projectTypeId: constants.edgeSqlDatabaseProjectTypeId, + projectGuid: 'BA5EBA11-C0DE-5EA7-ACED-BABB1E70A575', + sdkStyle: true + }); + + const project = await Project.openProject(projFilePath); + should(project.files.length).equal(7, `The 7 template files for an edge project should be present. Actual: ${project.files.length}`); + }); it('Should return silently when no SQL object name provided in prompts', async function (): Promise { for (const name of ['', ' ', undefined]) { @@ -123,6 +138,41 @@ describe('ProjectsController', function (): void { should(spy.calledWith(msg)).be.true(`showErrorMessage not called with expected message '${msg}' Actual '${spy.getCall(0).args[0]}'`); }); + it('Should not create file if no itemTypeName is selected', async function (): Promise { + sinon.stub(vscode.window, 'showQuickPick').resolves(undefined); + const spy = sinon.spy(vscode.window, 'showErrorMessage'); + const projController = new ProjectsController(testContext.outputChannel); + const project = await testUtils.createTestProject(baselines.newProjectFileBaseline); + + should(project.files.length).equal(0, 'There should be no files'); + await projController.addItemPrompt(project, ''); + should(project.files.length).equal(0, 'File should not have been added'); + should(spy.called).be.false(`showErrorMessage should not have been called called. Actual '${spy.getCall(0)?.args[0]}'`); + }); + + it('Should add existing item', async function (): Promise { + const tableName = 'table1'; + sinon.stub(vscode.window, 'showInputBox').resolves(tableName); + const spy = sinon.spy(vscode.window, 'showErrorMessage'); + const projController = new ProjectsController(testContext.outputChannel); + const project = await testUtils.createTestProject(baselines.newProjectFileBaseline); + + should(project.files.length).equal(0, 'There should be no files'); + await projController.addItemPrompt(project, '', { itemType: ItemType.script }); + should(project.files.length).equal(1, 'File should be successfully added'); + + // exclude item + const projTreeRoot = new ProjectRootTreeItem(project); + await projController.exclude(createWorkspaceTreeItem(projTreeRoot.children.find(x => x.friendlyName === 'table1.sql')!)); + should(project.files.length).equal(0, 'File should be successfully excluded'); + should(spy.called).be.false(`showErrorMessage not called with expected message. Actual '${spy.getCall(0)?.args[0]}'`); + + // add item back + sinon.stub(vscode.window, 'showOpenDialog').resolves([vscode.Uri.file(path.join(project.projectFolderPath, 'table1.sql'))]); + await projController.addExistingItemPrompt(createWorkspaceTreeItem(projTreeRoot)); + should(project.files.length).equal(1, 'File should be successfully re-added'); + }); + it('Should show error if trying to add a folder that already exists', async function (): Promise { const folderName = 'folder1'; const stub = sinon.stub(vscode.window, 'showInputBox').resolves(folderName);