Skip to content

Commit

Permalink
refactor: use execSync for tests (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 5, 2019
1 parent 89972ee commit b809154
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 104 deletions.
1 change: 0 additions & 1 deletion vision/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"devDependencies": {
"@google-cloud/storage": "^2.0.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0",
"uuid": "^3.2.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

const path = require('path');
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const cp = require('child_process');
const {assert} = require('chai');
const uuid = require('uuid');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const storage = new Storage();
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node async-batch-annotate-images.js`;
Expand All @@ -46,7 +47,7 @@ describe(`detect v1 p4 beta1`, () => {
});

it(`should annotate the remote landmark.jpg sample`, async () => {
const output = await exec(
const output = execSync(
`${cmd} gs://${bucketName}/${files[1].name} gs://${bucketName}/out/`
);
assert.match(output, /Json saved to: gs:\/\//);
Expand Down
7 changes: 4 additions & 3 deletions vision/samples/system-test/batch-annotate-files-gcs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

const path = require('path');
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const cp = require('child_process');
const {assert} = require('chai');
const uuid = require('uuid');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const storage = new Storage();
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node batch-annotate-files-gcs.js`;
Expand All @@ -46,7 +47,7 @@ describe(`detect v1 p4 beta1`, () => {
});

it(`should annotate the remote pdf-ocr.pdf in GCS bucket`, async () => {
const output = await exec(`${cmd} gs://${bucketName}/${files[0].name}`);
const output = execSync(`${cmd} gs://${bucketName}/${files[0].name}`);
assert.match(output, /Word text: Boring/);
assert.match(output, /Symbol: p/);
});
Expand Down
7 changes: 4 additions & 3 deletions vision/samples/system-test/batch-annotate-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

const path = require('path');
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const cp = require('child_process');
const {assert} = require('chai');
const uuid = require('uuid');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const storage = new Storage();
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node batch-annotate-files.js`;
Expand All @@ -46,7 +47,7 @@ describe(`detect v1 p4 beta1`, () => {
});

it(`should annotate the local pdf-ocr.pdf sample`, async () => {
const output = await exec(`${cmd} ${files[0].localPath}`);
const output = execSync(`${cmd} ${files[0].localPath}`);
assert.match(output, /Word text: Boring/);
assert.match(output, /Symbol: p/);
});
Expand Down
63 changes: 28 additions & 35 deletions vision/samples/system-test/detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

const path = require('path');
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const cp = require('child_process');
const uuid = require('uuid');
const {assert} = require('chai');
const vision = require('@google-cloud/vision');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const client = new vision.ImageAnnotatorClient();

const storage = new Storage();
Expand Down Expand Up @@ -61,116 +62,108 @@ describe(`detect`, () => {
});

it(`should detect faces in a local file`, async () => {
const output = await exec(`${cmd} faces ${files[0].localPath}`);
const output = execSync(`${cmd} faces ${files[0].localPath}`);
assert.match(output, /Faces:/);
assert.match(output, /Face #1:/);
});

it(`should detect faces in a remote file`, async () => {
const output = await exec(
`${cmd} faces-gcs ${bucketName} ${files[0].name}`
);
const output = execSync(`${cmd} faces-gcs ${bucketName} ${files[0].name}`);
assert.match(output, /Faces:/);
assert.match(output, /Face #1:/);
});

it(`should detect labels in a local file`, async () => {
const output = await exec(`${cmd} labels ${files[4].localPath}`);
const output = execSync(`${cmd} labels ${files[4].localPath}`);
assert.match(output, /Labels:/);
assert.match(output, /cat/);
});

it(`should detect labels in a remote file`, async () => {
const output = await exec(
`${cmd} labels-gcs ${bucketName} ${files[4].name}`
);
const output = execSync(`${cmd} labels-gcs ${bucketName} ${files[4].name}`);
assert.match(output, /Labels:/);
assert.match(output, /cat/);
});

it(`should detect landmarks in a local file`, async () => {
const output = await exec(`${cmd} landmarks ${files[1].localPath}`);
const output = execSync(`${cmd} landmarks ${files[1].localPath}`);
assert.match(output, /Landmarks:/);
assert.match(output, /Palace of Fine Arts/);
});

it(`should detect landmarks in a remote file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} landmarks-gcs ${bucketName} ${files[1].name}`
);
assert.match(output, /Landmarks:/);
assert.match(output, /Palace of Fine Arts/);
});

it(`should detect text in a local file`, async () => {
const output = await exec(`${cmd} text ${files[3].localPath}`);
const output = execSync(`${cmd} text ${files[3].localPath}`);
assert.match(output, /Text:/);
assert.match(output, /System Software Update/);
});

it(`should detect text in a remote file`, async () => {
const output = await exec(`${cmd} text-gcs ${bucketName} ${files[3].name}`);
const output = execSync(`${cmd} text-gcs ${bucketName} ${files[3].name}`);
assert.match(output, /Text:/);
assert.match(output, /System Software Update/);
});

it(`should detect logos in a local file`, async () => {
const output = await exec(`${cmd} logos ${files[9].localPath}`);
const output = execSync(`${cmd} logos ${files[9].localPath}`);
assert.match(output, /Logos:/);
assert.match(output, /google/);
});

it(`should detect logos in a remote file`, async () => {
const output = await exec(
`${cmd} logos-gcs ${bucketName} ${files[9].name}`
);
const output = execSync(`${cmd} logos-gcs ${bucketName} ${files[9].name}`);
assert.match(output, /Logos:/);
assert.match(output, /google/);
});

it(`should detect properties in a local file`, async () => {
const output = await exec(`${cmd} properties ${files[1].localPath}`);
const output = execSync(`${cmd} properties ${files[1].localPath}`);
assert.match(output, /{ color: { red: 69, green: 42, blue: 27/);
assert.ok(output.split(`\n`).length > 4, `Multiple colors were detected.`);
});

it(`should detect properties in a remote file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} properties-gcs ${bucketName} ${files[1].name}`
);
assert.match(output, /{ color: { red: 69, green: 42, blue: 27/);
assert.ok(output.split(`\n`).length > 4, `Multiple colors were detected.`);
});

it(`should detect safe-search in a local file`, async () => {
const output = await exec(`${cmd} safe-search ${files[4].localPath}`);
const output = execSync(`${cmd} safe-search ${files[4].localPath}`);
assert.match(output, /VERY_LIKELY/);
assert.match(output, /Racy:/);
});

it(`should detect safe-search in a remote file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} safe-search-gcs ${bucketName} ${files[4].name}`
);
assert.match(output, /Medical:/);
});

it(`should detect crop hints in a local file`, async () => {
const output = await exec(`${cmd} crops ${files[2].localPath}`);
const output = execSync(`${cmd} crops ${files[2].localPath}`);
assert.match(output, /Crop Hint 0:/);
assert.match(output, /Bound 2: \(280, 43\)/);
});

it(`should detect crop hints in a remote file`, async () => {
const output = await exec(
`${cmd} crops-gcs ${bucketName} ${files[2].name}`
);
const output = execSync(`${cmd} crops-gcs ${bucketName} ${files[2].name}`);
assert.match(output, /Crop Hint 0:/);
assert.match(output, /Bound 2: \(280, 43\)/);
});

it(`should detect similar web images in a local file`, async () => {
const output = await exec(`${cmd} web ${files[5].localPath}`);
const output = execSync(`${cmd} web ${files[5].localPath}`);

const [results] = await client.webDetection(files[5].localPath);
const webDetection = results.webDetection;
Expand All @@ -195,7 +188,7 @@ describe(`detect`, () => {
});

it(`should detect similar web images in a remote file`, async () => {
const output = await exec(`${cmd} web-gcs ${bucketName} ${files[5].name}`);
const output = execSync(`${cmd} web-gcs ${bucketName} ${files[5].name}`);

const [results] = await client.webDetection(
`gs://${bucketName}/${files[5].name}`
Expand All @@ -222,14 +215,14 @@ describe(`detect`, () => {
});

it(`should detect web entities with geo metadata in local file`, async () => {
const output = await exec(`${cmd} web-geo ${files[1].localPath}`);
const output = execSync(`${cmd} web-geo ${files[1].localPath}`);
assert.match(output, /Description:/);
assert.match(output, /Score:/);
assert.match(output, /Rome/);
});

it(`should detect web entities with geo metadata in remote file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} web-geo-gcs ${bucketName} ${files[1].name}`
);
assert.match(output, /Description:/);
Expand All @@ -238,35 +231,35 @@ describe(`detect`, () => {
});

it(`should read a document from a local file`, async () => {
const output = await exec(`${cmd} fulltext ${files[2].localPath}`);
const output = execSync(`${cmd} fulltext ${files[2].localPath}`);
assert.match(output, /Google Cloud Platform/);
assert.match(output, /Word text: Cloud/);
assert.match(output, /Word confidence: 0.9/);
});

it(`should read a document from a remote file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} fulltext-gcs ${bucketName} ${files[2].name}`
);
assert.match(output, /Google Cloud Platform/);
});

it(`should extract text from pdf file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} pdf ${bucketName} ${files[7].name} ${prefix}`
);
assert.match(output, /results/);
});

it(`should detect objects in a local file`, async () => {
const output = await exec(`${cmd} localize-objects ${files[8].localPath}`);
const output = execSync(`${cmd} localize-objects ${files[8].localPath}`);
assert.match(output, /Name: Bird/);
assert.match(output, /Name: Duck/);
assert.match(output, /Name: Toy/);
});

it(`should detect objects in a remote file`, async () => {
const output = await exec(
const output = execSync(
`${cmd} localize-objects-gcs gs://${bucketName}/${files[8].name}`
);
assert.match(output, /Name: Bird/);
Expand Down
13 changes: 7 additions & 6 deletions vision/samples/system-test/detect.v1p1beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
'use strict';

const path = require('path');
const execa = require('execa');
const cp = require('child_process');
const {assert} = require('chai');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const cmd = `node detect.v1p1beta1.js`;
const files = [`text.jpg`, `wakeupcat.jpg`, `landmark.jpg`, `city.jpg`].map(
name => {
Expand All @@ -32,25 +33,25 @@ const files = [`text.jpg`, `wakeupcat.jpg`, `landmark.jpg`, `city.jpg`].map(

describe(`detect v1 p1 beta1`, () => {
it(`should extract text from image file and print confidence`, async () => {
const output = await exec(`${cmd} fulltext ${files[0].localPath}`);
const output = execSync(`${cmd} fulltext ${files[0].localPath}`);
assert.match(output, /Word text: class/);
assert.match(output, /Word confidence:/);
});

it(`should detect safe search properties from image file`, async () => {
const output = await exec(`${cmd} safe-search ${files[1].localPath}`);
const output = execSync(`${cmd} safe-search ${files[1].localPath}`);
assert.match(output, /VERY_LIKELY/);
assert.match(output, /Racy:/);
});

it(`should detect web entities including best guess labels`, async () => {
const output = await exec(`${cmd} web ${files[2].localPath}`);
const output = execSync(`${cmd} web ${files[2].localPath}`);
assert.match(output, /Description: Palace Of Fine Arts/);
assert.match(output, /Best guess label: palace of fine arts/);
});

it(`should detect web entities using geographical metadata`, async () => {
const output = await exec(`${cmd} web-entities-geo ${files[3].localPath}`);
const output = execSync(`${cmd} web-entities-geo ${files[3].localPath}`);
assert.match(output, /Score:/);
});
});
9 changes: 5 additions & 4 deletions vision/samples/system-test/detect.v1p3beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

const path = require('path');
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const cp = require('child_process');
const {assert} = require('chai');
const uuid = require('uuid');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const storage = new Storage();
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const cmd = `node detect.v1p3beta1.js`;
Expand Down Expand Up @@ -49,12 +50,12 @@ describe(`detect v1 p3 beta1`, () => {
});

it(`should read handwriting in local handwritten.jpg sample`, async () => {
const output = await exec(`${cmd} detectHandwriting ${files[1]}`);
const output = execSync(`${cmd} detectHandwriting ${files[1]}`);
assert.match(output, /hand written message/);
});

it(`should read handwriting from handwritten.jpg in GCS bucket`, async () => {
const output = await exec(
const output = execSync(
`${cmd} detectHandwritingGCS gs://${bucketName}/${files[1].name}`
);
assert.match(output, /hand written message/);
Expand Down
7 changes: 4 additions & 3 deletions vision/samples/system-test/faceDetection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

const path = require('path');
const {assert} = require('chai');
const execa = require('execa');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const exec = async cmd => (await execa.shell(cmd)).stdout;
const cmd = `node faceDetection.js`;
const inputFile = path.join(__dirname, '../resources', 'face.png');
const outputFile = path.join(__dirname, '../../', 'out.png');

describe(`face detection`, () => {
it(`should detect faces`, async () => {
const output = await exec(`${cmd} ${inputFile} ${outputFile}`);
const output = execSync(`${cmd} ${inputFile} ${outputFile}`);
assert.match(output, /Found 1 face/);
assert.match(output, /Highlighting.../);
assert.match(output, /Finished!/);
Expand Down
Loading

0 comments on commit b809154

Please sign in to comment.