Skip to content

Commit 98cac5d

Browse files
authored
test: handle untyped null parameters in Mock Spanner server (#1707)
Support untyped null values in statement parameters for the mock Spanner server used for testing.
1 parent 91e157a commit 98cac5d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,4 +2260,22 @@ public void testGetDialectPostgreSQLPreloaded() {
22602260
StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL));
22612261
}
22622262
}
2263+
2264+
@Test
2265+
public void testUntypedNullParameters() {
2266+
Statement statement =
2267+
Statement.newBuilder("INSERT INTO FOO (BAR) VALUES (@p)")
2268+
.bind("p")
2269+
.to((Value) null)
2270+
.build();
2271+
mockSpanner.putStatementResult(StatementResult.update(statement, 1L));
2272+
2273+
DatabaseClient client =
2274+
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
2275+
Long updateCount =
2276+
client.readWriteTransaction().run(transaction -> transaction.executeUpdate(statement));
2277+
2278+
assertNotNull(updateCount);
2279+
assertEquals(1L, updateCount.longValue());
2280+
}
22632281
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/MockSpannerServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,13 @@ public void executeStreamingSql(
12261226
private Statement buildStatement(
12271227
String sql, Map<String, Type> paramTypes, com.google.protobuf.Struct params) {
12281228
Statement.Builder builder = Statement.newBuilder(sql);
1229+
// Set all untyped null values first.
1230+
for (Entry<String, com.google.protobuf.Value> entry : params.getFieldsMap().entrySet()) {
1231+
if (entry.getValue().hasNullValue() && !paramTypes.containsKey(entry.getKey())) {
1232+
builder.bind(entry.getKey()).to((Value) null);
1233+
}
1234+
}
1235+
12291236
for (Entry<String, Type> entry : paramTypes.entrySet()) {
12301237
final String fieldName = entry.getKey();
12311238
final Type fieldType = entry.getValue();

0 commit comments

Comments
 (0)