Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ts): convert tests to typescript #404

Merged
merged 1 commit into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.3.0",
"@types/extend": "^3.0.0",
"@types/mocha": "^5.2.5",
"async": "^2.6.1",
"binary-search-bounds": "^2.0.4",
Expand Down
16 changes: 10 additions & 6 deletions test/batch-transaction.js → test/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

const assert = require('assert');
import * as assert from 'assert';
const extend = require('extend');
const proxyquire = require('proxyquire');
const {util} = require('@google-cloud/common-grpc');
Expand All @@ -31,23 +31,27 @@ const fakePfy = extend({}, pfy, {
},
});

const fakeCodec = {
const fakeCodec: any = {
encode: util.noop,
Int: function() {},
Float: function() {},
SpannerDate: function() {},
};

function FakeTransaction(session) {
this.calledWith_ = arguments;
this.session = session;
class FakeTransaction {
calledWith_: IArguments;
session;
constructor(session) {
this.calledWith_ = arguments;
this.session = session;
}
}

describe('BatchTransaction', () => {
let BatchTransaction;
let batchTransaction;

const SESSION = {};
const SESSION: any = {};

before(() => {
BatchTransaction = proxyquire('../src/batch-transaction.js', {
Expand Down
6 changes: 3 additions & 3 deletions test/codec.js → test/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

'use strict';

const assert = require('assert');
import * as assert from 'assert';
const extend = require('extend');
const proxyquire = require('proxyquire');
const {util} = require('@google-cloud/common-grpc');

function FakeGrpcService() {}
const FakeGrpcService: any = class {};

describe('codec', () => {
let codecCached;
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('codec', () => {
},
});

assert.deepStrictEqual(decoded, Buffer.from(value, 'base64'));
assert.deepStrictEqual(decoded, Buffer.from(value as any, 'base64'));
});

it('should decode FLOAT64', () => {
Expand Down
86 changes: 52 additions & 34 deletions test/database.js → test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

'use strict';

const assert = require('assert');
const events = require('events');
const extend = require('extend');
const nodeutil = require('util');
import * as assert from 'assert';
import {EventEmitter} from 'events';
import * as extend from 'extend';
import { ApiError } from '@google-cloud/common';
const proxyquire = require('proxyquire');
const through = require('through2');
const {util} = require('@google-cloud/common-grpc');
Expand All @@ -43,40 +43,59 @@ const fakePfy = extend({}, pfy, {
},
});

function FakeBatchTransaction() {
this.calledWith_ = arguments;
class FakeBatchTransaction {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

function FakeGrpcServiceObject() {
this.calledWith_ = arguments;
events.EventEmitter.call(this);
class FakeGrpcServiceObject extends EventEmitter {
calledWith_: IArguments;
constructor() {
super();
this.calledWith_ = arguments;
}
}
nodeutil.inherits(FakeGrpcServiceObject, events.EventEmitter);

function FakePartialResultStream() {
this.calledWith_ = arguments;
class FakePartialResultStream {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

function FakeSession() {
this.calledWith_ = arguments;
class FakeSession {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

function FakeSessionPool() {
this.calledWith_ = arguments;
events.EventEmitter.call(this);
class FakeSessionPool extends EventEmitter {
calledWith_: IArguments;
constructor() {
super();
this.calledWith_ = arguments;
}
open(){}
}
nodeutil.inherits(FakeSessionPool, events.EventEmitter);
FakeSessionPool.prototype.open = util.noop;

function FakeTable() {
this.calledWith_ = arguments;
class FakeTable {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

function FakeTransactionRequest() {
this.calledWith_ = arguments;
class FakeTransactionRequest {
calledWith_: IArguments;
constructor() {
this.calledWith_ = arguments;
}
}

const fakeCodec = {
const fakeCodec: any = {
encode: util.noop,
Int: function() {},
Float: function() {},
Expand Down Expand Up @@ -485,7 +504,7 @@ describe('Database', () => {
});

it('should decorate the end() method', done => {
const transaction = {};
const transaction: any = {};
const end = function(callback) {
assert.strictEqual(this, transaction);
callback(); // done fn
Expand Down Expand Up @@ -610,7 +629,7 @@ describe('Database', () => {

describe('autoCreate', () => {
const error = new Error('Error.');
error.code = 5;
(error as ApiError).code = 5;

const OPTIONS = {
autoCreate: true,
Expand Down Expand Up @@ -690,7 +709,7 @@ describe('Database', () => {

it('should not auto create without error code 5', done => {
const error = new Error('Error.');
error.code = 'NOT-5';
(error as any).code = 'NOT-5';

const options = {
autoCreate: true,
Expand All @@ -711,7 +730,7 @@ describe('Database', () => {
});

it('should not auto create unless requested', done => {
const error = new Error('Error.');
const error = new ApiError('Error.');
error.code = 5;

database.getMetadata = function(callback) {
Expand Down Expand Up @@ -851,7 +870,7 @@ describe('Database', () => {
formattedName_: 'formatted-name',
};

const POOL = {};
const POOL: any = {};

beforeEach(() => {
CONFIG = {
Expand Down Expand Up @@ -942,7 +961,7 @@ describe('Database', () => {
formattedName_: 'formatted-name',
};

const POOL = {};
const POOL: any = {};

beforeEach(() => {
REQUEST_STREAM = through();
Expand Down Expand Up @@ -1248,7 +1267,7 @@ describe('Database', () => {
});

it('should pass json, jsonOptions to PartialResultStream', () => {
const query = extend({}, QUERY);
const query: any = extend({}, QUERY);
query.json = {};
query.jsonOptions = {};

Expand All @@ -1266,7 +1285,7 @@ describe('Database', () => {
done();
};

const query = extend({}, QUERY);
const query: any = extend({}, QUERY);
query.json = {};
query.jsonOptions = {};

Expand All @@ -1292,7 +1311,7 @@ describe('Database', () => {
const OPTIONS = {a: 'a'};
const FORMATTED_OPTIONS = {b: 'b'};

FakeTransactionRequest.formatTimestampOptions_ = function(options) {
(FakeTransactionRequest as any).formatTimestampOptions_ = function(options) {
assert.strictEqual(options, OPTIONS);
return FORMATTED_OPTIONS;
};
Expand Down Expand Up @@ -1700,7 +1719,6 @@ describe('Database', () => {
setImmediate(done);
return Promise.resolve();
};

database.getTransaction(OPTIONS, assert.ifError);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

'use strict';

const assert = require('assert');
import * as assert from 'assert';
import { ApiError } from '@google-cloud/common';

const spannerModule = require('../src');

const FAKE_STATUS_CODE = 1;
const error = new Error();
error.code = FAKE_STATUS_CODE;
(error as ApiError).code = FAKE_STATUS_CODE;

describe('DatabaseAdminClient', () => {
describe('listDatabases', () => {
Expand Down Expand Up @@ -689,7 +690,7 @@ describe('DatabaseAdminClient', () => {
});
});

function mockSimpleGrpcMethod(expectedRequest, response, error) {
function mockSimpleGrpcMethod(expectedRequest, response?, error?) {
return function(actualRequest, options, callback) {
assert.deepStrictEqual(actualRequest, expectedRequest);
if (error) {
Expand All @@ -702,7 +703,7 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
};
}

function mockLongRunningGrpcMethod(expectedRequest, response, error) {
function mockLongRunningGrpcMethod(expectedRequest, response, error?) {
return request => {
assert.deepStrictEqual(request, expectedRequest);
const mockOperation = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

'use strict';

const assert = require('assert');
import * as assert from 'assert';
import { ApiError } from '@google-cloud/common';

const spannerModule = require('../src');

const FAKE_STATUS_CODE = 1;
const error = new Error();
error.code = FAKE_STATUS_CODE;
(error as ApiError).code = FAKE_STATUS_CODE;

describe('InstanceAdminClient', () => {
describe('listInstanceConfigs', () => {
Expand Down Expand Up @@ -736,7 +737,7 @@ describe('InstanceAdminClient', () => {
});
});

function mockSimpleGrpcMethod(expectedRequest, response, error) {
function mockSimpleGrpcMethod(expectedRequest, response?, error?) {
return function(actualRequest, options, callback) {
assert.deepStrictEqual(actualRequest, expectedRequest);
if (error) {
Expand All @@ -749,7 +750,7 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
};
}

function mockLongRunningGrpcMethod(expectedRequest, response, error) {
function mockLongRunningGrpcMethod(expectedRequest, response, error?) {
return request => {
assert.deepStrictEqual(request, expectedRequest);
const mockOperation = {
Expand Down
9 changes: 5 additions & 4 deletions test/gapic-v1.js → test/gapic-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

'use strict';

const assert = require('assert');
import * as assert from 'assert';
import { ApiError } from '@google-cloud/common';

const spannerModule = require('../src');

const FAKE_STATUS_CODE = 1;
const error = new Error();
error.code = FAKE_STATUS_CODE;
(error as ApiError).code = FAKE_STATUS_CODE;

describe('InstanceAdminClient', () => {
describe('listInstanceConfigs', () => {
Expand Down Expand Up @@ -736,7 +737,7 @@ describe('InstanceAdminClient', () => {
});
});

function mockSimpleGrpcMethod(expectedRequest, response, error) {
function mockSimpleGrpcMethod(expectedRequest, response?, error?) {
return function(actualRequest, options, callback) {
assert.deepStrictEqual(actualRequest, expectedRequest);
if (error) {
Expand All @@ -749,7 +750,7 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
};
}

function mockLongRunningGrpcMethod(expectedRequest, response, error) {
function mockLongRunningGrpcMethod(expectedRequest, response?, error?) {
return request => {
assert.deepStrictEqual(request, expectedRequest);
const mockOperation = {
Expand Down
Loading