Skip to content

Commit

Permalink
refactor: modernize sample tests (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Dec 18, 2018
1 parent ae040b9 commit 2c1f609
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 451 deletions.
108 changes: 31 additions & 77 deletions translate/automl/automlTranslationDataset.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,15 @@

`use strict`;

async function createDataset(
projectId,
computeRegion,
datasetName,
source,
target
) {
async function createDataset(projectId) {
// [START automl_translation_create_dataset]
const automl = require(`@google-cloud/automl`).v1beta1;
const automl = require(`@google-cloud/automl`);

const client = new automl.AutoMlClient();

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const projectId = `The GCLOUD_PROJECT string, e.g. "my-gcloud-project"`;
// const computeRegion = `region-name, e.g. "us-central1"`;
// const datasetName = `name of the dataset to create, e.g. “myDataset”`;
// const source = `source language code, e.g. "en" `;
// const target = `target language code, e.g. "ja" `;
const computeRegion = 'us-central1';
const datasetName = 'myDataset';
const source = 'en';
const target = 'ja';

// A resource that represents Google Cloud Platform location.
const projectLocation = client.locationPath(projectId, computeRegion);
Expand Down Expand Up @@ -90,8 +79,7 @@ async function createDataset(
async function listDatasets(projectId, computeRegion, filter) {
// [START automl_translation_list_datasets]
const automl = require(`@google-cloud/automl`);

const client = new automl.v1beta1.AutoMlClient();
const client = new automl.AutoMlClient();

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

// Display the dataset information.
if (datasets.length === 0) {
console.log('No datasets found!');
return;
}
console.log(`List of datasets:`);
datasets.forEach(dataset => {
console.log(`Dataset name: ${dataset.name}`);
Expand All @@ -131,14 +123,12 @@ async function listDatasets(projectId, computeRegion, filter) {
console.log(`\tseconds: ${dataset.createTime.seconds}`);
console.log(`\tnanos: ${dataset.createTime.nanos}`);
});

// [END automl_translation_list_datasets]
}

async function getDataset(projectId, computeRegion, datasetId) {
// [START automl_translation_get_dataset]
const automl = require(`@google-cloud/automl`).v1beta1;

const automl = require(`@google-cloud/automl`);
const client = new automl.AutoMlClient();

/**
Expand Down Expand Up @@ -179,7 +169,7 @@ async function getDataset(projectId, computeRegion, datasetId) {

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

const client = new automl.AutoMlClient();

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

async function deleteDataset(projectId, computeRegion, datasetId) {
// [START automl_translation_delete_dataset]
const automl = require(`@google-cloud/automl`).v1beta1;

const automl = require(`@google-cloud/automl`);
const client = new automl.AutoMlClient();

/**
Expand Down Expand Up @@ -248,7 +237,7 @@ require(`yargs`)
computeRegion: {
alias: `c`,
type: `string`,
default: process.env.REGION_NAME,
default: 'us-central1',
requiresArg: true,
description: `region name e.g. "us-central1"`,
},
Expand Down Expand Up @@ -315,61 +304,26 @@ require(`yargs`)
description: `The target language to be translated to`,
},
})
.command(
`createDataset`,
`creates a new Dataset`,
{},
async opts =>
await createDataset(
opts.projectId,
opts.computeRegion,
opts.datasetName,
opts.source,
opts.target
).catch(console.error)
.command(`createDataset`, `creates a new Dataset`, {}, opts =>
createDataset(
opts.projectId,
opts.computeRegion,
opts.datasetName,
opts.source,
opts.target
)
)
.command(
`list-datasets`,
`list all Datasets`,
{},
async opts =>
await listDatasets(opts.projectId, opts.computeRegion, opts.filter).catch(
console.error
)
.command(`list-datasets`, `list all Datasets`, {}, opts =>
listDatasets(opts.projectId, opts.computeRegion, opts.filter)
)
.command(
`get-dataset`,
`Get a Dataset`,
{},
async opts =>
await getDataset(
opts.projectId,
opts.computeRegion,
opts.datasetId
).catch(console.error)
.command(`get-dataset`, `Get a Dataset`, {}, opts =>
getDataset(opts.projectId, opts.computeRegion, opts.datasetId)
)
.command(
`delete-dataset`,
`Delete a dataset`,
{},
async opts =>
await deleteDataset(
opts.projectId,
opts.computeRegion,
opts.datasetId
).catch(console.error)
.command(`delete-dataset`, `Delete a dataset`, {}, opts =>
deleteDataset(opts.projectId, opts.computeRegion, opts.datasetId)
)
.command(
`import-data`,
`Import labeled items into dataset`,
{},
async opts =>
await importData(
opts.projectId,
opts.computeRegion,
opts.datasetId,
opts.path
).catch(console.error)
.command(`import-data`, `Import labeled items into dataset`, {}, opts =>
importData(opts.projectId, opts.computeRegion, opts.datasetId, opts.path)
)
.example(`node $0 create-dataset -n "newDataSet" -s "en" -t "ja"`)
.example(`node $0 list-datasets -f "translationDatasetMetadata:*"`)
Expand Down
74 changes: 27 additions & 47 deletions translate/automl/automlTranslationModel.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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

const client = new automl.AutoMlClient();

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

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

const client = new automl.AutoMlClient();

Expand Down Expand Up @@ -122,7 +122,7 @@ async function listModels(projectId, computeRegion, filter) {

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

const client = new automl.AutoMlClient();

Expand Down Expand Up @@ -191,7 +191,7 @@ async function getModel(projectId, computeRegion, modelId) {

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

const client = new automl.AutoMlClient();

Expand Down Expand Up @@ -227,8 +227,7 @@ async function getModelEvaluation(
) {
// [START automl_translation_get_model_evaluation]
const automl = require(`@google-cloud/automl`);

const client = new automl.v1beta1.AutoMlClient();
const client = new automl.AutoMlClient();

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

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

const client = new automl.AutoMlClient();

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

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

const client = new automl.AutoMlClient();

Expand All @@ -305,7 +304,7 @@ require(`yargs`)
computeRegion: {
alias: `c`,
type: `string`,
default: process.env.REGION_NAME,
default: 'us-central1',
requiresArg: true,
description: `region name e.g. "us-central1"`,
},
Expand Down Expand Up @@ -370,51 +369,32 @@ require(`yargs`)
`get-operation-status`,
`Gets status of current operation`,
{},
async opts =>
await getOperationStatus(opts.operationFullId).catch(console.error)
opts => getOperationStatus(opts.operationFullId)
)
.command(
`list-models`,
`list all Models`,
{},
async opts =>
await listModels(opts.projectId, opts.computeRegion, opts.filter).catch(
console.error
)
.command(`list-models`, `list all Models`, {}, opts =>
listModels(opts.projectId, opts.computeRegion, opts.filter)
)
.command(`get-model`, `Get a Model`, {}, opts =>
getModel(opts.projectId, opts.computeRegion, opts.modelId)
)
.command(
`list-model-evaluations`,
`List model evaluations`,
{},
async opts =>
await listModelEvaluations(
opts.projectId,
opts.computeRegion,
opts.modelId,
opts.filter
).catch(console.error)
.command(`list-model-evaluations`, `List model evaluations`, {}, opts =>
listModelEvaluations(
opts.projectId,
opts.computeRegion,
opts.modelId,
opts.filter
)
)
.command(
`get-model-evaluation`,
`Get model evaluation`,
{},
async opts =>
await getModelEvaluation(
opts.projectId,
opts.computeRegion,
opts.modelId,
opts.modelEvaluationId
)
.command(`get-model-evaluation`, `Get model evaluation`, {}, opts =>
getModelEvaluation(
opts.projectId,
opts.computeRegion,
opts.modelId,
opts.modelEvaluationId
)
)
.command(
`delete-model`,
`Delete a Model`,
{},
async opts =>
await deleteModel(opts.projectId, opts.computeRegion, opts.modelId)
.command(`delete-model`, `Delete a Model`, {}, opts =>
deleteModel(opts.projectId, opts.computeRegion, opts.modelId)
)
.example(`node $0 create-model -i "DatasetID" -m "myModelName"`)
.example(`node $0 get-operation-status -i "datasetId" -o "OperationFullID"`)
Expand Down
24 changes: 10 additions & 14 deletions translate/automl/automlTranslationPredict.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function predict(
translationAllowFallback
) {
// [START automl_translation_predict]
const automl = require(`@google-cloud/automl`).v1beta1;
const automl = require(`@google-cloud/automl`);
const fs = require(`fs`);

// Create client for prediction service.
Expand Down Expand Up @@ -89,7 +89,7 @@ require(`yargs`)
computeRegion: {
alias: `c`,
type: `string`,
default: process.env.REGION_NAME,
default: 'us-central1',
requiresArg: true,
description: `region name e.g. "us-central1"`,
},
Expand Down Expand Up @@ -124,18 +124,14 @@ require(`yargs`)
`serve the request. Use false to not use Google translation model.`,
},
})
.command(
`predict`,
`classify the content`,
{},
async opts =>
await predict(
opts.projectId,
opts.computeRegion,
opts.modelId,
opts.filePath,
opts.translationAllowFallback
)
.command(`predict`, `classify the content`, {}, opts =>
predict(
opts.projectId,
opts.computeRegion,
opts.modelId,
opts.filePath,
opts.translationAllowFallback
)
)
.example(
`node $0 predict -i "modelId" -f "./resources/testInput.txt" -t "False"`
Expand Down
26 changes: 0 additions & 26 deletions translate/automl/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions translate/automl/system-test/.eslintrc.yml

This file was deleted.

Loading

0 comments on commit 2c1f609

Please sign in to comment.