This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathReadFormatTestRunner.java
More file actions
273 lines (251 loc) · 9.84 KB
/
Copy pathReadFormatTestRunner.java
File metadata and controls
273 lines (251 loc) · 9.84 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2017 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.google.cloud.spanner;
import static com.google.common.truth.Truth.assertThat;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.cloud.ByteArray;
import com.google.cloud.spanner.spi.v1.SpannerRpc;
import com.google.common.io.Resources;
import com.google.protobuf.util.JsonFormat;
import com.google.spanner.v1.MultiplexedSessionPrecommitToken;
import com.google.spanner.v1.PartialResultSet;
import com.google.spanner.v1.Transaction;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.InitializationError;
/** Test runner that runs tests specified in json file */
public class ReadFormatTestRunner extends ParentRunner<JSONObject> {
private static class NoOpListener implements AbstractResultSet.Listener {
@Override
public void onTransactionMetadata(Transaction transaction, boolean shouldIncludeId)
throws SpannerException {}
@Override
public SpannerException onError(
SpannerException e, boolean withBeginTransaction, boolean lastStatement) {
return e;
}
@Override
public void onDone(boolean withBeginTransaction) {}
@Override
public void onPrecommitToken(MultiplexedSessionPrecommitToken token) {}
}
public ReadFormatTestRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
@Override
protected Description describeChild(JSONObject child) {
try {
return Description.createTestDescription(
getTestClass().getJavaClass(), child.getString("name"));
} catch (JSONException e) {
throw new IllegalStateException("Illegal json object: " + child.toString(), e);
}
}
@Override
protected void runChild(JSONObject child, RunNotifier notifier) {
EachTestNotifier eachNotifier = new EachTestNotifier(notifier, describeChild(child));
eachNotifier.fireTestStarted();
try {
new TestCaseRunner(child).run();
eachNotifier.fireTestFinished();
} catch (Exception | AssertionError e) {
eachNotifier.addFailure(e);
}
}
@Override
protected List<JSONObject> getChildren() {
try {
List<JSONObject> children = new ArrayList<>();
String jsonStr =
Resources.toString(
Resources.getResource(this.getClass(), "read_tests.json"), StandardCharsets.UTF_8);
JSONObject json = new JSONObject(jsonStr);
JSONArray testCases = json.getJSONArray("tests");
for (int i = 0; i < testCases.length(); i++) {
JSONObject testCase = testCases.getJSONObject(i);
children.add(testCase);
}
return children;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private static class TestCaseRunner {
private GrpcResultSet resultSet;
private SpannerRpc.ResultStreamConsumer consumer;
private GrpcStreamIterator stream;
private JSONObject testCase;
TestCaseRunner(JSONObject testCase) {
this.testCase = testCase;
}
private void run() throws Exception {
stream =
new GrpcStreamIterator(
/* lastStatement= */ false, 10, /* cancelQueryWhenClientIsClosed= */ false);
stream.setCall(
new SpannerRpc.StreamingCall() {
@Override
public ApiCallContext getCallContext() {
return GrpcCallContext.createDefault();
}
@Override
public void cancel(@Nullable String message) {}
@Override
public void request(int numMessages) {}
},
false);
consumer = stream.consumer();
resultSet = new GrpcResultSet(stream, new NoOpListener());
JSONArray chunks = testCase.getJSONArray("chunks");
JSONObject expectedResult = testCase.getJSONObject("result");
for (int i = 0; i < chunks.length(); i++) {
PartialResultSet.Builder builder = PartialResultSet.newBuilder();
JsonFormat.parser().merge(chunks.getString(i), builder);
consumer.onPartialResultSet(builder.build());
}
consumer.onCompleted();
assertResultSet(resultSet, expectedResult.getJSONArray("value"));
}
private void assertResultSet(GrpcResultSet actual, JSONArray expected) throws Exception {
int i = 0;
while (actual.next()) {
Struct actualRow = actual.getCurrentRowAsStruct();
JSONArray expectedRow = expected.getJSONArray(i);
assertRow(actualRow, expectedRow);
i++;
}
assertThat(i).isEqualTo(expected.length());
}
private void assertRow(Struct actualRow, JSONArray expectedRow) throws Exception {
assertThat(actualRow.getColumnCount()).isEqualTo(expectedRow.length());
for (int i = 0; i < expectedRow.length(); i++) {
switch (actualRow.getColumnType(i).getCode()) {
case BOOL:
assertThat(actualRow.getBoolean(i)).isEqualTo(expectedRow.getBoolean(i));
break;
case STRING:
assertThat(actualRow.getString(i)).isEqualTo(expectedRow.getString(i));
break;
case JSON:
assertThat(actualRow.getJson(i)).isEqualTo(expectedRow.getString(i));
break;
case INT64:
assertThat(actualRow.getLong(i)).isEqualTo(expectedRow.getLong(i));
break;
case FLOAT32:
assertThat(actualRow.getFloat(i)).isEqualTo(expectedRow.getFloat(i));
break;
case FLOAT64:
assertThat(actualRow.getDouble(i)).isEqualTo(expectedRow.getDouble(i));
break;
case NUMERIC:
assertThat(actualRow.getBigDecimal(i)).isEqualTo(expectedRow.getBigDecimal(i));
break;
case BYTES:
assertThat(actualRow.getBytes(i))
.isEqualTo(ByteArray.fromBase64(expectedRow.getString(i)));
break;
case ARRAY:
Type elementType = actualRow.getColumnType(i).getArrayElementType();
assertArray(getRawList(actualRow, i, elementType), expectedRow.getJSONArray(i));
break;
default:
Assert.fail("Unexpected type code:" + actualRow.getColumnType(i).getCode());
}
}
}
private List<?> getRawList(Struct actualRow, int index, Type elementType) {
List<?> rawList = null;
switch (elementType.getCode()) {
case BOOL:
rawList = actualRow.getBooleanList(index);
break;
case STRING:
rawList = actualRow.getStringList(index);
break;
case JSON:
rawList = actualRow.getJsonList(index);
break;
case BYTES:
rawList = actualRow.getBytesList(index);
break;
case INT64:
rawList = actualRow.getLongList(index);
break;
case FLOAT32:
rawList = actualRow.getFloatList(index);
break;
case FLOAT64:
rawList = actualRow.getDoubleList(index);
break;
case NUMERIC:
rawList = actualRow.getBigDecimalList(index);
break;
case STRUCT:
rawList = actualRow.getStructList(index);
break;
default:
Assert.fail("Unexpected type code:" + elementType.getCode());
}
return rawList;
}
private void assertArray(List<?> actualValues, JSONArray expectedList) throws Exception {
assertThat(actualValues.size()).isEqualTo(expectedList.length());
for (int i = 0; i < actualValues.size(); i++) {
Object actualValue = actualValues.get(i);
if (actualValue == null) {
assertThat(expectedList.isNull(i)).isTrue();
} else {
if (actualValue instanceof Boolean) {
assertThat((Boolean) actualValue).isEqualTo(expectedList.getBoolean(i));
} else if (actualValue instanceof String) {
assertThat((String) actualValue).isEqualTo(expectedList.getString(i));
} else if (actualValue instanceof Long) {
assertThat((Long) actualValue).isEqualTo(expectedList.getLong(i));
} else if (actualValue instanceof Double) {
// "Infinity" is not a valid parseable json double value
if (expectedList.get(i) instanceof String) {
assertThat((Double) actualValue).isEqualTo(Double.valueOf(expectedList.getString(i)));
} else {
assertThat((Double) actualValue).isEqualTo(expectedList.getDouble(i));
}
} else if (actualValue instanceof BigDecimal) {
assertThat((BigDecimal) actualValue).isEqualTo(expectedList.getBigDecimal(i));
} else if (actualValue instanceof ByteArray) {
assertThat((ByteArray) actualValue)
.isEqualTo(ByteArray.fromBase64(expectedList.getString(i)));
} else if (actualValue instanceof Struct) {
Struct actualStruct = (Struct) actualValue;
JSONArray expectedFields = expectedList.getJSONArray(i);
assertRow(actualStruct, expectedFields);
}
}
}
}
}
}