Skip to content

Commit

Permalink
test: handle untyped null parameters in Mock Spanner server (#1707)
Browse files Browse the repository at this point in the history
Support untyped null values in statement parameters for the mock Spanner
server used for testing.
  • Loading branch information
olavloite authored Feb 22, 2022
1 parent 91e157a commit 98cac5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2260,4 +2260,22 @@ public void testGetDialectPostgreSQLPreloaded() {
StatementResult.detectDialectResult(Dialect.GOOGLE_STANDARD_SQL));
}
}

@Test
public void testUntypedNullParameters() {
Statement statement =
Statement.newBuilder("INSERT INTO FOO (BAR) VALUES (@p)")
.bind("p")
.to((Value) null)
.build();
mockSpanner.putStatementResult(StatementResult.update(statement, 1L));

DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
Long updateCount =
client.readWriteTransaction().run(transaction -> transaction.executeUpdate(statement));

assertNotNull(updateCount);
assertEquals(1L, updateCount.longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,13 @@ public void executeStreamingSql(
private Statement buildStatement(
String sql, Map<String, Type> paramTypes, com.google.protobuf.Struct params) {
Statement.Builder builder = Statement.newBuilder(sql);
// Set all untyped null values first.
for (Entry<String, com.google.protobuf.Value> entry : params.getFieldsMap().entrySet()) {
if (entry.getValue().hasNullValue() && !paramTypes.containsKey(entry.getKey())) {
builder.bind(entry.getKey()).to((Value) null);
}
}

for (Entry<String, Type> entry : paramTypes.entrySet()) {
final String fieldName = entry.getKey();
final Type fieldType = entry.getValue();
Expand Down

0 comments on commit 98cac5d

Please sign in to comment.