Skip to content

Commit da51a9b

Browse files
nnegreyShabirmean
authored andcommitted
samples: automl: break up and simplify the dataset tests (#1930)
* automl: break up and simplify dataset tests * remove bom from automl until bom is released with v1 of client library * Fix assert statement * Update license years and clean up comments * Remove score_threshold from Batch Predict * Fix typo in test * Switch tests to use centralized automl project, temporarily ignore long running tests until fixed in following PRs * Fix which project is used * Fix bucket path typo * lint: remove extra semi-colon
1 parent eb358ff commit da51a9b

30 files changed

+910
-900
lines changed

automl/snippets/src/main/java/com/example/automl/BatchPredict.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ static void batchPredict(String projectId, String modelId, String inputUri, Stri
6262
.setName(name.toString())
6363
.setInputConfig(inputConfig)
6464
.setOutputConfig(outputConfig)
65-
// [0.0-1.0] Only produce results higher than this value
66-
.putParams("score_threshold", "0.8")
6765
.build();
6866

6967
OperationFuture<BatchPredictResult, OperationMetadata> future =
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import java.util.concurrent.ExecutionException;
27+
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37+
public class DeleteDatasetTest {
38+
39+
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
private String datasetId;
43+
44+
private static void requireEnvVar(String varName) {
45+
assertNotNull(
46+
System.getenv(varName),
47+
"Environment variable '%s' is required to perform these tests.".format(varName));
48+
}
49+
50+
@BeforeClass
51+
public static void checkRequirements() {
52+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
53+
requireEnvVar("AUTOML_PROJECT_ID");
54+
}
55+
56+
@Before
57+
public void setUp() throws InterruptedException, ExecutionException, IOException {
58+
bout = new ByteArrayOutputStream();
59+
out = new PrintStream(bout);
60+
System.setOut(out);
61+
62+
// Create a fake dataset to be deleted
63+
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
64+
// To prevent name collisions when running tests in multiple java versions at once.
65+
// AutoML doesn't allow "-", but accepts "_"
66+
String datasetName =
67+
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
68+
LanguageEntityExtractionCreateDataset.createDataset(PROJECT_ID, datasetName);
69+
String got = bout.toString();
70+
datasetId = got.split("Dataset id: ")[1].split("\n")[0];
71+
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
System.setOut(out);
75+
}
76+
77+
@After
78+
public void tearDown() {
79+
System.setOut(null);
80+
}
81+
82+
@Test
83+
public void testDeleteDataset() throws IOException, ExecutionException, InterruptedException {
84+
DeleteDataset.deleteDataset(PROJECT_ID, datasetId);
85+
String got = bout.toString();
86+
assertThat(got).contains("Dataset deleted.");
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Google LLC
2+
* Copyright 2020 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@
2727
import java.io.ByteArrayOutputStream;
2828
import java.io.IOException;
2929
import java.io.PrintStream;
30-
import java.util.UUID;
3130
import java.util.concurrent.ExecutionException;
3231

3332
import org.junit.After;
@@ -37,29 +36,28 @@
3736
import org.junit.runner.RunWith;
3837
import org.junit.runners.JUnit4;
3938

40-
// Tests for Automl natural language text classification datasets.
4139
@RunWith(JUnit4.class)
4240
@SuppressWarnings("checkstyle:abbreviationaswordinname")
43-
public class LanguageTextClassificationDatasetManagementIT {
41+
public class ExportDatasetTest {
4442

45-
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
43+
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
44+
private static final String DATASET_ID = System.getenv("ENTITY_EXTRACTION_DATASET_ID");
4645
private static final String BUCKET_ID = PROJECT_ID + "-lcm";
4746
private static final String BUCKET = "gs://" + BUCKET_ID;
4847
private ByteArrayOutputStream bout;
4948
private PrintStream out;
50-
private String getdatasetId = "TCN2551826603472450019";
5149

5250
private static void requireEnvVar(String varName) {
5351
assertNotNull(
54-
System.getenv(varName),
55-
"Environment variable '%s' is required to perform these tests.".format(varName)
56-
);
52+
System.getenv(varName),
53+
"Environment variable '%s' is required to perform these tests.".format(varName));
5754
}
5855

5956
@BeforeClass
6057
public static void checkRequirements() {
6158
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
62-
requireEnvVar("GOOGLE_CLOUD_PROJECT");
59+
requireEnvVar("AUTOML_PROJECT_ID");
60+
requireEnvVar("ENTITY_EXTRACTION_DATASET_ID");
6361
}
6462

6563
@Before
@@ -71,68 +69,7 @@ public void setUp() {
7169

7270
@After
7371
public void tearDown() {
74-
System.setOut(null);
75-
}
76-
77-
@Test
78-
public void testCreateImportDeleteDataset()
79-
throws IOException, ExecutionException, InterruptedException {
80-
// Create a random dataset name with a length of 32 characters (max allowed by AutoML)
81-
// To prevent name collisions when running tests in multiple java versions at once.
82-
// AutoML doesn't allow "-", but accepts "_"
83-
String datasetName =
84-
String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26));
85-
86-
// Act
87-
LanguageTextClassificationCreateDataset.createDataset(PROJECT_ID, datasetName);
88-
89-
// Assert
90-
String got = bout.toString();
91-
String datasetId = got.split("Dataset id: ")[1].split("\n")[0];
92-
93-
// Act
94-
ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/happiness.csv");
95-
96-
// Assert
97-
got = bout.toString();
98-
assertThat(got).contains("Dataset id:");
99-
100-
// Act
101-
DeleteDataset.deleteDataset(PROJECT_ID, datasetId);
102-
103-
// Assert
104-
got = bout.toString();
105-
assertThat(got).contains("Dataset deleted.");
106-
}
107-
108-
@Test
109-
public void testListDataset() throws IOException {
110-
// Act
111-
ListDatasets.listDatasets(PROJECT_ID);
112-
113-
// Assert
114-
String got = bout.toString();
115-
assertThat(got).contains("Dataset id:");
116-
}
117-
118-
@Test
119-
public void testGetDataset() throws IOException {
120-
// Act
121-
GetDataset.getDataset(PROJECT_ID, getdatasetId);
122-
123-
// Assert
124-
String got = bout.toString();
125-
126-
assertThat(got).contains("Dataset id:");
127-
}
128-
129-
@Test
130-
public void testExportDataset() throws IOException, ExecutionException, InterruptedException {
131-
ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/");
132-
133-
String got = bout.toString();
134-
assertThat(got).contains("Dataset exported.");
135-
72+
// Delete the created files from GCS
13673
Storage storage = StorageOptions.getDefaultInstance().getService();
13774
Page<Blob> blobs =
13875
storage.list(
@@ -152,5 +89,14 @@ public void testExportDataset() throws IOException, ExecutionException, Interrup
15289
}
15390
}
15491
}
92+
93+
System.setOut(null);
94+
}
95+
96+
@Test
97+
public void testExportDataset() throws IOException, ExecutionException, InterruptedException {
98+
ExportDataset.exportDataset(PROJECT_ID, DATASET_ID, BUCKET + "/TEST_EXPORT_OUTPUT/");
99+
String got = bout.toString();
100+
assertThat(got).contains("Dataset exported.");
155101
}
156102
}

automl/snippets/src/test/java/com/example/automl/GenericModelManagementIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// Tests for Automl models.
3535
@RunWith(JUnit4.class)
3636
public class GenericModelManagementIT {
37-
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
3838
private String modelId;
3939
private String modelEvaluationId;
4040
private ByteArrayOutputStream bout;
@@ -50,7 +50,7 @@ private static void requireEnvVar(String varName) {
5050
@BeforeClass
5151
public static void checkRequirements() {
5252
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
53-
requireEnvVar("GOOGLE_CLOUD_PROJECT");
53+
requireEnvVar("AUTOML_PROJECT_ID");
5454
}
5555

5656
@Before
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.JUnit4;
32+
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class GetDatasetTest {
36+
37+
private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID");
38+
private static final String DATASET_ID = System.getenv("ENTITY_EXTRACTION_DATASET_ID");
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(
44+
System.getenv(varName),
45+
"Environment variable '%s' is required to perform these tests.".format(varName));
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
51+
requireEnvVar("AUTOML_PROJECT_ID");
52+
requireEnvVar("ENTITY_EXTRACTION_DATASET_ID");
53+
}
54+
55+
@Before
56+
public void setUp() {
57+
bout = new ByteArrayOutputStream();
58+
out = new PrintStream(bout);
59+
System.setOut(out);
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
System.setOut(null);
65+
}
66+
67+
@Test
68+
public void testGetDataset() throws IOException {
69+
GetDataset.getDataset(PROJECT_ID, DATASET_ID);
70+
String got = bout.toString();
71+
assertThat(got).contains("Dataset id:");
72+
}
73+
}

0 commit comments

Comments
 (0)