mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-05 09:35:39 -05:00
Add code coverage tests for notebookUtils (#8524)
This commit is contained in:
@@ -8,9 +8,11 @@ import { nb, IConnectionProfile } from 'azdata';
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
import { INotebookModel, ICellModel, IClientSession, IDefaultConnection, NotebookContentChange } from 'sql/workbench/contrib/notebook/browser/models/modelInterfaces';
|
||||
import { NotebookChangeType, CellType } from 'sql/workbench/contrib/notebook/common/models/contracts';
|
||||
import { INotebookManager } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { INotebookManager, INotebookService, INotebookEditor, ILanguageMagic, INotebookProvider, INavigationProvider } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { ISingleNotebookEditOperation } from 'sql/workbench/api/common/sqlExtHostTypes';
|
||||
import { IStandardKernelWithProvider } from 'sql/workbench/contrib/notebook/browser/models/notebookUtils';
|
||||
import { URI } from 'vs/workbench/workbench.web.api';
|
||||
import { RenderMimeRegistry } from 'sql/workbench/contrib/notebook/browser/outputs/registry';
|
||||
|
||||
export class NotebookModelStub implements INotebookModel {
|
||||
constructor(private _languageInfo?: nb.ILanguageInfo) {
|
||||
@@ -139,3 +141,64 @@ export class ServerManagerStub implements nb.ServerManager {
|
||||
return this.result;
|
||||
}
|
||||
}
|
||||
|
||||
export class NotebookServiceStub implements INotebookService {
|
||||
_serviceBrand: undefined;
|
||||
onNotebookEditorAdd: Event<INotebookEditor>;
|
||||
onNotebookEditorRemove: Event<INotebookEditor>;
|
||||
onNotebookEditorRename: Event<INotebookEditor>;
|
||||
isRegistrationComplete: boolean;
|
||||
registrationComplete: Promise<void>;
|
||||
languageMagics: ILanguageMagic[];
|
||||
registerProvider(providerId: string, provider: INotebookProvider): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
unregisterProvider(providerId: string): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
registerNavigationProvider(provider: INavigationProvider): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getNavigationProvider(notebookUri: URI): INavigationProvider {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getSupportedFileExtensions(): string[] {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getProvidersForFileType(fileType: string): string[] {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getStandardKernelsForProvider(provider: string): nb.IStandardKernel[] {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getOrCreateNotebookManager(providerId: string, uri: URI): Thenable<INotebookManager> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
addNotebookEditor(editor: INotebookEditor): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
removeNotebookEditor(editor: INotebookEditor): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
listNotebookEditors(): INotebookEditor[] {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
findNotebookEditor(notebookUri: URI): INotebookEditor {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
getMimeRegistry(): RenderMimeRegistry {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
renameNotebookEditor(oldUri: URI, newUri: URI, currentEditor: INotebookEditor): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
isNotebookTrustCached(notebookUri: URI, isDirty: boolean): Promise<boolean> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
serializeNotebookStateChange(notebookUri: URI, changeType: NotebookChangeType, cell?: ICellModel): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
navigateTo(notebookUri: URI, sectionId: string): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,86 +3,204 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IConnectionProfile } from 'azdata';
|
||||
import * as assert from 'assert';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
|
||||
import { TestCapabilitiesService } from 'sql/platform/capabilities/test/common/testCapabilitiesService';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { formatServerNameWithDatabaseNameForAttachTo, getServerFromFormattedAttachToName, getDatabaseFromFormattedAttachToName } from 'sql/workbench/contrib/notebook/browser/models/notebookUtils';
|
||||
import { mssqlProviderName } from 'sql/platform/connection/common/constants';
|
||||
import { nb, ServerInfo } from 'azdata';
|
||||
import { tryMatchCellMagic, getHostAndPortFromEndpoint, isStream, getProvidersForFileName, asyncForEach, clusterEndpointsProperty, getClusterEndpoints, RawEndpoint, IEndpoint, getStandardKernelsForProvider, IStandardKernelWithProvider } from 'sql/workbench/contrib/notebook/browser/models/notebookUtils';
|
||||
import { INotebookService, DEFAULT_NOTEBOOK_FILETYPE, DEFAULT_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NotebookServiceStub } from 'sql/workbench/contrib/notebook/test/electron-browser/common';
|
||||
|
||||
suite('notebookUtils', function (): void {
|
||||
let conn: IConnectionProfile = {
|
||||
connectionName: '',
|
||||
serverName: '',
|
||||
databaseName: '',
|
||||
userName: '',
|
||||
password: '',
|
||||
authenticationType: '',
|
||||
savePassword: true,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
providerName: mssqlProviderName,
|
||||
saveProfile: true,
|
||||
id: '',
|
||||
options: {},
|
||||
azureTenantId: undefined,
|
||||
azureAccount: undefined
|
||||
const mockNotebookService = TypeMoq.Mock.ofType<INotebookService>(NotebookServiceStub);
|
||||
const defaultTestProvider = 'testDefaultProvider';
|
||||
const testProvider = 'testProvider';
|
||||
const testKernel: nb.IStandardKernel = {
|
||||
name: 'testName',
|
||||
displayName: 'testDisplayName',
|
||||
connectionProviderIds: ['testId1', 'testId2']
|
||||
};
|
||||
|
||||
test('Should format server and database name correctly for attach to', async function (): Promise<void> {
|
||||
let capabilitiesService = new TestCapabilitiesService();
|
||||
let connProfile = new ConnectionProfile(capabilitiesService, conn);
|
||||
connProfile.serverName = 'serverName';
|
||||
connProfile.databaseName = 'databaseName';
|
||||
let attachToNameFormatted = formatServerNameWithDatabaseNameForAttachTo(connProfile);
|
||||
assert.equal(attachToNameFormatted, 'serverName (databaseName)');
|
||||
function setupMockNotebookService() {
|
||||
mockNotebookService.setup(n => n.getProvidersForFileType(TypeMoq.It.isAnyString()))
|
||||
.returns((fileName, service) => {
|
||||
if (fileName === DEFAULT_NOTEBOOK_FILETYPE) {
|
||||
return [defaultTestProvider];
|
||||
} else {
|
||||
return [testProvider];
|
||||
}
|
||||
});
|
||||
|
||||
// getStandardKernelsForProvider
|
||||
mockNotebookService.setup(n => n.getStandardKernelsForProvider(TypeMoq.It.isAnyString()))
|
||||
.returns((provider) => {
|
||||
return [testKernel];
|
||||
});
|
||||
}
|
||||
|
||||
test('isStream Test', async function (): Promise<void> {
|
||||
let result = isStream(<nb.ICellOutput>{
|
||||
output_type: 'stream'
|
||||
});
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
result = isStream(<nb.ICellOutput>{
|
||||
output_type: 'display_data'
|
||||
});
|
||||
assert.strictEqual(result, false);
|
||||
|
||||
result = isStream(<nb.ICellOutput>{
|
||||
output_type: undefined
|
||||
});
|
||||
assert.strictEqual(result, false);
|
||||
});
|
||||
|
||||
test('Should format server name correctly for attach to', async function (): Promise<void> {
|
||||
let capabilitiesService = new TestCapabilitiesService();
|
||||
let connProfile = new ConnectionProfile(capabilitiesService, conn);
|
||||
connProfile.serverName = 'serverName';
|
||||
let attachToNameFormatted = formatServerNameWithDatabaseNameForAttachTo(connProfile);
|
||||
assert.equal(attachToNameFormatted, 'serverName');
|
||||
test('getProvidersForFileName Test', async function (): Promise<void> {
|
||||
setupMockNotebookService();
|
||||
|
||||
let result = getProvidersForFileName('', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [defaultTestProvider]);
|
||||
|
||||
result = getProvidersForFileName('fileWithoutExtension', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [defaultTestProvider]);
|
||||
|
||||
result = getProvidersForFileName('test.sql', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [testProvider]);
|
||||
|
||||
mockNotebookService.setup(n => n.getProvidersForFileType(TypeMoq.It.isAnyString()))
|
||||
.returns(() => undefined);
|
||||
result = getProvidersForFileName('test.sql', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [DEFAULT_NOTEBOOK_PROVIDER]);
|
||||
});
|
||||
|
||||
test('Should format server name correctly for attach to when database is undefined', async function (): Promise<void> {
|
||||
let capabilitiesService = new TestCapabilitiesService();
|
||||
let connProfile = new ConnectionProfile(capabilitiesService, conn);
|
||||
connProfile.serverName = 'serverName';
|
||||
connProfile.databaseName = undefined;
|
||||
let attachToNameFormatted = formatServerNameWithDatabaseNameForAttachTo(connProfile);
|
||||
assert.equal(attachToNameFormatted, 'serverName');
|
||||
test('getStandardKernelsForProvider Test', async function (): Promise<void> {
|
||||
setupMockNotebookService();
|
||||
|
||||
let result = getStandardKernelsForProvider(undefined, undefined);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
result = getStandardKernelsForProvider(undefined, mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
result = getStandardKernelsForProvider('testProvider', undefined);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
result = getStandardKernelsForProvider('testProvider', mockNotebookService.object);
|
||||
assert.deepStrictEqual(result, [<IStandardKernelWithProvider>{
|
||||
name: 'testName',
|
||||
displayName: 'testDisplayName',
|
||||
connectionProviderIds: ['testId1', 'testId2'],
|
||||
notebookProvider: 'testProvider'
|
||||
}]);
|
||||
});
|
||||
|
||||
test('Should format server name as empty string when server/database are undefined', async function (): Promise<void> {
|
||||
let capabilitiesService = new TestCapabilitiesService();
|
||||
let connProfile = new ConnectionProfile(capabilitiesService, conn);
|
||||
connProfile.serverName = undefined;
|
||||
connProfile.databaseName = undefined;
|
||||
let attachToNameFormatted = formatServerNameWithDatabaseNameForAttachTo(connProfile);
|
||||
assert.equal(attachToNameFormatted, '');
|
||||
test('tryMatchCellMagic Test', async function (): Promise<void> {
|
||||
let result = tryMatchCellMagic(undefined);
|
||||
assert.strictEqual(result, undefined);
|
||||
|
||||
result = tryMatchCellMagic(' ');
|
||||
assert.strictEqual(result, null);
|
||||
|
||||
result = tryMatchCellMagic('text');
|
||||
assert.strictEqual(result, null);
|
||||
|
||||
result = tryMatchCellMagic('%%sql');
|
||||
assert.strictEqual(result, 'sql');
|
||||
|
||||
result = tryMatchCellMagic('%%sql\nselect @@VERSION\nselect * from TestTable');
|
||||
assert.strictEqual(result, 'sql');
|
||||
|
||||
result = tryMatchCellMagic('%%sql\n%%help');
|
||||
assert.strictEqual(result, 'sql');
|
||||
|
||||
result = tryMatchCellMagic('%%');
|
||||
assert.strictEqual(result, null);
|
||||
|
||||
result = tryMatchCellMagic('%% sql');
|
||||
assert.strictEqual(result, null);
|
||||
});
|
||||
|
||||
test('Should extract server name when no database specified', async function (): Promise<void> {
|
||||
let serverName = getServerFromFormattedAttachToName('serverName');
|
||||
let databaseName = getDatabaseFromFormattedAttachToName('serverName');
|
||||
assert.equal(serverName, 'serverName');
|
||||
assert.equal(databaseName, '');
|
||||
test('asyncForEach Test', async function (): Promise<void> {
|
||||
let totalResult = 0;
|
||||
await asyncForEach([1, 2, 3, 4], async (value) => {
|
||||
totalResult += value;
|
||||
});
|
||||
assert.strictEqual(totalResult, 10);
|
||||
|
||||
totalResult = 0;
|
||||
await asyncForEach([], async (value) => {
|
||||
totalResult += value;
|
||||
});
|
||||
assert.strictEqual(totalResult, 0);
|
||||
|
||||
// Shouldn't throw exceptions for these cases
|
||||
await asyncForEach(undefined, async (value) => {
|
||||
totalResult += value;
|
||||
});
|
||||
assert.strictEqual(totalResult, 0);
|
||||
|
||||
await asyncForEach([1, 2, 3, 4], undefined);
|
||||
});
|
||||
|
||||
test('Should extract server and database name', async function (): Promise<void> {
|
||||
let serverName = getServerFromFormattedAttachToName('serverName (databaseName)');
|
||||
let databaseName = getDatabaseFromFormattedAttachToName('serverName (databaseName)');
|
||||
assert.equal(serverName, 'serverName');
|
||||
assert.equal(databaseName, 'databaseName');
|
||||
test('getClusterEndpoints Test', async function (): Promise<void> {
|
||||
let serverInfo = <ServerInfo>{
|
||||
options: {}
|
||||
};
|
||||
|
||||
serverInfo.options[clusterEndpointsProperty] = undefined;
|
||||
let result = getClusterEndpoints(serverInfo);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
serverInfo.options[clusterEndpointsProperty] = [];
|
||||
result = getClusterEndpoints(serverInfo);
|
||||
assert.deepStrictEqual(result, []);
|
||||
|
||||
let testEndpoint = <RawEndpoint>{
|
||||
serviceName: 'testName',
|
||||
description: 'testDescription',
|
||||
endpoint: 'testEndpoint',
|
||||
protocol: 'testProtocol',
|
||||
ipAddress: 'testIpAddress',
|
||||
port: 1433
|
||||
};
|
||||
serverInfo.options[clusterEndpointsProperty] = [testEndpoint];
|
||||
result = getClusterEndpoints(serverInfo);
|
||||
assert.deepStrictEqual(result, [<IEndpoint>{
|
||||
serviceName: testEndpoint.serviceName,
|
||||
description: testEndpoint.description,
|
||||
endpoint: testEndpoint.endpoint,
|
||||
protocol: testEndpoint.protocol
|
||||
}]);
|
||||
|
||||
testEndpoint.endpoint = undefined;
|
||||
result = getClusterEndpoints(serverInfo);
|
||||
assert.deepStrictEqual(result, [<IEndpoint>{
|
||||
serviceName: testEndpoint.serviceName,
|
||||
description: testEndpoint.description,
|
||||
endpoint: 'https://testIpAddress:1433',
|
||||
protocol: testEndpoint.protocol
|
||||
}]);
|
||||
});
|
||||
|
||||
test('Should extract server and database name with other parentheses', async function (): Promise<void> {
|
||||
let serverName = getServerFromFormattedAttachToName('serv()erName (databaseName)');
|
||||
let databaseName = getDatabaseFromFormattedAttachToName('serv()erName (databaseName)');
|
||||
assert.equal(serverName, 'serv()erName');
|
||||
assert.equal(databaseName, 'databaseName');
|
||||
test('getHostAndPortFromEndpoint Test', async function (): Promise<void> {
|
||||
let result = getHostAndPortFromEndpoint('https://localhost:1433');
|
||||
assert.strictEqual(result.host, 'localhost');
|
||||
assert.strictEqual(result.port, '1433');
|
||||
|
||||
result = getHostAndPortFromEndpoint('tcp://localhost,12345');
|
||||
assert.strictEqual(result.host, 'localhost');
|
||||
assert.strictEqual(result.port, '12345');
|
||||
|
||||
result = getHostAndPortFromEndpoint('tcp://localhost');
|
||||
assert.strictEqual(result.host, 'localhost');
|
||||
assert.strictEqual(result.port, undefined);
|
||||
|
||||
result = getHostAndPortFromEndpoint('localhost');
|
||||
assert.strictEqual(result.host, '');
|
||||
assert.strictEqual(result.port, undefined);
|
||||
|
||||
result = getHostAndPortFromEndpoint('localhost:1433');
|
||||
assert.strictEqual(result.host, '');
|
||||
assert.strictEqual(result.port, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user