Skip to content

Commit 4b6c48f

Browse files
jmdobrypull[bot]
authored andcommitted
New quickstarts. (#226)
* New quickstarts. * Address comments.
1 parent 2a24f45 commit 4b6c48f

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

vision/samples/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
"redis": "^2.6.0-2"
1616
},
1717
"devDependencies": {
18-
"mocha": "^2.5.3"
18+
"mocha": "^3.1.0"
1919
},
2020
"optionalDependencies": {
2121
"canvas": "^1.3.15"
22+
},
23+
"engines": {
24+
"node": ">=4.3.2"
2225
}
2326
}

vision/samples/quickstart.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START vision_quickstart]
19+
// Imports the Google Cloud client library
20+
const Vision = require('@google-cloud/vision');
21+
22+
// Your Google Cloud Platform project ID
23+
const projectId = 'YOUR_PROJECT_ID';
24+
25+
// Instantiates a client
26+
const visionClient = Vision({
27+
projectId: projectId
28+
});
29+
30+
// The name of the image file to annotate
31+
const fileName = './resources/wakeupcat.jpg';
32+
33+
// Performs label detection on the image file
34+
visionClient.detectLabels(fileName, (err, labels) => {
35+
if (err) {
36+
console.error(err);
37+
return;
38+
}
39+
40+
console.log('Labels:');
41+
labels.forEach((label) => console.log(label));
42+
});
43+
// [END vision_quickstart]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const proxyquire = require(`proxyquire`).noPreserveCache();
19+
const vision = proxyquire(`@google-cloud/vision`, {})();
20+
const path = require(`path`);
21+
22+
describe(`vision:quickstart`, () => {
23+
let visionMock, VisionMock;
24+
25+
it(`should detect labels`, (done) => {
26+
const filePath = path.join(__dirname, `../resources/wakeupcat.jpg`);
27+
const expectedFileName = `./resources/wakeupcat.jpg`;
28+
29+
visionMock = {
30+
detectLabels: (_fileName, _callback) => {
31+
assert.equal(_fileName, expectedFileName);
32+
assert.equal(typeof _callback, 'function');
33+
34+
vision.detectLabels(filePath, (err, labels, apiResponse) => {
35+
_callback(err, labels, apiResponse);
36+
assert.ifError(err);
37+
assert.equal(Array.isArray(labels), true);
38+
assert.notEqual(apiResponse, undefined);
39+
assert.equal(console.log.called, true);
40+
assert.deepEqual(console.log.firstCall.args, [`Labels:`]);
41+
labels.forEach((label, i) => {
42+
assert.deepEqual(console.log.getCall(i + 1).args, [label]);
43+
});
44+
done();
45+
});
46+
}
47+
};
48+
VisionMock = sinon.stub().returns(visionMock);
49+
50+
proxyquire(`../quickstart`, {
51+
'@google-cloud/vision': VisionMock
52+
});
53+
});
54+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2016, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const proxyquire = require(`proxyquire`).noCallThru();
19+
20+
describe(`vision:quickstart`, () => {
21+
let visionMock, VisionMock;
22+
const error = new Error(`error`);
23+
const fileName = `./resources/wakeupcat.jpg`;
24+
25+
before(() => {
26+
visionMock = {
27+
detectLabels: sinon.stub().yields(error)
28+
};
29+
VisionMock = sinon.stub().returns(visionMock);
30+
});
31+
32+
it(`should handle error`, () => {
33+
proxyquire(`../quickstart`, {
34+
'@google-cloud/vision': VisionMock
35+
});
36+
37+
assert.equal(VisionMock.calledOnce, true);
38+
assert.deepEqual(VisionMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]);
39+
assert.equal(visionMock.detectLabels.calledOnce, true);
40+
assert.deepEqual(visionMock.detectLabels.firstCall.args.slice(0, -1), [fileName]);
41+
assert.equal(console.error.calledOnce, true);
42+
assert.deepEqual(console.error.firstCall.args, [error]);
43+
});
44+
});

0 commit comments

Comments
 (0)