diff --git a/src/app-profile.ts b/src/app-profile.ts index 611b965cc..24f8ee75f 100644 --- a/src/app-profile.ts +++ b/src/app-profile.ts @@ -67,9 +67,13 @@ export interface DeleteAppProfileOptions { export type CreateAppProfileCallback = ( err: ServiceError | null, - appProfile?: AppProfile + appProfile?: AppProfile, + apiResponse?: google.bigtable.admin.v2.IAppProfile ) => void; -export type CreateAppProfileResponse = [AppProfile]; +export type CreateAppProfileResponse = [ + AppProfile, + google.bigtable.admin.v2.IAppProfile +]; export type DeleteAppProfileCallback = ( err: ServiceError | null, apiResponse?: google.protobuf.Empty diff --git a/src/cluster.ts b/src/cluster.ts index e6dc9aca7..d42df1dd9 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -46,7 +46,7 @@ export type ApiResponse = [IOperation]; export type CreateClusterResponse = [ICluster, GaxOperation, IOperation]; export type BooleanResponse = [boolean]; export type GetClusterResponse = [ICluster, IOperation]; -export type GetClustersResponse = [ICluster[], IOperation]; +export type GetClustersResponse = [Cluster[], IOperation]; export type GetClusterMetadataResponse = [ICluster, IOperation]; export type SetClusterMetadataResponse = [Operation, google.protobuf.Empty]; @@ -56,7 +56,7 @@ export type ExistsClusterCallback = GenericCallback; export type GetClusterCallback = GenericClusterCallback; export type GetClustersCallback = ( err: ServiceError | null, - clusters?: ICluster[], + clusters?: Cluster[], apiResponse?: google.bigtable.admin.v2.IListClustersResponse ) => void; export type SetClusterMetadataCallback = GenericOperationCallback< diff --git a/src/family.ts b/src/family.ts index 75905bb7f..47b81c4ea 100644 --- a/src/family.ts +++ b/src/family.ts @@ -34,7 +34,7 @@ export interface InstanceCallback { } export interface GcRule { - age?: google.protobuf.IDuration; + age?: google.protobuf.IDuration | number; versions?: number; rule?: GcRule; union?: boolean; @@ -166,7 +166,7 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.`); if (ruleObj.age) { rules.push({ - maxAge: ruleObj.age, + maxAge: ruleObj.age as google.protobuf.IDuration, }); } diff --git a/src/filter.ts b/src/filter.ts index 348d7bde3..7fe22783a 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -43,8 +43,8 @@ export interface BoundData { } export interface Time { - start: Date; - end: Date; + start: Date | number; + end: Date | number; } // tslint:disable-next-line no-any @@ -136,7 +136,15 @@ export class Filter { * // => '(a|b|c)' */ static convertToRegExpString( - regex: RegExp | string | string[] | Buffer | number + regex: + | RegExp + | RegExp[] + | string + | string[] + | Buffer + | Buffer[] + | number + | number[] ): string | Buffer { if (is.regexp(regex)) { return regex.toString().replace(/^\/|\/$/g, ''); @@ -199,7 +207,11 @@ export class Filter { * // startTest2Exclusive: 'value3' * // } */ - static createRange(start: BoundData, end: BoundData, key: string) { + static createRange( + start: BoundData | null, + end: BoundData | null, + key: string + ) { const range = {}; if (start) { @@ -521,7 +533,17 @@ export class Filter { * } * ]; */ - family(family: RegExp): void { + family( + family: + | RegExp + | string + | number + | Buffer + | RegExp[] + | string[] + | number[] + | Buffer[] + ): void { const f = Filter.convertToRegExpString(family); this.set('familyNameRegexFilter', f); } diff --git a/src/instance.ts b/src/instance.ts index c75d03379..65d100418 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -187,16 +187,16 @@ Please use the format 'my-instance' or '${bigtable.projectName}/instances/my-ins * Instance.getTypeType_('production'); * // 1 */ - static getTypeType_(type: string): number { + static getTypeType_(type?: string): number { const types = { unspecified: 0, production: 1, development: 2, } as {[index: string]: number}; - if (is.string(type)) { + if (typeof type === 'string') { type = type.toLowerCase(); } - return types[type] || types.unspecified; + return types[type!] || types.unspecified; } /** diff --git a/src/mutation.ts b/src/mutation.ts index 82bf0c91d..02d1c3716 100644 --- a/src/mutation.ts +++ b/src/mutation.ts @@ -23,7 +23,8 @@ export type IMutateRowRequest = btTypes.bigtable.v2.IMutateRowRequest; export type ISetCell = btTypes.bigtable.v2.Mutation.ISetCell; export type Bytes = string | Buffer; -export type Data = Value | Value[] | MutationSettingsObj; +// tslint:disable-next-line:no-any +export type Data = any; export interface JsonObj { [k: string]: string | JsonObj; } @@ -158,7 +159,7 @@ export class Mutation { * @returns {object} * @private */ - static createTimeRange(start: Date, end: Date): TimeRange { + static createTimeRange(start: Date | number, end: Date | number): TimeRange { const range: TimeRange = {}; if (is.date(start)) { diff --git a/src/row.ts b/src/row.ts index 4a4eceb9a..3a93424e6 100644 --- a/src/row.ts +++ b/src/row.ts @@ -199,7 +199,7 @@ export class Row { * // } * // } */ - static formatChunks_(chunks: Chunk[], options: ConvertFromBytesUserOptions) { + static formatChunks_(chunks: Chunk[], options?: ConvertFromBytesUserOptions) { const rows: Row[] = []; let familyName: string | null; let qualifierName: string | null; diff --git a/test/app-profile.ts b/test/app-profile.ts index f0078f89a..0d97e284b 100644 --- a/test/app-profile.ts +++ b/test/app-profile.ts @@ -14,12 +14,15 @@ import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; -import {describe, it} from 'mocha'; +import {describe, it, before, beforeEach} from 'mocha'; import * as proxyquire from 'proxyquire'; +import {CallOptions} from 'google-gax'; + +/* tslint:disable:no-any */ let promisified = false; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass) { + promisifyAll(klass: Function) { if (klass.name === 'AppProfile') { promisified = true; } @@ -37,13 +40,13 @@ describe('Bigtable/AppProfile', () => { const APP_PROFILE_NAME = `${INSTANCE.name}/appProfiles/${APP_PROFILE_ID}`; // tslint:disable-next-line variable-name - let AppProfile; - let appProfile; + let AppProfile: any; + let appProfile: any; class FakeCluster { - instance; - id; - constructor(instance, id) { + instance: any; + id: any; + constructor(instance: any, id: any) { this.instance = instance; this.id = id; } @@ -156,7 +159,11 @@ describe('Bigtable/AppProfile', () => { it('should call createAppProfile from instance', done => { const options = {}; - appProfile.instance.createAppProfile = (id, options_, callback) => { + appProfile.instance.createAppProfile = ( + id: any, + options_: any, + callback: any + ) => { assert.strictEqual(id, appProfile.id); assert.strictEqual(options_, options); callback(); @@ -166,7 +173,11 @@ describe('Bigtable/AppProfile', () => { }); it('should not require options', done => { - appProfile.instance.createAppProfile = (id, options, callback) => { + appProfile.instance.createAppProfile = ( + id: any, + options: any, + callback: any + ) => { assert.deepStrictEqual(options, {}); callback(); }; @@ -177,7 +188,7 @@ describe('Bigtable/AppProfile', () => { describe('delete', () => { it('should make the correct request', done => { - appProfile.bigtable.request = (config, callback) => { + appProfile.bigtable.request = (config: any, callback: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'deleteAppProfile'); @@ -194,7 +205,7 @@ describe('Bigtable/AppProfile', () => { it('should accept gaxOptions', done => { const gaxOptions = {}; - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; @@ -203,7 +214,7 @@ describe('Bigtable/AppProfile', () => { }); it('should accept ignoreWarnings', done => { - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert.strictEqual(config.reqOpts.ignoreWarnings, true); done(); }; @@ -214,7 +225,7 @@ describe('Bigtable/AppProfile', () => { describe('exists', () => { it('should not require gaxOptions', done => { - appProfile.getMetadata = gaxOptions => { + appProfile.getMetadata = (gaxOptions: CallOptions) => { assert.deepStrictEqual(gaxOptions, {}); done(); }; @@ -225,7 +236,7 @@ describe('Bigtable/AppProfile', () => { it('should pass gaxOptions to getMetadata', done => { const gaxOptions = {}; - appProfile.getMetadata = gaxOptions_ => { + appProfile.getMetadata = (gaxOptions_: CallOptions) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; @@ -238,11 +249,14 @@ describe('Bigtable/AppProfile', () => { const error: any = new Error('Error.'); error.code = 5; - appProfile.getMetadata = (gaxOptions, callback) => { + appProfile.getMetadata = ( + gaxOptions: CallOptions, + callback: Function + ) => { callback(error); }; - appProfile.exists((err, exists) => { + appProfile.exists((err: Error, exists: boolean) => { assert.ifError(err); assert.strictEqual(exists, false); done(); @@ -254,22 +268,28 @@ describe('Bigtable/AppProfile', () => { const error: any = new Error('Error.'); error.code = 'NOT-5'; - appProfile.getMetadata = (gaxOptions, callback) => { + appProfile.getMetadata = ( + gaxOptions: CallOptions, + callback: Function + ) => { callback(error); }; - appProfile.exists(err => { + appProfile.exists((err: Error) => { assert.strictEqual(err, error); done(); }); }); it('should return true if no error', done => { - appProfile.getMetadata = (gaxOptions, callback) => { + appProfile.getMetadata = ( + gaxOptions: CallOptions, + callback: Function + ) => { callback(null, {}); }; - appProfile.exists((err, exists) => { + appProfile.exists((err: Error, exists: boolean) => { assert.ifError(err); assert.strictEqual(exists, true); done(); @@ -281,7 +301,7 @@ describe('Bigtable/AppProfile', () => { it('should call getMetadata', done => { const gaxOptions = {}; - appProfile.getMetadata = gaxOptions_ => { + appProfile.getMetadata = (gaxOptions_: CallOptions) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; @@ -290,7 +310,7 @@ describe('Bigtable/AppProfile', () => { }); it('should not require gaxOptions', done => { - appProfile.getMetadata = gaxOptions => { + appProfile.getMetadata = (gaxOptions: CallOptions) => { assert.deepStrictEqual(gaxOptions, {}); done(); }; @@ -301,11 +321,14 @@ describe('Bigtable/AppProfile', () => { it('should return an error from getMetadata', done => { const error = new Error('Error.'); - appProfile.getMetadata = (gaxOptions, callback) => { + appProfile.getMetadata = ( + gaxOptions: CallOptions, + callback: Function + ) => { callback(error); }; - appProfile.get(err => { + appProfile.get((err: Error) => { assert.strictEqual(err, error); done(); }); @@ -314,11 +337,14 @@ describe('Bigtable/AppProfile', () => { it('should return self and API response', done => { const metadata = {}; - appProfile.getMetadata = (gaxOptions, callback) => { + appProfile.getMetadata = ( + gaxOptions: CallOptions, + callback: Function + ) => { callback(null, metadata); }; - appProfile.get((err, appProfile_, metadata_) => { + appProfile.get((err: Error, appProfile_: {}, metadata_: {}) => { assert.ifError(err); assert.strictEqual(appProfile_, appProfile); assert.strictEqual(metadata_, metadata); @@ -329,7 +355,7 @@ describe('Bigtable/AppProfile', () => { describe('getMetadata', () => { it('should make correct request', done => { - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'getAppProfile'); @@ -348,7 +374,7 @@ describe('Bigtable/AppProfile', () => { it('should accept gaxOptions', done => { const gaxOptions = {}; - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; @@ -359,7 +385,7 @@ describe('Bigtable/AppProfile', () => { it('should update metadata', done => { const metadata = {}; - appProfile.bigtable.request = (config, callback) => { + appProfile.bigtable.request = (config: {}, callback: Function) => { callback(null, metadata); }; @@ -372,11 +398,11 @@ describe('Bigtable/AppProfile', () => { it('should execute callback with original arguments', done => { const args = [{}, {}, {}]; - appProfile.bigtable.request = (config, callback) => { + appProfile.bigtable.request = (config: {}, callback: Function) => { callback.apply(null, args); }; - appProfile.getMetadata((...argies) => { + appProfile.getMetadata((...argies: Array<{}>) => { assert.deepStrictEqual([].slice.call(argies), args); done(); }); @@ -385,7 +411,7 @@ describe('Bigtable/AppProfile', () => { describe('setMetadata', () => { it('should provide the proper request options', done => { - appProfile.bigtable.request = (config, callback) => { + appProfile.bigtable.request = (config: any, callback: Function) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'updateAppProfile'); assert.strictEqual(config.reqOpts.appProfile.name, APP_PROFILE_NAME); @@ -398,7 +424,7 @@ describe('Bigtable/AppProfile', () => { it('should respect the description option', done => { const options = {description: 'my-description'}; - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert( config.reqOpts.updateMask.paths.indexOf('description') !== -1, `updateMask does not should include 'description'` @@ -416,7 +442,7 @@ describe('Bigtable/AppProfile', () => { it('should respect the ignoreWarnings option', done => { const options = {ignoreWarnings: true}; - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert.strictEqual(config.reqOpts.ignoreWarnings, true); done(); }; @@ -431,7 +457,7 @@ describe('Bigtable/AppProfile', () => { it(`has an 'any' value`, done => { const options = {routing: 'any'}; - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert( config.reqOpts.updateMask.paths.indexOf( 'multi_cluster_routing_use_any' @@ -451,7 +477,7 @@ describe('Bigtable/AppProfile', () => { it(`has a cluster value`, done => { const options = {routing: cluster}; - appProfile.bigtable.request = config => { + appProfile.bigtable.request = (config: any) => { assert( config.reqOpts.updateMask.paths.indexOf( 'single_cluster_routing' @@ -471,10 +497,10 @@ describe('Bigtable/AppProfile', () => { it('should execute callback with all arguments', done => { const args = [{}, {}, {}]; - appProfile.bigtable.request = (config, callback) => { + appProfile.bigtable.request = (config: {}, callback: Function) => { callback.apply(null, args); }; - appProfile.setMetadata({}, (...argies) => { + appProfile.setMetadata({}, (...argies: Array<{}>) => { assert.deepStrictEqual([].slice.call(argies), args); done(); }); diff --git a/test/chunktransformer.ts b/test/chunktransformer.ts index 29724d4f7..0d8bf6faf 100644 --- a/test/chunktransformer.ts +++ b/test/chunktransformer.ts @@ -13,13 +13,14 @@ // limitations under the License. import * as assert from 'assert'; -import {describe, it} from 'mocha'; +import {describe, it, before, beforeEach, afterEach} from 'mocha'; import * as Long from 'long'; import * as proxyquire from 'proxyquire'; import * as sn from 'sinon'; import {RowStateEnum} from '../src/chunktransformer.js'; import {Mutation} from '../src/mutation.js'; +import {Row} from '../src/row.js'; const ROW_ID = 'my-row'; const CONVERTED_ROW_ID = 'my-converted-row'; @@ -40,10 +41,11 @@ const FakeMutation = { }; describe('Bigtable/ChunkTransformer', () => { - // tslint:disable-next-line variable-name - let ChunkTransformer; - let chunkTransformer; - let rows; + // tslint:disable-next-line variable-name no-any + let ChunkTransformer: any; + // tslint:disable:no-any + let chunkTransformer: any; + let rows: Row[]; before(() => { ChunkTransformer = proxyquire('../src/chunktransformer.js', { './mutation.js': {Mutation: FakeMutation}, @@ -52,7 +54,7 @@ describe('Bigtable/ChunkTransformer', () => { beforeEach(() => { chunkTransformer = new ChunkTransformer(); rows = []; - chunkTransformer.push = row => { + chunkTransformer.push = (row: Row) => { rows.push(row); }; }); @@ -97,10 +99,10 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('processNewRow', () => { - let processNewRowSpy; - let resetSpy; - let commitSpy; - let destroySpy; + let processNewRowSpy: sn.SinonSpy; + let resetSpy: sn.SinonSpy; + let commitSpy: sn.SinonSpy; + let destroySpy: sn.SinonSpy; beforeEach(() => { processNewRowSpy = sinon.spy(chunkTransformer, 'processNewRow'); resetSpy = sinon.spy(chunkTransformer, 'reset'); @@ -290,10 +292,10 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('processRowInProgress', () => { - let processRowInProgressSpy; - let resetSpy; - let commitSpy; - let destroySpy; + let processRowInProgressSpy: sn.SinonSpy; + let resetSpy: sn.SinonSpy; + let commitSpy: sn.SinonSpy; + let destroySpy: sn.SinonSpy; beforeEach(() => { processRowInProgressSpy = sinon.spy( chunkTransformer, @@ -668,10 +670,10 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('processCellInProgress', () => { - let processCellInProgressSpy; - let resetSpy; - let commitSpy; - let destroySpy; + let processCellInProgressSpy: sn.SinonSpy; + let resetSpy: sn.SinonSpy; + let commitSpy: sn.SinonSpy; + let destroySpy: sn.SinonSpy; beforeEach(() => { processCellInProgressSpy = sinon.spy( chunkTransformer, @@ -914,9 +916,9 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('_flush', () => { - let _flushSpy; - let callback; - let destroySpy; + let _flushSpy: sn.SinonSpy; + let callback: sn.SinonSpy; + let destroySpy: sn.SinonSpy; beforeEach(() => { _flushSpy = sinon.spy(chunkTransformer, '_flush'); callback = sinon.spy(); @@ -939,10 +941,10 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('_transform', () => { - let callback; - let processNewRowSpy; - let processRowInProgressSpy; - let processCellInProgressSpy; + let callback: sn.SinonSpy; + let processNewRowSpy: sn.SinonSpy; + let processRowInProgressSpy: sn.SinonSpy; + let processCellInProgressSpy: sn.SinonSpy; beforeEach(() => { callback = sinon.spy(); processNewRowSpy = sinon.spy(chunkTransformer, 'processNewRow'); @@ -1062,7 +1064,7 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('commit', () => { - let resetSpy; + let resetSpy: sn.SinonSpy; beforeEach(() => { resetSpy = sinon.spy(chunkTransformer, 'reset'); }); @@ -1116,7 +1118,7 @@ describe('Bigtable/ChunkTransformer', () => { }); }); describe('moveToNextState', () => { - let commitSpy; + let commitSpy: sn.SinonSpy; beforeEach(() => { commitSpy = sinon.spy(chunkTransformer, 'commit'); }); @@ -1200,7 +1202,7 @@ describe('Bigtable/ChunkTransformer', () => { describe('destroy', () => { it('should emit error when destroy is called with error', done => { const error = new Error('destroy error'); - chunkTransformer.on('error', err => { + chunkTransformer.on('error', (err: Error) => { assert.strictEqual(err, error, 'did not emit error'); done(); }); diff --git a/test/cluster.ts b/test/cluster.ts index 81768384f..fb5433f30 100644 --- a/test/cluster.ts +++ b/test/cluster.ts @@ -16,10 +16,13 @@ import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it} from 'mocha'; import * as proxyquire from 'proxyquire'; +import {CallOptions} from 'google-gax'; + +// tslint:disable no-any let promisified = false; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass) { + promisifyAll(klass: Function) { if (klass.name === 'Cluster') { promisified = true; } @@ -37,8 +40,8 @@ describe('Bigtable/Cluster', () => { const CLUSTER_NAME = `${INSTANCE.name}/clusters/${CLUSTER_ID}`; // tslint:disable-next-line variable-name - let Cluster; - let cluster; + let Cluster: any; + let cluster: any; before(() => { Cluster = proxyquire('../src/cluster.js', { @@ -116,7 +119,7 @@ describe('Bigtable/Cluster', () => { }); describe('getStorageType_', () => { - const types = { + const types: any = { unspecified: 0, ssd: 1, hdd: 2, @@ -141,7 +144,11 @@ describe('Bigtable/Cluster', () => { it('should call createCluster from instance', done => { const options = {}; - cluster.instance.createCluster = (id, options_, callback) => { + cluster.instance.createCluster = ( + id: string, + options_: {}, + callback: Function + ) => { assert.strictEqual(id, cluster.id); assert.strictEqual(options_, options); callback(); // done() @@ -151,7 +158,11 @@ describe('Bigtable/Cluster', () => { }); it('should not require options', done => { - cluster.instance.createCluster = (id, options, callback) => { + cluster.instance.createCluster = ( + id: string, + options: {}, + callback: Function + ) => { assert.deepStrictEqual(options, {}); callback(); // done() }; @@ -162,7 +173,7 @@ describe('Bigtable/Cluster', () => { describe('delete', () => { it('should make the correct request', done => { - cluster.bigtable.request = (config, callback) => { + cluster.bigtable.request = (config: any, callback: Function) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'deleteCluster'); @@ -181,7 +192,7 @@ describe('Bigtable/Cluster', () => { it('should accept gaxOptions', done => { const gaxOptions = {}; - cluster.bigtable.request = config => { + cluster.bigtable.request = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; @@ -192,7 +203,7 @@ describe('Bigtable/Cluster', () => { describe('exists', () => { it('should not require gaxOptions', done => { - cluster.getMetadata = gaxOptions => { + cluster.getMetadata = (gaxOptions: CallOptions) => { assert.deepStrictEqual(gaxOptions, {}); done(); }; @@ -203,8 +214,8 @@ describe('Bigtable/Cluster', () => { it('should pass gaxOptions to getMetadata', done => { const gaxOptions = {}; - cluster.getMetadata = gaxOptions_ => { - assert.strictEqual(gaxOptions_, gaxOptions); + cluster.getMetadata = (gaxOptions_: CallOptions) => { + assert.strictEqual(gaxOptions, gaxOptions); done(); }; @@ -216,11 +227,11 @@ describe('Bigtable/Cluster', () => { const error: any = new Error('Error.'); error.code = 5; - cluster.getMetadata = (gaxOptions, callback) => { + cluster.getMetadata = (gaxOptions: CallOptions, callback: Function) => { callback(error); }; - cluster.exists((err, exists) => { + cluster.exists((err: Error, exists: boolean) => { assert.ifError(err); assert.strictEqual(exists, false); done(); @@ -231,23 +242,20 @@ describe('Bigtable/Cluster', () => { // tslint:disable-next-line no-any const error: any = new Error('Error.'); error.code = 'NOT-5'; - - cluster.getMetadata = (gaxOptions, callback) => { + cluster.getMetadata = (_: CallOptions, callback: Function) => { callback(error); }; - - cluster.exists(err => { + cluster.exists((err: Error) => { assert.strictEqual(err, error); done(); }); }); it('should return true if no error', done => { - cluster.getMetadata = (gaxOptions, callback) => { + cluster.getMetadata = (gaxOptions: CallOptions, callback: Function) => { callback(null, {}); }; - - cluster.exists((err, exists) => { + cluster.exists((err: Error, exists: boolean) => { assert.ifError(err); assert.strictEqual(exists, true); done(); @@ -258,17 +266,15 @@ describe('Bigtable/Cluster', () => { describe('get', () => { it('should call getMetadata', done => { const gaxOptions = {}; - - cluster.getMetadata = gaxOptions_ => { + cluster.getMetadata = (gaxOptions_: {}) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; - cluster.get(gaxOptions, assert.ifError); }); it('should not require gaxOptions', done => { - cluster.getMetadata = gaxOptions => { + cluster.getMetadata = (gaxOptions: CallOptions) => { assert.deepStrictEqual(gaxOptions, {}); done(); }; @@ -279,11 +285,11 @@ describe('Bigtable/Cluster', () => { it('should return an error from getMetadata', done => { const error = new Error('Error.'); - cluster.getMetadata = (gaxOptions, callback) => { + cluster.getMetadata = (gaxOptions: CallOptions, callback: Function) => { callback(error); }; - cluster.get(err => { + cluster.get((err: Error) => { assert.strictEqual(err, error); done(); }); @@ -292,11 +298,11 @@ describe('Bigtable/Cluster', () => { it('should return self and API response', done => { const metadata = {}; - cluster.getMetadata = (gaxOptions, callback) => { + cluster.getMetadata = (gaxOptions: CallOptions, callback: Function) => { callback(null, metadata); }; - cluster.get((err, cluster_, metadata_) => { + cluster.get((err: Error, cluster_: {}, metadata_: {}) => { assert.ifError(err); assert.strictEqual(cluster_, cluster); assert.strictEqual(metadata_, metadata); @@ -307,40 +313,32 @@ describe('Bigtable/Cluster', () => { describe('getMetadata', () => { it('should make correct request', done => { - cluster.bigtable.request = config => { + cluster.bigtable.request = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'getCluster'); - assert.deepStrictEqual(config.reqOpts, { name: cluster.name, }); - assert.deepStrictEqual(config.gaxOpts, {}); - done(); }; - cluster.getMetadata(assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - cluster.bigtable.request = config => { + cluster.bigtable.request = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - cluster.getMetadata(gaxOptions, assert.ifError); }); it('should update metadata', done => { const metadata = {}; - - cluster.bigtable.request = (config, callback) => { + cluster.bigtable.request = (config: {}, callback: Function) => { callback(null, metadata); }; - cluster.getMetadata(() => { assert.strictEqual(cluster.metadata, metadata); done(); @@ -349,12 +347,10 @@ describe('Bigtable/Cluster', () => { it('should execute callback with original arguments', done => { const args = [{}, {}]; - - cluster.bigtable.request = (config, callback) => { + cluster.bigtable.request = (config: {}, callback: Function) => { callback.apply(null, args); }; - - cluster.getMetadata((...argsies) => { + cluster.getMetadata((...argsies: Array<{}>) => { assert.deepStrictEqual([].slice.call(argsies), args); done(); }); @@ -363,7 +359,7 @@ describe('Bigtable/Cluster', () => { describe('setMetadata', () => { it('should provide the proper request options', done => { - cluster.bigtable.request = (config, callback) => { + cluster.bigtable.request = (config: any, callback: Function) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'updateCluster'); assert.strictEqual(config.reqOpts.name, CLUSTER_NAME); @@ -381,13 +377,13 @@ describe('Bigtable/Cluster', () => { const getLocation = Cluster.getLocation_; const fakeLocation = 'a/b/c/d'; - Cluster.getLocation_ = (project, location) => { + Cluster.getLocation_ = (project: string, location: string) => { assert.strictEqual(project, PROJECT_ID); assert.strictEqual(location, options.location); return fakeLocation; }; - cluster.bigtable.request = config => { + cluster.bigtable.request = (config: any) => { assert.strictEqual(config.reqOpts.location, fakeLocation); Cluster.getLocation_ = getLocation; done(); @@ -401,7 +397,7 @@ describe('Bigtable/Cluster', () => { nodes: 3, }; - cluster.bigtable.request = config => { + cluster.bigtable.request = (config: any) => { assert.strictEqual(config.reqOpts.serveNodes, options.nodes); done(); }; @@ -417,12 +413,12 @@ describe('Bigtable/Cluster', () => { const getStorageType = Cluster.getStorageType_; const fakeStorageType = 'a'; - Cluster.getStorageType_ = storage => { + Cluster.getStorageType_ = (storage: {}) => { assert.strictEqual(storage, options.storage); return fakeStorageType; }; - cluster.bigtable.request = config => { + cluster.bigtable.request = (config: any) => { assert.strictEqual(config.reqOpts.defaultStorageType, fakeStorageType); Cluster.getStorageType_ = getStorageType; done(); @@ -434,11 +430,11 @@ describe('Bigtable/Cluster', () => { it('should execute callback with all arguments', done => { const args = [{}, {}]; - cluster.bigtable.request = (config, callback) => { + cluster.bigtable.request = (config: {}, callback: Function) => { callback.apply(null, args); }; - cluster.setMetadata({}, (...argsies) => { + cluster.setMetadata({}, (...argsies: Array<{}>) => { assert.deepStrictEqual([].slice.call(argsies), args); done(); }); diff --git a/test/family.ts b/test/family.ts index c353b04a6..f61208340 100644 --- a/test/family.ts +++ b/test/family.ts @@ -16,32 +16,40 @@ import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it} from 'mocha'; import * as proxyquire from 'proxyquire'; +import * as sinon from 'sinon'; +import {google} from '../protos/protos'; +import * as fm from '../src/family'; +import {Table} from '../src/table'; let promisified = false; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass) { + promisifyAll(klass: Function) { if (klass.name === 'Family') { promisified = true; } }, }); +const sandbox = sinon.createSandbox(); + describe('Bigtable/Family', () => { const FAMILY_ID = 'family-test'; - const TABLE = { - bigtable: {}, + const TABLE = ({ + bigtable: { + request: () => {}, + }, id: 'my-table', name: 'projects/my-project/instances/my-inststance/tables/my-table', getFamilies: () => {}, createFamily: () => {}, - }; + } as {}) as Table; const FAMILY_NAME = `${TABLE.name}/columnFamilies/${FAMILY_ID}`; // tslint:disable-next-line variable-name - let Family; - let family; + let Family: typeof fm.Family; + let family: fm.Family; // tslint:disable-next-line variable-name - let FamilyError; + let FamilyError: typeof fm.FamilyError; before(() => { // tslint:disable-next-line variable-name @@ -56,6 +64,8 @@ describe('Bigtable/Family', () => { family = new Family(TABLE, FAMILY_NAME); }); + afterEach(() => sandbox.restore()); + describe('instantiation', () => { it('should promisify all the things', () => { assert(promisified); @@ -97,9 +107,7 @@ describe('Bigtable/Family', () => { const originalRule = { age: 10, }; - const rule = Family.formatRule_(originalRule); - assert.deepStrictEqual(rule, { maxAge: originalRule.age, }); @@ -109,9 +117,7 @@ describe('Bigtable/Family', () => { const originalRule = { versions: 10, }; - const rule = Family.formatRule_(originalRule); - assert.deepStrictEqual(rule, { maxNumVersions: originalRule.versions, }); @@ -123,9 +129,7 @@ describe('Bigtable/Family', () => { versions: 2, union: true, }; - const rule = Family.formatRule_(originalRule); - assert.deepStrictEqual(rule, { union: { rules: [ @@ -145,9 +149,7 @@ describe('Bigtable/Family', () => { age: 10, versions: 2, }; - const rule = Family.formatRule_(originalRule); - assert.deepStrictEqual(rule, { intersection: { rules: [ @@ -168,9 +170,7 @@ describe('Bigtable/Family', () => { rule: {age: 30, versions: 2}, union: true, }; - const rule = Family.formatRule_(originalRule); - assert.deepStrictEqual(rule, { union: { rules: [ @@ -204,32 +204,38 @@ describe('Bigtable/Family', () => { describe('create', () => { it('should call createFamily from table', done => { const options = {}; - - family.table.createFamily = (id, options_, callback) => { + // tslint:disable-next-line:no-any + (family as any).table.createFamily = ( + id: string, + options_: {}, + callback: Function + ) => { assert.strictEqual(id, family.id); assert.strictEqual(options_, options); callback(); // done() }; - family.create(options, done); }); it('should not require options', done => { - family.table.createFamily = (name, options, callback) => { + // tslint:disable-next-line:no-any + (family as any).table.createFamily = ( + name: string, + options: {}, + callback: Function + ) => { assert.deepStrictEqual(options, {}); callback(); // done() }; - family.create(done); }); }); describe('delete', () => { it('should make the correct request', done => { - family.bigtable.request = (config, callback) => { + sandbox.stub(family.bigtable, 'request').callsFake((config, callback) => { assert.strictEqual(config.client, 'BigtableTableAdminClient'); assert.strictEqual(config.method, 'modifyColumnFamilies'); - assert.deepStrictEqual(config.reqOpts, { name: family.table.name, modifications: [ @@ -239,55 +245,43 @@ describe('Bigtable/Family', () => { }, ], }); - assert.deepStrictEqual(config.gaxOpts, {}); - - callback(); // done() - }; - + callback!(null); // done() + }); family.delete(done); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - family.bigtable.request = config => { + sandbox.stub(family.bigtable, 'request').callsFake(config => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); - }; - + }); family.delete(gaxOptions, assert.ifError); }); }); describe('exists', () => { it('should not require gaxOptions', done => { - family.getMetadata = gaxOptions => { + sandbox.stub(family, 'getMetadata').callsFake(gaxOptions => { assert.deepStrictEqual(gaxOptions, {}); done(); - }; - + }); family.exists(assert.ifError); }); it('should pass gaxOptions to getMetadata', done => { const gaxOptions = {}; - - family.getMetadata = gaxOptions_ => { + sandbox.stub(family, 'getMetadata').callsFake(gaxOptions_ => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); family.exists(gaxOptions, assert.ifError); }); it('should return false if FamilyError', done => { const error = new FamilyError('Error.'); - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, error); family.exists((err, exists) => { assert.ifError(err); assert.strictEqual(exists, false); @@ -297,11 +291,7 @@ describe('Bigtable/Family', () => { it('should return error if not FamilyError', done => { const error = new Error('Error.'); - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, error); family.exists(err => { assert.strictEqual(err, error); done(); @@ -309,10 +299,7 @@ describe('Bigtable/Family', () => { }); it('should return true if no error', done => { - family.getMetadata = (gaxOptions, callback) => { - callback(null, {}); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, null, {}); family.exists((err, exists) => { assert.ifError(err); assert.strictEqual(exists, true); @@ -326,63 +313,50 @@ describe('Bigtable/Family', () => { const options = { gaxOptions: {}, }; - - family.getMetadata = gaxOptions => { + sandbox.stub(family, 'getMetadata').callsFake(gaxOptions => { assert.strictEqual(gaxOptions, options.gaxOptions); done(); - }; - + }); family.get(options, assert.ifError); }); it('should not require an options object', done => { - family.getMetadata = gaxOptions => { + sandbox.stub(family, 'getMetadata').callsFake(gaxOptions => { assert.deepStrictEqual(gaxOptions, undefined); done(); - }; - + }); family.get(assert.ifError); }); it('should auto create with a FamilyError error', done => { const error = new FamilyError(TABLE.id); - const options = { autoCreate: true, gaxOptions: {}, }; - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - - family.create = (options_, callback) => { + sandbox.stub(family, 'getMetadata').callsArgOnWith(1, error); + // tslint:disable-next-line:no-any + (family as any).create = (options_: any, callback: Function) => { assert.strictEqual(options_.gaxOptions, options.gaxOptions); callback(); }; - family.get(options, done); }); it('should pass the rules when auto creating', done => { const error = new FamilyError(TABLE.id); - const options = { autoCreate: true, rule: { versions: 1, }, }; - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - - family.create = (options_, callback) => { + sandbox.stub(family, 'getMetadata').callsArgWith(1, error); + // tslint:disable-next-line:no-any + (family as any).create = (options_: {}, callback: Function) => { assert.deepStrictEqual(options.rule, {versions: 1}); callback(); }; - family.get(options, done); }); @@ -390,19 +364,13 @@ describe('Bigtable/Family', () => { // tslint:disable-next-line no-any const error: any = new Error('Error.'); error.code = 'NOT-5'; - const options = { autoCreate: true, }; - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, error); family.create = () => { throw new Error('Should not create.'); }; - family.get(options, err => { assert.strictEqual(err, error); done(); @@ -411,15 +379,10 @@ describe('Bigtable/Family', () => { it('should not auto create unless requested', done => { const error = new FamilyError(TABLE.id); - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, error); family.create = () => { throw new Error('Should not create.'); }; - family.get(err => { assert.strictEqual(err, error); done(); @@ -428,11 +391,7 @@ describe('Bigtable/Family', () => { it('should return an error from getMetadata', done => { const error = new Error('Error.'); - - family.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, error); family.get(err => { assert.strictEqual(err, error); done(); @@ -441,11 +400,7 @@ describe('Bigtable/Family', () => { it('should return self and API response', done => { const apiResponse = {}; - - family.getMetadata = (gaxOptions, callback) => { - callback(null, apiResponse); - }; - + sandbox.stub(family, 'getMetadata').callsArgWith(1, null, apiResponse); family.get((err, family_, apiResponse_) => { assert.ifError(err); assert.strictEqual(family_, family); @@ -458,23 +413,19 @@ describe('Bigtable/Family', () => { describe('getMetadata', () => { it('should accept gaxOptions', done => { const gaxOptions = {}; - - family.table.getFamilies = gaxOptions_ => { + sandbox.stub(family.table, 'getFamilies').callsFake(gaxOptions_ => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); family.getMetadata(gaxOptions, assert.ifError); }); it('should return an error to the callback', done => { const err = new Error('err'); const response = {}; - - family.table.getFamilies = (gaxOptions, callback) => { - callback(err, null, response); - }; - + sandbox + .stub(family.table, 'getFamilies') + .callsArgWith(1, err, null, response); family.getMetadata(err_ => { assert.strictEqual(err, err_); done(); @@ -486,12 +437,8 @@ describe('Bigtable/Family', () => { family.metadata = { a: 'a', b: 'b', - }; - - family.table.getFamilies = (gaxOptions, callback) => { - callback(null, [family]); - }; - + } as google.bigtable.admin.v2.IColumnFamily; + sandbox.stub(family.table, 'getFamilies').callsArgWith(1, null, [family]); family.getMetadata((err, metadata) => { assert.ifError(err); assert.strictEqual(metadata, family.metadata); @@ -500,10 +447,7 @@ describe('Bigtable/Family', () => { }); it('should return a custom error if no results', done => { - family.table.getFamilies = (gaxOptions, callback) => { - callback(null, []); - }; - + sandbox.stub(family.table, 'getFamilies').callsArgWith(1, null, []); family.getMetadata(err => { assert(err instanceof FamilyError); done(); @@ -513,10 +457,9 @@ describe('Bigtable/Family', () => { describe('setMetadata', () => { it('should provide the proper request options', done => { - family.bigtable.request = config => { + sandbox.stub(family.bigtable, 'request').callsFake(config => { assert.strictEqual(config.client, 'BigtableTableAdminClient'); assert.strictEqual(config.method, 'modifyColumnFamilies'); - assert.strictEqual(config.reqOpts.name, TABLE.name); assert.deepStrictEqual(config.reqOpts.modifications, [ { @@ -524,10 +467,8 @@ describe('Bigtable/Family', () => { update: {}, }, ]); - done(); - }; - + }); family.setMetadata({}, assert.ifError); }); @@ -537,21 +478,19 @@ describe('Bigtable/Family', () => { const formattedRule = { a: 'a', b: 'b', - }; + } as fm.IGcRule; const metadata = { rule: { c: 'c', d: 'd', }, - }; - - Family.formatRule_ = rule => { + } as fm.SetFamilyMetadataOptions; + sandbox.stub(Family, 'formatRule_').callsFake(rule => { assert.strictEqual(rule, metadata.rule); return formattedRule; - }; - - family.bigtable.request = config => { + }); + sandbox.stub(family.bigtable, 'request').callsFake(config => { assert.deepStrictEqual(config.reqOpts, { name: TABLE.name, modifications: [ @@ -565,18 +504,13 @@ describe('Bigtable/Family', () => { }); Family.formatRule_ = formatRule; done(); - }; - + }); family.setMetadata(metadata, assert.ifError); }); it('should return an error to the callback', done => { const error = new Error('err'); - - family.bigtable.request = (config, callback) => { - callback(error); - }; - + sandbox.stub(family.bigtable, 'request').callsArgWith(1, error); family.setMetadata({}, err => { assert.strictEqual(err, error); done(); @@ -590,11 +524,7 @@ describe('Bigtable/Family', () => { 'family-test': fakeMetadata, }, }; - - family.bigtable.request = (config, callback) => { - callback(null, response); - }; - + sandbox.stub(family.bigtable, 'request').callsArgWith(1, null, response); family.setMetadata({}, (err, metadata, apiResponse) => { assert.ifError(err); assert.strictEqual(metadata, fakeMetadata); diff --git a/test/filter.ts b/test/filter.ts index cc79b81a9..d2cda7bac 100644 --- a/test/filter.ts +++ b/test/filter.ts @@ -15,24 +15,26 @@ import * as assert from 'assert'; import {describe, it} from 'mocha'; import * as proxyquire from 'proxyquire'; -const sn = require('sinon'); +import * as sinon from 'sinon'; +import * as fr from '../src/filter'; +import {Row} from '../src/row'; -const sinon = sn.createSandbox(); +const sandbox = sinon.createSandbox(); // tslint:disable-next-line variable-name const FakeMutation = { - convertToBytes: sinon.spy(value => { + convertToBytes: sandbox.spy(value => { return value; }), - createTimeRange: sinon.stub(), + createTimeRange: sandbox.stub(), }; describe('Bigtable/Filter', () => { // tslint:disable-next-line variable-name - let Filter; + let Filter: typeof fr.Filter; // tslint:disable-next-line variable-name - let FilterError; - let filter; + let FilterError: typeof fr.FilterError; + let filter: fr.Filter; before(() => { // tslint:disable-next-line variable-name @@ -48,7 +50,7 @@ describe('Bigtable/Filter', () => { }); afterEach(() => { - sinon.restore(); + sandbox.restore(); }); describe('instantiation', () => { @@ -72,20 +74,17 @@ describe('Bigtable/Filter', () => { it('should convert an Array of buffers to a single string', () => { const faces = [Buffer.from('.|.'), Buffer.from('=|=')]; const str = Filter.convertToRegExpString(faces); - assert.strictEqual(str, '(\\.\\|\\.|=\\|=)'); }); it('should not do anything to a string', () => { const str1 = 'hello'; const str2 = Filter.convertToRegExpString(str1); - assert.strictEqual(str1, str2); }); it('should convert a number to a string', () => { const str = Filter.convertToRegExpString(1); - assert.strictEqual(str, '1'); }); @@ -93,7 +92,6 @@ describe('Bigtable/Filter', () => { const str1 = 'hello'; const buffer = Buffer.from(str1); const str2 = Filter.convertToRegExpString(buffer); - assert.deepStrictEqual(buffer, str2); }); @@ -101,27 +99,23 @@ describe('Bigtable/Filter', () => { const str1 = 'æ'; const buffer = Buffer.from('æ', 'binary'); const str2 = Filter.convertToRegExpString(buffer).toString('binary'); - assert.strictEqual(str1, str2); }); it('should throw an error for unknown types', () => { const errorReg = /Can't convert to RegExp String from unknown type\./; - assert.throws(() => { - Filter.convertToRegExpString(true); + Filter.convertToRegExpString((true as {}) as string); }, errorReg); }); }); describe('createRange', () => { it('should create a range object', () => { - const start = 'a'; - const end = 'b'; + const start = 'a' as fr.BoundData; + const end = 'b' as fr.BoundData; const key = 'Key'; - const range = Filter.createRange(start, end, key); - assert.deepStrictEqual(range, { startKeyClosed: start, endKeyClosed: end, @@ -129,11 +123,9 @@ describe('Bigtable/Filter', () => { }); it('should only create start bound', () => { - const start = 'a'; + const start = 'a' as fr.BoundData; const key = 'Key'; - const range = Filter.createRange(start, null, key); - assert(FakeMutation.convertToBytes.calledWithExactly(start)); assert.deepStrictEqual(range, { startKeyClosed: start, @@ -141,11 +133,9 @@ describe('Bigtable/Filter', () => { }); it('should only create an end bound', () => { - const end = 'b'; + const end = 'b' as fr.BoundData; const key = 'Key'; - const range = Filter.createRange(null, end, key); - assert(FakeMutation.convertToBytes.calledWithExactly(end)); assert.deepStrictEqual(range, { endKeyClosed: end, @@ -176,9 +166,8 @@ describe('Bigtable/Filter', () => { describe('parse', () => { it('should call each individual filter method', () => { - sinon.spy(Filter.prototype, 'row'); - sinon.spy(Filter.prototype, 'value'); - + sandbox.spy(Filter.prototype, 'row'); + sandbox.spy(Filter.prototype, 'value'); const fakeFilter = [ { row: 'a', @@ -187,14 +176,14 @@ describe('Bigtable/Filter', () => { value: 'b', }, ]; - Filter.parse(fakeFilter); - - assert.strictEqual(Filter.prototype.row.callCount, 1); - assert(Filter.prototype.row.calledWithExactly('a')); - - assert.strictEqual(Filter.prototype.value.callCount, 1); - assert(Filter.prototype.value.calledWithExactly('b')); + assert.strictEqual((Filter.prototype.row as sinon.SinonSpy).callCount, 1); + assert((Filter.prototype.row as sinon.SinonSpy).calledWithExactly('a')); + assert.strictEqual( + (Filter.prototype.value as sinon.SinonSpy).callCount, + 1 + ); + assert((Filter.prototype.value as sinon.SinonSpy).calledWithExactly('b')); }); it('should throw an error for unknown filters', () => { @@ -214,10 +203,13 @@ describe('Bigtable/Filter', () => { column: 'a', }, ]; - const stub = sinon.stub(Filter.prototype, 'toProto').returns(fakeProto); + const stub = sandbox.stub(Filter.prototype, 'toProto').returns(fakeProto); const parsedFilter = Filter.parse(fakeFilter); assert.strictEqual(parsedFilter, fakeProto); - assert.strictEqual(Filter.prototype.toProto.callCount, 1); + assert.strictEqual( + (Filter.prototype.toProto as sinon.SinonSpy).callCount, + 1 + ); stub.restore(); }); }); @@ -250,7 +242,7 @@ describe('Bigtable/Filter', () => { name: 'fake-column', }; - const spy = sinon.stub(Filter, 'convertToRegExpString').returnsArg(0); + const spy = sandbox.stub(Filter, 'convertToRegExpString').returnsArg(0); filter.set = (filterName, value) => { assert.strictEqual(filterName, 'columnQualifierRegexFilter'); @@ -311,12 +303,10 @@ describe('Bigtable/Filter', () => { b: 'b', }; const column = { - start: 'a', - end: 'b', + start: 'a' as fr.BoundData, + end: 'b' as fr.BoundData, }; - - const spy = sinon.stub(Filter, 'createRange').returns(fakeRange); - + const spy = sandbox.stub(Filter, 'createRange').returns(fakeRange); filter.set = (filterName, value) => { assert.strictEqual(filterName, 'columnRangeFilter'); assert.strictEqual(value, fakeRange); @@ -324,7 +314,6 @@ describe('Bigtable/Filter', () => { spy.restore(); done(); }; - filter.column(column); }); }); @@ -336,9 +325,7 @@ describe('Bigtable/Filter', () => { pass: {b: 'b'}, fail: {c: 'c'}, }; - - const spy = sinon.stub(Filter, 'parse').returnsArg(0); - + const spy = sandbox.stub(Filter, 'parse').returnsArg(0); filter.set = (filterName, value) => { assert.strictEqual(filterName, 'condition'); assert.deepStrictEqual(value, { @@ -346,16 +333,12 @@ describe('Bigtable/Filter', () => { trueFilter: condition.pass, falseFilter: condition.fail, }); - assert.strictEqual(spy.getCall(0).args[0], condition.test); assert.strictEqual(spy.getCall(1).args[0], condition.pass); assert.strictEqual(spy.getCall(2).args[0], condition.fail); - spy.restore(); - done(); }; - filter.condition(condition); }); }); @@ -363,9 +346,7 @@ describe('Bigtable/Filter', () => { describe('family', () => { it('should create a family name regex filter', done => { const familyName = 'fake-family'; - - const spy = sinon.stub(Filter, 'convertToRegExpString').returnsArg(0); - + const spy = sandbox.stub(Filter, 'convertToRegExpString').returnsArg(0); filter.set = (filterName, value) => { assert.strictEqual(filterName, 'familyNameRegexFilter'); assert.strictEqual(value, familyName); @@ -373,7 +354,6 @@ describe('Bigtable/Filter', () => { spy.restore(); done(); }; - filter.family(familyName); }); }); @@ -382,9 +362,9 @@ describe('Bigtable/Filter', () => { it('should create an interleave filter', done => { const fakeFilters = [{}, {}, {}]; - const spy = sinon.stub(Filter, 'parse').returnsArg(0); - - filter.set = (filterName, value) => { + const spy = sandbox.stub(Filter, 'parse').returnsArg(0); + // tslint:disable-next-line no-any + filter.set = (filterName, value: any) => { assert.strictEqual(filterName, 'interleave'); assert.strictEqual(value.filters[0], fakeFilters[0]); assert.strictEqual(value.filters[1], fakeFilters[1]); @@ -421,7 +401,7 @@ describe('Bigtable/Filter', () => { }; const convertedKey = 'abcd'; - const spy = sinon + const spy = sandbox .stub(Filter, 'convertToRegExpString') .returns(convertedKey); @@ -439,13 +419,11 @@ describe('Bigtable/Filter', () => { it('should accept the short-hand version of row key', done => { const rowKey = 'gwashington'; - filter.set = (filterName, value) => { assert.strictEqual(filterName, 'rowKeyRegexFilter'); assert.strictEqual(value, rowKey); done(); }; - filter.row(rowKey); }); @@ -453,27 +431,23 @@ describe('Bigtable/Filter', () => { const row = { sample: 10, }; - filter.set = (filterName, value) => { assert.strictEqual(filterName, 'rowSampleFilter'); assert.strictEqual(value, row.sample); done(); }; - - filter.row(row); + filter.row((row as {}) as Row); }); it('should set the cells per row offset filter', done => { const row = { cellOffset: 10, }; - filter.set = (filterName, value) => { assert.strictEqual(filterName, 'cellsPerRowOffsetFilter'); assert.strictEqual(value, row.cellOffset); done(); }; - filter.row(row); }); @@ -481,13 +455,11 @@ describe('Bigtable/Filter', () => { const row = { cellLimit: 10, }; - filter.set = (filterName, value) => { assert.strictEqual(filterName, 'cellsPerRowLimitFilter'); assert.strictEqual(value, row.cellLimit); done(); }; - filter.row(row); }); }); @@ -496,9 +468,7 @@ describe('Bigtable/Filter', () => { it('should create a filter object', () => { const key = 'notARealFilter'; const value = {a: 'b'}; - filter.set(key, value); - assert.strictEqual(filter.filters_[0][key], value); }); }); @@ -506,13 +476,11 @@ describe('Bigtable/Filter', () => { describe('sink', () => { it('should set the sink filter', done => { const sink = true; - filter.set = (filterName, value) => { assert.strictEqual(filterName, 'sink'); assert.strictEqual(value, sink); done(); }; - filter.sink(sink); }); }); @@ -523,16 +491,13 @@ describe('Bigtable/Filter', () => { start: 10, end: 10, }; - const spy = FakeMutation.createTimeRange.returns(fakeTimeRange); - filter.set = (filterName, value) => { assert.strictEqual(filterName, 'timestampRangeFilter'); assert.strictEqual(value, fakeTimeRange); assert(spy.calledWithExactly(fakeTimeRange.start, fakeTimeRange.end)); done(); }; - filter.time(fakeTimeRange); }); }); @@ -541,24 +506,20 @@ describe('Bigtable/Filter', () => { it('should return null when no filters are present', () => { const filter = new Filter(); const filterProto = filter.toProto(); - assert.strictEqual(filterProto, null); }); it('should return a plain filter if there is only 1', () => { filter.filters_ = [{}]; - const filterProto = filter.toProto(); - assert.strictEqual(filterProto, filter.filters_[0]); }); it('should create a chain filter if there are multiple', () => { filter.filters_ = [{}, {}]; - - const filterProto = filter.toProto(); - - assert.strictEqual(filterProto.chain.filters, filter.filters_); + // tslint:disable-next-line no-any + const filterProto = filter.toProto() as any; + assert.strictEqual(filterProto!.chain.filters, filter.filters_); }); }); @@ -570,19 +531,23 @@ describe('Bigtable/Filter', () => { const fakeRegExValue = 'abcd'; const fakeConvertedValue = 'dcba'; - const regSpy = sinon + const regSpy = sandbox .stub(Filter, 'convertToRegExpString') .returns(fakeRegExValue); - const bytesSpy = (FakeMutation.convertToBytes = sinon.spy(() => { - return fakeConvertedValue; - })); + // tslint:disable-next-line no-any + const bytesSpy = ((FakeMutation as any).convertToBytes = sandbox.spy( + () => { + return fakeConvertedValue; + } + )); filter.set = (filterName, val) => { assert.strictEqual(filterName, 'valueRegexFilter'); assert.strictEqual(fakeConvertedValue, val); assert(regSpy.calledWithExactly(value.value)); - assert(bytesSpy.calledWithExactly(fakeRegExValue)); + // tslint:disable-next-line no-any + assert((bytesSpy as any).calledWithExactly(fakeRegExValue)); regSpy.restore(); done(); }; @@ -596,19 +561,23 @@ describe('Bigtable/Filter', () => { const fakeRegExValue = 'abcd'; const fakeConvertedValue = 'dcba'; - const regSpy = sinon + const regSpy = sandbox .stub(Filter, 'convertToRegExpString') .returns(fakeRegExValue); - const bytesSpy = (FakeMutation.convertToBytes = sinon.spy(() => { - return fakeConvertedValue; - })); + // tslint:disable-next-line no-any + const bytesSpy = ((FakeMutation.convertToBytes as any) = sandbox.spy( + () => { + return fakeConvertedValue; + } + )); filter.set = (filterName, val) => { assert.strictEqual(filterName, 'valueRegexFilter'); assert.strictEqual(fakeConvertedValue, val); assert(regSpy.calledWithExactly(value)); - assert(bytesSpy.calledWithExactly(fakeRegExValue)); + // tslint:disable-next-line no-any + assert((bytesSpy as any).calledWithExactly(fakeRegExValue)); regSpy.restore(); done(); }; @@ -622,12 +591,10 @@ describe('Bigtable/Filter', () => { b: 'b', }; const value = { - start: 'a', - end: 'b', + start: 'a' as fr.BoundData, + end: 'b' as fr.BoundData, }; - - const spy = sinon.stub(Filter, 'createRange').returns(fakeRange); - + const spy = sandbox.stub(Filter, 'createRange').returns(fakeRange); filter.set = (filterName, val) => { assert.strictEqual(filterName, 'valueRangeFilter'); assert.strictEqual(val, fakeRange); @@ -635,7 +602,6 @@ describe('Bigtable/Filter', () => { spy.restore(); done(); }; - filter.value(value); }); @@ -643,13 +609,11 @@ describe('Bigtable/Filter', () => { const value = { strip: true, }; - filter.set = (filterName, val) => { assert.strictEqual(filterName, 'stripValueTransformer'); assert.strictEqual(val, value.strip); done(); }; - filter.value(value); }); }); diff --git a/test/index.ts b/test/index.ts index 0294a5069..a3d3656b6 100644 --- a/test/index.ts +++ b/test/index.ts @@ -34,9 +34,10 @@ const noop = () => {}; function fakeV2() {} let promisified = false; -let replaceProjectIdTokenOverride; +let replaceProjectIdTokenOverride: Function | null; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass, options) { + // tslint:disable-next-line no-any + promisifyAll(klass: Function, options: any) { if (klass.name !== 'Bigtable') { return; } @@ -49,7 +50,7 @@ const fakePromisify = Object.assign({}, promisify, { }, }); const fakeReplaceProjectIdToken = Object.assign({}, projectify, { - replaceProjectIdToken(reqOpts) { + replaceProjectIdToken(reqOpts: {}) { if (replaceProjectIdTokenOverride) { return replaceProjectIdTokenOverride.apply(null, arguments); } @@ -57,12 +58,12 @@ const fakeReplaceProjectIdToken = Object.assign({}, projectify, { }, }); -let googleAuthOverride; +let googleAuthOverride: Function | null; function fakeGoogleAuth() { return (googleAuthOverride || noop).apply(null, arguments); } -let retryRequestOverride; +let retryRequestOverride: Function | null; function fakeRetryRequest() { return (retryRequestOverride || require('retry-request')).apply( null, @@ -70,7 +71,8 @@ function fakeRetryRequest() { ); } -function createFake(klass) { +// tslint:disable-next-line no-any +function createFake(klass: any) { return class Fake extends klass { calledWith_: IArguments; constructor() { @@ -88,9 +90,10 @@ const FakeInstance = createFake(Instance); describe('Bigtable', () => { const PROJECT_ID = 'test-project'; const PROJECT_ID_TOKEN = '{{projectId}}'; - // tslint:disable-next-line variable-name - let Bigtable; - let bigtable; + // tslint:disable-next-line variable-name no-any + let Bigtable: any; + // tslint:disable-next-line no-any + let bigtable: any; before(() => { Bigtable = proxyquire('../src', { @@ -154,7 +157,7 @@ describe('Bigtable', () => { c: 'd', }; - googleAuthOverride = options_ => { + googleAuthOverride = (options_: {}) => { assert.deepStrictEqual( options_, Object.assign( @@ -332,30 +335,26 @@ describe('Bigtable', () => { const INSTANCE_ID = 'my-instance'; it('should provide the proper request options', done => { - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'createInstance'); - assert.strictEqual(config.reqOpts.parent, bigtable.projectName); assert.strictEqual(config.reqOpts.instanceId, INSTANCE_ID); assert.strictEqual(config.reqOpts.instance.displayName, INSTANCE_ID); - assert.strictEqual(config.gaxOpts, undefined); - done(); }; - bigtable.createInstance(INSTANCE_ID, assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - bigtable.createInstance(INSTANCE_ID, {gaxOptions}, assert.ifError); }); @@ -363,32 +362,30 @@ describe('Bigtable', () => { const options = { displayName: 'robocop', }; - - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.strictEqual( config.reqOpts.instance.displayName, options.displayName ); done(); }; - bigtable.createInstance(INSTANCE_ID, options, assert.ifError); }); it('should respect the type option', done => { const options = {type: 'development'}; const fakeTypeType = 99; - - FakeInstance.getTypeType_ = type => { + // tslint:disable-next-line no-any + FakeInstance.getTypeType_ = (type: string) => { assert.strictEqual(type, options.type); return fakeTypeType; }; - - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.deepStrictEqual(config.reqOpts.instance.type, fakeTypeType); done(); }; - bigtable.createInstance(INSTANCE_ID, options, assert.ifError); }); @@ -398,12 +395,11 @@ describe('Bigtable', () => { env: 'prod', }, }; - - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.deepStrictEqual(config.reqOpts.instance.labels, options.labels); done(); }; - bigtable.createInstance(INSTANCE_ID, options, assert.ifError); }); @@ -414,25 +410,22 @@ describe('Bigtable', () => { nodes: 3, storage: 'ssd', }; - const options = { clusters: [cluster], }; - const fakeLocation = 'a/b/c/d'; - FakeCluster.getLocation_ = (project, location) => { + FakeCluster.getLocation_ = (project: string, location: string) => { assert.strictEqual(project, PROJECT_ID); assert.strictEqual(location, cluster.location); return fakeLocation; }; - const fakeStorage = 20; - FakeCluster.getStorageType_ = storage => { + FakeCluster.getStorageType_ = (storage: {}) => { assert.strictEqual(storage, cluster.storage); return fakeStorage; }; - - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.deepStrictEqual(config.reqOpts.clusters, { 'my-cluster': { location: fakeLocation, @@ -449,45 +442,45 @@ describe('Bigtable', () => { it('should return an error to the callback', done => { const error = new Error('err'); - - bigtable.request = (config, callback) => { + bigtable.request = (config: {}, callback: Function) => { callback(error); }; - - bigtable.createInstance(INSTANCE_ID, (err, instance, operation) => { - assert.strictEqual(err, error); - assert.strictEqual(instance, undefined); - assert.strictEqual(operation, undefined); - done(); - }); + bigtable.createInstance( + INSTANCE_ID, + (err: Error, instance: Instance, operation: gax.Operation) => { + assert.strictEqual(err, error); + assert.strictEqual(instance, undefined); + assert.strictEqual(operation, undefined); + done(); + } + ); }); it('should return an instance to the callback', done => { const response = { name: 'my-operation', }; - const responseArg2 = {}; const responseArg3 = {}; - const fakeInstance = {}; - bigtable.instance = id => { + bigtable.instance = (id: string) => { assert.strictEqual(id, INSTANCE_ID); return fakeInstance; }; - - bigtable.request = (config, callback) => { + bigtable.request = (config: {}, callback: Function) => { callback(null, response, responseArg2, responseArg3); }; - - bigtable.createInstance(INSTANCE_ID, (err, instance, ...args) => { - assert.ifError(err); - assert.strictEqual(instance, fakeInstance); - assert.strictEqual(args[0], response); - assert.strictEqual(args[1], responseArg2); - assert.strictEqual(args[2], responseArg3); - done(); - }); + bigtable.createInstance( + INSTANCE_ID, + (err: Error, instance: Instance, ...args: Array<{}>) => { + assert.ifError(err); + assert.strictEqual(instance, fakeInstance); + assert.strictEqual(args[0], response); + assert.strictEqual(args[1], responseArg2); + assert.strictEqual(args[2], responseArg3); + done(); + } + ); }); it('should throw an error if cluster id not provided.', () => { @@ -510,7 +503,8 @@ describe('Bigtable', () => { describe('getInstances', () => { it('should provide the proper request options', done => { - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'listInstances'); assert.deepStrictEqual(config.reqOpts, { @@ -519,29 +513,25 @@ describe('Bigtable', () => { assert.deepStrictEqual(config.gaxOpts, {}); done(); }; - bigtable.getInstances(assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - bigtable.request = config => { + // tslint:disable-next-line no-any + bigtable.request = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - bigtable.getInstances(gaxOptions, assert.ifError); }); it('should return an error to the callback', done => { const error = new Error('err'); - - bigtable.request = (config, callback) => { + bigtable.request = (config: {}, callback: Function) => { callback(error); }; - - bigtable.getInstances(err => { + bigtable.getInstances((err: Error) => { assert.strictEqual(err, error); done(); }); @@ -558,29 +548,27 @@ describe('Bigtable', () => { }, ], }; - const fakeInstances = [{}, {}]; - - bigtable.request = (config, callback) => { + bigtable.request = (config: {}, callback: Function) => { callback(null, response); }; - let instanceCount = 0; - - bigtable.instance = name => { + bigtable.instance = (name: string) => { assert.strictEqual(name, response.instances[instanceCount].name); return fakeInstances[instanceCount++]; }; - bigtable.getInstances((err, instances, apiResponse) => { - assert.ifError(err); - assert.strictEqual(instances[0], fakeInstances[0]); - assert.strictEqual(instances[0].metadata, response.instances[0]); - assert.strictEqual(instances[1], fakeInstances[1]); - assert.strictEqual(instances[1].metadata, response.instances[1]); - assert.strictEqual(apiResponse, response); - done(); - }); + bigtable.getInstances( + (err: Error, instances: Instance[], apiResponse: {}) => { + assert.ifError(err); + assert.strictEqual(instances[0], fakeInstances[0]); + assert.strictEqual(instances[0].metadata, response.instances[0]); + assert.strictEqual(instances[1], fakeInstances[1]); + assert.strictEqual(instances[1].metadata, response.instances[1]); + assert.strictEqual(apiResponse, response); + done(); + } + ); }); }); @@ -609,10 +597,9 @@ describe('Bigtable', () => { }; beforeEach(() => { - bigtable.getProjectId_ = callback => { + bigtable.getProjectId_ = (callback: Function) => { callback(null, PROJECT_ID); }; - bigtable.api[CONFIG.client] = { [CONFIG.method]: noop, }; @@ -628,12 +615,10 @@ describe('Bigtable', () => { it('should return error if getting project ID failed', done => { const error = new Error('Error.'); - - bigtable.getProjectId_ = callback => { + bigtable.getProjectId_ = (callback: Function) => { callback(error); }; - - bigtable.request(CONFIG, err => { + bigtable.request(CONFIG, (err: Error) => { assert.strictEqual(err, error); done(); }); @@ -643,8 +628,8 @@ describe('Bigtable', () => { const fakeClient = { [CONFIG.method]: noop, }; - // tslint:disable-next-line only-arrow-functions - fakeV2[CONFIG.client] = function(options) { + // tslint:disable-next-line only-arrow-functions no-any + (fakeV2 as any)[CONFIG.client] = function(options: any) { assert.strictEqual(options, bigtable.options[CONFIG.client]); return fakeClient; }; @@ -654,8 +639,8 @@ describe('Bigtable', () => { }); it('should use the cached client', done => { - // tslint:disable-next-line only-arrow-functions - fakeV2[CONFIG.client] = function() { + // tslint:disable-next-line only-arrow-functions no-any + (fakeV2 as any)[CONFIG.client] = function() { done(new Error('Should not re-instantiate a GAX client.')); }; bigtable.request(CONFIG); @@ -666,7 +651,7 @@ describe('Bigtable', () => { replaceProjectIdTokenOverride = sinon.spy(); bigtable.api[CONFIG.client][CONFIG.method] = { bind() { - assert(!replaceProjectIdTokenOverride.called); + assert(!(replaceProjectIdTokenOverride as sn.SinonSpy).called); setImmediate(done); return noop; }, @@ -678,10 +663,9 @@ describe('Bigtable', () => { describe('replace projectID token', () => { beforeEach(() => { bigtable = new Bigtable(); - bigtable.getProjectId_ = callback => { + bigtable.getProjectId_ = (callback: Function) => { callback(null, PROJECT_ID); }; - bigtable.api[CONFIG.client] = { [CONFIG.method]: noop, }; @@ -689,23 +673,20 @@ describe('Bigtable', () => { it('should replace the project ID token', done => { const replacedReqOpts = {}; - - replaceProjectIdTokenOverride = (reqOpts, projectId) => { + // tslint:disable-next-line no-any + replaceProjectIdTokenOverride = (reqOpts: any, projectId: string) => { assert.notStrictEqual(reqOpts, CONFIG.reqOpts); assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); assert.strictEqual(projectId, PROJECT_ID); - return replacedReqOpts; }; - bigtable.api[CONFIG.client][CONFIG.method] = { - bind(gaxClient, reqOpts) { + bind(gaxClient: {}, reqOpts: {}) { assert.strictEqual(reqOpts, replacedReqOpts); setImmediate(done); return noop; }, }; - bigtable.request(CONFIG, assert.ifError); }); @@ -713,19 +694,16 @@ describe('Bigtable', () => { replaceProjectIdTokenOverride = () => { throw new Error('Should not have tried to replace token.'); }; - - bigtable.getProjectId_ = callback => { + bigtable.getProjectId_ = (callback: Function) => { callback(null, PROJECT_ID_TOKEN); }; - bigtable.api[CONFIG.client][CONFIG.method] = { - bind(gaxClient, reqOpts) { + bind(gaxClient: {}, reqOpts: {}) { assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); setImmediate(done); return noop; }, }; - bigtable.request(CONFIG, assert.ifError); }); }); @@ -733,7 +711,7 @@ describe('Bigtable', () => { describe('makeRequestCallback', () => { it('should prepare the request', done => { bigtable.api[CONFIG.client][CONFIG.method] = { - bind(gaxClient, reqOpts, gaxOpts) { + bind(gaxClient: {}, reqOpts: {}, gaxOpts: {}) { assert.strictEqual(gaxClient, bigtable.api[CONFIG.client]); assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); assert.strictEqual(gaxOpts, CONFIG.gaxOpts); @@ -747,30 +725,28 @@ describe('Bigtable', () => { it('should execute callback with error', done => { const error = new Error('Error.'); - - bigtable.api[CONFIG.client][CONFIG.method] = (...args) => { + bigtable.api[CONFIG.client][CONFIG.method] = (...args: Array<{}>) => { const callback: Function = [].slice.call(args).pop()!; callback(error); }; - - bigtable.request(CONFIG, err => { + bigtable.request(CONFIG, (err: Error) => { assert.strictEqual(err, error); done(); }); }); it('should execute the request function', () => { - bigtable.api[CONFIG.client][CONFIG.method] = (...args) => { + bigtable.api[CONFIG.client][CONFIG.method] = (...args: Array<{}>) => { const callback: Function = [].slice.call(args).pop()!; callback(null, args[0]); // so it ends the test }; - bigtable.request(CONFIG, assert.ifError); }); }); describe('makeRequestStream', () => { - let GAX_STREAM; + // tslint:disable-next-line no-any + let GAX_STREAM: any; beforeEach(() => { GAX_STREAM = through(); @@ -784,7 +760,8 @@ describe('Bigtable', () => { }); it('should use retry-request', done => { - retryRequestOverride = (_, config) => { + // tslint:disable-next-line no-any + retryRequestOverride = (_: {}, config: any) => { assert.strictEqual(config.currentRetryAttempt, 0); assert.strictEqual(config.objectMode, true); done(); @@ -804,13 +781,11 @@ describe('Bigtable', () => { it('should prepare the request once reading', done => { bigtable.api[CONFIG.client][CONFIG.method] = { - bind(gaxClient, reqOpts, gaxOpts) { + bind(gaxClient: {}, reqOpts: {}, gaxOpts: {}) { assert.strictEqual(gaxClient, bigtable.api[CONFIG.client]); assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); assert.strictEqual(gaxOpts, CONFIG.gaxOpts); - setImmediate(done); - return () => { return GAX_STREAM; }; @@ -823,15 +798,12 @@ describe('Bigtable', () => { it('should destroy the stream with prepare error', done => { const error = new Error('Error.'); - - bigtable.getProjectId_ = callback => { + bigtable.getProjectId_ = (callback: Function) => { callback(error); }; - const requestStream = bigtable.request(CONFIG); requestStream.emit('reading'); - - requestStream.on('error', err => { + requestStream.on('error', (err: Error) => { assert.strictEqual(err, error); done(); }); @@ -839,13 +811,10 @@ describe('Bigtable', () => { it('should destroy the stream with GAX error', done => { const error = new Error('Error.'); - const requestStream = bigtable.request(CONFIG); requestStream.emit('reading'); - GAX_STREAM.emit('error', error); - - requestStream.on('error', err => { + requestStream.on('error', (err: Error) => { assert.strictEqual(err, error); done(); }); @@ -870,7 +839,7 @@ describe('Bigtable', () => { describe('getProjectId_', () => { beforeEach(() => { bigtable.auth = { - getProjectId(callback) { + getProjectId(callback: Function) { callback(null, PROJECT_ID); }, }; @@ -878,14 +847,11 @@ describe('Bigtable', () => { it('should return the provided project ID', done => { const providedProjectId = 'provided-project-id'; - bigtable.auth.getProjectId = () => { throw new Error('Auth client should not be called.'); }; - bigtable.projectId = providedProjectId; - - bigtable.getProjectId_((err, projectId) => { + bigtable.getProjectId_((err: Error, projectId: string) => { assert.ifError(err); assert.strictEqual(projectId, providedProjectId); done(); @@ -896,11 +862,9 @@ describe('Bigtable', () => { bigtable.auth.getProjectId = () => { throw new Error('Auth client should not be called.'); }; - bigtable.projectId = PROJECT_ID_TOKEN; bigtable.customEndpoint = 'custom-endpoint'; - - bigtable.getProjectId_((err, projectId) => { + bigtable.getProjectId_((err: Error, projectId: string) => { assert.ifError(err); assert.strictEqual(projectId, PROJECT_ID_TOKEN); done(); @@ -909,14 +873,11 @@ describe('Bigtable', () => { it('should return error if project ID detection failed', done => { const error = new Error('Error.'); - - bigtable.auth.getProjectId = callback => { + bigtable.auth.getProjectId = (callback: Function) => { callback(error); }; - bigtable.projectId = PROJECT_ID_TOKEN; - - bigtable.getProjectId_(err => { + bigtable.getProjectId_((err: Error) => { assert.strictEqual(err, error); done(); }); @@ -924,14 +885,11 @@ describe('Bigtable', () => { it('should get and cache the project ID if not provided', done => { const detectedProjectId = 'detected-project-id'; - - bigtable.auth.getProjectId = callback => { + bigtable.auth.getProjectId = (callback: Function) => { callback(null, detectedProjectId); }; - bigtable.projectId = PROJECT_ID_TOKEN; - - bigtable.getProjectId_((err, projectId) => { + bigtable.getProjectId_((err: Error, projectId: string) => { assert.ifError(err); assert.strictEqual(projectId, detectedProjectId); assert.strictEqual(bigtable.projectId, detectedProjectId); diff --git a/test/instance.ts b/test/instance.ts index 2fbd51713..46d362bc3 100644 --- a/test/instance.ts +++ b/test/instance.ts @@ -18,15 +18,26 @@ import * as assert from 'assert'; import {describe, it} from 'mocha'; import * as sinon from 'sinon'; import * as proxyquire from 'proxyquire'; +import {ServiceError} from '@grpc/grpc-js'; -import {AppProfile} from '../src/app-profile'; -import {Cluster} from '../src/cluster'; +import * as inst from '../src/instance'; +import {AppProfile, AppProfileOptions} from '../src/app-profile'; +import {Cluster, CreateClusterOptions} from '../src/cluster'; import {Family} from '../src/family'; -import {Policy, Table} from '../src/table'; +import { + Policy, + Table, + GetIamPolicyOptions, + GetTablesOptions, +} from '../src/table'; +import {Bigtable} from '../src'; + +const sandbox = sinon.createSandbox(); let promisified = false; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass, options) { + // tslint:disable-next-line no-any + promisifyAll(klass: Function, options: any) { if (klass.name !== 'Instance') { return; } @@ -41,39 +52,40 @@ const fakePaginator = Object.assign({}, paginator, { // tslint:disable-next-line no-any (this as any).calledWith_ = arguments; }, - streamify(methodName) { + streamify(methodName: string) { return methodName; }, }, }); class FakeAppProfile extends AppProfile { - calledWith_; - constructor(...args) { + calledWith_: Array<{}>; + constructor(...args: [inst.Instance, string]) { super(args[0], args[1]); this.calledWith_ = args; } } class FakeCluster extends Cluster { - calledWith_; - constructor(...args) { + calledWith_: Array<{}>; + constructor(...args: [inst.Instance, string]) { super(args[0], args[1]); this.calledWith_ = args; } } class FakeFamily extends Family { - calledWith_; - constructor(...args) { + calledWith_: Array<{}>; + constructor(...args: [Table, string]) { super(args[0], args[1]); this.calledWith_ = args; } } class FakeTable extends Table { - calledWith_; - constructor(...args) { + calledWith_: Array<{}>; + VIEWS?: {[index: string]: number}; + constructor(...args: [inst.Instance, string]) { super(args[0], args[1]); this.calledWith_ = args; } @@ -82,13 +94,13 @@ class FakeTable extends Table { describe('Bigtable/Instance', () => { const INSTANCE_ID = 'my-instance'; // tslint:disable-next-line no-any - const BIGTABLE: any = {projectName: 'projects/my-project'}; + const BIGTABLE = {projectName: 'projects/my-project'} as Bigtable; const INSTANCE_NAME = `${BIGTABLE.projectName}/instances/${INSTANCE_ID}`; const APP_PROFILE_ID = 'my-app-profile'; const CLUSTER_ID = 'my-cluster'; // tslint:disable-next-line variable-name - let Instance; - let instance; + let Instance: typeof inst.Instance; + let instance: inst.Instance; before(() => { Instance = proxyquire('../src/instance.js', { @@ -105,6 +117,8 @@ describe('Bigtable/Instance', () => { instance = new Instance(BIGTABLE, INSTANCE_ID); }); + afterEach(() => sandbox.restore()); + describe('instantiation', () => { it('should extend the correct methods', () => { // tslint:disable-next-line no-any @@ -152,7 +166,7 @@ describe('Bigtable/Instance', () => { unspecified: 0, production: 1, development: 2, - }; + } as {[index: string]: number}; it('should default to unspecified', () => { assert.strictEqual(Instance.getTypeType_(), types.unspecified); @@ -167,7 +181,7 @@ describe('Bigtable/Instance', () => { }); Object.keys(types).forEach(type => { - it('should get the storage type for "' + type + '"', () => { + it(`should get the storage type for "${type}"`, () => { assert.strictEqual(Instance.getTypeType_(type), types[type]); }); }); @@ -175,41 +189,43 @@ describe('Bigtable/Instance', () => { describe('create', () => { it('should call createInstance from bigtable', done => { - const options = {}; - - instance.bigtable.createInstance = (id, options_, callback) => { + const options = {} as inst.InstanceOptions; + (instance.bigtable.createInstance as Function) = ( + id: string, + options_: {}, + callback: Function + ) => { assert.strictEqual(id, instance.id); assert.strictEqual(options_, options); callback(); // done() }; - instance.create(options, done); }); it('should not require options', done => { - instance.bigtable.createInstance = (id, options, callback) => { + (instance.bigtable.createInstance as Function) = ( + id: string, + options: {}, + callback: Function + ) => { assert.deepStrictEqual(options, {}); callback(); // done() }; - instance.create(done); }); }); describe('createAppProfile', () => { it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'createAppProfile'); - assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME); assert.strictEqual(config.reqOpts.appProfileId, APP_PROFILE_ID); - assert.strictEqual(config.gaxOpts, undefined); - done(); }; - instance.createAppProfile( APP_PROFILE_ID, {routing: 'any'}, @@ -228,46 +244,43 @@ describe('Bigtable/Instance', () => { const options = { routing: 'any', gaxOptions: {}, - }; - - instance.bigtable.request = config => { + } as AppProfileOptions; + // tslint:disable-next-line no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, options.gaxOptions); done(); }; - instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError); }); describe('should respect the routing option with', () => { - const cluster = new FakeCluster({}, CLUSTER_ID); + const cluster = new FakeCluster({} as inst.Instance, CLUSTER_ID); it(`an 'any' value`, done => { const options = { routing: 'any', - }; - - instance.bigtable.request = config => { + } as AppProfileOptions; + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual( config.reqOpts.appProfile.multiClusterRoutingUseAny, {} ); done(); }; - instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError); }); it(`a cluster value`, done => { const options = {routing: cluster}; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual( config.reqOpts.appProfile.singleClusterRouting, {clusterId: CLUSTER_ID} ); done(); }; - instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError); }); }); @@ -278,8 +291,8 @@ describe('Bigtable/Instance', () => { routing: cluster, allowTransactionalWrites: true, }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual( config.reqOpts.appProfile.singleClusterRouting .allowTransactionalWrites, @@ -287,7 +300,6 @@ describe('Bigtable/Instance', () => { ); done(); }; - instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError); }); @@ -295,16 +307,15 @@ describe('Bigtable/Instance', () => { const options = { routing: 'any', description: 'My App Profile', - }; - - instance.bigtable.request = config => { + } as AppProfileOptions; + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual( config.reqOpts.appProfile.description, options.description ); done(); }; - instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError); }); @@ -312,23 +323,22 @@ describe('Bigtable/Instance', () => { const options = { routing: 'any', ignoreWarnings: true, - }; - - instance.bigtable.request = config => { + } as AppProfileOptions; + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual(config.reqOpts.ignoreWarnings, true); done(); }; - instance.createAppProfile(APP_PROFILE_ID, options, assert.ifError); }); it('should execute callback with arguments from GAPIC', done => { const response = {}; - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); const fakeAppProfile = {}; - instance.appProfile = id => { + (instance.appProfile as Function) = (id: string) => { assert.strictEqual(id, APP_PROFILE_ID); return fakeAppProfile; }; @@ -347,53 +357,47 @@ describe('Bigtable/Instance', () => { describe('createCluster', () => { it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'createCluster'); - assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME); assert.strictEqual(config.reqOpts.clusterId, CLUSTER_ID); - assert.strictEqual(config.gaxOpts, undefined); - done(); }; - instance.createCluster(CLUSTER_ID, assert.ifError); }); it('should accept gaxOptions', done => { const options = { gaxOptions: {}, - }; - - instance.bigtable.request = config => { + } as CreateClusterOptions; + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, options.gaxOptions); done(); }; - instance.createCluster(CLUSTER_ID, options, assert.ifError); }); it('should respect the location option', done => { const options = { location: 'us-central1-b', - }; - + } as CreateClusterOptions; const fakeLocation = 'a/b/c/d'; - - // tslint:disable-next-line no-any - (FakeCluster as any).getLocation_ = (project, location) => { - assert.strictEqual(project, BIGTABLE.projectId); - assert.strictEqual(location, options.location); - return fakeLocation; - }; - - instance.bigtable.request = config => { + sandbox + .stub(FakeCluster, 'getLocation_') + .callsFake((project, location) => { + assert.strictEqual(project, BIGTABLE.projectId); + assert.strictEqual(location, options.location); + return fakeLocation; + }); + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.reqOpts.cluster.location, fakeLocation); done(); }; - instance.createCluster(CLUSTER_ID, options, assert.ifError); }); @@ -401,59 +405,53 @@ describe('Bigtable/Instance', () => { const options = { nodes: 3, }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.reqOpts.cluster.serveNodes, options.nodes); done(); }; - instance.createCluster(CLUSTER_ID, options, assert.ifError); }); it('should respect the storage option', done => { const options = { storage: 'ssd', - }; - + } as CreateClusterOptions; const fakeStorageType = 2; - - // tslint:disable-next-line no-any - (FakeCluster as any).getStorageType_ = type => { + sandbox.stub(FakeCluster, 'getStorageType_').callsFake(type => { assert.strictEqual(type, options.storage); return fakeStorageType; - }; - - instance.bigtable.request = config => { + }); + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual( config.reqOpts.cluster.defaultStorageType, fakeStorageType ); done(); }; - instance.createCluster(CLUSTER_ID, options, assert.ifError); }); it('should execute callback with arguments from GAPIC', done => { const response = {}; - - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; - + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); const fakeCluster = {}; - - instance.cluster = name => { + (instance.cluster as Function) = (name: string) => { assert.strictEqual(name, CLUSTER_ID); return fakeCluster; }; - - instance.createCluster(CLUSTER_ID, (err, cluster, apiResponse) => { - assert.ifError(err); - assert.strictEqual(cluster, fakeCluster); - assert.strictEqual(apiResponse, response); - done(); - }); + (instance.createCluster as Function)( + CLUSTER_ID, + (err: Error, cluster: {}, apiResponse: {}) => { + assert.ifError(err); + assert.strictEqual(cluster, fakeCluster); + assert.strictEqual(apiResponse, response); + done(); + } + ); }); }); @@ -464,24 +462,21 @@ describe('Bigtable/Instance', () => { it('should throw if an id is not provided', () => { assert.throws(() => { - instance.createTable(); + (instance.createTable as Function)(); }, /An id is required to create a table\./); }); it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableTableAdminClient'); assert.strictEqual(config.method, 'createTable'); - assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME); assert.strictEqual(config.reqOpts.tableId, TABLE_ID); assert.deepStrictEqual(config.reqOpts.table, {granularity: 0}); - assert.strictEqual(config.gaxOpts, undefined); - done(); }; - instance.createTable(TABLE_ID, assert.ifError); }); @@ -489,12 +484,11 @@ describe('Bigtable/Instance', () => { const options = { gaxOptions: {}, }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, options.gaxOptions); done(); }; - instance.createTable(TABLE_ID, options, assert.ifError); }); @@ -502,14 +496,12 @@ describe('Bigtable/Instance', () => { const options = { splits: ['a', 'b'], }; - const expectedSplits = [{key: 'a'}, {key: 'b'}]; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual(config.reqOpts.initialSplits, expectedSplits); done(); }; - instance.createTable(TABLE_ID, options, assert.ifError); }); @@ -518,16 +510,14 @@ describe('Bigtable/Instance', () => { const options = { families: ['a', 'b'], }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual(config.reqOpts.table.columnFamilies, { a: {}, b: {}, }); - done(); }; - instance.createTable(TABLE_ID, options, assert.ifError); }); @@ -540,16 +530,13 @@ describe('Bigtable/Instance', () => { }, ], }; - const fakeRule = {a: 'b'}; - - // tslint:disable-next-line no-any - (FakeFamily as any).formatRule_ = rule => { + (FakeFamily.formatRule_ as Function) = (rule: {}) => { assert.strictEqual(rule, options.families[0].rule); return fakeRule; }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual(config.reqOpts.table.columnFamilies, { e: { gcRule: fakeRule, @@ -557,7 +544,6 @@ describe('Bigtable/Instance', () => { }); done(); }; - instance.createTable(TABLE_ID, options, assert.ifError); }); }); @@ -566,22 +552,18 @@ describe('Bigtable/Instance', () => { const response = { name: TABLE_NAME, }; - - const fakeTable = {}; - - instance.table = id => { + const fakeTable = {} as Table; + sandbox.stub(instance, 'table').callsFake(id => { assert.strictEqual(id, response.name.split('/').pop()); return fakeTable; - }; - - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; - + }); + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); instance.createTable(TABLE_ID, (err, table, apiResponse) => { assert.ifError(err); assert.strictEqual(table, fakeTable); - assert.strictEqual(table.metadata, response); + assert.strictEqual(table!.metadata, response); assert.strictEqual(apiResponse, response); done(); }); @@ -591,11 +573,9 @@ describe('Bigtable/Instance', () => { describe('cluster', () => { it('should return a Cluster object', () => { const cluster = instance.cluster(CLUSTER_ID); - assert(cluster instanceof FakeCluster); - - const args = cluster.calledWith_; - + // tslint:disable-next-line:no-any + const args = (cluster as any).calledWith_; assert.strictEqual(args[0], instance); assert.strictEqual(args[1], CLUSTER_ID); }); @@ -603,64 +583,55 @@ describe('Bigtable/Instance', () => { describe('delete', () => { it('should make the correct request', done => { - instance.bigtable.request = (config, callback) => { + (instance.bigtable.request as Function) = ( + // tslint:disable-next-line:no-any + config: any, + callback: Function + ) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'deleteInstance'); - assert.deepStrictEqual(config.reqOpts, { name: instance.name, }); - assert.deepStrictEqual(config.gaxOpts, {}); - callback(); // done() }; - instance.delete(done); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - instance.delete(gaxOptions, assert.ifError); }); }); describe('exists', () => { it('should not require gaxOptions', done => { - instance.getMetadata = gaxOptions => { + (instance.getMetadata as Function) = (gaxOptions: {}) => { assert.deepStrictEqual(gaxOptions, {}); done(); }; - instance.exists(assert.ifError); }); it('should pass gaxOptions to getMetadata', done => { const gaxOptions = {}; - - instance.getMetadata = gaxOptions_ => { + (instance.getMetadata as Function) = (gaxOptions_: {}) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; - instance.exists(gaxOptions, assert.ifError); }); it('should return false if error code is 5', done => { - // tslint:disable-next-line no-any - const error: any = new Error('Error.'); + const error = new Error('Error.') as ServiceError; error.code = 5; - - instance.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(instance, 'getMetadata').callsArgWith(1, error); instance.exists((err, exists) => { assert.ifError(err); assert.strictEqual(exists, false); @@ -669,14 +640,9 @@ describe('Bigtable/Instance', () => { }); it('should return error if code is not 5', done => { - // tslint:disable-next-line no-any - const error: any = new Error('Error.'); - error.code = 'NOT-5'; - - instance.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + const error = new Error('Error.') as ServiceError; + error.code = ('NOT-5' as {}) as number; + sandbox.stub(instance, 'getMetadata').callsArgWith(1, error); instance.exists(err => { assert.strictEqual(err, error); done(); @@ -684,10 +650,7 @@ describe('Bigtable/Instance', () => { }); it('should return true if no error', done => { - instance.getMetadata = (gaxOptions, callback) => { - callback(null, {}); - }; - + sandbox.stub(instance, 'getMetadata').callsArgWith(1, null, {}); instance.exists((err, exists) => { assert.ifError(err); assert.strictEqual(exists, true); @@ -699,31 +662,24 @@ describe('Bigtable/Instance', () => { describe('get', () => { it('should call getMetadata', done => { const gaxOptions = {}; - - instance.getMetadata = gaxOptions_ => { + (instance.getMetadata as Function) = (gaxOptions_: {}) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; - instance.get(gaxOptions, assert.ifError); }); it('should not require gaxOptions', done => { - instance.getMetadata = gaxOptions => { + (instance.getMetadata as Function) = (gaxOptions: {}) => { assert.deepStrictEqual(gaxOptions, {}); done(); }; - instance.get(assert.ifError); }); it('should return an error from getMetadata', done => { const error = new Error('Error.'); - - instance.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(instance, 'getMetadata').callsArgWith(1, error); instance.get(err => { assert.strictEqual(err, error); done(); @@ -732,11 +688,7 @@ describe('Bigtable/Instance', () => { it('should return self and API response', done => { const metadata = {}; - - instance.getMetadata = (gaxOptions, callback) => { - callback(null, metadata); - }; - + sandbox.stub(instance, 'getMetadata').callsArgWith(1, null, metadata); instance.get((err, instance_, metadata_) => { assert.ifError(err); assert.strictEqual(instance_, instance); @@ -748,7 +700,8 @@ describe('Bigtable/Instance', () => { describe('getAppProfiles', () => { it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'listAppProfiles'); assert.deepStrictEqual(config.reqOpts, { @@ -757,28 +710,22 @@ describe('Bigtable/Instance', () => { assert.deepStrictEqual(config.gaxOpts, {}); done(); }; - instance.getAppProfiles(assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - instance.getAppProfiles(gaxOptions, assert.ifError); }); it('should return error from gapic', done => { const error = new Error('Error.'); - - instance.bigtable.request = (config, callback) => { - callback(error); - }; - + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, error); instance.getAppProfiles(err => { assert.strictEqual(err, error); done(); @@ -787,17 +734,15 @@ describe('Bigtable/Instance', () => { it('should return an array of AppProfile objects', done => { const response = [{name: 'a'}, {name: 'b'}]; - - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; - + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); instance.getAppProfiles((err, appProfiles, apiResponse) => { assert.ifError(err); - assert.strictEqual(appProfiles[0].id, 'a'); - assert.deepStrictEqual(appProfiles[0].metadata, response[0]); - assert.strictEqual(appProfiles[1].id, 'b'); - assert.deepStrictEqual(appProfiles[1].metadata, response[1]); + assert.strictEqual(appProfiles![0].id, 'a'); + assert.deepStrictEqual(appProfiles![0].metadata, response[0]); + assert.strictEqual(appProfiles![1].id, 'b'); + assert.deepStrictEqual(appProfiles![1].metadata, response[1]); assert.strictEqual(apiResponse, response); done(); }); @@ -806,7 +751,8 @@ describe('Bigtable/Instance', () => { describe('getClusters', () => { it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'listClusters'); assert.deepStrictEqual(config.reqOpts, { @@ -815,28 +761,22 @@ describe('Bigtable/Instance', () => { assert.deepStrictEqual(config.gaxOpts, {}); done(); }; - instance.getClusters(assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - instance.getClusters(gaxOptions, assert.ifError); }); it('should return error from gapic', done => { const error = new Error('Error.'); - - instance.bigtable.request = (config, callback) => { - callback(error); - }; - + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, error); instance.getClusters(err => { assert.strictEqual(err, error); done(); @@ -854,26 +794,21 @@ describe('Bigtable/Instance', () => { }, ], }; - const fakeClusters = [{}, {}]; - - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; - + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); let clusterCount = 0; - - instance.cluster = name => { + (instance.cluster as Function) = (name: string) => { assert.strictEqual(name, response.clusters[clusterCount].name); return fakeClusters[clusterCount++]; }; - instance.getClusters((err, clusters, apiResponse) => { assert.ifError(err); - assert.strictEqual(clusters[0], fakeClusters[0]); - assert.strictEqual(clusters[0].metadata, response.clusters[0]); - assert.strictEqual(clusters[1], fakeClusters[1]); - assert.strictEqual(clusters[1].metadata, response.clusters[1]); + assert.strictEqual(clusters![0], fakeClusters[0]); + assert.strictEqual(clusters![0].metadata, response.clusters[0]); + assert.strictEqual(clusters![1], fakeClusters[1]); + assert.strictEqual(clusters![1].metadata, response.clusters[1]); assert.strictEqual(apiResponse, response); done(); }); @@ -882,7 +817,8 @@ describe('Bigtable/Instance', () => { describe('getIamPolicy', () => { it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'getIamPolicy'); assert.strictEqual(config.reqOpts.resource, instance.name); @@ -890,15 +826,15 @@ describe('Bigtable/Instance', () => { assert.strictEqual(config.gaxOpt, undefined); done(); }; - instance.getIamPolicy(assert.ifError); + (instance.getIamPolicy as Function)(assert.ifError); }); it('should accept options', done => { const requestedPolicyVersion = 0; const gaxOptions = {}; - const options = {gaxOptions, requestedPolicyVersion}; - - instance.bigtable.request = config => { + const options: GetIamPolicyOptions = {gaxOptions, requestedPolicyVersion}; + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); assert.strictEqual( config.reqOpts.options.requestedPolicyVersion, @@ -911,21 +847,17 @@ describe('Bigtable/Instance', () => { it('should return error', done => { const error = new Error('error'); - instance.bigtable.request = (config, callback) => { - callback(error); - }; - instance.getIamPolicy(err => { + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, error); + (instance.getIamPolicy as Function)((err: Error) => { assert.strictEqual(err, error); done(); }); }); it('should call decodePolicyEtag', () => { - instance.bigtable.request = (config, callback) => { - callback(null, {}); - }; + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, null, {}); const spy = sinon.stub(Table, 'decodePolicyEtag'); - instance.getIamPolicy(assert.ifError); + (instance.getIamPolicy as Function)(assert.ifError); assert.strictEqual(spy.calledOnce, true); spy.restore(); }); @@ -933,40 +865,34 @@ describe('Bigtable/Instance', () => { describe('getMetadata', () => { it('should make correct request', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'getInstance'); - assert.deepStrictEqual(config.reqOpts, { name: instance.name, }); - assert.deepStrictEqual(config.gaxOpts, {}); - done(); }; - instance.getMetadata(assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - instance.getMetadata(gaxOptions, assert.ifError); }); it('should update metadata', done => { const metadata = {}; - - instance.bigtable.request = (config, callback) => { - callback(null, metadata); - }; - + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, metadata); instance.getMetadata(() => { assert.strictEqual(instance.metadata, metadata); done(); @@ -975,11 +901,12 @@ describe('Bigtable/Instance', () => { it('should execute callback with original arguments', done => { const args = [{}, {}, {}]; - - instance.bigtable.request = (config, callback) => { + (instance.bigtable.request as Function) = ( + config: {}, + callback: Function + ) => { callback.apply(null, args); }; - instance.getMetadata((...args) => { assert.deepStrictEqual([].slice.call(args), args); done(); @@ -997,7 +924,8 @@ describe('Bigtable/Instance', () => { }); it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableTableAdminClient'); assert.strictEqual(config.method, 'listTables'); assert.strictEqual(config.reqOpts.parent, INSTANCE_NAME); @@ -1005,7 +933,6 @@ describe('Bigtable/Instance', () => { assert.strictEqual(config.gaxOpts, undefined); done(); }; - instance.getTables(assert.ifError); }); @@ -1013,12 +940,11 @@ describe('Bigtable/Instance', () => { const options = { gaxOptions: {}, }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, options.gaxOptions); done(); }; - instance.getTables(options, assert.ifError); }); @@ -1026,13 +952,12 @@ describe('Bigtable/Instance', () => { it('should set the "' + view + '" view', done => { const options = { view, - }; - - instance.bigtable.request = config => { - assert.strictEqual(config.reqOpts.view, views[view]); + } as GetTablesOptions; + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { + assert.strictEqual(config.reqOpts.view, views[view as 'unspecified']); done(); }; - instance.getTables(options, assert.ifError); }); }); @@ -1048,48 +973,48 @@ describe('Bigtable/Instance', () => { ]; const fakeTables = [{}, {}]; - - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; - + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); let tableCount = 0; - - instance.table = id => { + (instance.table as Function) = (id: string) => { assert.strictEqual(id, response[tableCount].name.split('/').pop()); return fakeTables[tableCount++]; }; instance.getTables((err, tables) => { assert.ifError(err); - assert.strictEqual(tables[0], fakeTables[0]); - assert.strictEqual(tables[0].metadata, response[0]); - assert.strictEqual(tables[1], fakeTables[1]); - assert.strictEqual(tables[1].metadata, response[1]); + assert.strictEqual(tables![0], fakeTables[0]); + assert.strictEqual(tables![0].metadata, response[0]); + assert.strictEqual(tables![1], fakeTables[1]); + assert.strictEqual(tables![1].metadata, response[1]); done(); }); }); it('should return original GAPIC response arguments', done => { const response = [{}, null, {}, {}]; - - instance.bigtable.request = (config, callback) => { + (instance.bigtable.request as Function) = ( + config: {}, + callback: Function + ) => { callback.apply(null, response); }; - instance.getTables((...args) => { assert.strictEqual(args[0], response[0]); assert.strictEqual(args[2], response[2]); - assert.strictEqual(args[3], response[3]); + // tslint:disable-next-line:no-any + assert.strictEqual((args as any)[3], response[3]); done(); }); }); }); describe('setIamPolicy', () => { - const policy = {}; + const policy = {} as Policy; it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'setIamPolicy'); assert.strictEqual(config.reqOpts.resource, instance.name); @@ -1102,8 +1027,8 @@ describe('Bigtable/Instance', () => { it('should accept gaxOptions', done => { const gaxOptions = {}; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; @@ -1124,8 +1049,8 @@ describe('Bigtable/Instance', () => { }, ], }; - - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.reqOpts.policy, policy); done(); }; @@ -1134,7 +1059,8 @@ describe('Bigtable/Instance', () => { it('should encode policy etag', done => { const policy = {etag: 'ABS'}; - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual( config.reqOpts.policy.etag, Buffer.from(policy.etag) @@ -1146,9 +1072,7 @@ describe('Bigtable/Instance', () => { it('should return error', done => { const error = new Error('error'); - instance.bigtable.request = (config, callback) => { - callback(error); - }; + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, error); instance.setIamPolicy(policy, err => { assert.strictEqual(err, error); done(); @@ -1156,9 +1080,7 @@ describe('Bigtable/Instance', () => { }); it('should call decodePolicyEtag', () => { - instance.bigtable.request = (config, callback) => { - callback(null, {}); - }; + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, null, {}); const spy = sinon.stub(Table, 'decodePolicyEtag'); instance.setIamPolicy(policy, assert.ifError); assert.strictEqual(spy.calledOnce, true); @@ -1173,24 +1095,24 @@ describe('Bigtable/Instance', () => { instance: {name: instance.name, displayName: 'updateDisplayName'}, updateMask: {paths: ['display_name']}, }; - - instance.bigtable.request = (config, callback) => { + (instance.bigtable.request as Function) = ( + // tslint:disable-next-line:no-any + config: any, + callback: Function + ) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'partialUpdateInstance'); assert.deepStrictEqual(config.reqOpts, expectedMetadata); callback(); // done() }; - instance.setMetadata(metadata, done); }); it('should update metadata property with API response', done => { const response = {}; - - instance.bigtable.request = (config, callback) => { - callback(null, response); - }; - + sandbox + .stub(instance.bigtable, 'request') + .callsArgWith(1, null, response); instance.setMetadata({}, err => { assert.ifError(err); assert.strictEqual(instance.metadata, response); @@ -1200,11 +1122,12 @@ describe('Bigtable/Instance', () => { it('should execute callback with all arguments', done => { const args = [{}, {}, {}]; - - instance.bigtable.request = (config, callback) => { + (instance.bigtable.request as Function) = ( + config: {}, + callback: Function + ) => { callback.apply(null, args); }; - instance.setMetadata({}, (...args) => { assert.deepStrictEqual([].slice.call(args), args); done(); @@ -1217,8 +1140,8 @@ describe('Bigtable/Instance', () => { it('should return a table instance', () => { const table = instance.table(TABLE_ID); - const args = table.calledWith_; - + // tslint:disable-next-line:no-any + const args = (table as any).calledWith_; assert(table instanceof FakeTable); assert.strictEqual(args[0], instance); assert.strictEqual(args[1], TABLE_ID); @@ -1228,7 +1151,8 @@ describe('Bigtable/Instance', () => { describe('testIamPermissions', () => { const permissions = 'bigtable.tables.get'; it('should provide the proper request options', done => { - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'testIamPermissions'); assert.strictEqual(config.reqOpts.resource, instance.name); @@ -1241,7 +1165,8 @@ describe('Bigtable/Instance', () => { it('should accept permissions as array', done => { const permissions = [`bigtable.tables.get`, `bigtable.tables.list`]; - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.deepStrictEqual(config.reqOpts.permissions, permissions); done(); }; @@ -1250,7 +1175,8 @@ describe('Bigtable/Instance', () => { it('should accept gaxOptions', done => { const gaxOptions = {}; - instance.bigtable.request = config => { + // tslint:disable-next-line:no-any + (instance.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; @@ -1259,7 +1185,10 @@ describe('Bigtable/Instance', () => { it('should unpack permissions from resp object', done => { const testPermissions = [`bigtable.tables.get`, `bigtable.tables.list`]; - instance.bigtable.request = (config, callback) => { + (instance.bigtable.request as Function) = ( + config: {}, + callback: Function + ) => { callback(null, {permissions: testPermissions}); }; instance.testIamPermissions(testPermissions, (err, permissions) => { @@ -1273,9 +1202,7 @@ describe('Bigtable/Instance', () => { it('should return error', done => { const permission = 'bigtable.tables.get'; const error = new Error('error'); - instance.bigtable.request = (config, callback) => { - callback(error); - }; + sandbox.stub(instance.bigtable, 'request').callsArgWith(1, error); instance.testIamPermissions(permission, (err, resp) => { assert.strictEqual(err, error); done(); diff --git a/test/mutation.ts b/test/mutation.ts index 3411f87b6..cc889b516 100644 --- a/test/mutation.ts +++ b/test/mutation.ts @@ -15,15 +15,15 @@ import * as assert from 'assert'; import {describe, it, afterEach} from 'mocha'; import * as Long from 'long'; -import * as sn from 'sinon'; +import * as sinon from 'sinon'; -import {IMutateRowRequest, Mutation} from '../src/mutation.js'; +import {IMutateRowRequest, Mutation, IMutation} from '../src/mutation.js'; -const sinon = sn.createSandbox(); +const sandbox = sinon.createSandbox(); describe('Bigtable/Mutation', () => { afterEach(() => { - sinon.restore(); + sandbox.restore(); }); describe('instantiation', () => { @@ -148,7 +148,7 @@ describe('Bigtable/Mutation', () => { it('should not create a new Buffer needlessly', () => { const message = 'Hello!'; const encoded = Buffer.from(message); - const stub = sinon.stub(Buffer, 'from'); + const stub = sandbox.stub(Buffer, 'from'); const decoded = Mutation.convertFromBytes(encoded); assert.strictEqual(stub.called, false); assert.strictEqual(decoded.toString(), message); @@ -193,23 +193,23 @@ describe('Bigtable/Mutation', () => { const timestamp = Date.now(); const dateObj = new Date(timestamp); const range = Mutation.createTimeRange(dateObj, dateObj); - assert.strictEqual(range.startTimestampMicros, timestamp * 1000); assert.strictEqual(range.endTimestampMicros, timestamp * 1000); }); }); describe('encodeSetCell', () => { - let convertCalls; // tslint:disable-next-line no-any - const fakeTime: any = new Date('2018-1-1'); + let convertCalls: any[]; + // tslint:disable-next-line no-any + const fakeTime = new Date('2018-1-1') as any; // tslint:disable-next-line no-any - const realTimestamp: any = new Date(); + const realTimestamp = new Date() as any; beforeEach(() => { - sinon.stub(global, 'Date').returns(fakeTime); + sandbox.stub(global, 'Date').returns(fakeTime); convertCalls = []; - sinon.stub(Mutation, 'convertToBytes').callsFake(value => { + sandbox.stub(Mutation, 'convertToBytes').callsFake(value => { convertCalls.push(value); return value; }); @@ -304,29 +304,19 @@ describe('Bigtable/Mutation', () => { }); describe('encodeDelete', () => { - let convert; // tslint:disable-next-line no-any let convertCalls: any[] = []; - before(() => { - convert = Mutation.convertToBytes; - Mutation.convertToBytes = value => { - convertCalls.push(value); - return value; - }; - }); - - after(() => { - Mutation.convertToBytes = convert; - }); - beforeEach(() => { convertCalls = []; + sandbox.stub(Mutation, 'convertToBytes').callsFake(value => { + convertCalls.push(value); + return value; + }); }); it('should create a delete row mutation', () => { const mutation = Mutation.encodeDelete(); - assert.deepStrictEqual(mutation, [ { deleteFromRow: {}, @@ -352,11 +342,8 @@ describe('Bigtable/Mutation', () => { family: 'followed', qualifier: null, }; - - sinon.stub(Mutation, 'parseColumnName').returns(fakeColumnName); - + sandbox.stub(Mutation, 'parseColumnName').returns(fakeColumnName); const mutation = Mutation.encodeDelete(['follows']); - assert.deepStrictEqual(mutation, [ { deleteFromFamily: { @@ -368,7 +355,6 @@ describe('Bigtable/Mutation', () => { it('should create a delete column mutation', () => { const mutation = Mutation.encodeDelete(['follows:gwashington']); - assert.deepStrictEqual(mutation, [ { deleteFromColumn: { @@ -425,20 +411,14 @@ describe('Bigtable/Mutation', () => { }); describe('parse', () => { - let toProto; let toProtoCalled = false; const fakeData = {a: 'a'} as IMutateRowRequest; - before(() => { - toProto = Mutation.prototype.toProto; - Mutation.prototype.toProto = () => { + beforeEach(() => { + sandbox.stub(Mutation.prototype, 'toProto').callsFake(() => { toProtoCalled = true; return fakeData; - }; - }); - - after(() => { - Mutation.prototype.toProto = toProto; + }); }); it('should create a new mutation object and parse it', () => { @@ -447,9 +427,7 @@ describe('Bigtable/Mutation', () => { method: 'b', data: 'c', } as Mutation; - const mutation = Mutation.parse(fakeMutationData); - assert.strictEqual(toProtoCalled, true); assert.strictEqual(mutation, fakeData); }); @@ -485,23 +463,14 @@ describe('Bigtable/Mutation', () => { }); describe('toProto', () => { - let convert; // tslint:disable-next-line no-any let convertCalls: any[] = []; - before(() => { - convert = Mutation.convertToBytes; - Mutation.convertToBytes = value => { + beforeEach(() => { + sandbox.stub(Mutation, 'convertToBytes').callsFake(value => { convertCalls.push(value); return value; - }; - }); - - after(() => { - Mutation.convertToBytes = convert; - }); - - beforeEach(() => { + }); convertCalls = []; }); @@ -512,37 +481,29 @@ describe('Bigtable/Mutation', () => { method: 'insert', data: [], }; - const mutation = new Mutation(data); - Mutation.encodeSetCell = _data => { assert.strictEqual(_data, data.data); return fakeEncoded; }; - const mutationProto = mutation.toProto(); - assert.strictEqual(mutationProto.mutations, fakeEncoded); assert.strictEqual(mutationProto.rowKey, data.key); assert.strictEqual(convertCalls[0], data.key); }); it('should encode delete mutations when method is delete', () => { - const fakeEncoded = [{b: 'b'}]; + const fakeEncoded = ([{b: 'b'}] as {}) as IMutation[]; const data = { key: 'b', method: 'delete', data: [], }; - - // tslint:disable-next-line no-any - (Mutation as any).encodeDelete = _data => { + sandbox.stub(Mutation, 'encodeDelete').callsFake(_data => { assert.strictEqual(_data, data.data); return fakeEncoded; - }; - + }); const mutation = new Mutation(data).toProto(); - assert.strictEqual(mutation.mutations, fakeEncoded); assert.strictEqual(mutation.rowKey, data.key); assert.strictEqual(convertCalls[0], data.key); diff --git a/test/row.ts b/test/row.ts index db33108d3..345adfe84 100644 --- a/test/row.ts +++ b/test/row.ts @@ -16,15 +16,17 @@ import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; import {describe, it} from 'mocha'; import * as proxyquire from 'proxyquire'; - -const sn = require('sinon'); +import * as sinon from 'sinon'; import {Mutation} from '../src/mutation.js'; +import * as rw from '../src/row'; +import {Table, Entry} from '../src/table.js'; +import {Chunk} from '../src/chunktransformer.js'; -const sinon = sn.createSandbox(); +const sandbox = sinon.createSandbox(); let promisified = false; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass) { + promisifyAll(klass: Function) { if (klass.name === 'Row') { promisified = true; } @@ -36,24 +38,24 @@ const CONVERTED_ROW_ID = 'my-converted-row'; const TABLE = { bigtable: {}, name: '/projects/project/instances/my-instance/tables/my-table', -}; +} as Table; // tslint:disable-next-line variable-name const FakeMutation = { methods: Mutation.methods, - convertToBytes: sinon.spy(value => { + convertToBytes: sandbox.spy(value => { if (value === ROW_ID) { return CONVERTED_ROW_ID; } return value; }), - convertFromBytes: sinon.spy(value => { + convertFromBytes: sandbox.spy(value => { return value; }), - parseColumnName: sinon.spy(column => { + parseColumnName: sandbox.spy(column => { return Mutation.parseColumnName(column); }), - parse: sinon.spy(entry => { + parse: sandbox.spy(entry => { return { mutations: entry, }; @@ -62,17 +64,17 @@ const FakeMutation = { // tslint:disable-next-line variable-name const FakeFilter = { - parse: sinon.spy(filter => { + parse: sandbox.spy(filter => { return filter; }), }; describe('Bigtable/Row', () => { // tslint:disable-next-line variable-name - let Row; + let Row: typeof rw.Row; // tslint:disable-next-line variable-name - let RowError; - let row; + let RowError: typeof rw.RowError; + let row: rw.Row; before(() => { // tslint:disable-next-line variable-name @@ -90,9 +92,12 @@ describe('Bigtable/Row', () => { }); afterEach(() => { + sandbox.restore(); Object.keys(FakeMutation).forEach(spy => { - if (FakeMutation[spy].resetHistory) { - FakeMutation[spy].resetHistory(); + // tslint:disable-next-line:no-any + if ((FakeMutation as any)[spy].resetHistory) { + // tslint:disable-next-line:no-any + (FakeMutation as any)[spy].resetHistory(); } }); }); @@ -124,7 +129,7 @@ describe('Bigtable/Row', () => { beforeEach(() => { convert = FakeMutation.convertFromBytes; - FakeMutation.convertFromBytes = sinon.spy(val => { + FakeMutation.convertFromBytes = sandbox.spy(val => { return val.replace('unconverted', 'converted'); }); }); @@ -154,7 +159,7 @@ describe('Bigtable/Row', () => { { commitRow: true, }, - ]; + ] as Chunk[]; const rows = Row.formatChunks_(chunks); @@ -178,7 +183,7 @@ describe('Bigtable/Row', () => { }); it('should inherit the row key', () => { - const chunks = [ + const chunks = ([ { rowKey: 'unconvertedKey', }, @@ -199,7 +204,7 @@ describe('Bigtable/Row', () => { }, commitRow: true, }, - ]; + ] as {}) as Chunk[]; const rows = Row.formatChunks_(chunks); @@ -240,7 +245,7 @@ describe('Bigtable/Row', () => { { commitRow: true, }, - ]; + ] as Chunk[]; const rows = Row.formatChunks_(chunks); @@ -286,7 +291,7 @@ describe('Bigtable/Row', () => { { commitRow: true, }, - ]; + ] as Chunk[]; const rows = Row.formatChunks_(chunks); @@ -320,10 +325,12 @@ describe('Bigtable/Row', () => { decode: false, }; - FakeMutation.convertFromBytes = sinon.spy((val, options) => { - assert.deepStrictEqual(options, {userOptions: formatOptions}); - return val.replace('unconverted', 'converted'); - }); + (FakeMutation.convertFromBytes as Function) = sandbox.spy( + (val, options) => { + assert.deepStrictEqual(options, {userOptions: formatOptions}); + return val.replace('unconverted', 'converted'); + } + ); const timestamp1 = 123; const timestamp2 = 345; @@ -353,7 +360,7 @@ describe('Bigtable/Row', () => { { commitRow: true, }, - ]; + ] as Chunk[]; const rows = Row.formatChunks_(chunks, formatOptions); @@ -385,7 +392,9 @@ describe('Bigtable/Row', () => { // 1 === qualifier // 2 === value const args = FakeMutation.convertFromBytes.getCall(2).args; - assert.deepStrictEqual(args[1], {userOptions: formatOptions}); + assert.deepStrictEqual((args as string[])[1], { + userOptions: formatOptions, + }); }); it('should use the encoding scheme provided', () => { @@ -393,10 +402,12 @@ describe('Bigtable/Row', () => { encoding: 'binary', }; - FakeMutation.convertFromBytes = sinon.spy((val, options) => { - assert.deepStrictEqual(options, {userOptions: formatOptions}); - return val.toString(formatOptions.encoding); - }); + (FakeMutation.convertFromBytes as Function) = sandbox.spy( + (val, options) => { + assert.deepStrictEqual(options, {userOptions: formatOptions}); + return val.toString(formatOptions.encoding); + } + ); const chunks = [ { @@ -413,7 +424,7 @@ describe('Bigtable/Row', () => { timestampMicros: 123, commitRow: true, }, - ]; + ] as Chunk[]; const rows = Row.formatChunks_(chunks, formatOptions); @@ -438,12 +449,12 @@ describe('Bigtable/Row', () => { // 0 === row key // 1 === qualifier // 2 === value - const args = FakeMutation.convertFromBytes.getCall(2).args; + const args: string[] = FakeMutation.convertFromBytes.getCall(2).args; assert.deepStrictEqual(args[1], {userOptions: formatOptions}); }); it('should discard old data when reset row is found', () => { - const chunks = [ + const chunks = ([ { rowKey: 'unconvertedKey', familyName: { @@ -476,7 +487,7 @@ describe('Bigtable/Row', () => { { commitRow: true, }, - ]; + ] as {}) as Chunk[]; const rows = Row.formatChunks_(chunks); @@ -536,7 +547,6 @@ describe('Bigtable/Row', () => { it('should format the families into a user-friendly format', () => { const formatted = Row.formatFamilies_(families); assert.deepStrictEqual(formatted, formattedRowData); - const convertStpy = FakeMutation.convertFromBytes; assert.strictEqual(convertStpy.callCount, 2); assert.strictEqual(convertStpy.getCall(0).args[0], 'test-column'); @@ -547,9 +557,7 @@ describe('Bigtable/Row', () => { const formatted = Row.formatFamilies_(families, { decode: false, }); - assert.deepStrictEqual(formatted, formattedRowData); - const convertStpy = FakeMutation.convertFromBytes; assert.strictEqual(convertStpy.callCount, 1); assert.strictEqual(convertStpy.getCall(0).args[0], 'test-column'); @@ -558,14 +566,13 @@ describe('Bigtable/Row', () => { describe('create', () => { it('should provide the proper request options', done => { - row.table.mutate = (entry, gaxOptions) => { + (row.table.mutate as Function) = (entry: Entry, gaxOptions: {}) => { assert.strictEqual(entry.key, row.id); assert.strictEqual(entry.data, undefined); assert.strictEqual(entry.method, Mutation.methods.INSERT); assert.strictEqual(gaxOptions, undefined); done(); }; - row.create(assert.ifError); }); @@ -576,12 +583,10 @@ describe('Bigtable/Row', () => { b: 'b', }, }; - - row.table.mutate = entry => { + (row.table.mutate as Function) = (entry: Entry) => { assert.strictEqual(entry.data, options.entry); done(); }; - row.create(options, assert.ifError); }); @@ -589,23 +594,17 @@ describe('Bigtable/Row', () => { const options = { gaxOptions: {}, }; - - row.table.mutate = (entry, gaxOptions) => { + (row.table.mutate as Function) = (entry: Entry, gaxOptions: {}) => { assert.strictEqual(gaxOptions, options.gaxOptions); done(); }; - row.create(options, assert.ifError); }); it('should return an error to the callback', done => { const err = new Error('err'); const response = {}; - - row.table.mutate = (entry, gaxOptions, callback) => { - callback(err, response); - }; - + sandbox.stub(row.table, 'mutate').callsArgWith(2, err, response); row.create((err_, row, apiResponse) => { assert.strictEqual(err, err_); assert.strictEqual(row, null); @@ -616,11 +615,7 @@ describe('Bigtable/Row', () => { it('should return the Row instance', done => { const response = {}; - - row.table.mutate = (entry, gaxOptions, callback) => { - callback(null, response); - }; - + sandbox.stub(row.table, 'mutate').callsArgWith(2, null, response); row.create((err, row_, apiResponse) => { assert.ifError(err); assert.strictEqual(row, row_); @@ -641,18 +636,20 @@ describe('Bigtable/Row', () => { it('should throw if a rule is not provided', () => { assert.throws(() => { - row.createRules(); + (row.createRules as Function)(); }, /At least one rule must be provided\./); }); it('should read/modify/write rules', done => { - row.bigtable.request = (config, callback) => { + (row.bigtable.request as Function) = ( + // tslint:disable-next-line:no-any + config: any, + callback: Function + ) => { assert.strictEqual(config.client, 'BigtableClient'); assert.strictEqual(config.method, 'readModifyWriteRow'); - assert.strictEqual(config.reqOpts.tableName, TABLE.name); assert.strictEqual(config.reqOpts.rowKey, CONVERTED_ROW_ID); - assert.deepStrictEqual(config.reqOpts.rules, [ { familyName: 'a', @@ -661,76 +658,71 @@ describe('Bigtable/Row', () => { incrementAmount: 1, }, ]); - const spy = FakeMutation.convertToBytes; - assert.strictEqual(spy.getCall(0).args[0], 'b'); assert.strictEqual(spy.getCall(1).args[0], 'c'); assert.strictEqual(spy.getCall(2).args[0], ROW_ID); - callback(); // done() }; - row.createRules(rules, done); }); it('should use an appProfileId', done => { const bigtableInstance = row.bigtable; bigtableInstance.appProfileId = 'app-profile-id-12345'; - - bigtableInstance.request = config => { + // tslint:disable-next-line:no-any + (bigtableInstance.request as Function) = (config: any) => { assert.strictEqual( config.reqOpts.appProfileId, bigtableInstance.appProfileId ); done(); }; - row.createRules(rules, assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - row.bigtable.request = config => { + // tslint:disable-next-line:no-any + (row.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); }; - row.createRules(rules, gaxOptions, assert.ifError); }); }); describe('delete', () => { it('should provide the proper request options', done => { - row.table.mutate = (mutation, gaxOptions, callback) => { + (row.table.mutate as Function) = ( + // tslint:disable-next-line:no-any + mutation: any, + gaxOptions: {}, + callback: Function + ) => { assert.strictEqual(mutation.key, ROW_ID); assert.strictEqual(mutation.method, FakeMutation.methods.DELETE); assert.deepStrictEqual(gaxOptions, {}); callback(); // done() }; - row.delete(done); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - row.table.mutate = (mutation, gaxOptions_) => { + (row.table.mutate as Function) = (mutation: {}, gaxOptions_: {}) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; - row.delete(gaxOptions, done); }); it('should remove existing data', done => { const gaxOptions = {}; - row.table.mutate = (mutation, gaxOptions_) => { + (row.table.mutate as Function) = (mutation: {}, gaxOptions_: {}) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); }; - row.delete(gaxOptions, done); assert.strictEqual(row.data, undefined); }); @@ -740,33 +732,32 @@ describe('Bigtable/Row', () => { const columns = ['a:b', 'c']; it('should provide the proper request options', done => { - row.table.mutate = (mutation, gaxOptions, callback) => { + (row.table.mutate as Function) = ( + // tslint:disable-next-line:no-any + mutation: any, + gaxOptions: {}, + callback: Function + ) => { assert.strictEqual(mutation.key, ROW_ID); assert.strictEqual(mutation.data, columns); assert.strictEqual(mutation.method, FakeMutation.methods.DELETE); assert.deepStrictEqual(gaxOptions, {}); callback(); // done() }; - row.deleteCells(columns, done); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - row.table.mutate = (mutation, gaxOptions_) => { + sandbox.stub(row.table, 'mutate').callsFake((mutation, gaxOptions_) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); row.deleteCells(columns, gaxOptions, done); }); it('should remove existing data', done => { - row.table.mutate = (mutation, gaxOptions, callback) => { - callback(); // done() - }; - + sandbox.stub(row.table, 'mutate').callsArg(2); row.deleteCells(columns, done); assert.strictEqual(row.data, undefined); }); @@ -774,32 +765,25 @@ describe('Bigtable/Row', () => { describe('exists', () => { it('should not require gaxOptions', done => { - row.getMetadata = gaxOptions => { + sandbox.stub(row, 'getMetadata').callsFake(gaxOptions => { assert.deepStrictEqual(gaxOptions, {}); done(); - }; - + }); row.exists(assert.ifError); }); it('should pass gaxOptions to getMetadata', done => { const gaxOptions = {}; - - row.getMetadata = gaxOptions_ => { + sandbox.stub(row, 'getMetadata').callsFake(gaxOptions_ => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); row.exists(gaxOptions, assert.ifError); }); it('should return false if error is RowError', done => { const error = new RowError('Error.'); - - row.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(row, 'getMetadata').callsArgWith(1, error); row.exists((err, exists) => { assert.ifError(err); assert.strictEqual(exists, false); @@ -809,11 +793,7 @@ describe('Bigtable/Row', () => { it('should return error if not RowError', done => { const error = new Error('Error.'); - - row.getMetadata = (gaxOptions, callback) => { - callback(error); - }; - + sandbox.stub(row, 'getMetadata').callsArgWith(1, error); row.exists(err => { assert.strictEqual(err, error); done(); @@ -821,10 +801,7 @@ describe('Bigtable/Row', () => { }); it('should return true if no error', done => { - row.getMetadata = (gaxOptions, callback) => { - callback(null, {}); - }; - + sandbox.stub(row, 'getMetadata').callsArgWith(1, null, {}); row.exists((err, exists) => { assert.ifError(err); assert.strictEqual(exists, true); @@ -834,22 +811,22 @@ describe('Bigtable/Row', () => { }); describe('filter', () => { - const mutations = [ + const mutations = ([ { method: 'insert', data: { a: 'a', }, }, - ]; + ] as {}) as rw.FilterConfigOption[]; - const fakeMutations = { + const fakeMutations = ({ mutations: [ { a: 'b', }, ], - }; + } as {}) as {mutations: rw.FilterConfigOption}; beforeEach(() => { FakeMutation.parse.resetHistory(); @@ -865,15 +842,16 @@ describe('Bigtable/Row', () => { column: 'b', }; - FakeFilter.parse = sinon.spy(() => { + (FakeFilter.parse as Function) = sandbox.spy(() => { return fakeParsedFilter; }); - FakeMutation.parse = sinon.spy(() => { + (FakeMutation.parse as Function) = sandbox.spy(() => { return fakeMutations; }); - row.bigtable.request = config => { + // tslint:disable-next-line:no-any + (row.bigtable.request as Function) = (config: any) => { assert.strictEqual(config.client, 'BigtableClient'); assert.strictEqual(config.method, 'checkAndMutateRow'); assert.strictEqual(config.reqOpts.tableName, TABLE.name); @@ -892,14 +870,11 @@ describe('Bigtable/Row', () => { ); assert.strictEqual(config.gaxOpts, undefined); - assert.strictEqual(FakeMutation.parse.callCount, 2); assert.strictEqual(FakeMutation.parse.getCall(0).args[0], mutations[0]); assert.strictEqual(FakeMutation.parse.getCall(1).args[0], mutations[0]); - assert.strictEqual(FakeFilter.parse.callCount, 1); assert(FakeFilter.parse.calledWithExactly(filter)); - done(); }; @@ -918,12 +893,10 @@ describe('Bigtable/Row', () => { column: 'a', }; const gaxOptions = {}; - - row.bigtable.request = config => { + sandbox.stub(row.bigtable, 'request').callsFake(config => { assert.strictEqual(config.gaxOpts, gaxOptions); done(); - }; - + }); row.filter(filter, {gaxOptions}, assert.ifError); }); @@ -931,58 +904,56 @@ describe('Bigtable/Row', () => { const filter = { column: 'a', }; - const bigtableInstance = row.bigtable; bigtableInstance.appProfileId = 'app-profile-id-12345'; - - bigtableInstance.request = config => { + sandbox.stub(bigtableInstance, 'request').callsFake(config => { assert.strictEqual( config.reqOpts.appProfileId, bigtableInstance.appProfileId ); done(); - }; - + }); row.filter(filter, assert.ifError); }); it('should return an error to the callback', done => { const err = new Error('err'); const response = {}; - - row.bigtable.request = (config, callback) => { - callback(err, response); - }; - - row.filter({}, mutations, (err_, matched, apiResponse) => { - assert.strictEqual(err, err_); - assert.strictEqual(matched, null); - assert.strictEqual(response, apiResponse); - done(); - }); + sandbox.stub(row.bigtable, 'request').callsArgWith(1, err, response); + (row.filter as Function)( + {}, + mutations, + (err_: Error, matched: boolean, apiResponse: {}) => { + assert.strictEqual(err, err_); + assert.strictEqual(matched, null); + assert.strictEqual(response, apiResponse); + done(); + } + ); }); it('should return a matched flag', done => { const response = { predicateMatched: true, }; - - row.bigtable.request = (config, callback) => { - callback(null, response); - }; - - row.filter({}, mutations, (err, matched, apiResponse) => { - assert.ifError(err); - assert(matched); - assert.strictEqual(response, apiResponse); - done(); - }); + sandbox.stub(row.bigtable, 'request').callsArgWith(1, null, response); + (row.filter as Function)( + {}, + mutations, + (err: Error, matched: boolean, apiResponse: {}) => { + assert.ifError(err); + assert(matched); + assert.strictEqual(response, apiResponse); + done(); + } + ); }); }); describe('get', () => { it('should provide the proper request options', done => { - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.strictEqual(reqOpts.keys[0], ROW_ID); assert.strictEqual(reqOpts.filter, undefined); assert.strictEqual(FakeMutation.parseColumnName.callCount, 0); @@ -1004,13 +975,13 @@ describe('Bigtable/Row', () => { }, ]; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.deepStrictEqual(reqOpts.filter, expectedFilter); assert.strictEqual(FakeMutation.parseColumnName.callCount, 1); assert(FakeMutation.parseColumnName.calledWith(keys[0])); done(); }; - row.get(keys, assert.ifError); }); @@ -1040,7 +1011,8 @@ describe('Bigtable/Row', () => { }, ]; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.deepStrictEqual(reqOpts.filter, expectedFilter); const spy = FakeMutation.parseColumnName; @@ -1063,7 +1035,8 @@ describe('Bigtable/Row', () => { }, ]; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.deepStrictEqual(reqOpts.filter, expectedFilter); assert.strictEqual(FakeMutation.parseColumnName.callCount, 1); assert(FakeMutation.parseColumnName.calledWith(keys[0])); @@ -1101,7 +1074,8 @@ describe('Bigtable/Row', () => { }, ]; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.deepStrictEqual(reqOpts.filter, expectedFilter); assert.strictEqual(FakeMutation.parseColumnName.callCount, 1); assert(FakeMutation.parseColumnName.calledWith(keys[0])); @@ -1154,7 +1128,8 @@ describe('Bigtable/Row', () => { }, ]; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.deepStrictEqual(reqOpts.filter, expectedFilter); assert.strictEqual(FakeMutation.parseColumnName.callCount, 2); assert(FakeMutation.parseColumnName.calledWith(keys[0])); @@ -1166,7 +1141,7 @@ describe('Bigtable/Row', () => { }); it('should respect filter in options object', done => { - const keys = []; + const keys = [] as string[]; const options = { decode: false, @@ -1174,7 +1149,8 @@ describe('Bigtable/Row', () => { }; const expectedFilter = options.filter; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.deepStrictEqual(reqOpts.filter, expectedFilter); done(); }; @@ -1187,7 +1163,8 @@ describe('Bigtable/Row', () => { decode: false, }; - row.table.getRows = reqOpts => { + // tslint:disable-next-line:no-any + (row.table.getRows as Function) = (reqOpts: any) => { assert.strictEqual(reqOpts.decode, options.decode); assert(!reqOpts.filter); done(); @@ -1198,11 +1175,7 @@ describe('Bigtable/Row', () => { it('should return an error to the callback', done => { const error = new Error('err'); - - row.table.getRows = (r, callback) => { - callback(error); - }; - + sandbox.stub(row.table, 'getRows').callsArgWith(1, error); row.get((err, row) => { assert.strictEqual(error, err); assert.strictEqual(row, undefined); @@ -1211,13 +1184,10 @@ describe('Bigtable/Row', () => { }); it('should return a custom error if the row is not found', done => { - row.table.getRows = (r, callback) => { - callback(null, []); - }; - + sandbox.stub(row.table, 'getRows').callsArgWith(1, null, []); row.get((err, row_) => { assert(err instanceof RowError); - assert.strictEqual(err.message, 'Unknown row: ' + row.id + '.'); + assert.strictEqual(err!.message, 'Unknown row: ' + row.id + '.'); assert.deepStrictEqual(row_, undefined); done(); }); @@ -1225,16 +1195,11 @@ describe('Bigtable/Row', () => { it('should update the row data upon success', done => { const fakeRow = new Row(TABLE, ROW_ID); - fakeRow.data = { a: 'a', b: 'b', }; - - row.table.getRows = (r, callback) => { - callback(null, [fakeRow]); - }; - + sandbox.stub(row.table, 'getRows').callsArgWith(1, null, [fakeRow]); row.get((err, row_) => { assert.ifError(err); assert.strictEqual(row_, row); @@ -1256,11 +1221,7 @@ describe('Bigtable/Row', () => { row.data = { c: 'c', }; - - row.table.getRows = (r, callback) => { - callback(null, [fakeRow]); - }; - + sandbox.stub(row.table, 'getRows').callsArgWith(1, null, [fakeRow]); row.get(keys, (err, data) => { assert.ifError(err); assert.deepStrictEqual(Object.keys(data), keys); @@ -1272,11 +1233,7 @@ describe('Bigtable/Row', () => { describe('getMetadata', () => { it('should return an error to the callback', done => { const error = new Error('err'); - - row.get = (options, callback) => { - callback(error); - }; - + sandbox.stub(row, 'get').callsArgWith(1, error); row.getMetadata((err, metadata) => { assert.strictEqual(error, err); assert.strictEqual(metadata, undefined); @@ -1289,13 +1246,8 @@ describe('Bigtable/Row', () => { a: 'a', b: 'b', }; - - row.get = (options, callback) => { - callback(null, row); - }; - + sandbox.stub(row, 'get').callsArgWith(1, null, row); row.metadata = fakeMetadata; - row.getMetadata((err, metadata) => { assert.ifError(err); assert.strictEqual(metadata, fakeMetadata); @@ -1308,14 +1260,11 @@ describe('Bigtable/Row', () => { const fakeOptions = { decode: false, }; - - row.get = (options, callback) => { + (row.get as Function) = (options: {}, callback: Function) => { assert.strictEqual(options, fakeOptions); callback(null, row); }; - row.metadata = fakeMetadata; - row.getMetadata(fakeOptions, (err, metadata) => { assert.ifError(err); assert.strictEqual(metadata, fakeMetadata); @@ -1326,10 +1275,10 @@ describe('Bigtable/Row', () => { describe('increment', () => { const COLUMN_NAME = 'a:b'; - let formatFamiliesSpy; + let formatFamiliesSpy: sinon.SinonSpy; beforeEach(() => { - formatFamiliesSpy = sinon.stub(Row, 'formatFamilies_').returns({ + formatFamiliesSpy = sandbox.stub(Row, 'formatFamilies_').returns({ a: { b: [ { @@ -1345,59 +1294,48 @@ describe('Bigtable/Row', () => { }); it('should provide the proper request options', done => { - row.createRules = (reqOpts, gaxOptions) => { - assert.strictEqual(reqOpts.column, COLUMN_NAME); - assert.strictEqual(reqOpts.increment, 1); + sandbox.stub(row, 'createRules').callsFake((reqOpts, gaxOptions) => { + assert.strictEqual((reqOpts as rw.Rule).column, COLUMN_NAME); + assert.strictEqual((reqOpts as rw.Rule).increment, 1); assert.deepStrictEqual(gaxOptions, {}); done(); - }; - + }); row.increment(COLUMN_NAME, assert.ifError); }); it('should optionally accept an increment amount', done => { const increment = 10; - - row.createRules = reqOpts => { - assert.strictEqual(reqOpts.increment, increment); + sandbox.stub(row, 'createRules').callsFake(reqOpts => { + assert.strictEqual((reqOpts as rw.Rule).increment, increment); done(); - }; - + }); row.increment(COLUMN_NAME, increment, assert.ifError); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - row.createRules = (reqOpts, gaxOptions_) => { + sandbox.stub(row, 'createRules').callsFake((reqOpts, gaxOptions_) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); row.increment(COLUMN_NAME, gaxOptions, assert.ifError); }); it('should accept increment amount and gaxOptions', done => { const increment = 10; const gaxOptions = {}; - - row.createRules = (reqOpts, gaxOptions_) => { - assert.strictEqual(reqOpts.increment, increment); + sandbox.stub(row, 'createRules').callsFake((reqOpts, gaxOptions_) => { + assert.strictEqual((reqOpts as rw.Rule).increment, increment); assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); row.increment(COLUMN_NAME, increment, gaxOptions, assert.ifError); }); it('should return an error to the callback', done => { const error = new Error('err'); const response = {}; - - row.createRules = (r, gaxOptions, callback) => { - callback(error, response); - }; - + sandbox.stub(row, 'createRules').callsArgWith(2, error, response); row.increment(COLUMN_NAME, (err, value, apiResponse) => { assert.strictEqual(err, error); assert.strictEqual(value, null); @@ -1430,10 +1368,7 @@ describe('Bigtable/Row', () => { }, }; - row.createRules = (r, gaxOptions, callback) => { - callback(null, response); - }; - + sandbox.stub(row, 'createRules').callsArgWith(2, null, response); row.increment(COLUMN_NAME, (err, value, apiResponse) => { assert.ifError(err); assert.strictEqual(value, fakeValue); @@ -1453,32 +1388,33 @@ describe('Bigtable/Row', () => { }; it('should insert an object', done => { - row.table.mutate = (entry, gaxOptions, callback) => { + (row.table.mutate as Function) = ( + // tslint:disable-next-line:no-any + entry: any, + gaxOptions: {}, + callback: Function + ) => { assert.strictEqual(entry.data, data); callback(); // done() }; - row.save(data, done); }); it('should accept gaxOptions', done => { const gaxOptions = {}; - - row.table.mutate = (entry, gaxOptions_) => { + sandbox.stub(row.table, 'mutate').callsFake((entry, gaxOptions_) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); row.save(data, gaxOptions, assert.ifError); }); it('should remove existing data', done => { const gaxOptions = {}; - row.table.mutate = (entry, gaxOptions_) => { + sandbox.stub(row.table, 'mutate').callsFake((entry, gaxOptions_) => { assert.strictEqual(gaxOptions_, gaxOptions); done(); - }; - + }); row.save(data, gaxOptions, assert.ifError); assert.strictEqual(row.data, undefined); }); diff --git a/test/table.ts b/test/table.ts index 98a36d565..45140981e 100644 --- a/test/table.ts +++ b/test/table.ts @@ -35,7 +35,8 @@ const noop = () => {}; let promisified = false; const fakePromisify = Object.assign({}, promisify, { - promisifyAll(klass, options) { + // tslint:disable-next-line:no-any + promisifyAll(klass: Function, options: any) { if (klass.name !== 'Table') { return; } @@ -44,7 +45,8 @@ const fakePromisify = Object.assign({}, promisify, { }, }); -function createFake(klass) { +// tslint:disable-next-line:no-any +function createFake(klass: any) { return class Fake extends klass { calledWith_: IArguments; constructor() {