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());
});
});

View File

@@ -3,6 +3,7 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import * as should from 'should';
import * as TypeMoq from 'typemoq';
@@ -12,28 +13,34 @@ import { AppContext } from '../../common/appContext';
import { JupyterController } from '../../jupyter/jupyterController';
import { LocalPipPackageManageProvider } from '../../jupyter/localPipPackageManageProvider';
import { MockExtensionContext } from '../common/stubs';
import { NotebookUtils } from '../../common/notebookUtils';
describe('JupyterController tests', function () {
describe('Jupyter Controller', function () {
let mockExtensionContext: vscode.ExtensionContext;
let appContext: AppContext;
let controller: JupyterController;
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
let connection: azdata.connection.ConnectionProfile;
this.beforeAll(() => {
mockExtensionContext = new MockExtensionContext();
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
appContext = new AppContext(mockExtensionContext, mockApiWrapper.object);
connection = new azdata.connection.ConnectionProfile();
});
this.beforeEach(() => {
mockApiWrapper = TypeMoq.Mock.ofType<ApiWrapper>();
mockApiWrapper.setup(x => x.getCurrentConnection()).returns(() => { return Promise.resolve(connection); });
appContext = new AppContext(mockExtensionContext, mockApiWrapper.object);
controller = new JupyterController(appContext);
});
it('should activate new JupyterController successfully', async () => {
should(controller.extensionContext).deepEqual(appContext.extensionContext, 'Extension context should be passed through');
should(controller.jupyterInstallation).equal(undefined, 'JupyterInstallation should be undefined before controller activation');
await should(controller.activate()).not.be.rejected();
// On activation, local pip and local conda package providers should exist
should(controller.packageManageProviders.size).equal(2, 'Local pip and conda package providers should be default providers');
should(controller.jupyterInstallation.extensionPath).equal(appContext.extensionContext.extensionPath, 'JupyterInstallation extension path should match appContext extension path');
});
it('should create new packageManageProvider successfully', async () => {
@@ -50,4 +57,43 @@ describe('JupyterController tests', function () {
should.throws(() => controller.registerPackageManager('provider1', mockProvider.object));
should(controller.packageManageProviders.size).equal(1, 'Package manage providers should still equal 1');
});
it('should should get defaultConnection() successfully', async () => {
let defaultConnection = await controller.getDefaultConnection();
should(defaultConnection).deepEqual(connection, 'getDefaultConnection() did not return expected result');
});
it('should show error message for doManagePackages before activation', async () => {
await controller.doManagePackages();
mockApiWrapper.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.once());
});
it('should not show error message for doManagePackages after activation', async () => {
await controller.activate();
await controller.doManagePackages();
mockApiWrapper.verify(x => x.showErrorMessage(TypeMoq.It.isAny()), TypeMoq.Times.never());
});
it('Returns expected values from notebook provider', async () => {
await controller.activate();
should(controller.notebookProvider.standardKernels).deepEqual([], 'Notebook provider standard kernels should return empty array');
should(controller.notebookProvider.providerId).equal('jupyter', 'Notebook provider should be jupyter');
should(controller.notebookProvider.getNotebookManager(undefined)).be.rejected();
should(controller.notebookProvider.notebookManagerCount).equal(0);
controller.notebookProvider.handleNotebookClosed(undefined);
});
it('Returns notebook manager for real notebook editor', async () => {
await controller.activate();
let notebookUtils = new NotebookUtils(mockApiWrapper.object);
const notebookEditor = await notebookUtils.newNotebook(undefined);
let notebookManager = await controller.notebookProvider.getNotebookManager(notebookEditor.document.uri);
should(controller.notebookProvider.notebookManagerCount).equal(1);
// Session manager should not be immediately ready
should(notebookManager.sessionManager.isReady).equal(false);
// Session manager should not immediately have specs
should(notebookManager.sessionManager.specs).equal(undefined);
controller.notebookProvider.handleNotebookClosed(notebookEditor.document.uri);
});
});