Skip to content

Commit

Permalink
test: fix mock server ARRAY<BYTES> parameter cast (googleapis#2344)
Browse files Browse the repository at this point in the history
* test: fix mock server ARRAY<BYTES> parameter cast

The recent change that made decoding BYTES and ARRAY<BYTES> lazy did not
update all code paths in the mock Spanner server that is used for testing.
This meant that tests that tried to use ARRAY<BYTES> query parameters would
run into a ClassCastException. This problem only occurs in (internal) test
code and does not affect user code.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and arpan14 committed Apr 28, 2023
1 parent fb2cc33 commit d3fcb86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.cloud.spanner.MockSpannerTestUtil.READ_ONE_KEY_VALUE_STATEMENT;
import static com.google.cloud.spanner.MockSpannerTestUtil.READ_TABLE_NAME;
import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1;
import static com.google.cloud.spanner.MockSpannerTestUtil.SELECT1_RESULTSET;
import static com.google.cloud.spanner.SpannerApiFutures.get;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -2943,6 +2944,24 @@ public void testMetadataUnknownTypes() {
}
}

@Test
public void testStatementWithBytesArrayParameter() {
Statement statement =
Statement.newBuilder("select id from test where b=@p1")
.bind("p1")
.toBytesArray(
ImmutableList.of(ByteArray.copyFrom("test1"), ByteArray.copyFrom("test2")))
.build();
mockSpanner.putStatementResult(StatementResult.query(statement, SELECT1_RESULTSET));
DatabaseClient client =
spanner.getDatabaseClient(DatabaseId.of(TEST_PROJECT, TEST_INSTANCE, TEST_DATABASE));
try (ResultSet resultSet = client.singleUse().executeQuery(statement)) {
assertTrue(resultSet.next());
assertEquals(1L, resultSet.getLong(0));
assertFalse(resultSet.next());
}
}

static void assertAsString(String expected, ResultSet resultSet, int col) {
assertEquals(expected, resultSet.getValue(col).getAsString());
assertEquals(ImmutableList.of(expected), resultSet.getValue(col).getAsStringList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.spanner.AbstractResultSet.GrpcStruct;
import com.google.cloud.spanner.AbstractResultSet.LazyByteArray;
import com.google.cloud.spanner.SessionPool.SessionPoolTransactionContext;
import com.google.cloud.spanner.TransactionRunnerImpl.TransactionContextImpl;
import com.google.common.base.Optional;
Expand Down Expand Up @@ -1362,9 +1363,11 @@ private Statement buildStatement(
builder
.bind(fieldName)
.toBytesArray(
(Iterable<ByteArray>)
GrpcStruct.decodeArrayValue(
com.google.cloud.spanner.Type.bytes(), value.getListValue()));
Iterables.transform(
(Iterable<LazyByteArray>)
GrpcStruct.decodeArrayValue(
com.google.cloud.spanner.Type.bytes(), value.getListValue()),
LazyByteArray::getByteArray));
break;
case DATE:
builder
Expand Down

0 comments on commit d3fcb86

Please sign in to comment.