-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: automl: create model tests (#1933)
* automl: add create model tests * Use a fake model to speed up batch predict test * Update method names and clean up Translate model test * Rename translate model * Run code formatter * Import order * copy paste typo * License year * Fix sample error, fix typos in test * lint: line length
- Loading branch information
1 parent
b6fa1b9
commit cd35d2d
Showing
9 changed files
with
561 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ml/snippets/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.automl; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class LanguageEntityExtractionCreateModelTest { | ||
|
||
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); | ||
private static final String DATASET_ID = "TEN0000000000000000000"; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
"Environment variable '%s' is required to perform these tests.".format(varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("AUTOML_PROJECT_ID"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testLanguageEntityExtractionCreateModel() { | ||
// As entity extraction does not let you cancel model creation, instead try to create a model | ||
// from a nonexistent dataset, but other elements of the request were valid. | ||
try { | ||
// Create a random dataset name with a length of 32 characters (max allowed by AutoML) | ||
// To prevent name collisions when running tests in multiple java versions at once. | ||
// AutoML doesn't allow "-", but accepts "_" | ||
String modelName = | ||
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); | ||
LanguageEntityExtractionCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); | ||
String got = bout.toString(); | ||
assertThat(got).contains("Dataset does not exist"); | ||
} catch (IOException | ExecutionException | InterruptedException e) { | ||
assertThat(e.getMessage()).contains("Dataset does not exist"); | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...l/snippets/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.automl; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import com.google.cloud.automl.v1.AutoMlClient; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class LanguageSentimentAnalysisCreateModelTest { | ||
|
||
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); | ||
private static final String DATASET_ID = System.getenv("SENTIMENT_ANALYSIS_DATASET_ID"); | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private String operationId; | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
"Environment variable '%s' is required to perform these tests.".format(varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("AUTOML_PROJECT_ID"); | ||
requireEnvVar("SENTIMENT_ANALYSIS_DATASET_ID"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
// Cancel the operation | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
client.getOperationsClient().cancelOperation(operationId); | ||
} | ||
|
||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testLanguageSentimentAnalysisCreateModel() | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Create a random dataset name with a length of 32 characters (max allowed by AutoML) | ||
// To prevent name collisions when running tests in multiple java versions at once. | ||
// AutoML doesn't allow "-", but accepts "_" | ||
String modelName = | ||
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); | ||
LanguageSentimentAnalysisCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); | ||
|
||
String got = bout.toString(); | ||
assertThat(got).contains("Training started"); | ||
|
||
operationId = got.split("Training operation name: ")[1].split("\n")[0]; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
.../snippets/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.example.automl; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
import com.google.cloud.automl.v1.AutoMlClient; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class LanguageTextClassificationCreateModelTest { | ||
|
||
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); | ||
private static final String DATASET_ID = System.getenv("TEXT_CLASSIFICATION_DATASET_ID"); | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
private String operationId; | ||
|
||
private static void requireEnvVar(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
"Environment variable '%s' is required to perform these tests.".format(varName)); | ||
} | ||
|
||
@BeforeClass | ||
public static void checkRequirements() { | ||
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
requireEnvVar("AUTOML_PROJECT_ID"); | ||
requireEnvVar("TEXT_CLASSIFICATION_DATASET_ID"); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
// Cancel the operation | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
client.getOperationsClient().cancelOperation(operationId); | ||
} | ||
|
||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testLanguageTextClassificationCreateModel() | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Create a random dataset name with a length of 32 characters (max allowed by AutoML) | ||
// To prevent name collisions when running tests in multiple java versions at once. | ||
// AutoML doesn't allow "-", but accepts "_" | ||
String modelName = | ||
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); | ||
LanguageTextClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); | ||
|
||
String got = bout.toString(); | ||
assertThat(got).contains("Training started"); | ||
|
||
operationId = got.split("Training operation name: ")[1].split("\n")[0]; | ||
} | ||
} |
Oops, something went wrong.