Skip to content

Commit

Permalink
fix: unit test template has wrong paging response type (#382)
Browse files Browse the repository at this point in the history
* fix paging reesponse type for primitive

* fix pagingresponsetype

* update baseline

* use buffer.from()

* feedback
  • Loading branch information
xiaozhenliu-gg5 authored Apr 2, 2020
1 parent f531b22 commit cfd29ff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
32 changes: 8 additions & 24 deletions baselines/logging/test/gapic_logging_service_v2_v2.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,7 @@ describe('v2.LoggingServiceV2Client', () => {
},
},
};
const expectedResponse = [
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
];
const expectedResponse = [new String(), new String(), new String()];
client.innerApiCalls.listLogs = stubSimpleCall(expectedResponse);
const [response] = await client.listLogs(request);
assert.deepStrictEqual(response, expectedResponse);
Expand All @@ -694,11 +690,7 @@ describe('v2.LoggingServiceV2Client', () => {
},
},
};
const expectedResponse = [
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
];
const expectedResponse = [new String(), new String(), new String()];
client.innerApiCalls.listLogs = stubSimpleCallWithCallback(expectedResponse);
const promise = new Promise((resolve, reject) => {
client.listLogs(
Expand Down Expand Up @@ -749,16 +741,12 @@ describe('v2.LoggingServiceV2Client', () => {
const request = generateSampleMessage(new protos.google.logging.v2.ListLogsRequest());
request.parent = '';
const expectedHeaderRequestParams = "parent=";
const expectedResponse = [
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
];
const expectedResponse = [new String(), new String(), new String()];
client.descriptors.page.listLogs.createStream = stubPageStreamingCall(expectedResponse);
const stream = client.listLogsStream(request);
const promise = new Promise((resolve, reject) => {
const responses: protosTYPE_STRING[] = [];
stream.on('data', (response: protosTYPE_STRING) => {
const responses: string[] = [];
stream.on('data', (response: string) => {
responses.push(response);
});
stream.on('end', () => {
Expand Down Expand Up @@ -792,8 +780,8 @@ describe('v2.LoggingServiceV2Client', () => {
client.descriptors.page.listLogs.createStream = stubPageStreamingCall(undefined, expectedError);
const stream = client.listLogsStream(request);
const promise = new Promise((resolve, reject) => {
const responses: protosTYPE_STRING[] = [];
stream.on('data', (response: protosTYPE_STRING) => {
const responses: string[] = [];
stream.on('data', (response: string) => {
responses.push(response);
});
stream.on('end', () => {
Expand Down Expand Up @@ -821,11 +809,7 @@ describe('v2.LoggingServiceV2Client', () => {
client.initialize();
const request = generateSampleMessage(new protos.google.logging.v2.ListLogsRequest());
request.parent = '';
const expectedHeaderRequestParams = "parent=";const expectedResponse = [
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
generateSampleMessage(new protosTYPE_STRING()),
];
const expectedHeaderRequestParams = "parent=";const expectedResponse = [new String(), new String(), new String()];
client.descriptors.page.listLogs.asyncIterate = stubAsyncIterationCall(expectedResponse);
const responses: string[] = [];
const iterable = client.listLogsAsync(request);
Expand Down
40 changes: 31 additions & 9 deletions templates/typescript_gapic/_util.njk
Original file line number Diff line number Diff line change
Expand Up @@ -263,28 +263,41 @@ request.{{ oneComment.paramName.toCamelCase() }}
{%- endmacro -%}

{%- macro initPagingResponse(method) -%}
{%- if method.pagingResponseType.includes('TYPE_STRING') -%}
const expectedResponse = [new String(), new String(), new String()];
{%- elif method.pagingResponseType.includes('TYPE_BOOL') -%}
const expectedResponse = [new Boolean(), new Boolean(), new Boolean()];
{%- elif method.pagingResponseType.includes('TYPE_BYTES') -%}
const expectedResponse = [Buffer.from(''), Buffer.from(''), Buffer.from('')];
{%- elif method.pagingResponseType.includes('TYPE_') -%}
const expectedResponse = [new Number(), new Number(), new Number()];
{%- else -%}
const expectedResponse = [
generateSampleMessage(new protos{{ method.pagingResponseType }}()),
generateSampleMessage(new protos{{ method.pagingResponseType }}()),
generateSampleMessage(new protos{{ method.pagingResponseType }}()),
];
{%- endif %}
{%- endmacro -%}

{%- macro typescriptType(protobufType) -%}
{#- protobufType can be an array (TYPE_STRING[]), hence .startsWith -#}
{%- if protobufType.startsWith('TYPE_BYTES') -%}
{%- set type = protobufType.replace('TYPE_BYTES', 'Buffer') -%}
{%- elif protobufType.startsWith('TYPE_BOOL') -%}
{%- set type = protobufType.replace('TYPE_BOOL', 'boolean') -%}
{%- elif protobufType.startsWith('TYPE_STRING') -%}
{%- set type = protobufType.replace('TYPE_STRING', 'string') -%}
{%- elif protobufType.startsWith('TYPE_') -%}
{%- if protobufType.includes('TYPE_BYTES') -%}
{%- set type = 'Buffer' -%}
{%- elif protobufType.includes('TYPE_BOOL') -%}
{%- set type = 'boolean' -%}
{%- elif protobufType.includes('TYPE_STRING') -%}
{%- set type = 'string' -%}
{%- elif protobufType.includes('TYPE_') -%}
{#- any other type is essentially a number: int32, uint32, etc. -#}
{%- set type = protobufType.replace(r/TYPE_\w+/, 'number') -%}
{%- set type = 'number' -%}
{%- else -%}
{%- set type = '' -%}
{%- endif -%}
{%- if protobufType.includes('[]') -%}
{{ type }}[]
{%- else -%}
{{ type }}
{%- endif -%}
{%- endmacro -%}

{%- macro toInterface(type) -%}
Expand All @@ -300,3 +313,12 @@ protos{{ type.substring(0, index + 1) + 'I' + type.substring(index + 1) }}
{%- macro convertParamType(paramType) -%}
{{- typescriptType(paramType) -}}
{%- endmacro -%}

{%- macro promiseResponsePaging(method) -%}
{%- set tsType = typescriptType(method.pagingResponseType) -%}
{%- if tsType.length > 0 -%}
{{ tsType }}
{%- else -%}
protos{{ method.pagingResponseType }}
{%- endif -%}
{%- endmacro -%}
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
client.descriptors.page.{{ method.name.toCamelCase() }}.createStream = stubPageStreamingCall(expectedResponse);
const stream = client.{{ method.name.toCamelCase() }}Stream(request);
const promise = new Promise((resolve, reject) => {
const responses: protos{{ method.pagingResponseType }}[] = [];
stream.on('data', (response: protos{{ method.pagingResponseType }}) => {
const responses: {{ util.promiseResponsePaging(method) }}[] = [];
stream.on('data', (response: {{ util.promiseResponsePaging(method) }}) => {
responses.push(response);
});
stream.on('end', () => {
Expand Down Expand Up @@ -655,8 +655,8 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
client.descriptors.page.{{ method.name.toCamelCase() }}.createStream = stubPageStreamingCall(undefined, expectedError);
const stream = client.{{ method.name.toCamelCase() }}Stream(request);
const promise = new Promise((resolve, reject) => {
const responses: protos{{ method.pagingResponseType }}[] = [];
stream.on('data', (response: protos{{ method.pagingResponseType }}) => {
const responses: {{ util.promiseResponsePaging(method) }}[] = [];
stream.on('data', (response: {{ util.promiseResponsePaging(method) }}) => {
responses.push(response);
});
stream.on('end', () => {
Expand Down
4 changes: 3 additions & 1 deletion typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ function pagingResponseType(
) {
return field.typeName; //.google.showcase.v1beta1.EchoResponse
}
return plugin.google.protobuf.FieldDescriptorProto.Type[field.type];
const type = plugin.google.protobuf.FieldDescriptorProto.Type[field.type];
// .google.protobuf.FieldDescriptorProto.Type.TYPE_STRING
return '.google.protobuf.FieldDescriptorProto.Type.' + type;
}

export function getSingleHeaderParam(
Expand Down

0 comments on commit cfd29ff

Please sign in to comment.