From 7461cf844de65b08ce16ba9bd403d229b18d6a72 Mon Sep 17 00:00:00 2001 From: Cory Rivera Date: Tue, 21 Jul 2020 10:56:25 -0700 Subject: [PATCH] Add some unit tests for PyPiClient. (#11442) --- .../notebook/src/jupyter/jupyterController.ts | 4 +-- .../jupyter/localPipPackageManageProvider.ts | 4 +-- .../jupyter/{pipyClient.ts => pypiClient.ts} | 4 +-- .../localPackageManageProvider.test.ts | 26 ++++++++++++++++--- 4 files changed, 28 insertions(+), 10 deletions(-) rename extensions/notebook/src/jupyter/{pipyClient.ts => pypiClient.ts} (94%) diff --git a/extensions/notebook/src/jupyter/jupyterController.ts b/extensions/notebook/src/jupyter/jupyterController.ts index 91dd72d591..52a4cf23cb 100644 --- a/extensions/notebook/src/jupyter/jupyterController.ts +++ b/extensions/notebook/src/jupyter/jupyterController.ts @@ -28,7 +28,7 @@ import { IPackageManageProvider } from '../types'; import { LocalPipPackageManageProvider } from './localPipPackageManageProvider'; import { LocalCondaPackageManageProvider } from './localCondaPackageManageProvider'; import { ManagePackagesDialogModel, ManagePackageDialogOptions } from '../dialog/managePackages/managePackagesDialogModel'; -import { PiPyClient } from './pipyClient'; +import { PyPiClient } from './pypiClient'; import { ConfigurePythonDialog } from '../dialog/configurePython/configurePythonDialog'; let untitledCounter = 0; @@ -243,7 +243,7 @@ export class JupyterController implements vscode.Disposable { } private registerDefaultPackageManageProviders(): void { - this.registerPackageManager(LocalPipPackageManageProvider.ProviderId, new LocalPipPackageManageProvider(this._jupyterInstallation, new PiPyClient())); + this.registerPackageManager(LocalPipPackageManageProvider.ProviderId, new LocalPipPackageManageProvider(this._jupyterInstallation, new PyPiClient())); this.registerPackageManager(LocalCondaPackageManageProvider.ProviderId, new LocalCondaPackageManageProvider(this._jupyterInstallation)); } diff --git a/extensions/notebook/src/jupyter/localPipPackageManageProvider.ts b/extensions/notebook/src/jupyter/localPipPackageManageProvider.ts index 4d3372e93f..b071b41313 100644 --- a/extensions/notebook/src/jupyter/localPipPackageManageProvider.ts +++ b/extensions/notebook/src/jupyter/localPipPackageManageProvider.ts @@ -7,7 +7,7 @@ import { IPackageManageProvider, IPackageDetails, IPackageTarget, IPackageOvervi import { IJupyterServerInstallation } from './jupyterServerInstallation'; import * as constants from '../common/constants'; import * as utils from '../common/utils'; -import { IPiPyClient } from './pipyClient'; +import { IPyPiClient } from './pypiClient'; export class LocalPipPackageManageProvider implements IPackageManageProvider { @@ -18,7 +18,7 @@ export class LocalPipPackageManageProvider implements IPackageManageProvider { constructor( private jupyterInstallation: IJupyterServerInstallation, - private pipyClient: IPiPyClient) { + private pipyClient: IPyPiClient) { } /** diff --git a/extensions/notebook/src/jupyter/pipyClient.ts b/extensions/notebook/src/jupyter/pypiClient.ts similarity index 94% rename from extensions/notebook/src/jupyter/pipyClient.ts rename to extensions/notebook/src/jupyter/pypiClient.ts index 2a82ceb740..523c640303 100644 --- a/extensions/notebook/src/jupyter/pipyClient.ts +++ b/extensions/notebook/src/jupyter/pypiClient.ts @@ -9,11 +9,11 @@ import * as constants from '../common/constants'; const localize = nls.loadMessageBundle(); -export interface IPiPyClient { +export interface IPyPiClient { fetchPypiPackage(packageName: string): Promise; } -export class PiPyClient implements IPiPyClient { +export class PyPiClient implements IPyPiClient { private readonly RequestTimeout = 10000; private getLink(packageName: string): string { diff --git a/extensions/notebook/src/test/managePackages/localPackageManageProvider.test.ts b/extensions/notebook/src/test/managePackages/localPackageManageProvider.test.ts index dec65c6b1e..840be2bb30 100644 --- a/extensions/notebook/src/test/managePackages/localPackageManageProvider.test.ts +++ b/extensions/notebook/src/test/managePackages/localPackageManageProvider.test.ts @@ -12,11 +12,11 @@ import { JupyterServerInstallation, PythonPkgDetails, IJupyterServerInstallation import { LocalCondaPackageManageProvider } from '../../jupyter/localCondaPackageManageProvider'; import * as constants from '../../common/constants'; import { LocalPipPackageManageProvider } from '../../jupyter/localPipPackageManageProvider'; -import { IPiPyClient, PiPyClient } from '../../jupyter/pipyClient'; +import { IPyPiClient, PyPiClient } from '../../jupyter/pypiClient'; interface TestContext { serverInstallation: IJupyterServerInstallation; - piPyClient: IPiPyClient; + piPyClient: IPyPiClient; } describe('Manage Package Providers', () => { @@ -183,6 +183,24 @@ describe('Manage Package Providers', () => { }); }); + it('Fetch pypi package test', async function (): Promise { + let pypiClient = new PyPiClient(); + + // Fetch a package required by notebooks + let pkgName = 'jupyter'; + let packageJsonResult = await pypiClient.fetchPypiPackage(pkgName); + should(packageJsonResult).not.be.undefined(); + + let packageInfo = JSON.parse(packageJsonResult); + should(packageInfo).not.be.undefined(); + should(packageInfo.info).not.be.undefined(); + should(packageInfo.info.name).not.be.undefined(); + should(packageInfo.info.name.toString().toLowerCase()).equal(pkgName); + + // Try to fetch an empty string to ensure retrieval fails + await should(pypiClient.fetchPypiPackage('')).be.rejected(); + }); + function createContext(): TestContext { return { serverInstallation: { @@ -221,8 +239,8 @@ describe('Manage Package Providers', () => { return mockInstance; } - function createPipyClient(testContext: TestContext): TypeMoq.IMock { - let mockInstance = TypeMoq.Mock.ofType(PiPyClient); + function createPipyClient(testContext: TestContext): TypeMoq.IMock { + let mockInstance = TypeMoq.Mock.ofType(PyPiClient); mockInstance.setup(x => x.fetchPypiPackage(TypeMoq.It.isAny())).returns((packageName) => testContext.piPyClient.fetchPypiPackage(packageName)); return mockInstance;