private Struct execute(Statement statement, Type expectedColumnType) { Type rowType = Type.struct(StructField.of("", expectedColumnType)); return executeWithRowResultType(statement, rowType); }
@Test public void getterWhenNull() throws Exception { Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type))); Mockito.when(reader.isNull(0)).thenReturn(true); expectedException.expect(NullPointerException.class); getterByIndex(0); }
@Test public void getterByNameWhenNull() throws Exception { Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type))); Mockito.when(reader.isNull(0)).thenReturn(true); expectedException.expect(NullPointerException.class); getterByName("F1"); } }
@Test public void emptyStruct() { Type t = Type.struct(); assertThat(t.getCode()).isEqualTo(Type.Code.STRUCT); assertThat(t.getStructFields()).isEmpty(); assertThat(t.toString()).isEqualTo("STRUCT<>"); assertProtoEquals(t.toProto(), "code: STRUCT struct_type {}"); }
@Test public void arrayOfStructEmpty() { Type structType = Type.struct(StructField.of("", Type.string()), StructField.of("", Type.int64())); Struct row = execute( Statement.of("SELECT ARRAY(SELECT AS STRUCT * FROM (SELECT 'a', 1) WHERE 0 = 1)"), Type.array(structType)); assertThat(row.isNull(0)).isFalse(); List<Struct> value = row.getStructList(0); assertThat(value.size()).isEqualTo(0); }
@Test public void analyzePlan() { Statement statement = Statement.of("SELECT 1 AS column UNION ALL SELECT 2"); ResultSet resultSet = statement.analyzeQuery(client.singleUse(TimestampBound.strong()), QueryAnalyzeMode.PLAN); assertThat(resultSet.next()).isFalse(); assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("column", Type.int64()))); ResultSetStats receivedStats = resultSet.getStats(); assertThat(receivedStats).isNotNull(); assertThat(receivedStats.hasQueryPlan()).isTrue(); assertThat(receivedStats.hasQueryStats()).isFalse(); }
@Test public void structFieldIndexNotFound() { Type t = Type.struct(StructField.of("f1", Type.int64())); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Field not found: f2"); t.getFieldIndex("f2"); }
@Test public void bindStructWithNullStructField() { Type emptyStructType = Type.struct(new ArrayList<StructField>()); Struct p = Struct.newBuilder().set("f1").to(emptyStructType, null).build(); Struct row = execute(Statement.newBuilder("SELECT @p.f1 IS NULL").bind("p").to(p).build(), Type.bool()); assertThat(row.getBoolean(0)).isTrue(); }
@Test public void exceptionIfNextIsNotCalled() { ResultSet rs = ResultSets.forRows( Type.struct(Type.StructField.of("f1", Type.string())), Arrays.asList(Struct.newBuilder().set("f1").to("x").build())); expected.expect(IllegalStateException.class); rs.getCurrentRowAsStruct(); } }
@Test public void empty() { Type type = Type.struct(Type.StructField.of("f", Type.string())); PartialResultSet partialResultSet = PartialResultSet.newBuilder().setMetadata(makeMetadata(type)).build(); consumer.onPartialResultSet(partialResultSet); consumer.onCompleted(); assertThat(resultSet.next()).isFalse(); assertThat(resultSet.getType()).isEqualTo(type); }
@Test public void closeResultSet() { ResultSet rs = ResultSets.forRows( Type.struct(Type.StructField.of("f1", Type.string())), Arrays.asList(Struct.newBuilder().set("f1").to("x").build())); rs.close(); expected.expect(IllegalStateException.class); rs.getCurrentRowAsStruct(); }
@Test public void unnamedFields() { Struct struct = Struct.newBuilder().add(Value.int64(2)).add(Value.int64(3)).build(); assertThat(struct.getType()) .isEqualTo( Type.struct( Type.StructField.of("", Type.int64()), Type.StructField.of("", Type.int64()))); assertThat(struct.getLong(0)).isEqualTo(2); assertThat(struct.getLong(1)).isEqualTo(3); }
@Ignore // Not yet supported by the backend. @Test public void arrayOfStructNull() { Type structType = Type.struct(StructField.of("", Type.string()), StructField.of("", Type.int64())); Struct row = execute( Statement.of("SELECT CAST (NULL AS ARRAY<STRUCT<string,int64>>)"), Type.array(structType)); assertThat(row.isNull(0)).isTrue(); }
@Test public void structFieldIndexAmbiguous() { Type t = Type.struct(StructField.of("f1", Type.int64()), StructField.of("f1", Type.string())); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Ambiguous field name: f1"); t.getFieldIndex("f1"); }
@Test public void resultSetIterationWithStructColumns() { Type nestedStructType = Type.struct(Type.StructField.of("g1", Type.string())); Type type = Type.struct( Type.StructField.of("f1", nestedStructType), Type.StructField.of("f2", Type.int64())); Struct value1 = Struct.newBuilder().set("g1").to("abc").build(); Struct struct1 = Struct.newBuilder().set("f1").to(value1).set("f2").to((Long) null).build(); expected.expect(UnsupportedOperationException.class); expected.expectMessage("STRUCT-typed columns are not supported inside ResultSets."); ResultSets.forRows(type, Arrays.asList(struct1)); }
@Test public void getDate() { consumer.onPartialResultSet( PartialResultSet.newBuilder() .setMetadata(makeMetadata(Type.struct(Type.StructField.of("f", Type.date())))) .addValues(Value.date(Date.fromYearMonthDay(2018, 5, 29)).toProto()) .build()); consumer.onCompleted(); assertThat(resultSet.next()).isTrue(); assertThat(resultSet.getDate(0)).isEqualTo(Date.fromYearMonthDay(2018, 5, 29)); }
@Test public void getTimestamp() { consumer.onPartialResultSet( PartialResultSet.newBuilder() .setMetadata(makeMetadata(Type.struct(Type.StructField.of("f", Type.timestamp())))) .addValues(Value.timestamp(Timestamp.parseTimestamp("0001-01-01T00:00:00Z")).toProto()) .build()); consumer.onCompleted(); assertThat(resultSet.next()).isTrue(); assertThat(resultSet.getTimestamp(0)) .isEqualTo(Timestamp.parseTimestamp("0001-01-01T00:00:00Z")); }
@Test public void emptyMultipleResponses() { PartialResultSet partialResultSet = PartialResultSet.newBuilder() .setMetadata(makeMetadata(Type.struct(Type.StructField.of("f", Type.string())))) .build(); consumer.onPartialResultSet(partialResultSet); consumer.onPartialResultSet(PartialResultSet.getDefaultInstance()); consumer.onPartialResultSet(PartialResultSet.getDefaultInstance()); consumer.onCompleted(); assertThat(resultSet.next()).isFalse(); }
@Test public void singleResponse() { consumer.onPartialResultSet( PartialResultSet.newBuilder() .setMetadata(makeMetadata(Type.struct(Type.StructField.of("f", Type.string())))) .addValues(Value.string("a").toProto()) .addValues(Value.string("b").toProto()) .addValues(Value.string("c").toProto()) .build()); consumer.onCompleted(); assertThat(consumeAllString()).containsExactly("a", "b", "c").inOrder(); }
@Test public void multiResponseChunkingStreamClosed() { consumer.onPartialResultSet( PartialResultSet.newBuilder() .setMetadata(makeMetadata(Type.struct(Type.StructField.of("f", Type.string())))) .addValues(Value.string("abcdefg").toProto()) .setChunkedValue(true) .build()); consumer.onCompleted(); expectedException.expect(isSpannerException(ErrorCode.INTERNAL)); resultSet.next(); }