/
SpannerStandaloneExamplesIT.java
221 lines (206 loc) · 8.19 KB
/
SpannerStandaloneExamplesIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright 2020 Google Inc.
*
* 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.spanner;
import static com.google.common.truth.Truth.assertThat;
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Instance;
import com.google.cloud.spanner.InstanceId;
import com.google.cloud.spanner.KeySet;
import com.google.cloud.spanner.Mutation;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Integration tests for Cloud Spanner cloud client examples. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public class SpannerStandaloneExamplesIT {
// The instance needs to exist for tests to pass.
private static String instanceId = System.getProperty("spanner.test.instance");
private static String baseDatabaseId = System.getProperty("spanner.sample.database", "mysample");
private static String databaseId = SpannerSampleIT.formatForTest(baseDatabaseId);
private static DatabaseId dbId;
private static DatabaseAdminClient dbClient;
private static Spanner spanner;
private String runExample(Runnable example) {
PrintStream stdOut = System.out;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bout);
System.setOut(out);
example.run();
System.setOut(stdOut);
return bout.toString();
}
@BeforeClass
public static void createTestDatabase() throws Exception {
SpannerOptions options =
SpannerOptions.newBuilder().setAutoThrottleAdministrativeRequests().build();
spanner = options.getService();
dbClient = spanner.getDatabaseAdminClient();
if (instanceId == null) {
Iterator<Instance> iterator =
spanner.getInstanceAdminClient().listInstances().iterateAll().iterator();
if (iterator.hasNext()) {
instanceId = iterator.next().getId().getInstance();
}
}
dbId = DatabaseId.of(options.getProjectId(), instanceId, databaseId);
dbClient
.createDatabase(
instanceId,
databaseId,
ImmutableList.of(
"CREATE TABLE Singers ("
+ " SingerId INT64 NOT NULL,"
+ " FirstName STRING(1024),"
+ " LastName STRING(1024),"
+ " SingerInfo BYTES(MAX)"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Venues ("
+ "VenueId INT64 NOT NULL,"
+ "Revenue NUMERIC"
+ ") PRIMARY KEY (VenueId)"))
.get();
}
@AfterClass
public static void dropTestDatabase() throws Exception {
dbClient.dropDatabase(dbId.getInstanceId().getInstance(), dbId.getDatabase());
spanner.close();
}
@Before
public void deleteTestData() {
String projectId = spanner.getOptions().getProjectId();
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
client.write(Collections.singleton(Mutation.delete("Singers", KeySet.all())));
client.write(Collections.singleton(Mutation.delete("Venues", KeySet.all())));
}
@Test
public void executeSqlWithCustomTimeoutAndRetrySettings_shouldWriteData() {
String projectId = spanner.getOptions().getProjectId();
String out =
runExample(
() ->
CustomTimeoutAndRetrySettingsExample.executeSqlWithCustomTimeoutAndRetrySettings(
projectId, instanceId, databaseId));
assertThat(out).contains("1 record inserted.");
}
@Test
public void executeSqlWithTimeout_shouldWriteData() {
String projectId = spanner.getOptions().getProjectId();
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
String out = runExample(() -> StatementTimeoutExample.executeSqlWithTimeout(client));
assertThat(out).contains("1 record inserted.");
}
@Test
public void addNumericColumn_shouldSuccessfullyAddColumn()
throws InterruptedException, ExecutionException {
OperationFuture<Void, UpdateDatabaseDdlMetadata> operation =
spanner
.getDatabaseAdminClient()
.updateDatabaseDdl(
instanceId,
databaseId,
ImmutableList.of("ALTER TABLE Venues DROP COLUMN Revenue"),
null);
operation.get();
String out =
runExample(
() -> {
try {
AddNumericColumnSample.addNumericColumn(
spanner.getDatabaseAdminClient(), instanceId, databaseId);
} catch (ExecutionException e) {
System.out.printf(
"Adding column `Revenue` failed: %s%n", e.getCause().getMessage());
} catch (InterruptedException e) {
System.out.printf("Adding column `Revenue` was interrupted%n");
}
});
assertThat(out).contains("Successfully added column `Revenue`");
}
@Test
public void updateNumericData_shouldWriteData() {
String projectId = spanner.getOptions().getProjectId();
String out =
runExample(
() ->
UpdateNumericDataSample.updateNumericData(
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId))));
assertThat(out).contains("Venues successfully updated");
}
@Test
public void queryWithNumericParameter_shouldReturnResults() {
String projectId = spanner.getOptions().getProjectId();
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
client.write(
ImmutableList.of(
Mutation.newInsertOrUpdateBuilder("Venues")
.set("VenueId")
.to(4L)
.set("Revenue")
.to(new BigDecimal("35000"))
.build(),
Mutation.newInsertOrUpdateBuilder("Venues")
.set("VenueId")
.to(19L)
.set("Revenue")
.to(new BigDecimal("104500"))
.build(),
Mutation.newInsertOrUpdateBuilder("Venues")
.set("VenueId")
.to(42L)
.set("Revenue")
.to(new BigDecimal("99999999999999999999999999999.99"))
.build()));
String out =
runExample(() -> QueryWithNumericParameterSample.queryWithNumericParameter(client));
assertThat(out).contains("4 35000");
}
@Test
public void createInstanceWithProcessingUnits_shouldReturnNumberOfProcessingUnits() {
String projectId = spanner.getOptions().getProjectId();
String instanceId = SpannerSampleIT.formatForTest("inst-pu");
try {
String out =
runExample(() -> CreateInstanceWithProcessingUnitsExample.createInstance(projectId, instanceId));
assertThat(out)
.contains(
String.format(
"Instance %s was successfully created with %d processing units",
InstanceId.of(projectId, instanceId), 200));
} finally {
spanner.getInstanceAdminClient().deleteInstance(instanceId);
}
}
}