diff --git a/samples/faceDetection.js b/samples/faceDetection.js index 45921271..b8478fcb 100644 --- a/samples/faceDetection.js +++ b/samples/faceDetection.js @@ -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); diff --git a/samples/package.json b/samples/package.json index a11a974d..833d46aa 100644 --- a/samples/package.json +++ b/samples/package.json @@ -17,7 +17,8 @@ "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", @@ -25,7 +26,5 @@ "mocha": "^5.0.0", "uuid": "^3.2.1" }, - "optionalDependencies": { - "canvas": "^2.0.0" - } + "optionalDependencies": {} } diff --git a/samples/productSearch/products.v1p3beta1.js b/samples/productSearch/products.v1p3beta1.js index 9c51369f..65aafd32 100644 --- a/samples/productSearch/products.v1p3beta1.js +++ b/samples/productSearch/products.v1p3beta1.js @@ -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()}`); diff --git a/samples/productSearch/similarProducts.v1p3beta1.js b/samples/productSearch/similarProducts.v1p3beta1.js index 939465fd..6dc0c645 100644 --- a/samples/productSearch/similarProducts.v1p3beta1.js +++ b/samples/productSearch/similarProducts.v1p3beta1.js @@ -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'; diff --git a/samples/resources/shoes_1.jpg b/samples/resources/shoes_1.jpg new file mode 100644 index 00000000..78318eef Binary files /dev/null and b/samples/resources/shoes_1.jpg differ diff --git a/samples/system-test/faceDetection.test.js b/samples/system-test/faceDetection.test.js index e6edabc4..382dd74d 100644 --- a/samples/system-test/faceDetection.test.js +++ b/samples/system-test/faceDetection.test.js @@ -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); @@ -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); }); }); diff --git a/samples/productSearch/system-test/importProductSets.v1p3beta1.test.js b/samples/system-test/importProductSets.v1p3beta1.test.js similarity index 95% rename from samples/productSearch/system-test/importProductSets.v1p3beta1.test.js rename to samples/system-test/importProductSets.v1p3beta1.test.js index 9938c437..acdc6652 100644 --- a/samples/productSearch/system-test/importProductSets.v1p3beta1.test.js +++ b/samples/system-test/importProductSets.v1p3beta1.test.js @@ -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 = { diff --git a/samples/productSearch/system-test/productSearch.v1p3beta1.test.js b/samples/system-test/productSearch.v1p3beta1.test.js similarity index 66% rename from samples/productSearch/system-test/productSearch.v1p3beta1.test.js rename to samples/system-test/productSearch.v1p3beta1.test.js index e9f48b95..be8a4f93 100644 --- a/samples/productSearch/system-test/productSearch.v1p3beta1.test.js +++ b/samples/system-test/productSearch.v1p3beta1.test.js @@ -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', }; @@ -47,36 +49,32 @@ 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 () => { @@ -84,6 +82,10 @@ describe(`product search`, () => { 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 }); @@ -100,7 +102,7 @@ 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 @@ -108,10 +110,6 @@ describe(`product search`, () => { cwd ); - console.log('---------------'); - console.log(output); - console.log('---------------'); - assert.ok(output.includes(`Product removed from product set.`)); }); }); diff --git a/samples/productSearch/system-test/productSets.v1p3beta1.test.js b/samples/system-test/productSets.v1p3beta1.test.js similarity index 89% rename from samples/productSearch/system-test/productSets.v1p3beta1.test.js rename to samples/system-test/productSets.v1p3beta1.test.js index 836ff56f..312defa0 100644 --- a/samples/productSearch/system-test/productSets.v1p3beta1.test.js +++ b/samples/system-test/productSets.v1p3beta1.test.js @@ -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( @@ -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 () => { diff --git a/samples/productSearch/system-test/products.v1p3beta1.test.js b/samples/system-test/products.v1p3beta1.test.js similarity index 63% rename from samples/productSearch/system-test/products.v1p3beta1.test.js rename to samples/system-test/products.v1p3beta1.test.js index 4c318312..d30a4277 100644 --- a/samples/productSearch/system-test/products.v1p3beta1.test.js +++ b/samples/system-test/products.v1p3beta1.test.js @@ -22,13 +22,13 @@ const productSearch = new vision.ProductSearchClient(); const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); const cmd = `node products.v1p3beta1.js`; -const cwd = path.join(__dirname, `..`); +const cwd = path.join(__dirname, `..`, `productSearch`); // Shared fixture data for product tests const testProduct = { projectId: process.env.GCLOUD_PROJECT, location: 'us-west1', - productId: 'test_product_id_1', + productId: `test_products_id${uuid.v4()}`, productDisplayName: 'test_product_display_name_1', productCategory: 'homegoods', productKey: 'myKey', @@ -40,7 +40,6 @@ testProduct.productPath = productSearch.productPath( testProduct.productId ); testProduct.createdProductPaths = []; - // Helper function: returns product if exists else false async function getProductOrFalse(productPath) { try { @@ -49,17 +48,14 @@ async function getProductOrFalse(productPath) { } catch (err) { if (err.message.includes('Not found')) { return false; - } else { - throw err; } } } describe(`products`, () => { before(tools.checkCredentials); - before(async () => { - // Create a test product for each test + // Create a test product set for each test await productSearch.createProduct({ parent: productSearch.locationPath( testProduct.projectId, @@ -75,14 +71,13 @@ describe(`products`, () => { }); after(async () => { - // Delete products after each test + // Delete products sets after each test testProduct.createdProductPaths.forEach(async path => { try { await productSearch.deleteProduct({name: path}); } catch (err) {} // ignore error }); }); - it(`should create product`, async () => { const newProductId = `ProductId${uuid.v4()}`; const newProductPath = productSearch.productPath( @@ -91,9 +86,8 @@ describe(`products`, () => { newProductId ); assert.strictEqual(await getProductOrFalse(newProductPath), false); - testProduct.createdProductPaths.push(newProductPath); - const output = await tools.runAsync( + let output = await tools.runAsync( `${cmd} createProduct "${testProduct.projectId}" "${ testProduct.location }" "${newProductId}" "${testProduct.productDisplayName}" "${ @@ -107,24 +101,58 @@ describe(`products`, () => { const newProduct = await getProductOrFalse(newProductPath); assert.ok(newProduct.displayName === testProduct.productDisplayName); assert.ok(newProduct.productCategory === testProduct.productCategory); + + output = await tools.runAsync( + `${cmd} deleteProduct "${testProduct.projectId}" "${ + testProduct.location + }" "${newProductId}"`, + cwd + ); + assert.ok(output.includes(`Product deleted.`)); }); it(`should get product`, async () => { - const output = await tools.runAsync( + const newProductId = `ProductId${uuid.v4()}`; + const newProductPath = productSearch.productPath( + testProduct.projectId, + testProduct.location, + newProductId + ); + assert.strictEqual(await getProductOrFalse(newProductPath), false); + let output = await tools.runAsync( + `${cmd} createProduct "${testProduct.projectId}" "${ + testProduct.location + }" "${newProductId}" "${testProduct.productDisplayName}" "${ + testProduct.productCategory + }"`, + cwd + ); + + assert.ok(output.includes(`Product name: ${newProductPath}`)); + + output = await tools.runAsync( `${cmd} getProduct "${testProduct.projectId}" "${ testProduct.location - }" "${testProduct.productId}"`, + }" "${newProductId}"`, cwd ); - assert.ok(output.includes(`Product name: ${testProduct.productPath}`)); - assert.ok(output.includes(`Product id: ${testProduct.productId}`)); + assert.ok(output.includes(`Product name: ${newProductPath}`)); + assert.ok(output.includes(`Product id: ${newProductId}`)); assert.ok(output.includes(`Product display name:`)); assert.ok(output.includes(`Product description:`)); assert.ok( output.includes(`Product category: ${testProduct.productCategory}`) ); assert.ok(output.includes(`Product labels:`)); + + output = await tools.runAsync( + `${cmd} deleteProduct "${testProduct.projectId}" "${ + testProduct.location + }" "${newProductId}"`, + cwd + ); + assert.ok(output.includes(`Product deleted.`)); }); it(`should list products`, async () => { @@ -135,45 +163,31 @@ describe(`products`, () => { cwd ); - assert.ok(output.includes(`Product name: ${testProduct.productPath}`)); assert.ok(output.includes(`Product id: ${testProduct.productId}`)); - assert.ok( - output.includes(`Product display name: ${testProduct.productDisplayName}`) - ); - assert.ok(output.includes(`Product description:`)); - assert.ok( - output.includes(`Product category: ${testProduct.productCategory}`) - ); assert.ok(output.includes(`Product labels:`)); }); - it(`should delete product`, async () => { - const product = await productSearch.getProduct({ - name: `${testProduct.productPath}`, - }); - assert.ok(product); - - const output = await tools.runAsync( - `${cmd} deleteProduct "${testProduct.projectId}" "${ + it(`should update product label`, async () => { + const newProductId = `ProductId${uuid.v4()}`; + const newProductPath = productSearch.productPath( + testProduct.projectId, + testProduct.location, + newProductId + ); + let output = await tools.runAsync( + `${cmd} createProduct "${testProduct.projectId}" "${ testProduct.location - }" "${testProduct.productId}"`, + }" "${newProductId}" "${testProduct.productDisplayName}" "${ + testProduct.productCategory + }"`, cwd ); - assert.ok(output.includes(`Product deleted.`)); - try { - await productSearch.getProduct({name: `${testProduct.productPath}`}); - assert.fail('Product was not deleted'); - } catch (err) { - assert.ok(err.message.includes('Not found')); - } - }); - - it(`should update product label`, async () => { - const output = await tools.runAsync( + assert.ok(output.includes(`Product name: ${newProductPath}`)); + output = await tools.runAsync( `${cmd} updateProductLabels "${testProduct.projectId}" "${ testProduct.location - }" "${testProduct.productId}" "${testProduct.productKey}" "${ + }" "${newProductId}" "${testProduct.productKey}" "${ testProduct.productValue }"`, cwd @@ -192,4 +206,39 @@ describe(`products`, () => { output.includes(`Product category: ${testProduct.productCategory}`) ); }); + + it(`should delete product`, async () => { + const newProductId = `ProductId${uuid.v4()}`; + const newProductPath = productSearch.productPath( + testProduct.projectId, + testProduct.location, + newProductId + ); + assert.strictEqual(await getProductOrFalse(newProductPath), false); + let output = await tools.runAsync( + `${cmd} createProduct "${testProduct.projectId}" "${ + testProduct.location + }" "${newProductId}" "${testProduct.productDisplayName}" "${ + testProduct.productCategory + }"`, + cwd + ); + + assert.ok(output.includes(`Product name: ${newProductPath}`)); + + output = await tools.runAsync( + `${cmd} deleteProduct "${testProduct.projectId}" "${ + testProduct.location + }" "${newProductId}"`, + cwd + ); + assert.ok(output.includes(`Product deleted.`)); + + try { + await productSearch.getProduct({name: `${newProductPath}`}); + assert.fail('Product was not deleted'); + } catch (err) { + assert.ok(err.message.includes('Not found')); + } + }); }); diff --git a/samples/productSearch/system-test/referenceImages.v1p3beta1.test.js b/samples/system-test/referenceImages.v1p3beta1.test.js similarity index 79% rename from samples/productSearch/system-test/referenceImages.v1p3beta1.test.js rename to samples/system-test/referenceImages.v1p3beta1.test.js index 614be56d..586dc1a3 100644 --- a/samples/productSearch/system-test/referenceImages.v1p3beta1.test.js +++ b/samples/system-test/referenceImages.v1p3beta1.test.js @@ -22,17 +22,17 @@ const productSearchClient = new vision.ProductSearchClient(); const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); const cmd = `node referenceImages.v1p3beta1.js`; -const cwd = path.join(__dirname, `..`); +const cwd = path.join(__dirname, `..`, `productSearch`); // Shared fixture data for product tests const testProduct = { projectId: process.env.GCLOUD_PROJECT, location: 'us-west1', - productId: 'test_product_id_1', + productId: 'test_product_ref_image_id_1', productDisplayName: 'test_product_display_name_1', productCategory: 'homegoods', productReferenceImageId: `ReferenceImage${uuid.v4()}`, - productImageUri: 'gs://python-docs-samples-tests/product_search/shoes_1.jpg', + productImageUri: 'gs://nodejs-docs-samples/product-search/shoes_1.jpg', }; testProduct.productPath = productSearchClient.productPath( testProduct.projectId, @@ -46,20 +46,19 @@ describe(`reference images`, () => { before(async () => { // Create a test product for each test - try { - await productSearchClient.createProduct({ - parent: productSearchClient.locationPath( - testProduct.projectId, - testProduct.location - ), - productId: testProduct.productId, - product: { - displayName: testProduct.productDisplayName, - productCategory: testProduct.productCategory, - }, - }); - testProduct.createdProductPaths.push(testProduct.productPath); - } catch (err) {} // ignore error + + await productSearchClient.createProduct({ + parent: productSearchClient.locationPath( + testProduct.projectId, + testProduct.location + ), + productId: testProduct.productId, + product: { + displayName: testProduct.productDisplayName, + productCategory: testProduct.productCategory, + }, + }); + testProduct.createdProductPaths.push(testProduct.productPath); }); after(async () => { diff --git a/samples/productSearch/system-test/similarProducts.v1p3beta1.test.js b/samples/system-test/similarProducts.v1p3beta1.test.js similarity index 94% rename from samples/productSearch/system-test/similarProducts.v1p3beta1.test.js rename to samples/system-test/similarProducts.v1p3beta1.test.js index 5b81e98c..bf11523e 100644 --- a/samples/productSearch/system-test/similarProducts.v1p3beta1.test.js +++ b/samples/system-test/similarProducts.v1p3beta1.test.js @@ -21,12 +21,14 @@ const productSearch = new vision.ProductSearchClient(); const assert = require('assert'); const tools = require(`@google-cloud/nodejs-repo-tools`); const cmd = `node similarProducts.v1p3beta1.js`; -const cwd = path.join(__dirname, `..`); +const cwd = path.join(__dirname, `..`, `productSearch`); const filter = ['', 'style=womens']; -const localPath = './resources/shoes_1.jpg'; +const localPath = './../resources/shoes_1.jpg'; const gcsUri = 'gs://nodejs-docs-samples/product-search/shoes_1.jpg'; // Shared fixture data for product tests +//Need to have a product set already imported and indexed +// (gs://nodejs-docs-samples/product-search/indexed_product_set.csv) const testSimilarProducts = { projectId: process.env.GCLOUD_PROJECT, location: 'us-west1', @@ -49,7 +51,6 @@ describe(`similar products`, () => { }" "${localPath}" "${filter[0]}"`, cwd ); - assert.ok(output.includes(`Similar product information:`)); assert.ok( output.includes( @@ -69,7 +70,6 @@ describe(`similar products`, () => { }" "${localPath}" "${filter[1]}"`, cwd ); - assert.ok(output.includes(`Similar product information:`)); assert.ok( output.includes( @@ -88,7 +88,6 @@ describe(`similar products`, () => { }" "${gcsUri}" "${filter[0]}"`, cwd ); - assert.ok(output.includes(`Similar product information:`)); assert.ok( output.includes( @@ -108,7 +107,6 @@ describe(`similar products`, () => { }" "${gcsUri}" "${filter[1]}"`, cwd ); - assert.ok(output.includes(`Similar product information:`)); assert.ok( output.includes(