Skip to content

Commit

Permalink
Cleanup App Engine samples and re-work tests. (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored and Ace Nassri committed Apr 24, 2017
1 parent 53c5293 commit 1354bc9
Show file tree
Hide file tree
Showing 7 changed files with 2,986 additions and 166 deletions.
2 changes: 1 addition & 1 deletion vision/samples/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ function detectFulltextGCS (bucketName, fileName) {
// [END vision_fulltext_detection_gcs]
}

require(`yargs`)
require(`yargs`) // eslint-disable-line
.demand(1)
.command(
`faces <fileName>`,
Expand Down
35 changes: 26 additions & 9 deletions vision/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,40 @@
"name": "nodejs-docs-samples-vision",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"cloud": {
"requiresKeyFile": true,
"requiresProjectId": true
},
"engines": {
"node": ">=4.3.2"
},
"scripts": {
"test": "cd ..; npm run st -- --verbose vision/system-test/*.test.js"
"lint": "samples lint",
"pretest": "npm run lint",
"system-test": "ava -T 1m --verbose system-test/*.test.js",
"test": "npm run system-test"
},
"dependencies": {
"@google-cloud/storage": "1.0.0",
"@google-cloud/vision": "0.11.0",
"@google-cloud/storage": "1.1.0",
"@google-cloud/vision": "0.11.2",
"async": "2.3.0",
"natural": "0.5.0",
"natural": "0.5.1",
"redis": "2.7.1",
"yargs": "7.0.2"
"yargs": "7.1.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.3.1",
"ava": "0.19.1",
"proxyquire": "1.7.11",
"sinon": "2.1.0"
},
"optionalDependencies": {
"canvas": "1.6.5"
},
"engines": {
"node": ">=4.3.2"
}
}
57 changes: 29 additions & 28 deletions vision/samples/system-test/detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

'use strict';

require(`../../system-test/_setup`);

const path = require(`path`);
const storage = require(`@google-cloud/storage`)();
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);
const uuid = require(`uuid`);

const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
Expand All @@ -38,6 +38,7 @@ const files = [
};
});

test.before(tools.checkCredentials);
test.before(async () => {
const [bucket] = await storage.createBucket(bucketName);
await Promise.all(files.map((file) => bucket.upload(file.localPath)));
Expand All @@ -51,123 +52,123 @@ test.after.always(async () => {
});

test(`should detect faces in a local file`, async (t) => {
const output = await runAsync(`${cmd} faces ${files[0].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} faces ${files[0].localPath}`, cwd);
t.true(output.includes(`Faces:`));
t.true(output.includes(`Face #1:`));
});

test(`should detect faces in a remote file`, async (t) => {
const output = await runAsync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`, cwd);
const output = await tools.runAsync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`, cwd);
t.true(output.includes(`Faces:`));
t.true(output.includes(`Face #1:`));
});

test(`should detect labels in a local file`, async (t) => {
const output = await runAsync(`${cmd} labels ${files[4].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} labels ${files[4].localPath}`, cwd);
t.true(output.includes(`Labels:`));
t.true(output.includes(`cat`));
});

test(`should detect labels in a remote file`, async (t) => {
const output = await runAsync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`, cwd);
const output = await tools.runAsync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`, cwd);
t.true(output.includes(`Labels:`));
t.true(output.includes(`cat`));
});

test(`should detect landmarks in a local file`, async (t) => {
const output = await runAsync(`${cmd} landmarks ${files[1].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} landmarks ${files[1].localPath}`, cwd);
t.true(output.includes(`Landmarks:`));
t.true(output.includes(`Palace of Fine Arts`));
});

test(`should detect landmarks in a remote file`, async (t) => {
const output = await runAsync(`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`, cwd);
const output = await tools.runAsync(`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`, cwd);
t.true(output.includes(`Landmarks:`));
t.true(output.includes(`Palace of Fine Arts`));
});

test(`should detect text in a local file`, async (t) => {
const output = await runAsync(`${cmd} text ${files[3].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} text ${files[3].localPath}`, cwd);
t.true(output.includes(`Text:`));
t.true(output.includes(`System Software Update`));
});

test(`should detect text in a remote file`, async (t) => {
const output = await runAsync(`${cmd} text-gcs ${bucketName} ${files[3].name}`, cwd);
const output = await tools.runAsync(`${cmd} text-gcs ${bucketName} ${files[3].name}`, cwd);
t.true(output.includes(`Text:`));
t.true(output.includes(`System Software Update`));
});

test(`should detect logos in a local file`, async (t) => {
const output = await runAsync(`${cmd} logos ${files[2].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} logos ${files[2].localPath}`, cwd);
t.true(output.includes(`Logos:`));
t.true(output.includes(`Google`));
});

test(`should detect logos in a remote file`, async (t) => {
const output = await runAsync(`${cmd} logos-gcs ${bucketName} ${files[2].name}`, cwd);
const output = await tools.runAsync(`${cmd} logos-gcs ${bucketName} ${files[2].name}`, cwd);
t.true(output.includes(`Logos:`));
t.true(output.includes(`Google`));
});

test(`should detect properties in a local file`, async (t) => {
const output = await runAsync(`${cmd} properties ${files[1].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} properties ${files[1].localPath}`, cwd);
t.true(output.includes(`Colors:`));
t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`);
});

test(`should detect properties in a remote file`, async (t) => {
const output = await runAsync(`${cmd} properties-gcs ${bucketName} ${files[1].name}`, cwd);
const output = await tools.runAsync(`${cmd} properties-gcs ${bucketName} ${files[1].name}`, cwd);
t.true(output.includes(`Colors:`));
t.true(output.split(`\n`).length > 4, `Multiple colors were detected.`);
});

test(`should detect safe-search in a local file`, async (t) => {
const output = await runAsync(`${cmd} safe-search ${files[4].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} safe-search ${files[4].localPath}`, cwd);
t.true(output.includes(`Medical:`));
});

test(`should detect safe-search in a remote file`, async (t) => {
const output = await runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
const output = await tools.runAsync(`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`, cwd);
t.true(output.includes(`Medical:`));
});

test(`should detect crop hints in a local file`, async (t) => {
const output = await runAsync(`${cmd} crops ${files[2].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} crops ${files[2].localPath}`, cwd);
t.true(output.includes(`Crop Hint 0:`));
t.true(output.includes(`Bound 2: (280, 43)`));
});

test(`should detect crop hints in a remote file`, async (t) => {
const output = await runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd);
const output = await tools.runAsync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`, cwd);
t.true(output.includes(`Crop Hint 0:`));
t.true(output.includes(`Bound 2: (280, 43)`));
});

test(`should detect similar web images in a local file`, async (t) => {
const output = await runAsync(`${cmd} web ${files[5].localPath}`, cwd);
t.true(output.includes('Full matches found: 5'));
const output = await tools.runAsync(`${cmd} web ${files[5].localPath}`, cwd);
t.true(output.includes('Full matches found:'));
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
t.true(output.includes('Partial matches found: 5'));
t.true(output.includes('Web entities found: 5'));
t.true(output.includes('Partial matches found:'));
t.true(output.includes('Web entities found:'));
t.true(output.includes('Description: Google Cloud Platform'));
});

test(`should detect similar web images in a remote file`, async (t) => {
const output = await runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd);
t.true(output.includes('Full matches found: 5'));
const output = await tools.runAsync(`${cmd} web-gcs ${bucketName} ${files[5].name}`, cwd);
t.true(output.includes('Full matches found:'));
t.true(output.includes('URL: https://cloud.google.com/vision/docs/images/'));
t.true(output.includes('Partial matches found: 5'));
t.true(output.includes('Web entities found: 5'));
t.true(output.includes('Partial matches found:'));
t.true(output.includes('Web entities found:'));
t.true(output.includes('Description: Google'));
});

test(`should read a document from a local file`, async (t) => {
const output = await runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd);
const output = await tools.runAsync(`${cmd} fulltext ${files[2].localPath}`, cwd);
t.true(output.includes('Google Cloud Platform'));
});

test(`should read a document from a remote file`, async (t) => {
const output = await runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd);
const output = await tools.runAsync(`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`, cwd);
t.true(output.includes('Google Cloud Platform'));
});
11 changes: 7 additions & 4 deletions vision/samples/system-test/faceDetection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

'use strict';

require(`../../system-test/_setup`);

const fs = require(`fs`);
const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

class MockCanvas {
getContext () {
Expand All @@ -36,9 +36,11 @@ class MockCanvas {
if (event === 'end') {
setTimeout(cb, 1000);
} else if (event === `data`) {
/* eslint-disable */
cb(`test`);
cb(`foo`);
cb(`bar`);
/* eslint-enable */
}
}
};
Expand All @@ -51,8 +53,9 @@ const faceDetectionExample = require(`../faceDetection`);
const inputFile = path.join(__dirname, `../resources`, `face.png`);
const outputFile = path.join(__dirname, `../../vision`, `out.png`);

test.before(stubConsole);
test.after.always(restoreConsole);
test.before(tools.checkCredentials);
test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);

test.cb(`should detect faces`, (t) => {
faceDetectionExample.main(inputFile, outputFile, MockCanvas, (err, faces) => {
Expand Down
12 changes: 7 additions & 5 deletions vision/samples/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@

'use strict';

require(`../../system-test/_setup`);

const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
const sinon = require(`sinon`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const vision = proxyquire(`@google-cloud/vision`, {})();
const path = require(`path`);

test.before(stubConsole);
test.after.always(restoreConsole);
test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);

test.cb(`should detect labels`, (t) => {
const filePath = path.join(__dirname, `../resources/wakeupcat.jpg`);
Expand Down
6 changes: 4 additions & 2 deletions vision/samples/system-test/textDetection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

'use strict';

require(`../../system-test/_setup`);

const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const inputDir = path.join(__dirname, `../resources`);
const textDetectionSample = require(`../textDetection`);

test.before(tools.checkCredentials);

test.cb(`should detect texts`, (t) => {
textDetectionSample.main(inputDir, (err, textResponse) => {
t.ifError(err);
Expand Down
Loading

0 comments on commit 1354bc9

Please sign in to comment.