Add some unit tests for PyPiClient. (#11442)

This commit is contained in:
Cory Rivera
2020-07-21 10:56:25 -07:00
committed by GitHub
parent f780db907d
commit 7461cf844d
4 changed files with 28 additions and 10 deletions

View File

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

View File

@@ -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) {
}
/**

View File

@@ -9,11 +9,11 @@ import * as constants from '../common/constants';
const localize = nls.loadMessageBundle();
export interface IPiPyClient {
export interface IPyPiClient {
fetchPypiPackage(packageName: string): Promise<any>;
}
export class PiPyClient implements IPiPyClient {
export class PyPiClient implements IPyPiClient {
private readonly RequestTimeout = 10000;
private getLink(packageName: string): string {

View File

@@ -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<void> {
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<IPiPyClient> {
let mockInstance = TypeMoq.Mock.ofType(PiPyClient);
function createPipyClient(testContext: TestContext): TypeMoq.IMock<IPyPiClient> {
let mockInstance = TypeMoq.Mock.ofType(PyPiClient);
mockInstance.setup(x => x.fetchPypiPackage(TypeMoq.It.isAny())).returns((packageName) =>
testContext.piPyClient.fetchPypiPackage(packageName));
return mockInstance;