Skip to content

Commit 1edb853

Browse files
munkhuushmglAce Nassri
authored andcommitted
chore: refactored all glossaries samples to use predefined glossary i… (#665)
1 parent edc19dc commit 1edb853

7 files changed

+19
-264
lines changed

translate/test/v3/translate_batch_translate_text_with_glossary.test.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const uuid = require('uuid');
2323

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

26+
const GLOSSARY_ID = 'DO_NET_DELETE_TEST_GLOSSARY_ES';
2627
const REGION_TAG = 'translate_batch_translate_text_with_glossary';
2728

2829
async function clearBucket(projectId, storage, bucketUuid) {
@@ -42,7 +43,6 @@ async function clearBucket(projectId, storage, bucketUuid) {
4243
describe(REGION_TAG, () => {
4344
const translationClient = new TranslationServiceClient();
4445
const location = 'us-central1';
45-
const glossaryId = `my_test_glossary_${uuid.v4()}`;
4646
const bucketUuid = uuid.v4();
4747
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATE_WITH_GLOS_OUTPUT/`;
4848
const storage = new Storage();
@@ -62,27 +62,6 @@ describe(REGION_TAG, () => {
6262
console.error(error);
6363
}
6464
});
65-
66-
// Create glossary
67-
const request = {
68-
parent: `projects/${projectId}/locations/${location}`,
69-
glossary: {
70-
languageCodesSet: {
71-
languageCodes: ['en', 'es'],
72-
},
73-
inputConfig: {
74-
gcsSource: {
75-
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
76-
},
77-
},
78-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
79-
},
80-
};
81-
82-
// Create glossary using a long-running operation.
83-
const [operation] = await translationClient.createGlossary(request);
84-
// Wait for operation to complete.
85-
await operation.promise();
8665
});
8766

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

9473
const outputUri = `gs://${projectId}/${bucketName}`;
9574
const output = execSync(
96-
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId}`
75+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${GLOSSARY_ID}`
9776
);
9877
assert.match(output, /Total Characters: 13/);
9978
assert.match(output, /Translated Characters: 13/);
@@ -112,15 +91,5 @@ describe(REGION_TAG, () => {
11291
if (length > 0) {
11392
await Promise.all(files.map(file => file.delete()));
11493
}
115-
116-
// Delete the Glossary
117-
const request = {
118-
parent: `projects/${projectId}/locations/${location}`,
119-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
120-
};
121-
// Delete glossary using a long-running operation.
122-
const [operation] = await translationClient.deleteGlossary(request);
123-
// Wait for operation to complete.
124-
await operation.promise();
12594
});
12695
});

translate/test/v3/translate_batch_translate_text_with_glossary_and_model.test.js

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const uuid = require('uuid');
2323

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

26+
const GLOSSARY_ID = 'DO_NOT_DELETE_TEST_GLOSSARY';
2627
const REGION_TAG = 'translate_batch_translate_text_with_glossary_and_model';
2728

2829
async function clearBucket(projectId, storage, bucketUuid) {
@@ -42,7 +43,6 @@ async function clearBucket(projectId, storage, bucketUuid) {
4243
describe(REGION_TAG, () => {
4344
const translationClient = new TranslationServiceClient();
4445
const location = 'us-central1';
45-
const glossaryId = `my_test_glossary_${uuid.v4()}`;
4646
const modelId = 'TRL1218052175389786112';
4747
const bucketUuid = uuid.v4();
4848
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATE_GLOSS_MODEL_OUTPUT/`;
@@ -63,27 +63,6 @@ describe(REGION_TAG, () => {
6363
console.error(error);
6464
}
6565
});
66-
67-
// Create glossary
68-
const request = {
69-
parent: `projects/${projectId}/locations/${location}`,
70-
glossary: {
71-
languageCodesSet: {
72-
languageCodes: ['en', 'ja'],
73-
},
74-
inputConfig: {
75-
gcsSource: {
76-
inputUri: 'gs://cloud-samples-data/translation/glossary_ja.csv',
77-
},
78-
},
79-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
80-
},
81-
};
82-
83-
// Create glossary using a long-running operation.
84-
const [operation] = await translationClient.createGlossary(request);
85-
// Wait for operation to complete.
86-
await operation.promise();
8766
});
8867

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

9675
const outputUri = `gs://${projectId}/${bucketName}`;
9776
const output = execSync(
98-
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId} ${modelId}`
77+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${GLOSSARY_ID} ${modelId}`
9978
);
10079
assert.match(output, /Total Characters: 25/);
10180
assert.match(output, /Translated Characters: 25/);
@@ -117,15 +96,5 @@ describe(REGION_TAG, () => {
11796
if (length > 0) {
11897
await Promise.all(files.map(file => file.delete()));
11998
}
120-
121-
// Delete the Glossary
122-
const request = {
123-
parent: `projects/${projectId}/locations/${location}`,
124-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
125-
};
126-
// Delete glossary using a long-running operation.
127-
const [operation] = await translationClient.deleteGlossary(request);
128-
// Wait for operation to complete.
129-
await operation.promise();
13099
});
131100
});

translate/test/v3/translate_get_glossary.test.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,25 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const {describe, it, before, after} = require('mocha');
18+
const {describe, it} = require('mocha');
1919
const {TranslationServiceClient} = require('@google-cloud/translate');
2020
const cp = require('child_process');
21-
const uuid = require('uuid');
2221

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

24+
const GLOSSARY_ID = 'DO_NOT_DELETE_TEST_GLOSSARY';
2525
const REGION_TAG = 'translate_get_glossary';
2626

2727
describe(REGION_TAG, () => {
2828
const translationClient = new TranslationServiceClient();
2929
const location = 'us-central1';
30-
const glossaryId = `my_test_glossary_${uuid.v4()}`;
31-
32-
before(async () => {
33-
// Add a glossary to get
34-
const projectId = await translationClient.getProjectId();
35-
const glossary = {
36-
languageCodesSet: {
37-
languageCodes: ['en', 'es'],
38-
},
39-
inputConfig: {
40-
gcsSource: {
41-
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
42-
},
43-
},
44-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
45-
};
46-
47-
// Construct request
48-
const request = {
49-
parent: `projects/${projectId}/locations/${location}`,
50-
glossary: glossary,
51-
};
52-
53-
// Create glossary using a long-running operation.
54-
// You can wait for now, or get results later.
55-
const [operation] = await translationClient.createGlossary(request);
56-
57-
// Wait for operation to complete.
58-
await operation.promise();
59-
});
6030

6131
it('should get a glossary', async () => {
6232
const projectId = await translationClient.getProjectId();
6333

6434
const output = execSync(
65-
`node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
35+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${GLOSSARY_ID}`
6636
);
67-
assert.match(output, /my_test_glossary_/);
68-
});
69-
70-
after(async () => {
71-
//delete the glossary we created
72-
const projectId = await translationClient.getProjectId();
73-
const request = {
74-
parent: `projects/${projectId}/locations/${location}`,
75-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
76-
};
77-
78-
// Delete glossary using a long-running operation.
79-
// You can wait for now, or get results later.
80-
const [operation] = await translationClient.deleteGlossary(request);
81-
82-
// Wait for operation to complete.
83-
await operation.promise();
37+
assert.match(output, /DO_NOT_DELETE_TEST_GLOSSARY/);
8438
});
8539
});

translate/test/v3/translate_list_glossary.test.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const {describe, it, before, after} = require('mocha');
18+
const {describe, it} = require('mocha');
1919
const {TranslationServiceClient} = require('@google-cloud/translate');
20-
const uuid = require('uuid');
2120
const cp = require('child_process');
2221

2322
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
@@ -27,37 +26,6 @@ const REGION_TAG = 'translate_list_glossary';
2726
describe(REGION_TAG, () => {
2827
const translationClient = new TranslationServiceClient();
2928
const location = 'us-central1';
30-
const glossaryId = `my_test_glossary_${uuid.v4()}`;
31-
32-
before(async () => {
33-
// Add a glossary to be deleted
34-
const projectId = await translationClient.getProjectId();
35-
36-
const glossary = {
37-
languageCodesSet: {
38-
languageCodes: ['en', 'es'],
39-
},
40-
inputConfig: {
41-
gcsSource: {
42-
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
43-
},
44-
},
45-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
46-
};
47-
48-
// Construct request
49-
const request = {
50-
parent: `projects/${projectId}/locations/${location}`,
51-
glossary: glossary,
52-
};
53-
54-
// Create glossary using a long-running operation.
55-
// You can wait for now, or get results later.
56-
const [operation] = await translationClient.createGlossary(request);
57-
58-
// Wait for operation to complete.
59-
await operation.promise();
60-
});
6129

6230
it('should list glossaries in project', async () => {
6331
const projectId = await translationClient.getProjectId();
@@ -68,21 +36,6 @@ describe(REGION_TAG, () => {
6836
output,
6937
/gs:\/\/cloud-samples-data\/translation\/glossary.csv/
7038
);
71-
});
72-
73-
after(async () => {
74-
const projectId = await translationClient.getProjectId();
75-
76-
const request = {
77-
parent: `projects/${projectId}/locations/${location}`,
78-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
79-
};
80-
81-
// Delete glossary using a long-running operation.
82-
// You can wait for now, or get results later.
83-
const [operation] = await translationClient.deleteGlossary(request);
84-
85-
// Wait for operation to complete.
86-
await operation.promise();
39+
assert.match(output, /DO_NOT_DELETE_TEST_GLOSSARY/);
8740
});
8841
});

translate/test/v3/translate_translate_text_with_glossary.test.js

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,25 @@
1515
'use strict';
1616

1717
const {assert} = require('chai');
18-
const {describe, it, before, after} = require('mocha');
18+
const {describe, it} = require('mocha');
1919
const {TranslationServiceClient} = require('@google-cloud/translate');
20-
const uuid = require('uuid');
2120
const cp = require('child_process');
2221

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

24+
const GLOSSARY_ID = 'DO_NET_DELETE_TEST_GLOSSARY_ES';
2525
const REGION_TAG = 'translate_translate_text_with_glossary';
2626

2727
describe(REGION_TAG, () => {
2828
const translationClient = new TranslationServiceClient();
2929
const location = 'us-central1';
30-
const glossaryId = `my_test_glossary_${uuid.v4()}`;
31-
32-
before(async () => {
33-
// Add a glossary to be translate with
34-
const projectId = await translationClient.getProjectId();
35-
36-
const glossary = {
37-
languageCodesSet: {
38-
languageCodes: ['en', 'es'],
39-
},
40-
inputConfig: {
41-
gcsSource: {
42-
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
43-
},
44-
},
45-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
46-
};
47-
48-
// Construct request
49-
const request = {
50-
parent: `projects/${projectId}/locations/${location}`,
51-
glossary: glossary,
52-
};
53-
54-
// Create glossary using a long-running operation.
55-
// You can wait for now, or get results later.
56-
const [operation] = await translationClient.createGlossary(request);
57-
58-
// Wait for operation to complete.
59-
await operation.promise();
60-
});
6130

6231
it('should translate text with a glossary in project', async () => {
6332
const projectId = await translationClient.getProjectId();
6433
const input = 'directions';
6534
const output = execSync(
66-
`node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId} ${input}`
35+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${GLOSSARY_ID} ${input}`
6736
);
6837
assert.match(output, /indicaciones/);
6938
});
70-
71-
after(async () => {
72-
const projectId = await translationClient.getProjectId();
73-
const request = {
74-
parent: `projects/${projectId}/locations/${location}`,
75-
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
76-
};
77-
78-
// Delete glossary using a long-running operation.
79-
// You can wait for now, or get results later.
80-
const [operation] = await translationClient.deleteGlossary(request);
81-
82-
// Wait for operation to complete.
83-
await operation.promise();
84-
});
8539
});

0 commit comments

Comments
 (0)