Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

docs(samples): Add samples for translate v3 beta #234

Merged
merged 9 commits into from
Apr 3, 2019
3 changes: 2 additions & 1 deletion samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node": ">=8"
},
"scripts": {
"test": "mocha system-test"
"test": "mocha system-test --recursive --timeout 90000"
},
"dependencies": {
"@google-cloud/translate": "^3.0.0",
Expand All @@ -18,6 +18,7 @@
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"@google-cloud/storage": "^2.4.3",
"mocha": "^6.0.0",
"uuid": "^3.3.2"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const {Storage} = require('@google-cloud/storage');
const execa = require('execa');
const uuid = require('uuid');
const exec = async cmd => (await execa.shell(cmd)).stdout;

const REGION_TAG = 'translate_batch_translate_text_beta';

describe(REGION_TAG, () => {
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const bucketUuid = uuid.v4();
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`;
const storage = new Storage();

before(async () => {
const projectId = await translationClient.getProjectId();

//Create bucket if needed
await storage
.createBucket(projectId, {
location: 'US',
storageClass: 'COLDLINE',
})
.catch(error => {
if (error.code !== 409) {
//if it's not a duplicate bucket error, let the user know
console.error(error);
}
});
});

it('should batch translate the input text', async () => {
leahecole marked this conversation as resolved.
Show resolved Hide resolved
const projectId = await translationClient.getProjectId();
const inputUri = `gs://cloud-samples-data/translation/text.txt`;

const outputUri = `gs://${projectId}/${bucketName}`;
const output = await exec(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
);
assert.match(output, /Total Characters: 13/);
assert.match(output, /Translated Characters: 13/);
});

// Delete the folder from GCS for cleanup
after(async function() {
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
const projectId = await translationClient.getProjectId();
const options = {
prefix: `translation-${bucketUuid}`,
};

const bucket = await storage.bucket(projectId);
const [files] = await bucket.getFiles(options);
const length = files.length;
if (length > 0) {
await Promise.all(files.map(file => file.delete()));
}
});
});
63 changes: 63 additions & 0 deletions samples/system-test/v3beta1/translate_create_glossary_beta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;

const REGION_TAG = 'translate_create_glossary_beta';

describe(REGION_TAG, () => {
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
const translationClient = new TranslationServiceClient();

it('should create a glossary', async function() {
const projectId = await translationClient.getProjectId();
const location = 'us-central1';
const glossaryId = 'test-glossary';
const output = await exec(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
);
assert.match(
output,
/gs:\/\/cloud-samples-data\/translation\/glossary.csv/
);
});

after('cleanup for glossary create', async function() {
const projectId = await translationClient.getProjectId();
const location = 'us-central1';
const glossaryId = 'test-glossary';
// Delete the glossary to clean up
const name = translationClient.glossaryPath(
projectId,
location,
glossaryId
);
const request = {
parent: translationClient.locationPath(projectId, location),
name: name,
};

// Delete glossary using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.deleteGlossary(request);

// Wait for operation to complete.
await operation.promise();
});
});
68 changes: 68 additions & 0 deletions samples/system-test/v3beta1/translate_delete_glossary_beta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;

const REGION_TAG = 'translate_delete_glossary_beta';

describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = 'glossary';

before(async function() {
leahecole marked this conversation as resolved.
Show resolved Hide resolved
// Add a glossary to be deleted
// const translationClient = new TranslationServiceClient();
const projectId = await translationClient.getProjectId();
const glossary = {
languageCodesSet: {
languageCodes: ['en', 'es'],
},
inputConfig: {
gcsSource: {
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
},
},
name: translationClient.glossaryPath(projectId, location, glossaryId),
};

// Construct request
const request = {
parent: translationClient.locationPath(projectId, location),
glossary: glossary,
};

// Create glossary using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.createGlossary(request);

// Wait for operation to complete.
await operation.promise();
});

it('should delete a glossary', async () => {
leahecole marked this conversation as resolved.
Show resolved Hide resolved
const projectId = await translationClient.getProjectId();

const output = await exec(
bcoe marked this conversation as resolved.
Show resolved Hide resolved
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
);
assert.match(output, /glossary/);
});
});
37 changes: 37 additions & 0 deletions samples/system-test/v3beta1/translate_detect_language_beta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;

const REGION_TAG = 'translate_detect_language_beta';

describe(REGION_TAG, () => {
it('should detect the language of the input text', async () => {
const translationClient = new TranslationServiceClient();
const projectId = await translationClient.getProjectId();
const location = 'global';
const text = `'Hæ sæta'`;
const output = await exec(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${text}`
);
assert.match(output, /Language Code: is/);
assert.match(output, /Confidence: 1/);
});
});
88 changes: 88 additions & 0 deletions samples/system-test/v3beta1/translate_get_glossary_beta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;

const REGION_TAG = 'translate_get_glossary_beta';

describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = 'test-glossary';

before(async function() {
leahecole marked this conversation as resolved.
Show resolved Hide resolved
// Add a glossary to get
const projectId = await translationClient.getProjectId();
const glossary = {
languageCodesSet: {
languageCodes: ['en', 'es'],
},
inputConfig: {
gcsSource: {
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
},
},
name: translationClient.glossaryPath(projectId, location, glossaryId),
};

// Construct request
const request = {
parent: translationClient.locationPath(projectId, location),
glossary: glossary,
};

// Create glossary using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.createGlossary(request);

// Wait for operation to complete.
await operation.promise();
});

it('should get a glossary', async () => {
leahecole marked this conversation as resolved.
Show resolved Hide resolved
const projectId = await translationClient.getProjectId();

const output = await exec(
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
);
assert.match(output, /test-glossary/);
});

after(async function() {
//delete the glossary we created
const projectId = await translationClient.getProjectId();
const name = translationClient.glossaryPath(
projectId,
location,
glossaryId
);
const request = {
parent: translationClient.locationPath(projectId, location),
name: name,
};

// Delete glossary using a long-running operation.
// You can wait for now, or get results later.
const [operation] = await translationClient.deleteGlossary(request);

// Wait for operation to complete.
await operation.promise();
});
});
33 changes: 33 additions & 0 deletions samples/system-test/v3beta1/translate_list_codes_beta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2019, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const {assert} = require('chai');
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1;
const execa = require('execa');
const exec = async cmd => (await execa.shell(cmd)).stdout;

const REGION_TAG = 'translate_list_codes_beta';

describe(REGION_TAG, () => {
it('should list available language codes', async () => {
const translationClient = new TranslationServiceClient();
const projectId = await translationClient.getProjectId();
const output = await exec(`node v3beta1/${REGION_TAG}.js ${projectId}`);
assert.match(output, /Language Code: en/);
assert.match(output, /Language Code: fr/);
});
});
Loading