diff --git a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResultSetsTest.java b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResultSetsTest.java index 361ade38e09c..c5830ab591d8 100644 --- a/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResultSetsTest.java +++ b/google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResultSetsTest.java @@ -17,7 +17,14 @@ package com.google.cloud.spanner; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; +import com.google.cloud.ByteArray; +import com.google.cloud.Date; +import com.google.cloud.Timestamp; +import com.google.common.primitives.Booleans; +import com.google.common.primitives.Doubles; +import com.google.common.primitives.Longs; import java.util.Arrays; import java.util.List; import org.junit.Rule; @@ -34,11 +41,51 @@ public class ResultSetsTest { @Test public void resultSetIteration() { + double doubleVal = 1.2; + String stringVal = "stringVal"; + String byteVal = "101"; + long usecs = 32343; + int year = 2018; + int month = 5; + int day = 26; + boolean[] boolArray = { true, false, true, true, false }; + long[] longArray = { Long.MAX_VALUE, Long.MIN_VALUE, 0, 1, -1 }; + double[] doubleArray = { Double.MIN_VALUE, Double.MAX_VALUE, 0, 1, -1, 1.2341 }; + ByteArray[] byteArray = { + ByteArray.copyFrom("123"), + ByteArray.copyFrom("456"), + ByteArray.copyFrom("789") + }; + Timestamp[] timestampArray = { + Timestamp.ofTimeMicroseconds(101), + Timestamp.ofTimeMicroseconds(202), + Timestamp.ofTimeMicroseconds(303) + }; + Date[] dateArray = { + Date.fromYearMonthDay(1, 2, 3), + Date.fromYearMonthDay(4, 5, 6), + Date.fromYearMonthDay(7, 8, 9) + }; + String[] stringArray = { "abc", "def", "ghi" }; + Type type = Type.struct( Type.StructField.of("f1", Type.string()), Type.StructField.of("f2", Type.int64()), - Type.StructField.of("f3", Type.bool())); + Type.StructField.of("f3", Type.bool()), + Type.StructField.of("doubleVal", Type.float64()), + Type.StructField.of("stringVal", Type.string()), + Type.StructField.of("byteVal", Type.bytes()), + Type.StructField.of("timestamp", Type.timestamp()), + Type.StructField.of("date", Type.date()), + Type.StructField.of("boolArray", Type.array(Type.bool())), + Type.StructField.of("longArray", Type.array(Type.int64())), + Type.StructField.of("doubleArray", Type.array(Type.float64())), + Type.StructField.of("byteArray", Type.array(Type.bytes())), + Type.StructField.of("timestampArray", Type.array(Type.timestamp())), + Type.StructField.of("dateArray", Type.array(Type.date())), + Type.StructField.of("stringArray", Type.array(Type.string())) + ); Struct struct1 = Struct.newBuilder() .set("f1") @@ -47,6 +94,30 @@ public void resultSetIteration() { .to(2) .set("f3") .to(Value.bool(true)) + .set("doubleVal") + .to(Value.float64(doubleVal)) + .set("stringVal") + .to(stringVal) + .set("byteVal") + .to(Value.bytes(ByteArray.copyFrom(byteVal))) + .set("timestamp") + .to(Timestamp.ofTimeMicroseconds(usecs)) + .set("date") + .to(Date.fromYearMonthDay(year, month, day)) + .set("boolArray") + .to(Value.boolArray(boolArray)) + .set("longArray") + .to(Value.int64Array(longArray)) + .set("doubleArray") + .to(Value.float64Array(doubleArray)) + .set("byteArray") + .to(Value.bytesArray(Arrays.asList(byteArray))) + .set("timestampArray") + .to(Value.timestampArray(Arrays.asList(timestampArray))) + .set("dateArray") + .to(Value.dateArray(Arrays.asList(dateArray))) + .set("stringArray") + .to(Value.stringArray(Arrays.asList(stringArray))) .build(); Struct struct2 = Struct.newBuilder() @@ -56,13 +127,46 @@ public void resultSetIteration() { .to(3) .set("f3") .to(Value.bool(null)) + .set("doubleVal") + .to(Value.float64(doubleVal)) + .set("stringVal") + .to(stringVal) + .set("byteVal") + .to(Value.bytes(ByteArray.copyFrom(byteVal))) + .set("timestamp") + .to(Timestamp.ofTimeMicroseconds(usecs)) + .set("date") + .to(Date.fromYearMonthDay(year, month, day)) + .set("boolArray") + .to(Value.boolArray(boolArray)) + .set("longArray") + .to(Value.int64Array(longArray)) + .set("doubleArray") + .to(Value.float64Array(doubleArray)) + .set("byteArray") + .to(Value.bytesArray(Arrays.asList(byteArray))) + .set("timestampArray") + .to(Value.timestampArray(Arrays.asList(timestampArray))) + .set("dateArray") + .to(Value.dateArray(Arrays.asList(dateArray))) + .set("stringArray") + .to(Value.stringArray(Arrays.asList(stringArray))) .build(); ResultSet rs = ResultSets.forRows(type, Arrays.asList(struct1, struct2)); + try { + rs.getType(); + fail("Exception expected"); + } catch(IllegalStateException e) { + assertThat(e.getMessage()) + .contains("Must be preceded by a next() call"); + } + assertThat(rs.next()).isTrue(); assertThat(rs.getType()).isEqualTo(type); - assertThat(rs.getColumnCount()).isEqualTo(3); + assertThat(rs.getColumnCount()).isEqualTo(type.getStructFields().size()); assertThat(rs.getColumnIndex("f1")).isEqualTo(0); + assertThat(rs.getColumnType("nonexistent")).isNull(); assertThat(rs.getColumnType("f1")).isEqualTo(Type.string()); assertThat(rs.getColumnType(0)).isEqualTo(Type.string()); assertThat(rs.getColumnIndex("f2")).isEqualTo(1); @@ -75,12 +179,52 @@ public void resultSetIteration() { assertThat(rs.getString(0)).isEqualTo("x"); assertThat(rs.getLong(1)).isEqualTo(2L); assertThat(rs.getBoolean(2)).isTrue(); + assertThat(rs.getBoolean("f3")).isTrue(); + assertThat(rs.getDouble("doubleVal")).isWithin(0.0).of(doubleVal); + assertThat(rs.getDouble(3)).isWithin(0.0).of(doubleVal); + assertThat(rs.getString(4)).isEqualTo(stringVal); + assertThat(rs.getString("stringVal")).isEqualTo(stringVal); + assertThat(rs.getBytes(5)).isEqualTo(ByteArray.copyFrom(byteVal)); + assertThat(rs.getBytes("byteVal")).isEqualTo(ByteArray.copyFrom(byteVal)); + assertThat(rs.getTimestamp(6)).isEqualTo(Timestamp.ofTimeMicroseconds(usecs)); + assertThat(rs.getTimestamp("timestamp")).isEqualTo(Timestamp.ofTimeMicroseconds(usecs)); + assertThat(rs.getDate(7)).isEqualTo(Date.fromYearMonthDay(year, month, day)); + assertThat(rs.getDate("date")).isEqualTo(Date.fromYearMonthDay(year, month, day)); + assertThat(rs.getBooleanArray(8)).isEqualTo(boolArray); + assertThat(rs.getBooleanArray("boolArray")).isEqualTo(boolArray); + assertThat(rs.getBooleanList(8)).isEqualTo(Booleans.asList(boolArray)); + assertThat(rs.getBooleanList("boolArray")).isEqualTo(Booleans.asList(boolArray)); + assertThat(rs.getLongArray(9)).isEqualTo(longArray); + assertThat(rs.getLongArray("longArray")).isEqualTo(longArray); + assertThat(rs.getLongList(9)).isEqualTo(Longs.asList(longArray)); + assertThat(rs.getLongList("longArray")).isEqualTo(Longs.asList(longArray)); + assertThat(rs.getDoubleArray(10)).isEqualTo(doubleArray, 0.0); + assertThat(rs.getDoubleArray("doubleArray")).isEqualTo(doubleArray, 0.0); + assertThat(rs.getDoubleList(10)).isEqualTo(Doubles.asList(doubleArray)); + assertThat(rs.getDoubleList("doubleArray")).isEqualTo(Doubles.asList(doubleArray)); + assertThat(rs.getBytesList(11)).isEqualTo(Arrays.asList(byteArray)); + assertThat(rs.getBytesList("byteArray")).isEqualTo(Arrays.asList(byteArray)); + assertThat(rs.getTimestampList(12)).isEqualTo(Arrays.asList(timestampArray)); + assertThat(rs.getTimestampList("timestampArray")).isEqualTo(Arrays.asList(timestampArray)); + assertThat(rs.getDateList(13)).isEqualTo(Arrays.asList(dateArray)); + assertThat(rs.getDateList("dateArray")).isEqualTo(Arrays.asList(dateArray)); + assertThat(rs.getStringList(14)).isEqualTo(Arrays.asList(stringArray)); + assertThat(rs.getStringList("stringArray")).isEqualTo(Arrays.asList(stringArray)); + assertThat(rs.next()).isTrue(); assertThat(rs.getCurrentRowAsStruct()).isEqualTo(struct2); assertThat(rs.getString(0)).isEqualTo("y"); assertThat(rs.getLong(1)).isEqualTo(3L); assertThat(rs.isNull(2)).isTrue(); assertThat(rs.next()).isFalse(); + + try { + rs.getStats(); + fail("Exception expected"); + } catch(UnsupportedOperationException e) { + assertThat(e.getMessage()) + .contains("ResultSetStats are available only for results returned from analyzeQuery"); + } } @Test