Skip to content

Commit

Permalink
Add Jupyter Notebook Manager Tests (microsoft#10955)
Browse files Browse the repository at this point in the history
* jupyter notebook manager tests

* cover config duplicate integrationTest
  • Loading branch information
chlafreniere authored Jun 17, 2020
1 parent 8b68d52 commit 77ba5a3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 12 deletions.
104 changes: 104 additions & 0 deletions extensions/notebook/src/test/model/notebookManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as should from 'should';
import * as TypeMoq from 'typemoq';
import * as vscode from 'vscode';
import * as azdata from 'azdata';
import 'mocha';

import { LocalJupyterServerManager, ServerInstanceFactory, IServerManagerOptions } from '../../jupyter/jupyterServerManager';
import { JupyterServerInstallation } from '../../jupyter/jupyterServerInstallation';
import { Deferred } from '../../common/promise';
import { ApiWrapper } from '../../common/apiWrapper';
import { MockExtensionContext } from '../common/stubs';
import { JupyterSessionManager } from '../../jupyter/jupyterSessionManager';
import { JupyterNotebookManager } from '../../jupyter/jupyterNotebookManager';
import { initInstallAndInstance } from './serverManager.test';

describe('Jupyter Notebook Manager', function (): void {
const pythonKernelSpec: azdata.nb.IKernelSpec = {
name: 'python3',
display_name: 'Python 3'
};
let expectedPath = 'my/notebook.ipynb';
let serverManager: LocalJupyterServerManager;
let sessionManager: JupyterSessionManager;
let notebookManager: JupyterNotebookManager;
let deferredInstall: Deferred<void>;
let mockApiWrapper: TypeMoq.IMock<ApiWrapper>;
let mockExtensionContext: MockExtensionContext;
let mockFactory: TypeMoq.IMock<ServerInstanceFactory>;
let serverManagerOptions: IServerManagerOptions;
beforeEach(() => {
mockExtensionContext = new MockExtensionContext();
mockApiWrapper = TypeMoq.Mock.ofType(ApiWrapper);
mockApiWrapper.setup(a => a.showErrorMessage(TypeMoq.It.isAny()));
mockApiWrapper.setup(a => a.getWorkspacePathFromUri(TypeMoq.It.isAny())).returns(() => undefined);
mockFactory = TypeMoq.Mock.ofType(ServerInstanceFactory);

deferredInstall = new Deferred<void>();
let mockInstall = TypeMoq.Mock.ofType(JupyterServerInstallation, undefined, undefined, '/root');
mockInstall.setup(j => j.promptForPythonInstall(TypeMoq.It.isAny())).returns(() => deferredInstall.promise);
mockInstall.object.execOptions = { env: Object.assign({}, process.env) };

serverManagerOptions = {
documentPath: expectedPath,
jupyterInstallation: mockInstall.object,
extensionContext: mockExtensionContext,
apiWrapper: mockApiWrapper.object,
factory: mockFactory.object
};
serverManager = new LocalJupyterServerManager(serverManagerOptions);

sessionManager = new JupyterSessionManager();
notebookManager = new JupyterNotebookManager(serverManager, sessionManager);
});

it('Server settings should be set', async function (): Promise<void> {
should(notebookManager.serverSettings).be.undefined();
let expectedUri = vscode.Uri.parse('http://localhost:1234?token=abcdefghijk');
initInstallAndInstance(expectedUri, mockFactory);
deferredInstall.resolve();

// When I start the server
await serverManager.startServer(pythonKernelSpec);
should(notebookManager.serverSettings.baseUrl).equal('http://localhost:1234', 'Server settings did not match expected value');
});

it('Session Manager should exist', async function (): Promise<void> {
should(notebookManager.sessionManager).deepEqual(sessionManager);
});

it('Server Manager should exist', async function (): Promise<void> {
should(notebookManager.serverManager).deepEqual(serverManager);
});

it('Content manager should always be undefined', async function (): Promise<void> {
should(notebookManager.contentManager).be.undefined();
let expectedUri = vscode.Uri.parse('http://localhost:1234?token=abcdefghijk');
initInstallAndInstance(expectedUri, mockFactory);
deferredInstall.resolve();

// When I start the server
await serverManager.startServer(pythonKernelSpec);
should(notebookManager.contentManager).be.undefined();
});

it('Session and server managers should be shutdown/stopped on dispose', async function(): Promise<void> {
let sessionManager = TypeMoq.Mock.ofType<JupyterSessionManager>();
let serverManager = TypeMoq.Mock.ofType<LocalJupyterServerManager>();
notebookManager = new JupyterNotebookManager(serverManager.object, sessionManager.object, mockApiWrapper.object);
sessionManager.setup(s => s.shutdownAll()).returns(() => new Promise((resolve) => resolve()));
serverManager.setup(s => s.stopServer()).returns(() => new Promise((resolve) => resolve()));

// After I dispose the notebook manager
notebookManager.dispose();

// Session and server managers should be shutdown/stopped
sessionManager.verify((s) => s.shutdownAll(), TypeMoq.Times.once());
serverManager.verify((s) => s.stopServer(), TypeMoq.Times.once());
});
});
24 changes: 12 additions & 12 deletions extensions/notebook/src/test/model/serverManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Local Jupyter Server Manager', function (): void {
it('Should configure and start install', async function (): Promise<void> {
// Given an install and instance that start with no issues
let expectedUri = vscode.Uri.parse('http://localhost:1234?token=abcdefghijk');
let mockServerInstance = initInstallAndInstance(expectedUri);
let mockServerInstance = initInstallAndInstance(expectedUri, mockFactory);
deferredInstall.resolve();

// When I start the server
Expand All @@ -89,7 +89,7 @@ describe('Local Jupyter Server Manager', function (): void {
it('Should call stop on server instance', async function (): Promise<void> {
// Given an install and instance that start with no issues
let expectedUri = vscode.Uri.parse('http://localhost:1234?token=abcdefghijk');
let mockServerInstance = initInstallAndInstance(expectedUri);
let mockServerInstance = initInstallAndInstance(expectedUri, mockFactory);
mockServerInstance.setup(s => s.stop()).returns(() => Promise.resolve());
deferredInstall.resolve();

Expand All @@ -104,7 +104,7 @@ describe('Local Jupyter Server Manager', function (): void {
it('Should call stop when extension is disposed', async function (): Promise<void> {
// Given an install and instance that start with no issues
let expectedUri = vscode.Uri.parse('http://localhost:1234?token=abcdefghijk');
let mockServerInstance = initInstallAndInstance(expectedUri);
let mockServerInstance = initInstallAndInstance(expectedUri, mockFactory);
mockServerInstance.setup(s => s.stop()).returns(() => Promise.resolve());
deferredInstall.resolve();

Expand All @@ -116,13 +116,13 @@ describe('Local Jupyter Server Manager', function (): void {
// Then I expect stop to have been called on the server instance
mockServerInstance.verify(s => s.stop(), TypeMoq.Times.once());
});

function initInstallAndInstance(uri: vscode.Uri): TypeMoq.IMock<IServerInstance> {
let mockServerInstance = TypeMoq.Mock.ofType(JupyterServerInstanceStub);
mockFactory.setup(f => f.createInstance(TypeMoq.It.isAny())).returns(() => mockServerInstance.object);
mockServerInstance.setup(s => s.configure()).returns(() => Promise.resolve());
mockServerInstance.setup(s => s.start()).returns(() => Promise.resolve());
mockServerInstance.setup(s => s.uri).returns(() => uri);
return mockServerInstance;
}
});

export function initInstallAndInstance(uri: vscode.Uri, mockFactory: TypeMoq.IMock<ServerInstanceFactory>): TypeMoq.IMock<IServerInstance> {
let mockServerInstance = TypeMoq.Mock.ofType(JupyterServerInstanceStub);
mockFactory.setup(f => f.createInstance(TypeMoq.It.isAny())).returns(() => mockServerInstance.object);
mockServerInstance.setup(s => s.configure()).returns(() => Promise.resolve());
mockServerInstance.setup(s => s.start()).returns(() => Promise.resolve());
mockServerInstance.setup(s => s.uri).returns(() => uri);
return mockServerInstance;
}

0 comments on commit 77ba5a3

Please sign in to comment.