Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions functions/ocr/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ function renameImageForSave (filename, lang) {
* a file is uploaded to the Cloud Storage bucket you created
* for uploading images.
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data A Google Cloud Storage File object.
* @param {object} event.data (Node 6) A Google Cloud Storage File object.
* @param {object} event (Node 8+) A Google Cloud Storage File object.
*/
exports.processImage = (event) => {
let file = event.data;
let file = event.data || event;

return Promise.resolve()
.then(() => {
Expand Down Expand Up @@ -143,14 +143,14 @@ exports.processImage = (event) => {
* by the TRANSLATE_TOPIC value in the config.json file. The
* function translates text using the Google Translate API.
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data The Cloud Pub/Sub Message object.
* @param {string} event.data.data The "data" property of the Cloud Pub/Sub
* @param {object} event.data (Node 6) The Cloud Pub/Sub Message object.
* @param {object} event (Node 8+) The Cloud Pub/Sub Message object.
* @param {string} {messageObject}.data The "data" property of the Cloud Pub/Sub
* Message. This property will be a base64-encoded string that you must decode.
*/
exports.translateText = (event) => {
const pubsubMessage = event.data;
const jsonStr = Buffer.from(pubsubMessage.data, 'base64').toString();
const pubsubData = event.data.data || event.data;
const jsonStr = Buffer.from(pubsubData, 'base64').toString();
const payload = JSON.parse(jsonStr);

return Promise.resolve()
Expand Down Expand Up @@ -195,14 +195,14 @@ exports.translateText = (event) => {
* by the RESULT_TOPIC value in the config.json file. The
* function saves the data packet to a file in GCS.
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data The Cloud Pub/Sub Message object.
* @param {string} event.data.data The "data" property of the Cloud Pub/Sub
* @param {object} event.data (Node 6) The Cloud Pub/Sub Message object.
* @param {object} event (Node 8+) The Cloud Pub/Sub Message object.
* @param {string} {messageObject}.data The "data" property of the Cloud Pub/Sub
* Message. This property will be a base64-encoded string that you must decode.
*/
exports.saveResult = (event) => {
const pubsubMessage = event.data;
const jsonStr = Buffer.from(pubsubMessage.data, 'base64').toString();
const pubsubData = event.data.data || event.data;
const jsonStr = Buffer.from(pubsubData, 'base64').toString();
const payload = JSON.parse(jsonStr);

return Promise.resolve()
Expand Down
2 changes: 1 addition & 1 deletion functions/ocr/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"node": ">=4.3.2"
},
"scripts": {
"lint": "samples lint",
"lint": "repo-tools lint",
"pretest": "npm run lint",
"test": "ava -T 20s --verbose test/*.test.js"
},
Expand Down
54 changes: 51 additions & 3 deletions functions/ocr/app/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test.serial(`processImage fails without a name`, async (t) => {
t.deepEqual(err, error);
});

test.serial(`processImage processes an image`, async (t) => {
test.serial(`processImage processes an image with Node 6 arguments`, async (t) => {
const event = {
data: {
bucket: bucketName,
Expand All @@ -123,6 +123,21 @@ test.serial(`processImage processes an image`, async (t) => {
t.deepEqual(console.log.getCall(3).args, [`File ${event.data.name} processed.`]);
});

test.serial(`processImage processes an image with Node 8 arguments`, async (t) => {
const data = {
bucket: bucketName,
name: filename
};
const sample = getSample();

await sample.program.processImage(data);
t.is(console.log.callCount, 4);
t.deepEqual(console.log.getCall(0).args, [`Looking for text in image ${filename}`]);
t.deepEqual(console.log.getCall(1).args, [`Extracted text from image (${text.length} chars)`]);
t.deepEqual(console.log.getCall(2).args, [`Detected language "ja" for ${filename}`]);
t.deepEqual(console.log.getCall(3).args, [`File ${data.name} processed.`]);
});

test.serial(`translateText fails without text`, async (t) => {
const error = new Error(`Text not provided. Make sure you have a "text" property in your request`);
const event = {
Expand Down Expand Up @@ -157,7 +172,7 @@ test.serial(`translateText fails without a lang`, async (t) => {
t.deepEqual(err, error);
});

test.serial(`translateText translates and publishes text`, async (t) => {
test.serial(`translateText translates and publishes text with Node 6 arguments`, async (t) => {
const event = {
data: {
data: Buffer.from(
Expand All @@ -179,6 +194,26 @@ test.serial(`translateText translates and publishes text`, async (t) => {
t.deepEqual(console.log.secondCall.args, [`Text translated to ${lang}`]);
});

test.serial(`translateText translates and publishes text with Node 8 arguments`, async (t) => {
const data = {
data: Buffer.from(
JSON.stringify({
text,
filename,
lang
})
).toString(`base64`)
};
const sample = getSample();

sample.mocks.translate.translate.returns(Promise.resolve([translation]));

await sample.program.translateText(data);
t.is(console.log.callCount, 2);
t.deepEqual(console.log.firstCall.args, [`Translating text into ${lang}`]);
t.deepEqual(console.log.secondCall.args, [`Text translated to ${lang}`]);
});

test.serial(`saveResult fails without text`, async (t) => {
const error = new Error(`Text not provided. Make sure you have a "text" property in your request`);
const event = {
Expand Down Expand Up @@ -215,7 +250,7 @@ test.serial(`saveResult fails without a lang`, async (t) => {
t.deepEqual(err, error);
});

test.serial(`saveResult translates and publishes text`, async (t) => {
test.serial(`saveResult translates and publishes text with Node 6 arguments`, async (t) => {
const event = {
data: {
data: Buffer.from(JSON.stringify({ text, filename, lang })).toString(`base64`)
Expand All @@ -230,6 +265,19 @@ test.serial(`saveResult translates and publishes text`, async (t) => {
t.deepEqual(console.log.getCall(2).args, [`File saved.`]);
});

test.serial(`saveResult translates and publishes text with Node 8 arguments`, async (t) => {
const data = {
data: Buffer.from(JSON.stringify({ text, filename, lang })).toString(`base64`)
};
const sample = getSample();

await sample.program.saveResult(data);
t.is(console.log.callCount, 3);
t.deepEqual(console.log.getCall(0).args, [`Received request to save file ${filename}`]);
t.deepEqual(console.log.getCall(1).args, [`Saving result to ${filename}_to_${lang}.txt in bucket ${sample.mocks.config.RESULT_BUCKET}`]);
t.deepEqual(console.log.getCall(2).args, [`File saved.`]);
});

test.serial(`saveResult translates and publishes text with dot in filename`, async (t) => {
const event = {
data: {
Expand Down