Skip to content

Commit

Permalink
test: make product search tests run (#269)
Browse files Browse the repository at this point in the history
test: make product search tests run
  • Loading branch information
nirupa-kumar authored and JustinBeckwith committed Nov 29, 2018
1 parent d1056e3 commit cb47bd6
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 182 deletions.
2 changes: 1 addition & 1 deletion vision/samples/faceDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function highlightFaces(inputFile, faces, outputFile, Canvas, callback) {
// Open the original image into a canvas
const img = new Image();
img.src = image;
const canvas = new Canvas(img.width, img.height);
const canvas = new Canvas.Canvas(img.width, img.height);
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0, img.width, img.height);

Expand Down
7 changes: 3 additions & 4 deletions vision/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
"mathjs": "^5.0.4",
"natural": "^0.6.1",
"redis": "^2.8.0",
"yargs": "^12.0.0"
"yargs": "^12.0.0",
"canvas": "^2.0.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"@google-cloud/storage": "^2.0.0",
"mocha": "^5.0.0",
"uuid": "^3.2.1"
},
"optionalDependencies": {
"canvas": "^2.0.0"
}
"optionalDependencies": {}
}
2 changes: 1 addition & 1 deletion vision/samples/productSearch/products.v1p3beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async function listProducts(projectId, location) {
// Resource path that represents Google Cloud Platform location.
const locationPath = client.locationPath(projectId, location);

const [products] = client.listProducts({parent: locationPath});
const [products] = await client.listProducts({parent: locationPath});
products.forEach(product => {
console.log(`Product name: ${product.name}`);
console.log(`Product id: ${product.name.split('/').pop()}`);
Expand Down
2 changes: 1 addition & 1 deletion vision/samples/productSearch/similarProducts.v1p3beta1.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function getSimilarProductsFile(
/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const projectId = 'java-docs-samples-testing';
// const projectId = 'nodejs-docs-samples';
// const location = 'us-west1';
// const productSetId = 'indexed_product_set_id_for_testing';
// const productCategory = 'apparel';
Expand Down
Binary file added vision/samples/resources/shoes_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 14 additions & 53 deletions vision/samples/system-test/faceDetection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,13 @@

'use strict';

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

class MockCanvas {
getContext() {
return {
drawImage: () => {},
beginPath: () => {},
lineTo: () => {},
stroke: () => {},
};
}

pngStream() {
return {
on: (event, cb) => {
if (event === 'end') {
setTimeout(cb, 1000);
} else if (event === `data`) {
/* eslint-disable */
cb(`test`);
cb(`foo`);
cb(`bar`);
/* eslint-enable */
}
},
};
}
}

MockCanvas.Image = class Image {};

const faceDetectionExample = require(`../faceDetection`);
const inputFile = path.join(__dirname, `../resources`, `face.png`);
const outputFile = path.join(__dirname, `../../vision`, `out.png`);
const tools = require('@google-cloud/nodejs-repo-tools');
const cmd = `node faceDetection.js`;
const cwd = path.join(__dirname, `..`);
const inputFile = path.join(__dirname, '../resources', 'face.png');
const outputFile = path.join(__dirname, '../../', 'out.png');

describe(`face detection`, () => {
before(tools.checkCredentials);
Expand All @@ -66,23 +36,14 @@ describe(`face detection`, () => {
console.warn('Face detection timed out!');
}
}, 60);

faceDetectionExample.main(
inputFile,
outputFile,
MockCanvas,
(err, faces) => {
assert.ifError(err);
assert.strictEqual(faces.length, 1);

const image = fs.readFileSync(outputFile);
assert.strictEqual(image.toString(`utf8`), `testfoobar`);
assert.ok(console.log.calledWith(`Found 1 face`));
assert.ok(console.log.calledWith(`Highlighting...`));
assert.ok(console.log.calledWith(`Finished!`));
done = true;
clearTimeout(timeout);
}
const output = await tools.runAsync(
`${cmd} ${inputFile} ${outputFile}`,
cwd
);
assert.ok(output.includes('Found 1 face'));
assert.ok(output.includes('Highlighting...'));
assert.ok(output.includes('Finished!'));
done = true;
clearTimeout(timeout);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const path = require(`path`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const cmd = `node importProductSets.v1p3beta1.js`;
const cwd = path.join(__dirname, `..`);
const cwd = path.join(__dirname, `..`, `productSearch`);

//Shared fixture data for product tests
const testImportProductSets = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
'use strict';

const path = require(`path`);
const uuid = require(`uuid`);
const vision = require('@google-cloud/vision').v1p3beta1;
const productSearchClient = new vision.ProductSearchClient();
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const cmd = `node productSearch.v1p3beta1.js`;
const cwd = path.join(__dirname, `..`);
const cwd = path.join(__dirname, `..`, `productSearch`);

// Shared fixture data for product tests
const testProductSet = {
projectId: process.env.GCLOUD_PROJECT,
location: 'us-west1',
productCategory: 'homegoods',
productId: 'test_product_id_1',
productSetId: 'test_product_set_id_1',
productId: `test_product_id${uuid.v4()}`,
productDisplayName: 'test_product_display_name_1',
productSetId: `test_product_set_id${uuid.v4()}`,
productSetDisplayName: 'test_product_set_display_name_1',
};

Expand All @@ -47,43 +49,43 @@ describe(`product search`, () => {

before(async () => {
// Create a test product set for each test
try {
await productSearchClient.createProduct({
parent: productSearchClient.locationPath(
testProductSet.projectId,
testProductSet.location
),
productId: testProductSet.productId,
product: {
displayName: testProductSet.productDisplayName,
productCategory: testProductSet.productCategory,
},
});
testProductSet.createdProductPaths.push(testProductSet.productPath);
} catch (err) {} // ignore error
await productSearchClient.createProduct({
parent: productSearchClient.locationPath(
testProductSet.projectId,
testProductSet.location
),
productId: testProductSet.productId,
product: {
displayName: testProductSet.productDisplayName,
productCategory: testProductSet.productCategory,
},
});
testProductSet.createdProductPaths.push(testProductSet.productPath);

try {
await productSearchClient.createProductSet({
parent: productSearchClient.locationPath(
testProductSet.projectId,
testProductSet.location
),
productSetId: testProductSet.productSetId,
productSet: {
displayName: testProductSet.productSetDisplayName,
},
});
testProductSet.createdProductSetPaths.push(
testProductSet.createdProductSetPaths
);
} catch (err) {} // ignore error
await productSearchClient.createProductSet({
parent: productSearchClient.locationPath(
testProductSet.projectId,
testProductSet.location
),
productSetId: testProductSet.productSetId,
productSet: {
displayName: testProductSet.productSetDisplayName,
},
});
testProductSet.createdProductSetPaths.push(
testProductSet.createdProductSetPaths
);
});

after(async () => {
// Delete products sets after each test
testProductSet.createdProductSetPaths.forEach(async path => {
try {
await productSearchClient.deleteProductSet({name: path});
} catch (err) {} // ignore error
});
testProductSet.createdProductPaths.forEach(async path => {
try {
await productSearchClient.deleteProduct({name: path});
} catch (err) {} // ignore error
});
Expand All @@ -100,18 +102,14 @@ describe(`product search`, () => {
assert.ok(output.includes(`Product added to product set.`));
});

test(`remove a product from a product set`, async () => {
it(`should remove a product from a product set`, async () => {
const output = await tools.runAsync(
`${cmd} removeProductFromProductSet "${testProductSet.projectId}" "${
testProductSet.location
}" "${testProductSet.productId}" "${testProductSet.productSetId}"`,
cwd
);

console.log('---------------');
console.log(output);
console.log('---------------');

assert.ok(output.includes(`Product removed from product set.`));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ const productSearch = new vision.ProductSearchClient();
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const cmd = `node productSets.v1p3beta1.js`;
const cwd = path.join(__dirname, `..`);
const cwd = path.join(__dirname, `..`, `productSearch`);

// Shared fixture data for product tests
const testProductSet = {
projectId: process.env.GCLOUD_PROJECT,
location: 'us-west1',
productSetId: 'test_product_set_id_1',
productSetId: `test_product_set_id${uuid.v4()}`,
productSetDisplayName: 'test_product_set_display_name_1',
};
testProductSet.productSetPath = productSearch.productSetPath(
Expand Down Expand Up @@ -57,21 +57,19 @@ describe(`product sets`, () => {

before(async () => {
// Create a test product set for each test
try {
await productSearch.createProductSet({
parent: productSearch.locationPath(
testProductSet.projectId,
testProductSet.location
),
productSetId: testProductSet.productSetId,
productSet: {
displayName: testProductSet.productSetDisplayName,
},
});
testProductSet.createdProductSetPaths.push(
testProductSet.createdProductSetPaths
);
} catch (err) {} // ignore error
await productSearch.createProductSet({
parent: productSearch.locationPath(
testProductSet.projectId,
testProductSet.location
),
productSetId: testProductSet.productSetId,
productSet: {
displayName: testProductSet.productSetDisplayName,
},
});
testProductSet.createdProductSetPaths.push(
testProductSet.createdProductSetPaths
);
});

after(async () => {
Expand Down
Loading

0 comments on commit cb47bd6

Please sign in to comment.