Skip to content

Commit 4ae0c80

Browse files
JustinBeckwithAce Nassri
authored and
Ace Nassri
committed
refactor: modernize sample tests (#199)
1 parent 2a12f48 commit 4ae0c80

11 files changed

+313
-451
lines changed

translate/automl/automlTranslationDataset.js

100755100644
+31-77
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,15 @@
2323

2424
`use strict`;
2525

26-
async function createDataset(
27-
projectId,
28-
computeRegion,
29-
datasetName,
30-
source,
31-
target
32-
) {
26+
async function createDataset(projectId) {
3327
// [START automl_translation_create_dataset]
34-
const automl = require(`@google-cloud/automl`).v1beta1;
28+
const automl = require(`@google-cloud/automl`);
3529

3630
const client = new automl.AutoMlClient();
37-
38-
/**
39-
* TODO(developer): Uncomment the following line before running the sample.
40-
*/
41-
// const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
42-
// const computeRegion = `region-name, e.g. "us-central1"`;
43-
// const datasetName = `name of the dataset to create, e.g. “myDataset”`;
44-
// const source = `source language code, e.g. "en" `;
45-
// const target = `target language code, e.g. "ja" `;
31+
const computeRegion = 'us-central1';
32+
const datasetName = 'myDataset';
33+
const source = 'en';
34+
const target = 'ja';
4635

4736
// A resource that represents Google Cloud Platform location.
4837
const projectLocation = client.locationPath(projectId, computeRegion);
@@ -90,8 +79,7 @@ async function createDataset(
9079
async function listDatasets(projectId, computeRegion, filter) {
9180
// [START automl_translation_list_datasets]
9281
const automl = require(`@google-cloud/automl`);
93-
94-
const client = new automl.v1beta1.AutoMlClient();
82+
const client = new automl.AutoMlClient();
9583

9684
/**
9785
* TODO(developer): Uncomment the following line before running the sample.
@@ -110,6 +98,10 @@ async function listDatasets(projectId, computeRegion, filter) {
11098
});
11199

112100
// Display the dataset information.
101+
if (datasets.length === 0) {
102+
console.log('No datasets found!');
103+
return;
104+
}
113105
console.log(`List of datasets:`);
114106
datasets.forEach(dataset => {
115107
console.log(`Dataset name: ${dataset.name}`);
@@ -131,14 +123,12 @@ async function listDatasets(projectId, computeRegion, filter) {
131123
console.log(`\tseconds: ${dataset.createTime.seconds}`);
132124
console.log(`\tnanos: ${dataset.createTime.nanos}`);
133125
});
134-
135126
// [END automl_translation_list_datasets]
136127
}
137128

138129
async function getDataset(projectId, computeRegion, datasetId) {
139130
// [START automl_translation_get_dataset]
140-
const automl = require(`@google-cloud/automl`).v1beta1;
141-
131+
const automl = require(`@google-cloud/automl`);
142132
const client = new automl.AutoMlClient();
143133

144134
/**
@@ -179,7 +169,7 @@ async function getDataset(projectId, computeRegion, datasetId) {
179169

180170
async function importData(projectId, computeRegion, datasetId, path) {
181171
// [START automl_translation_import_data]
182-
const automl = require(`@google-cloud/automl`).v1beta1;
172+
const automl = require(`@google-cloud/automl`);
183173

184174
const client = new automl.AutoMlClient();
185175

@@ -219,8 +209,7 @@ async function importData(projectId, computeRegion, datasetId, path) {
219209

220210
async function deleteDataset(projectId, computeRegion, datasetId) {
221211
// [START automl_translation_delete_dataset]
222-
const automl = require(`@google-cloud/automl`).v1beta1;
223-
212+
const automl = require(`@google-cloud/automl`);
224213
const client = new automl.AutoMlClient();
225214

226215
/**
@@ -248,7 +237,7 @@ require(`yargs`)
248237
computeRegion: {
249238
alias: `c`,
250239
type: `string`,
251-
default: process.env.REGION_NAME,
240+
default: 'us-central1',
252241
requiresArg: true,
253242
description: `region name e.g. "us-central1"`,
254243
},
@@ -315,61 +304,26 @@ require(`yargs`)
315304
description: `The target language to be translated to`,
316305
},
317306
})
318-
.command(
319-
`createDataset`,
320-
`creates a new Dataset`,
321-
{},
322-
async opts =>
323-
await createDataset(
324-
opts.projectId,
325-
opts.computeRegion,
326-
opts.datasetName,
327-
opts.source,
328-
opts.target
329-
).catch(console.error)
307+
.command(`createDataset`, `creates a new Dataset`, {}, opts =>
308+
createDataset(
309+
opts.projectId,
310+
opts.computeRegion,
311+
opts.datasetName,
312+
opts.source,
313+
opts.target
314+
)
330315
)
331-
.command(
332-
`list-datasets`,
333-
`list all Datasets`,
334-
{},
335-
async opts =>
336-
await listDatasets(opts.projectId, opts.computeRegion, opts.filter).catch(
337-
console.error
338-
)
316+
.command(`list-datasets`, `list all Datasets`, {}, opts =>
317+
listDatasets(opts.projectId, opts.computeRegion, opts.filter)
339318
)
340-
.command(
341-
`get-dataset`,
342-
`Get a Dataset`,
343-
{},
344-
async opts =>
345-
await getDataset(
346-
opts.projectId,
347-
opts.computeRegion,
348-
opts.datasetId
349-
).catch(console.error)
319+
.command(`get-dataset`, `Get a Dataset`, {}, opts =>
320+
getDataset(opts.projectId, opts.computeRegion, opts.datasetId)
350321
)
351-
.command(
352-
`delete-dataset`,
353-
`Delete a dataset`,
354-
{},
355-
async opts =>
356-
await deleteDataset(
357-
opts.projectId,
358-
opts.computeRegion,
359-
opts.datasetId
360-
).catch(console.error)
322+
.command(`delete-dataset`, `Delete a dataset`, {}, opts =>
323+
deleteDataset(opts.projectId, opts.computeRegion, opts.datasetId)
361324
)
362-
.command(
363-
`import-data`,
364-
`Import labeled items into dataset`,
365-
{},
366-
async opts =>
367-
await importData(
368-
opts.projectId,
369-
opts.computeRegion,
370-
opts.datasetId,
371-
opts.path
372-
).catch(console.error)
325+
.command(`import-data`, `Import labeled items into dataset`, {}, opts =>
326+
importData(opts.projectId, opts.computeRegion, opts.datasetId, opts.path)
373327
)
374328
.example(`node $0 create-dataset -n "newDataSet" -s "en" -t "ja"`)
375329
.example(`node $0 list-datasets -f "translationDatasetMetadata:*"`)

translate/automl/automlTranslationModel.js

100755100644
+27-47
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
async function createModel(projectId, computeRegion, datasetId, modelName) {
2727
// [START automl_translation_create_model]
28-
const automl = require(`@google-cloud/automl`).v1beta1;
28+
const automl = require(`@google-cloud/automl`);
2929

3030
const client = new automl.AutoMlClient();
3131

@@ -80,7 +80,7 @@ async function createModel(projectId, computeRegion, datasetId, modelName) {
8080

8181
async function listModels(projectId, computeRegion, filter) {
8282
// [START automl_translation_list_models]
83-
const automl = require(`@google-cloud/automl`).v1beta1;
83+
const automl = require(`@google-cloud/automl`);
8484

8585
const client = new automl.AutoMlClient();
8686

@@ -122,7 +122,7 @@ async function listModels(projectId, computeRegion, filter) {
122122

123123
async function getModel(projectId, computeRegion, modelId) {
124124
// [START automl_translation_get_model]
125-
const automl = require(`@google-cloud/automl`).v1beta1;
125+
const automl = require(`@google-cloud/automl`);
126126

127127
const client = new automl.AutoMlClient();
128128

@@ -191,7 +191,7 @@ async function getModel(projectId, computeRegion, modelId) {
191191

192192
async function listModelEvaluations(projectId, computeRegion, modelId, filter) {
193193
// [START automl_translation_list_model_evaluations]
194-
const automl = require(`@google-cloud/automl`).v1beta1;
194+
const automl = require(`@google-cloud/automl`);
195195

196196
const client = new automl.AutoMlClient();
197197

@@ -227,8 +227,7 @@ async function getModelEvaluation(
227227
) {
228228
// [START automl_translation_get_model_evaluation]
229229
const automl = require(`@google-cloud/automl`);
230-
231-
const client = new automl.v1beta1.AutoMlClient();
230+
const client = new automl.AutoMlClient();
232231

233232
/**
234233
* TODO(developer): Uncomment the following line before running the sample.
@@ -257,7 +256,7 @@ async function getModelEvaluation(
257256

258257
async function deleteModel(projectId, computeRegion, modelId) {
259258
// [START automl_translation_delete_model]
260-
const automl = require(`@google-cloud/automl`).v1beta1;
259+
const automl = require(`@google-cloud/automl`);
261260

262261
const client = new automl.AutoMlClient();
263262

@@ -282,7 +281,7 @@ async function deleteModel(projectId, computeRegion, modelId) {
282281

283282
async function getOperationStatus(operationFullId) {
284283
// [START automl_translation_get_operation_status]
285-
const automl = require(`@google-cloud/automl`).v1beta1;
284+
const automl = require(`@google-cloud/automl`);
286285

287286
const client = new automl.AutoMlClient();
288287

@@ -305,7 +304,7 @@ require(`yargs`)
305304
computeRegion: {
306305
alias: `c`,
307306
type: `string`,
308-
default: process.env.REGION_NAME,
307+
default: 'us-central1',
309308
requiresArg: true,
310309
description: `region name e.g. "us-central1"`,
311310
},
@@ -370,51 +369,32 @@ require(`yargs`)
370369
`get-operation-status`,
371370
`Gets status of current operation`,
372371
{},
373-
async opts =>
374-
await getOperationStatus(opts.operationFullId).catch(console.error)
372+
opts => getOperationStatus(opts.operationFullId)
375373
)
376-
.command(
377-
`list-models`,
378-
`list all Models`,
379-
{},
380-
async opts =>
381-
await listModels(opts.projectId, opts.computeRegion, opts.filter).catch(
382-
console.error
383-
)
374+
.command(`list-models`, `list all Models`, {}, opts =>
375+
listModels(opts.projectId, opts.computeRegion, opts.filter)
384376
)
385377
.command(`get-model`, `Get a Model`, {}, opts =>
386378
getModel(opts.projectId, opts.computeRegion, opts.modelId)
387379
)
388-
.command(
389-
`list-model-evaluations`,
390-
`List model evaluations`,
391-
{},
392-
async opts =>
393-
await listModelEvaluations(
394-
opts.projectId,
395-
opts.computeRegion,
396-
opts.modelId,
397-
opts.filter
398-
).catch(console.error)
380+
.command(`list-model-evaluations`, `List model evaluations`, {}, opts =>
381+
listModelEvaluations(
382+
opts.projectId,
383+
opts.computeRegion,
384+
opts.modelId,
385+
opts.filter
386+
)
399387
)
400-
.command(
401-
`get-model-evaluation`,
402-
`Get model evaluation`,
403-
{},
404-
async opts =>
405-
await getModelEvaluation(
406-
opts.projectId,
407-
opts.computeRegion,
408-
opts.modelId,
409-
opts.modelEvaluationId
410-
)
388+
.command(`get-model-evaluation`, `Get model evaluation`, {}, opts =>
389+
getModelEvaluation(
390+
opts.projectId,
391+
opts.computeRegion,
392+
opts.modelId,
393+
opts.modelEvaluationId
394+
)
411395
)
412-
.command(
413-
`delete-model`,
414-
`Delete a Model`,
415-
{},
416-
async opts =>
417-
await deleteModel(opts.projectId, opts.computeRegion, opts.modelId)
396+
.command(`delete-model`, `Delete a Model`, {}, opts =>
397+
deleteModel(opts.projectId, opts.computeRegion, opts.modelId)
418398
)
419399
.example(`node $0 create-model -i "DatasetID" -m "myModelName"`)
420400
.example(`node $0 get-operation-status -i "datasetId" -o "OperationFullID"`)

translate/automl/automlTranslationPredict.js

100755100644
+10-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function predict(
3131
translationAllowFallback
3232
) {
3333
// [START automl_translation_predict]
34-
const automl = require(`@google-cloud/automl`).v1beta1;
34+
const automl = require(`@google-cloud/automl`);
3535
const fs = require(`fs`);
3636

3737
// Create client for prediction service.
@@ -89,7 +89,7 @@ require(`yargs`)
8989
computeRegion: {
9090
alias: `c`,
9191
type: `string`,
92-
default: process.env.REGION_NAME,
92+
default: 'us-central1',
9393
requiresArg: true,
9494
description: `region name e.g. "us-central1"`,
9595
},
@@ -124,18 +124,14 @@ require(`yargs`)
124124
`serve the request. Use false to not use Google translation model.`,
125125
},
126126
})
127-
.command(
128-
`predict`,
129-
`classify the content`,
130-
{},
131-
async opts =>
132-
await predict(
133-
opts.projectId,
134-
opts.computeRegion,
135-
opts.modelId,
136-
opts.filePath,
137-
opts.translationAllowFallback
138-
)
127+
.command(`predict`, `classify the content`, {}, opts =>
128+
predict(
129+
opts.projectId,
130+
opts.computeRegion,
131+
opts.modelId,
132+
opts.filePath,
133+
opts.translationAllowFallback
134+
)
139135
)
140136
.example(
141137
`node $0 predict -i "modelId" -f "./resources/testInput.txt" -t "False"`

translate/automl/package.json

-26
This file was deleted.

translate/automl/system-test/.eslintrc.yml

-5
This file was deleted.

0 commit comments

Comments
 (0)