Skip to content

Commit

Permalink
chore: refactored all glossaries samples to use predefined glossary i… (
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored and Ace Nassri committed Nov 17, 2022
1 parent dd27937 commit d1101e7
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const GLOSSARY_ID = 'DO_NET_DELETE_TEST_GLOSSARY_ES';
const REGION_TAG = 'translate_batch_translate_text_with_glossary';

async function clearBucket(projectId, storage, bucketUuid) {
Expand All @@ -42,7 +43,6 @@ async function clearBucket(projectId, storage, bucketUuid) {
describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = `my_test_glossary_${uuid.v4()}`;
const bucketUuid = uuid.v4();
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATE_WITH_GLOS_OUTPUT/`;
const storage = new Storage();
Expand All @@ -62,27 +62,6 @@ describe(REGION_TAG, () => {
console.error(error);
}
});

// Create glossary
const request = {
parent: `projects/${projectId}/locations/${location}`,
glossary: {
languageCodesSet: {
languageCodes: ['en', 'es'],
},
inputConfig: {
gcsSource: {
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
},
},
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
},
};

// Create glossary using a long-running operation.
const [operation] = await translationClient.createGlossary(request);
// Wait for operation to complete.
await operation.promise();
});

it('should batch translate the input text with a glossary', async () => {
Expand All @@ -93,7 +72,7 @@ describe(REGION_TAG, () => {

const outputUri = `gs://${projectId}/${bucketName}`;
const output = execSync(
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId}`
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${GLOSSARY_ID}`
);
assert.match(output, /Total Characters: 13/);
assert.match(output, /Translated Characters: 13/);
Expand All @@ -112,15 +91,5 @@ describe(REGION_TAG, () => {
if (length > 0) {
await Promise.all(files.map(file => file.delete()));
}

// Delete the Glossary
const request = {
parent: `projects/${projectId}/locations/${location}`,
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};
// Delete glossary using a long-running operation.
const [operation] = await translationClient.deleteGlossary(request);
// Wait for operation to complete.
await operation.promise();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const GLOSSARY_ID = 'DO_NOT_DELETE_TEST_GLOSSARY';
const REGION_TAG = 'translate_batch_translate_text_with_glossary_and_model';

async function clearBucket(projectId, storage, bucketUuid) {
Expand All @@ -42,7 +43,6 @@ async function clearBucket(projectId, storage, bucketUuid) {
describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = `my_test_glossary_${uuid.v4()}`;
const modelId = 'TRL1218052175389786112';
const bucketUuid = uuid.v4();
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATE_GLOSS_MODEL_OUTPUT/`;
Expand All @@ -63,27 +63,6 @@ describe(REGION_TAG, () => {
console.error(error);
}
});

// Create glossary
const request = {
parent: `projects/${projectId}/locations/${location}`,
glossary: {
languageCodesSet: {
languageCodes: ['en', 'ja'],
},
inputConfig: {
gcsSource: {
inputUri: 'gs://cloud-samples-data/translation/glossary_ja.csv',
},
},
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
},
};

// Create glossary using a long-running operation.
const [operation] = await translationClient.createGlossary(request);
// Wait for operation to complete.
await operation.promise();
});

it('should batch translate the input text with a glossary', async function () {
Expand All @@ -95,7 +74,7 @@ describe(REGION_TAG, () => {

const outputUri = `gs://${projectId}/${bucketName}`;
const output = execSync(
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId} ${modelId}`
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${GLOSSARY_ID} ${modelId}`
);
assert.match(output, /Total Characters: 25/);
assert.match(output, /Translated Characters: 25/);
Expand All @@ -117,15 +96,5 @@ describe(REGION_TAG, () => {
if (length > 0) {
await Promise.all(files.map(file => file.delete()));
}

// Delete the Glossary
const request = {
parent: `projects/${projectId}/locations/${location}`,
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};
// Delete glossary using a long-running operation.
const [operation] = await translationClient.deleteGlossary(request);
// Wait for operation to complete.
await operation.promise();
});
});
54 changes: 4 additions & 50 deletions translate/test/v3/translate_get_glossary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,25 @@
'use strict';

const {assert} = require('chai');
const {describe, it, before, after} = require('mocha');
const {describe, it} = require('mocha');
const {TranslationServiceClient} = require('@google-cloud/translate');
const cp = require('child_process');
const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const GLOSSARY_ID = 'DO_NOT_DELETE_TEST_GLOSSARY';
const REGION_TAG = 'translate_get_glossary';

describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = `my_test_glossary_${uuid.v4()}`;

before(async () => {
// 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: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// Construct request
const request = {
parent: `projects/${projectId}/locations/${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 () => {
const projectId = await translationClient.getProjectId();

const output = execSync(
`node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
`node v3/${REGION_TAG}.js ${projectId} ${location} ${GLOSSARY_ID}`
);
assert.match(output, /my_test_glossary_/);
});

after(async () => {
//delete the glossary we created
const projectId = await translationClient.getProjectId();
const request = {
parent: `projects/${projectId}/locations/${location}`,
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// 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();
assert.match(output, /DO_NOT_DELETE_TEST_GLOSSARY/);
});
});
51 changes: 2 additions & 49 deletions translate/test/v3/translate_list_glossary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
'use strict';

const {assert} = require('chai');
const {describe, it, before, after} = require('mocha');
const {describe, it} = require('mocha');
const {TranslationServiceClient} = require('@google-cloud/translate');
const uuid = require('uuid');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
Expand All @@ -27,37 +26,6 @@ const REGION_TAG = 'translate_list_glossary';
describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = `my_test_glossary_${uuid.v4()}`;

before(async () => {
// Add a glossary to be deleted
const projectId = await translationClient.getProjectId();

const glossary = {
languageCodesSet: {
languageCodes: ['en', 'es'],
},
inputConfig: {
gcsSource: {
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
},
},
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// Construct request
const request = {
parent: `projects/${projectId}/locations/${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 list glossaries in project', async () => {
const projectId = await translationClient.getProjectId();
Expand All @@ -68,21 +36,6 @@ describe(REGION_TAG, () => {
output,
/gs:\/\/cloud-samples-data\/translation\/glossary.csv/
);
});

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

const request = {
parent: `projects/${projectId}/locations/${location}`,
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// 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();
assert.match(output, /DO_NOT_DELETE_TEST_GLOSSARY/);
});
});
52 changes: 3 additions & 49 deletions translate/test/v3/translate_translate_text_with_glossary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,25 @@
'use strict';

const {assert} = require('chai');
const {describe, it, before, after} = require('mocha');
const {describe, it} = require('mocha');
const {TranslationServiceClient} = require('@google-cloud/translate');
const uuid = require('uuid');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const GLOSSARY_ID = 'DO_NET_DELETE_TEST_GLOSSARY_ES';
const REGION_TAG = 'translate_translate_text_with_glossary';

describe(REGION_TAG, () => {
const translationClient = new TranslationServiceClient();
const location = 'us-central1';
const glossaryId = `my_test_glossary_${uuid.v4()}`;

before(async () => {
// Add a glossary to be translate with
const projectId = await translationClient.getProjectId();

const glossary = {
languageCodesSet: {
languageCodes: ['en', 'es'],
},
inputConfig: {
gcsSource: {
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
},
},
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// Construct request
const request = {
parent: `projects/${projectId}/locations/${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 translate text with a glossary in project', async () => {
const projectId = await translationClient.getProjectId();
const input = 'directions';
const output = execSync(
`node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId} ${input}`
`node v3/${REGION_TAG}.js ${projectId} ${location} ${GLOSSARY_ID} ${input}`
);
assert.match(output, /indicaciones/);
});

after(async () => {
const projectId = await translationClient.getProjectId();
const request = {
parent: `projects/${projectId}/locations/${location}`,
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
};

// 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();
});
});
Loading

0 comments on commit d1101e7

Please sign in to comment.