Skip to content

Commit

Permalink
chore: convert unit test to ts (#50)
Browse files Browse the repository at this point in the history
* merge

* convert unit test to ts

* paging

* feedback

* format

* replace type: any

* fix type

* update baseline

* clean

* remove copy test folder to build/
  • Loading branch information
xiaozhenliu-gg5 authored Oct 23, 2019
1 parent fd2abbb commit 5aaa5d1
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 163 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"homepage": "https://github.com/googleapis/gapic-generator-typescript#readme",
"devDependencies": {
"@types/command-line-args": "^5.0.0",
"@types/get-stdin": "^5.0.1",
"@types/fs-extra": "^8.0.1",
"@types/mocha": "^5.2.5",
"@types/get-stdin": "^5.0.1",
"@types/mocha": "^5.2.7",
"@types/node": "^11.13.22",
"@types/nunjucks": "^3.1.0",
"@types/rimraf": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion templates/typescript_gapic/package.json.njk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"lint": "gts check",
"clean": "gts clean",
"compile": "tsc -p . && cp -r protos build/ && cp -r test build/",
"compile": "tsc -p . && cp -r protos build/",
"compile-protos": "compileProtos src",
"fix": "gts fix",
"prepare": "npm run compile-protos && npm run compile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ const {PassThrough} = require('stream');
{% endif %}

const FAKE_STATUS_CODE = 1;
const error = new Error();
error.code = FAKE_STATUS_CODE;
class FakeError{
code: number;
constructor(n: number){
this.code = n;
}
}
const error = new FakeError(FAKE_STATUS_CODE);
export interface Callback {
(err: FakeError|null, response?: {} | null): {};
}

function mockSimpleGrpcMethod(expectedRequest, response, error) {
return function(actualRequest, options, callback) {
export class Operation{
constructor(){};
promise() {() => {}};
}

function mockSimpleGrpcMethod(expectedRequest: {}, response: {} | null, error: FakeError | null) {
return function(actualRequest: {}, options: {}, callback: Callback) {
assert.deepStrictEqual(actualRequest, expectedRequest);
if (error) {
callback(error);
Expand All @@ -24,12 +37,12 @@ function mockSimpleGrpcMethod(expectedRequest, response, error) {
};
}
{%- if (service.serverStreaming.length > 0) %}
function mockServerStreamingGrpcMethod(expectedRequest, response, error) {
return actualRequest => {
function mockServerStreamingGrpcMethod(expectedRequest: {}, response: {} | null, error: FakeError | null) {
return (actualRequest: {}) => {
assert.deepStrictEqual(actualRequest, expectedRequest);
const mockStream = new PassThrough({
objectMode: true,
transform: (chunk, enc, callback) => {
transform: (chunk: {}, enc: {}, callback: Callback) => {
if (error) {
callback(error);
}
Expand All @@ -43,11 +56,11 @@ function mockServerStreamingGrpcMethod(expectedRequest, response, error) {
}
{%- endif %}
{%- if (service.bidiStreaming.length > 0) %}
function mockBidiStreamingGrpcMethod(expectedRequest, response, error) {
function mockBidiStreamingGrpcMethod(expectedRequest: {}, response: {} | null, error: FakeError | null) {
return () => {
const mockStream = new PassThrough({
objectMode: true,
transform: (chunk, enc, callback) => {
transform: (chunk: {}, enc: {}, callback: Callback) => {
assert.deepStrictEqual(chunk, expectedRequest);
if (error) {
callback(error);
Expand All @@ -62,8 +75,8 @@ function mockBidiStreamingGrpcMethod(expectedRequest, response, error) {
}
{%- endif %}
{%- if (service.longRunning.length > 0) %}
function mockLongRunningGrpcMethod(expectedRequest, response, error) {
return request => {
function mockLongRunningGrpcMethod(expectedRequest: {}, response: {} | null, error?: {} | null) {
return (request: {}) => {
assert.deepStrictEqual(request, expectedRequest);
const mockOperation = {
promise: function() {
Expand Down Expand Up @@ -119,9 +132,10 @@ describe('{{ service.name }}Client', () => {
// Mock gRPC layer
client._innerApiCalls.{{ method.name.toCamelCase() }} = mockSimpleGrpcMethod(
request,
expectedResponse
expectedResponse,
null
);
client.{{ method.name.toCamelCase() }}(request, (err, response) => {
client.{{ method.name.toCamelCase() }}(request, (err: {}, response: {}) => {
assert.ifError(err);
assert.deepStrictEqual(response, expectedResponse);
done();
Expand All @@ -143,8 +157,8 @@ describe('{{ service.name }}Client', () => {
null,
error
);
client.{{ method.name.toCamelCase() }}(request, (err, response) => {
assert(err instanceof Error);
client.{{ method.name.toCamelCase() }}(request, (err: FakeError, response: {}) => {
assert(err instanceof FakeError);
assert.strictEqual(err.code, FAKE_STATUS_CODE);
assert(typeof response === 'undefined');
done();
Expand All @@ -168,13 +182,13 @@ describe('{{ service.name }}Client', () => {
request,
expectedResponse
);
client.{{ method.name.toCamelCase() }}(request).then(responses => {
client.{{ method.name.toCamelCase() }}(request).then((responses: [Operation]) => {
const operation = responses[0];
return operation.promise();
}).then(responses => {
return operation? operation.promise() : {};
}).then((responses: [Operation]) => {
assert.deepStrictEqual(responses[0], expectedResponse);
done();
}).catch(err => {
}).catch((err: {}) => {
done(err);
});
});
Expand All @@ -194,13 +208,13 @@ describe('{{ service.name }}Client', () => {
null,
error
);
client.{{ method.name.toCamelCase() }}(request).then(responses => {
client.{{ method.name.toCamelCase() }}(request).then((responses: [Operation]) => {
const operation = responses[0];
return operation.promise();
return operation? operation.promise() : {};
}).then(() => {
assert.fail();
}).catch(err => {
assert(err instanceof Error);
}).catch((err: FakeError) => {
assert(err instanceof FakeError);
assert.strictEqual(err.code, FAKE_STATUS_CODE);
done();
});
Expand All @@ -219,13 +233,13 @@ describe('{{ service.name }}Client', () => {
// Mock response
const expectedResponse = {};
// Mock gRPC layer
client._innerApiCalls.{{ method.name.toCamelCase() }} = mockServerStreamingGrpcMethod(request, expectedResponse);
client._innerApiCalls.{{ method.name.toCamelCase() }} = mockServerStreamingGrpcMethod(request, expectedResponse, null);
const stream = client.{{ method.name.toCamelCase() }}(request);
stream.on('data', response =>{
stream.on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
});
stream.on('error', err => {
stream.on('error', (err: FakeError) => {
done(err);
});
stream.write();
Expand All @@ -245,8 +259,8 @@ describe('{{ service.name }}Client', () => {
stream.on('data', () =>{
assert.fail();
});
stream.on('error', err => {
assert(err instanceof Error);
stream.on('error', (err: FakeError) => {
assert(err instanceof FakeError);
assert.strictEqual(err.code, FAKE_STATUS_CODE);
done();
});
Expand All @@ -266,11 +280,11 @@ describe('{{ service.name }}Client', () => {
// Mock response
const expectedResponse = {};
// Mock gRPC layer
client._innerApiCalls.{{ method.name.toCamelCase() }} = mockBidiStreamingGrpcMethod(request, expectedResponse);
const stream = client.{{ method.name.toCamelCase() }}().on('data', response =>{
client._innerApiCalls.{{ method.name.toCamelCase() }} = mockBidiStreamingGrpcMethod(request, expectedResponse, null);
const stream = client.{{ method.name.toCamelCase() }}().on('data', (response: {}) =>{
assert.deepStrictEqual(response, expectedResponse);
done();
}).on('error', err => {
}).on('error', (err: FakeError) => {
done(err);
});
stream.write(request);
Expand All @@ -288,8 +302,8 @@ describe('{{ service.name }}Client', () => {
client._innerApiCalls.{{ method.name.toCamelCase() }} = mockBidiStreamingGrpcMethod(request, null, error);
const stream = client.{{ method.name.toCamelCase() }}().on('data', () =>{
assert.fail();
}).on('error', err => {
assert(err instanceof Error);
}).on('error', (err: FakeError) => {
assert(err instanceof FakeError);
assert.strictEqual(err.code, FAKE_STATUS_CODE);
done();
});
Expand All @@ -309,11 +323,11 @@ describe('{{ service.name }}Client', () => {
// Mock response
const expectedResponse = {};
// Mock Grpc layer
client._innerApiCalls.{{ method.name.toCamelCase() }} = (actualRequest, options, callback) => {
client._innerApiCalls.{{ method.name.toCamelCase() }} = (actualRequest: {}, options: {}, callback: Callback) => {
assert.deepStrictEqual(actualRequest, request);
callback(null, expectedResponse);
};
client.{{ method.name.toCamelCase() }}(request, (err, response) => {
client.{{ method.name.toCamelCase() }}(request, (err: FakeError, response: {}) => {
assert.ifError(err);
assert.deepStrictEqual(response, expectedResponse);
done();
Expand Down
2 changes: 1 addition & 1 deletion typescript/test/testdata/keymanager/package.json.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"scripts": {
"lint": "gts check",
"clean": "gts clean",
"compile": "tsc -p . && cp -r protos build/ && cp -r test build/",
"compile": "tsc -p . && cp -r protos build/",
"compile-protos": "compileProtos src",
"fix": "gts fix",
"prepare": "npm run compile-protos && npm run compile",
Expand Down
Loading

0 comments on commit 5aaa5d1

Please sign in to comment.