Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit e224ace

Browse files
nirupa-kumarJustinBeckwith
authored andcommitted
test: make product search tests run (#269)
test: make product search tests run
1 parent 7def116 commit e224ace

12 files changed

+184
-182
lines changed

samples/faceDetection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function highlightFaces(inputFile, faces, outputFile, Canvas, callback) {
6565
// Open the original image into a canvas
6666
const img = new Image();
6767
img.src = image;
68-
const canvas = new Canvas(img.width, img.height);
68+
const canvas = new Canvas.Canvas(img.width, img.height);
6969
const context = canvas.getContext('2d');
7070
context.drawImage(img, 0, 0, img.width, img.height);
7171

samples/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
"mathjs": "^5.0.4",
1818
"natural": "^0.6.1",
1919
"redis": "^2.8.0",
20-
"yargs": "^12.0.0"
20+
"yargs": "^12.0.0",
21+
"canvas": "^2.0.0"
2122
},
2223
"devDependencies": {
2324
"@google-cloud/nodejs-repo-tools": "^3.0.0",
2425
"@google-cloud/storage": "^2.0.0",
2526
"mocha": "^5.0.0",
2627
"uuid": "^3.2.1"
2728
},
28-
"optionalDependencies": {
29-
"canvas": "^2.0.0"
30-
}
29+
"optionalDependencies": {}
3130
}

samples/productSearch/products.v1p3beta1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async function listProducts(projectId, location) {
102102
// Resource path that represents Google Cloud Platform location.
103103
const locationPath = client.locationPath(projectId, location);
104104

105-
const [products] = client.listProducts({parent: locationPath});
105+
const [products] = await client.listProducts({parent: locationPath});
106106
products.forEach(product => {
107107
console.log(`Product name: ${product.name}`);
108108
console.log(`Product id: ${product.name.split('/').pop()}`);

samples/productSearch/similarProducts.v1p3beta1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function getSimilarProductsFile(
3333
/**
3434
* TODO(developer): Uncomment the following line before running the sample.
3535
*/
36-
// const projectId = 'java-docs-samples-testing';
36+
// const projectId = 'nodejs-docs-samples';
3737
// const location = 'us-west1';
3838
// const productSetId = 'indexed_product_set_id_for_testing';
3939
// const productCategory = 'apparel';

samples/resources/shoes_1.jpg

298 KB
Loading

samples/system-test/faceDetection.test.js

+14-53
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,13 @@
1515

1616
'use strict';
1717

18-
const fs = require(`fs`);
19-
const path = require(`path`);
18+
const path = require('path');
2019
const assert = require('assert');
21-
const tools = require(`@google-cloud/nodejs-repo-tools`);
22-
23-
class MockCanvas {
24-
getContext() {
25-
return {
26-
drawImage: () => {},
27-
beginPath: () => {},
28-
lineTo: () => {},
29-
stroke: () => {},
30-
};
31-
}
32-
33-
pngStream() {
34-
return {
35-
on: (event, cb) => {
36-
if (event === 'end') {
37-
setTimeout(cb, 1000);
38-
} else if (event === `data`) {
39-
/* eslint-disable */
40-
cb(`test`);
41-
cb(`foo`);
42-
cb(`bar`);
43-
/* eslint-enable */
44-
}
45-
},
46-
};
47-
}
48-
}
49-
50-
MockCanvas.Image = class Image {};
51-
52-
const faceDetectionExample = require(`../faceDetection`);
53-
const inputFile = path.join(__dirname, `../resources`, `face.png`);
54-
const outputFile = path.join(__dirname, `../../vision`, `out.png`);
20+
const tools = require('@google-cloud/nodejs-repo-tools');
21+
const cmd = `node faceDetection.js`;
22+
const cwd = path.join(__dirname, `..`);
23+
const inputFile = path.join(__dirname, '../resources', 'face.png');
24+
const outputFile = path.join(__dirname, '../../', 'out.png');
5525

5626
describe(`face detection`, () => {
5727
before(tools.checkCredentials);
@@ -66,23 +36,14 @@ describe(`face detection`, () => {
6636
console.warn('Face detection timed out!');
6737
}
6838
}, 60);
69-
70-
faceDetectionExample.main(
71-
inputFile,
72-
outputFile,
73-
MockCanvas,
74-
(err, faces) => {
75-
assert.ifError(err);
76-
assert.strictEqual(faces.length, 1);
77-
78-
const image = fs.readFileSync(outputFile);
79-
assert.strictEqual(image.toString(`utf8`), `testfoobar`);
80-
assert.ok(console.log.calledWith(`Found 1 face`));
81-
assert.ok(console.log.calledWith(`Highlighting...`));
82-
assert.ok(console.log.calledWith(`Finished!`));
83-
done = true;
84-
clearTimeout(timeout);
85-
}
39+
const output = await tools.runAsync(
40+
`${cmd} ${inputFile} ${outputFile}`,
41+
cwd
8642
);
43+
assert.ok(output.includes('Found 1 face'));
44+
assert.ok(output.includes('Highlighting...'));
45+
assert.ok(output.includes('Finished!'));
46+
done = true;
47+
clearTimeout(timeout);
8748
});
8849
});

samples/productSearch/system-test/importProductSets.v1p3beta1.test.js samples/system-test/importProductSets.v1p3beta1.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const path = require(`path`);
1919
const assert = require('assert');
2020
const tools = require(`@google-cloud/nodejs-repo-tools`);
2121
const cmd = `node importProductSets.v1p3beta1.js`;
22-
const cwd = path.join(__dirname, `..`);
22+
const cwd = path.join(__dirname, `..`, `productSearch`);
2323

2424
//Shared fixture data for product tests
2525
const testImportProductSets = {

samples/productSearch/system-test/productSearch.v1p3beta1.test.js samples/system-test/productSearch.v1p3beta1.test.js

+35-37
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@
1616
'use strict';
1717

1818
const path = require(`path`);
19+
const uuid = require(`uuid`);
1920
const vision = require('@google-cloud/vision').v1p3beta1;
2021
const productSearchClient = new vision.ProductSearchClient();
2122
const assert = require('assert');
2223
const tools = require(`@google-cloud/nodejs-repo-tools`);
2324
const cmd = `node productSearch.v1p3beta1.js`;
24-
const cwd = path.join(__dirname, `..`);
25+
const cwd = path.join(__dirname, `..`, `productSearch`);
2526

2627
// Shared fixture data for product tests
2728
const testProductSet = {
2829
projectId: process.env.GCLOUD_PROJECT,
2930
location: 'us-west1',
3031
productCategory: 'homegoods',
31-
productId: 'test_product_id_1',
32-
productSetId: 'test_product_set_id_1',
32+
productId: `test_product_id${uuid.v4()}`,
33+
productDisplayName: 'test_product_display_name_1',
34+
productSetId: `test_product_set_id${uuid.v4()}`,
3335
productSetDisplayName: 'test_product_set_display_name_1',
3436
};
3537

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

4850
before(async () => {
4951
// Create a test product set for each test
50-
try {
51-
await productSearchClient.createProduct({
52-
parent: productSearchClient.locationPath(
53-
testProductSet.projectId,
54-
testProductSet.location
55-
),
56-
productId: testProductSet.productId,
57-
product: {
58-
displayName: testProductSet.productDisplayName,
59-
productCategory: testProductSet.productCategory,
60-
},
61-
});
62-
testProductSet.createdProductPaths.push(testProductSet.productPath);
63-
} catch (err) {} // ignore error
52+
await productSearchClient.createProduct({
53+
parent: productSearchClient.locationPath(
54+
testProductSet.projectId,
55+
testProductSet.location
56+
),
57+
productId: testProductSet.productId,
58+
product: {
59+
displayName: testProductSet.productDisplayName,
60+
productCategory: testProductSet.productCategory,
61+
},
62+
});
63+
testProductSet.createdProductPaths.push(testProductSet.productPath);
6464

65-
try {
66-
await productSearchClient.createProductSet({
67-
parent: productSearchClient.locationPath(
68-
testProductSet.projectId,
69-
testProductSet.location
70-
),
71-
productSetId: testProductSet.productSetId,
72-
productSet: {
73-
displayName: testProductSet.productSetDisplayName,
74-
},
75-
});
76-
testProductSet.createdProductSetPaths.push(
77-
testProductSet.createdProductSetPaths
78-
);
79-
} catch (err) {} // ignore error
65+
await productSearchClient.createProductSet({
66+
parent: productSearchClient.locationPath(
67+
testProductSet.projectId,
68+
testProductSet.location
69+
),
70+
productSetId: testProductSet.productSetId,
71+
productSet: {
72+
displayName: testProductSet.productSetDisplayName,
73+
},
74+
});
75+
testProductSet.createdProductSetPaths.push(
76+
testProductSet.createdProductSetPaths
77+
);
8078
});
8179

8280
after(async () => {
8381
// Delete products sets after each test
8482
testProductSet.createdProductSetPaths.forEach(async path => {
8583
try {
8684
await productSearchClient.deleteProductSet({name: path});
85+
} catch (err) {} // ignore error
86+
});
87+
testProductSet.createdProductPaths.forEach(async path => {
88+
try {
8789
await productSearchClient.deleteProduct({name: path});
8890
} catch (err) {} // ignore error
8991
});
@@ -100,18 +102,14 @@ describe(`product search`, () => {
100102
assert.ok(output.includes(`Product added to product set.`));
101103
});
102104

103-
test(`remove a product from a product set`, async () => {
105+
it(`should remove a product from a product set`, async () => {
104106
const output = await tools.runAsync(
105107
`${cmd} removeProductFromProductSet "${testProductSet.projectId}" "${
106108
testProductSet.location
107109
}" "${testProductSet.productId}" "${testProductSet.productSetId}"`,
108110
cwd
109111
);
110112

111-
console.log('---------------');
112-
console.log(output);
113-
console.log('---------------');
114-
115113
assert.ok(output.includes(`Product removed from product set.`));
116114
});
117115
});

samples/productSearch/system-test/productSets.v1p3beta1.test.js samples/system-test/productSets.v1p3beta1.test.js

+15-17
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ const productSearch = new vision.ProductSearchClient();
2222
const assert = require('assert');
2323
const tools = require(`@google-cloud/nodejs-repo-tools`);
2424
const cmd = `node productSets.v1p3beta1.js`;
25-
const cwd = path.join(__dirname, `..`);
25+
const cwd = path.join(__dirname, `..`, `productSearch`);
2626

2727
// Shared fixture data for product tests
2828
const testProductSet = {
2929
projectId: process.env.GCLOUD_PROJECT,
3030
location: 'us-west1',
31-
productSetId: 'test_product_set_id_1',
31+
productSetId: `test_product_set_id${uuid.v4()}`,
3232
productSetDisplayName: 'test_product_set_display_name_1',
3333
};
3434
testProductSet.productSetPath = productSearch.productSetPath(
@@ -57,21 +57,19 @@ describe(`product sets`, () => {
5757

5858
before(async () => {
5959
// Create a test product set for each test
60-
try {
61-
await productSearch.createProductSet({
62-
parent: productSearch.locationPath(
63-
testProductSet.projectId,
64-
testProductSet.location
65-
),
66-
productSetId: testProductSet.productSetId,
67-
productSet: {
68-
displayName: testProductSet.productSetDisplayName,
69-
},
70-
});
71-
testProductSet.createdProductSetPaths.push(
72-
testProductSet.createdProductSetPaths
73-
);
74-
} catch (err) {} // ignore error
60+
await productSearch.createProductSet({
61+
parent: productSearch.locationPath(
62+
testProductSet.projectId,
63+
testProductSet.location
64+
),
65+
productSetId: testProductSet.productSetId,
66+
productSet: {
67+
displayName: testProductSet.productSetDisplayName,
68+
},
69+
});
70+
testProductSet.createdProductSetPaths.push(
71+
testProductSet.createdProductSetPaths
72+
);
7573
});
7674

7775
after(async () => {

0 commit comments

Comments
 (0)