Add more unit tests for NotebookInput (#8654)

This commit is contained in:
Cory Rivera
2019-12-12 14:59:56 -08:00
committed by GitHub
parent 6638db1f35
commit 866bfc61ba
8 changed files with 419 additions and 90 deletions

View File

@@ -1,60 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { INotebookEditor, INotebookSection, INotebookParams } from 'sql/workbench/services/notebook/browser/notebookService';
import { ICellModel, INotebookModel } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
import { ISingleNotebookEditOperation } from 'sql/workbench/api/common/sqlExtHostTypes';
import { CellType } from 'sql/workbench/contrib/notebook/common/models/contracts';
export class NotebookComponentStub implements INotebookEditor {
get notebookParams(): INotebookParams {
throw new Error('Method not implemented.');
}
get id(): string {
throw new Error('Method not implemented.');
}
get cells(): ICellModel[] {
throw new Error('Method not implemented.');
}
get modelReady(): Promise<INotebookModel> {
throw new Error('Method not implemented.');
}
get model(): INotebookModel {
throw new Error('Method not implemented.');
}
isDirty(): boolean {
throw new Error('Method not implemented.');
}
isActive(): boolean {
throw new Error('Method not implemented.');
}
isVisible(): boolean {
throw new Error('Method not implemented.');
}
executeEdits(edits: ISingleNotebookEditOperation[]): boolean {
throw new Error('Method not implemented.');
}
runCell(cell: ICellModel): Promise<boolean> {
throw new Error('Method not implemented.');
}
runAllCells(startCell?: ICellModel, endCell?: ICellModel): Promise<boolean> {
throw new Error('Method not implemented.');
}
clearOutput(cell: ICellModel): Promise<boolean> {
throw new Error('Method not implemented.');
}
clearAllOutputs(): Promise<boolean> {
throw new Error('Method not implemented.');
}
getSections(): INotebookSection[] {
throw new Error('Method not implemented.');
}
navigateToSection(sectionId: string): void {
throw new Error('Method not implemented.');
}
addCell(cellType: CellType, index?: number, event?: Event) {
throw new Error('Method not implemented.');
}
}

View File

@@ -9,12 +9,12 @@ import * as assert from 'assert';
import { AddCellAction, ClearAllOutputsAction, CollapseCellsAction, TrustedAction, RunAllCellsAction, NewNotebookAction } from 'sql/workbench/contrib/notebook/browser/notebookActions';
import { CellType } from 'sql/workbench/contrib/notebook/common/models/contracts';
import { INotebookEditor } from 'sql/workbench/services/notebook/browser/notebookService';
import { NotebookComponentStub } from 'sql/workbench/contrib/notebook/test/browser/common';
import { ICellModel, INotebookModel } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { TestCommandService } from 'vs/editor/test/browser/editorTestServices';
import { NotebookComponentStub } from 'sql/workbench/contrib/notebook/test/stubs';
suite('Notebook Actions', function (): void {
test('Add Cell Action', async function (): Promise<void> {

View File

@@ -0,0 +1,174 @@
/*---------------------------------------------------------------------------------------------
* 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 * as assert from 'assert';
import { nb } from 'azdata';
import { workbenchInstantiationService } from 'vs/workbench/test/workbenchTestServices';
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import { UntitledNotebookInput } from 'sql/workbench/contrib/notebook/common/models/untitledNotebookInput';
import { FileNotebookInput } from 'sql/workbench/contrib/notebook/common/models/fileNotebookInput';
import { IConnectionProfile } from 'sql/platform/connection/common/interfaces';
import { UntitledTextEditorModel } from 'vs/workbench/common/editor/untitledTextEditorModel';
import { NodeStub, NotebookServiceStub } from 'sql/workbench/contrib/notebook/test/stubs';
import { basenameOrAuthority } from 'vs/base/common/resources';
import { UntitledTextEditorInput } from 'vs/workbench/common/editor/untitledTextEditorInput';
import { SimpleUriLabelService } from 'vs/editor/standalone/browser/simpleServices';
import { IExtensionService, NullExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { INotebookService, IProviderInfo } from 'sql/workbench/services/notebook/browser/notebookService';
suite('Notebook Input', function (): void {
const instantiationService = workbenchInstantiationService();
const testTitle = 'TestTitle';
const testProvider = 'TestProvider';
const untitledUri = URI.from({ scheme: Schemas.untitled, path: 'TestPath' });
const mockExtensionService = TypeMoq.Mock.ofType<IExtensionService>(NullExtensionService);
mockExtensionService.setup(s => s.whenInstalledExtensionsRegistered()).returns(() => Promise.resolve(true));
const mockNotebookService = TypeMoq.Mock.ofType<INotebookService>(NotebookServiceStub);
mockNotebookService.setup(s => s.getProvidersForFileType(TypeMoq.It.isAny())).returns(() => [testProvider]);
mockNotebookService.setup(s => s.getStandardKernelsForProvider(TypeMoq.It.isAny())).returns(() => {
return [{
name: 'TestName',
displayName: 'TestDisplayName',
connectionProviderIds: ['TestId'],
notebookProvider: 'TestProvider'
}];
});
let untitledTextInput: UntitledTextEditorInput;
let untitledNotebookInput: UntitledNotebookInput;
setup(() => {
untitledTextInput = new UntitledTextEditorInput(untitledUri, false, '', '', '', instantiationService, undefined, new SimpleUriLabelService(), undefined, undefined, undefined);
untitledNotebookInput = new UntitledNotebookInput(
testTitle, untitledUri, untitledTextInput,
undefined, instantiationService, mockNotebookService.object, mockExtensionService.object);
});
test('File Notebook Input', async function (): Promise<void> {
let fileUri = URI.from({ scheme: Schemas.file, path: 'TestPath' });
let fileNotebookInput = new FileNotebookInput(
testTitle, fileUri, undefined,
undefined, instantiationService, mockNotebookService.object, mockExtensionService.object);
let inputId = fileNotebookInput.getTypeId();
assert.strictEqual(inputId, FileNotebookInput.ID);
assert.strictEqual(fileNotebookInput.isUntitled(), false, 'File Input should not be untitled');
});
test('Untitled Notebook Input', async function (): Promise<void> {
let inputId = untitledNotebookInput.getTypeId();
assert.strictEqual(inputId, UntitledNotebookInput.ID);
assert.ok(untitledNotebookInput.isUntitled(), 'Untitled Input should be untitled');
});
test('Getters and Setters', async function (): Promise<void> {
// Input title
assert.strictEqual(untitledNotebookInput.getTitle(), testTitle);
let noTitleInput = instantiationService.createInstance(UntitledNotebookInput, undefined, untitledUri, undefined);
assert.strictEqual(noTitleInput.getTitle(), basenameOrAuthority(untitledUri));
// Text Input
assert.strictEqual(untitledNotebookInput.textInput, untitledTextInput);
// Notebook URI
assert.deepStrictEqual(untitledNotebookInput.notebookUri, untitledUri);
// Content Manager
assert.notStrictEqual(untitledNotebookInput.editorOpenedTimestamp, undefined);
// Notebook editor timestamp
assert.notStrictEqual(untitledNotebookInput.contentManager, undefined);
// Layout changed event
assert.notStrictEqual(untitledNotebookInput.layoutChanged, undefined);
// Connection Profile
let testProfile = <IConnectionProfile>{};
untitledNotebookInput.connectionProfile = testProfile;
assert.strictEqual(untitledNotebookInput.connectionProfile, testProfile);
// Default Kernel
let testDefaultKernel: nb.IKernelSpec = {
name: 'TestName',
language: 'TestLanguage',
display_name: 'TestDisplayName'
};
untitledNotebookInput.defaultKernel = testDefaultKernel;
assert.strictEqual(untitledNotebookInput.defaultKernel, testDefaultKernel);
// Untitled Editor Model
let testModel = <UntitledTextEditorModel>{};
untitledNotebookInput.untitledEditorModel = testModel;
assert.strictEqual(untitledNotebookInput.untitledEditorModel, testModel);
// getResource
assert.strictEqual(untitledNotebookInput.getResource(), untitledUri);
// Standard kernels
let testKernels = [{
name: 'TestName1',
displayName: 'TestDisplayName1',
connectionProviderIds: ['TestId1'],
notebookProvider: 'TestProvider'
}, {
name: 'TestName2',
displayName: 'TestDisplayName2',
connectionProviderIds: ['TestId2'],
notebookProvider: 'TestProvider'
}];
untitledNotebookInput.standardKernels = testKernels;
assert.deepStrictEqual(untitledNotebookInput.standardKernels, testKernels);
// Provider Info
let provider = await untitledNotebookInput.getProviderInfo();
assert.deepStrictEqual(provider, <IProviderInfo>{
providerId: testProvider,
providers: [testProvider]
});
});
test('Parent container', async function (): Promise<void> {
// Undefined container
let testContainer = undefined;
untitledNotebookInput.container = testContainer;
assert.strictEqual(untitledNotebookInput.container, testContainer);
// Dispose old container when setting new one
let removedContainer: HTMLElement;
let mockParentNode = TypeMoq.Mock.ofType<Node>(NodeStub);
mockParentNode.setup(e => e.removeChild(TypeMoq.It.isAny())).returns(oldChild => {
removedContainer = oldChild;
return oldChild;
});
testContainer = <HTMLElement>{ parentNode: mockParentNode.object };
untitledNotebookInput.container = testContainer;
assert.strictEqual(untitledNotebookInput.container, testContainer);
let oldContainer = testContainer;
testContainer = <HTMLElement>{};
untitledNotebookInput.container = testContainer;
assert.strictEqual(untitledNotebookInput.container, testContainer);
mockParentNode.verify(e => e.removeChild(TypeMoq.It.isAny()), TypeMoq.Times.once());
assert.strictEqual(removedContainer, oldContainer);
});
test('Matches other input', async function (): Promise<void> {
assert.strictEqual(untitledNotebookInput.matches(undefined), false, 'Input should not match undefined.');
assert.ok(untitledNotebookInput.matches(untitledNotebookInput), 'Input should match itself.');
let otherTestUri = URI.from({ scheme: Schemas.untitled, path: 'OtherTestPath' });
let otherTextInput = new UntitledTextEditorInput(otherTestUri, false, '', '', '', instantiationService, undefined, new SimpleUriLabelService(), undefined, undefined, undefined);
let otherInput = instantiationService.createInstance(UntitledNotebookInput, 'OtherTestInput', otherTestUri, otherTextInput);
assert.strictEqual(untitledNotebookInput.matches(otherInput), false, 'Input should not match different input.');
});
});