Add more notebook extension tests (#11143)

* more tests

* More tests

* More tests

* Add prompt tests
This commit is contained in:
Chris LaFreniere
2020-06-30 11:01:51 -07:00
committed by GitHub
parent a32f42d475
commit a8a7559229
18 changed files with 295 additions and 101 deletions

View File

@@ -14,15 +14,21 @@ import * as uuid from 'uuid';
import { promises as fs } from 'fs';
import { ApiWrapper } from '../../common/apiWrapper';
import { tryDeleteFile } from './testUtils';
import { CellTypes } from '../../contracts/content';
describe('notebookUtils Tests', function (): void {
let notebookUtils: NotebookUtils;
let apiWrapperMock: TypeMoq.IMock<ApiWrapper>;
before(function (): void {
beforeEach(function (): void {
apiWrapperMock = TypeMoq.Mock.ofInstance(new ApiWrapper());
notebookUtils = new NotebookUtils(apiWrapperMock.object);
});
this.afterAll(async function (): Promise<void> {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
describe('newNotebook', function (): void {
it('Should open a new notebook successfully', async function (): Promise<void> {
should(azdata.nb.notebookDocuments.length).equal(0, 'There should be not any open Notebook documents');
@@ -80,4 +86,117 @@ describe('notebookUtils Tests', function (): void {
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('runActiveCell', function () {
it('shows error when no notebook visible', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
await notebookUtils.runActiveCell();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('does not show error when notebook visible', async function (): Promise<void> {
let mockNotebookEditor = TypeMoq.Mock.ofType<azdata.nb.NotebookEditor>();
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => mockNotebookEditor.object);
await notebookUtils.runActiveCell();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
mockNotebookEditor.verify(x => x.runCell(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('clearActiveCellOutput', function () {
it('shows error when no notebook visible', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
await notebookUtils.clearActiveCellOutput();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('does not show error when notebook visible', async function (): Promise<void> {
let mockNotebookEditor = TypeMoq.Mock.ofType<azdata.nb.NotebookEditor>();
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => mockNotebookEditor.object);
await notebookUtils.clearActiveCellOutput();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
mockNotebookEditor.verify(x => x.clearOutput(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('runAllCells', function () {
it('shows error when no notebook visible', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
await notebookUtils.runAllCells();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('does not show error when notebook visible', async function (): Promise<void> {
let mockNotebookEditor = TypeMoq.Mock.ofType<azdata.nb.NotebookEditor>();
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => mockNotebookEditor.object);
await notebookUtils.runAllCells();
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
mockNotebookEditor.verify(x => x.runAllCells(TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});
describe('addCell', function () {
it('shows error when no notebook visible for code cell', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
await notebookUtils.addCell('code');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('shows error when no notebook visible for markdown cell', async function (): Promise<void> {
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => undefined);
await notebookUtils.addCell('markdown');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('does not show error when notebook visible for code cell', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
await notebookUtils.addCell('code');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(notebookEditor.document.cells.length).equal(1);
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Code);
});
it('does not show error when notebook visible for markdown cell', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
await notebookUtils.addCell('markdown');
apiWrapperMock.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
should(notebookEditor.document.cells.length).equal(1);
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Markdown);
});
});
describe('analyzeNotebook', function () {
it('creates cell when oeContext exists', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
apiWrapperMock.setup(x => x.showNotebookDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(notebookEditor));
const oeContext: azdata.ObjectExplorerContext = {
connectionProfile: undefined,
isConnectionNode: true,
nodeInfo: {
nodePath: 'path/HDFS/path2',
errorMessage: undefined,
isLeaf: false,
label: 'fakeLabel',
metadata: undefined,
nodeStatus: undefined,
nodeSubType: undefined,
nodeType: undefined
}
}
await notebookUtils.analyzeNotebook(oeContext);
should(notebookEditor.document.cells.length).equal(1, 'One cell should exist');
should(notebookEditor.document.cells[0].contents.cell_type).equal(CellTypes.Code, 'Cell was created with incorrect type');
});
it('does not create new cell when oeContext does not exist', async function (): Promise<void> {
const notebookEditor = await notebookUtils.newNotebook(undefined);
apiWrapperMock.setup(x => x.getActiveNotebookEditor()).returns(() => notebookEditor);
apiWrapperMock.setup(x => x.showNotebookDocument(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve(notebookEditor));
await notebookUtils.analyzeNotebook();
should(notebookEditor.document.cells.length).equal(0, 'No cells should exist');
});
});
});

View File

@@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as TypeMoq from 'typemoq';
import { IPrompter, confirm, IQuestion } from '../../prompts/question';
import CodeAdapter from '../../prompts/adapter';
import { ApiWrapper } from '../../common/apiWrapper';
describe('Prompt', () => {
let prompter: IPrompter;
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
before(function () {
prompter = new CodeAdapter();
});
beforeEach(function () {
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
mockApiWrapper.setup(x => x.showErrorMessage(TypeMoq.It.isAny()));
});
it('Should not find prompt for invalid question type', async function (): Promise<void> {
await prompter.promptSingle<boolean>(<IQuestion>{
type: 'invalidType',
message: 'sample message',
default: false
}, mockApiWrapper.object);
mockApiWrapper.verify(s => s.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
mockApiWrapper.verify(s => s.showQuickPick(TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.never());
});
it('Should find prompt for confirm type', async function (): Promise<void> {
mockApiWrapper.setup(x => x.showQuickPick(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(() => Promise.resolve('Yes'));
await prompter.promptSingle<boolean>(<IQuestion>{
type: confirm,
message: 'sample message',
default: false
}, mockApiWrapper.object);
mockApiWrapper.verify(s => s.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
mockApiWrapper.verify(s => s.showQuickPick(TypeMoq.It.isAny(), TypeMoq.It.isAny()), TypeMoq.Times.once());
});
});