Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Commit

Permalink
fix: createDataset now correctly returns operation
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi-automation authored and bcoe committed Oct 8, 2019
1 parent 41e6943 commit 770b572
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 22 deletions.
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ accept your pull requests.
1. Ensure that your code adheres to the existing style in the code to which
you are contributing.
1. Ensure that your code has an appropriate set of tests which all pass.
1. Title your pull request following [Conventional Commits](https://www.conventionalcommits.org/) styling.
1. Submit a pull request.

## Running the tests
Expand All @@ -46,8 +47,16 @@ accept your pull requests.

1. Run the tests:

# Run all tests.
npm test

# Run all unit tests.
npm run test-only

# Run all system tests.
gcloud auth application-default login
npm run system-test

1. Lint (and maybe fix) any changes:

npm run fix
Expand Down
73 changes: 69 additions & 4 deletions src/v1/auto_ml_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ class AutoMlClient {
grpc: gaxGrpc.grpc,
}).operationsClient(opts);

const createDatasetResponse = protoFilesRoot.lookup(
'google.cloud.automl.v1.Dataset'
);
const createDatasetMetadata = protoFilesRoot.lookup(
'google.cloud.automl.v1.OperationMetadata'
);
const deleteDatasetResponse = protoFilesRoot.lookup(
'google.protobuf.Empty'
);
Expand All @@ -211,6 +217,11 @@ class AutoMlClient {
);

this._descriptors.longrunning = {
createDataset: new gaxModule.LongrunningDescriptor(
this.operationsClient,
createDatasetResponse.decode.bind(createDatasetResponse),
createDatasetMetadata.decode.bind(createDatasetMetadata)
),
deleteDataset: new gaxModule.LongrunningDescriptor(
this.operationsClient,
deleteDatasetResponse.decode.bind(deleteDatasetResponse),
Expand Down Expand Up @@ -356,9 +367,9 @@ class AutoMlClient {
* @param {function(?Error, ?Object)} [callback]
* The function which will be called with the result of the API call.
*
* The second parameter to the callback is an object representing [Operation]{@link google.longrunning.Operation}.
* The second parameter to the callback is a [gax.Operation]{@link https://googleapis.github.io/gax-nodejs/classes/Operation.html} object.
* @returns {Promise} - The promise which resolves to an array.
* The first element of the array is an object representing [Operation]{@link google.longrunning.Operation}.
* The first element of the array is a [gax.Operation]{@link https://googleapis.github.io/gax-nodejs/classes/Operation.html} object.
* The promise has a method named "cancel" which cancels the ongoing API call.
*
* @example
Expand All @@ -375,14 +386,68 @@ class AutoMlClient {
* parent: formattedParent,
* dataset: dataset,
* };
*
* // Handle the operation using the promise pattern.
* client.createDataset(request)
* .then(responses => {
* const response = responses[0];
* // doThingsWith(response)
* const [operation, initialApiResponse] = responses;
*
* // Operation#promise starts polling for the completion of the LRO.
* return operation.promise();
* })
* .then(responses => {
* const result = responses[0];
* const metadata = responses[1];
* const finalApiResponse = responses[2];
* })
* .catch(err => {
* console.error(err);
* });
*
* const formattedParent = client.locationPath('[PROJECT]', '[LOCATION]');
* const dataset = {};
* const request = {
* parent: formattedParent,
* dataset: dataset,
* };
*
* // Handle the operation using the event emitter pattern.
* client.createDataset(request)
* .then(responses => {
* const [operation, initialApiResponse] = responses;
*
* // Adding a listener for the "complete" event starts polling for the
* // completion of the operation.
* operation.on('complete', (result, metadata, finalApiResponse) => {
* // doSomethingWith(result);
* });
*
* // Adding a listener for the "progress" event causes the callback to be
* // called on any change in metadata when the operation is polled.
* operation.on('progress', (metadata, apiResponse) => {
* // doSomethingWith(metadata)
* });
*
* // Adding a listener for the "error" event handles any errors found during polling.
* operation.on('error', err => {
* // throw(err);
* });
* })
* .catch(err => {
* console.error(err);
* });
*
* const formattedParent = client.locationPath('[PROJECT]', '[LOCATION]');
* const dataset = {};
* const request = {
* parent: formattedParent,
* dataset: dataset,
* };
*
* // Handle the operation using the await pattern.
* const [operation] = await client.createDataset(request);
*
* const [response] = await operation.promise();
*/
createDataset(request, options, callback) {
if (options instanceof Function && callback === undefined) {
Expand Down
6 changes: 3 additions & 3 deletions synth.metadata
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"updateTime": "2019-10-05T11:07:46.985586Z",
"updateTime": "2019-10-08T11:08:39.162333Z",
"sources": [
{
"generator": {
Expand All @@ -12,8 +12,8 @@
"git": {
"name": "googleapis",
"remote": "https://github.com/googleapis/googleapis.git",
"sha": "ceb8e2fb12f048cc94caae532ef0b4cf026a78f3",
"internalRef": "272971705"
"sha": "122bdbf877ad87439f8dd9d1474a8e5dde188087",
"internalRef": "273381131"
}
},
{
Expand Down
67 changes: 52 additions & 15 deletions test/gapic-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('AutoMlClient', () => {
assert(client);
});

describe('createDataset', () => {
describe('createDataset', function() {
it('invokes createDataset without error', done => {
const client = new automlModule.v1.AutoMlClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
Expand All @@ -66,23 +66,37 @@ describe('AutoMlClient', () => {

// Mock response
const name = 'name3373707';
const done_ = true;
const displayName = 'displayName1615086568';
const description = 'description-1724546052';
const exampleCount = 1517063674;
const etag = 'etag3123477';
const expectedResponse = {
name: name,
done: done_,
displayName: displayName,
description: description,
exampleCount: exampleCount,
etag: etag,
};

// Mock Grpc layer
client._innerApiCalls.createDataset = mockSimpleGrpcMethod(
client._innerApiCalls.createDataset = mockLongRunningGrpcMethod(
request,
expectedResponse
);

client.createDataset(request, (err, response) => {
assert.ifError(err);
assert.deepStrictEqual(response, expectedResponse);
done();
});
client
.createDataset(request)
.then(responses => {
const operation = responses[0];
return operation.promise();
})
.then(responses => {
assert.deepStrictEqual(responses[0], expectedResponse);
done();
})
.catch(err => {
done(err);
});
});

it('invokes createDataset with error', done => {
Expand All @@ -100,18 +114,41 @@ describe('AutoMlClient', () => {
};

// Mock Grpc layer
client._innerApiCalls.createDataset = mockSimpleGrpcMethod(
client._innerApiCalls.createDataset = mockLongRunningGrpcMethod(
request,
null,
error
);

client.createDataset(request, (err, response) => {
assert(err instanceof Error);
assert.strictEqual(err.code, FAKE_STATUS_CODE);
assert(typeof response === 'undefined');
done();
client
.createDataset(request)
.then(responses => {
const operation = responses[0];
return operation.promise();
})
.then(() => {
assert.fail();
})
.catch(err => {
assert(err instanceof Error);
assert.strictEqual(err.code, FAKE_STATUS_CODE);
done();
});
});

it('has longrunning decoder functions', () => {
const client = new automlModule.v1.AutoMlClient({
credentials: {client_email: 'bogus', private_key: 'bogus'},
projectId: 'bogus',
});
assert(
client._descriptors.longrunning.createDataset.responseDecoder instanceof
Function
);
assert(
client._descriptors.longrunning.createDataset.metadataDecoder instanceof
Function
);
});
});

Expand Down

0 comments on commit 770b572

Please sign in to comment.