private void addInternal(String fieldName, Value value) { types.add(Type.StructField.of(fieldName, value.getType())); values.add(value); }
@Test public void int64Wrapper() { Value v = Value.int64(Long.valueOf(123)); assertThat(v.getType()).isEqualTo(Type.int64()); assertThat(v.isNull()).isFalse(); assertThat(v.getInt64()).isEqualTo(123); assertThat(v.toString()).isEqualTo("123"); }
@Test public void string() { Value v = Value.string("abc"); assertThat(v.getType()).isEqualTo(Type.string()); assertThat(v.isNull()).isFalse(); assertThat(v.getString()).isEqualTo("abc"); }
@Test public void bool() { Value v = Value.bool(true); assertThat(v.getType()).isEqualTo(Type.bool()); assertThat(v.isNull()).isFalse(); assertThat(v.getBool()).isTrue(); assertThat(v.toString()).isEqualTo("true"); }
@Test public void int64() { Value v = Value.int64(123); assertThat(v.getType()).isEqualTo(Type.int64()); assertThat(v.isNull()).isFalse(); assertThat(v.getInt64()).isEqualTo(123); assertThat(v.toString()).isEqualTo("123"); }
@Test public void float64Wrapper() { Value v = Value.float64(Double.valueOf(1.23)); assertThat(v.getType()).isEqualTo(Type.float64()); assertThat(v.isNull()).isFalse(); assertThat(v.getFloat64()).isWithin(0.0001).of(1.23); assertThat(v.toString()).isEqualTo("1.23"); }
@Test public void boolWrapper() { Value v = Value.bool(Boolean.FALSE); assertThat(v.getType()).isEqualTo(Type.bool()); assertThat(v.isNull()).isFalse(); assertThat(v.getBool()).isFalse(); assertThat(v.toString()).isEqualTo("false"); }
@Test public void float64() { Value v = Value.float64(1.23); assertThat(v.getType()).isEqualTo(Type.float64()); assertThat(v.isNull()).isFalse(); assertThat(v.getFloat64()).isWithin(0.0001).of(1.23); assertThat(v.toString()).isEqualTo("1.23"); }
@Test public void date() { String date = "2016-09-15"; Date t = Date.parseDate(date); Value v = Value.date(t); assertThat(v.getType()).isEqualTo(Type.date()); assertThat(v.isNull()).isFalse(); assertThat(v.getDate()).isSameAs(t); assertThat(v.toString()).isEqualTo(date); }
@Test public void timestamp() { String timestamp = "2016-09-15T00:00:00Z"; Timestamp t = Timestamp.parseTimestamp(timestamp); Value v = Value.timestamp(t); assertThat(v.getType()).isEqualTo(Type.timestamp()); assertThat(v.isNull()).isFalse(); assertThat(v.isCommitTimestamp()).isFalse(); assertThat(v.getTimestamp()).isSameAs(t); assertThat(v.toString()).isEqualTo(timestamp); }
@Test public void bytes() { ByteArray bytes = newByteArray("abc"); Value v = Value.bytes(bytes); assertThat(v.getType()).isEqualTo(Type.bytes()); assertThat(v.isNull()).isFalse(); assertThat(v.getBytes()).isSameAs(bytes); assertThat(v.toString()).isEqualTo(bytes.toString()); }
@Test public void float64WrapperNull() { Value v = Value.float64(null); assertThat(v.getType()).isEqualTo(Type.float64()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getFloat64(); }
@Test public void bytesNull() { Value v = Value.bytes(null); assertThat(v.getType()).isEqualTo(Type.bytes()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getBytes(); }
@Test public void dateNull() { Value v = Value.date(null); assertThat(v.getType()).isEqualTo(Type.date()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getDate(); }
@Test public void timestampNull() { Value v = Value.timestamp(null); assertThat(v.getType()).isEqualTo(Type.timestamp()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); assertThat(v.isCommitTimestamp()).isFalse(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getTimestamp(); }
@Test public void boolWrapperNull() { Value v = Value.bool(null); assertThat(v.getType()).isEqualTo(Type.bool()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getBool(); }
@Test public void stringNull() { Value v = Value.string(null); assertThat(v.getType()).isEqualTo(Type.string()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getString(); }
@Test public void int64WrapperNull() { Value v = Value.int64(null); assertThat(v.getType()).isEqualTo(Type.int64()); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getInt64(); }
@Test public void nullStruct() { List<Type.StructField> fieldTypes = Arrays.asList( Type.StructField.of("f1", Type.string()), Type.StructField.of("f2", Type.int64())); Value v = Value.struct(Type.struct(fieldTypes), null); assertThat(v.getType().getStructFields()).isEqualTo(fieldTypes); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(NullPointerException.class); expectedException.expectMessage("Illegal call to create a NULL struct value."); Value.struct(null); }
@Test public void struct() { Struct struct = Struct.newBuilder().set("f1").to("v1").set("f2").to(30).build(); Value v1 = Value.struct(struct); assertThat(v1.getType()).isEqualTo(struct.getType()); assertThat(v1.isNull()).isFalse(); assertThat(v1.getStruct()).isEqualTo(struct); assertThat(v1.toString()).isEqualTo("[v1, 30]"); Value v2 = Value.struct(struct.getType(), struct); assertThat(v2).isEqualTo(v1); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Mismatch between struct value and type."); Value.struct(Type.struct(Arrays.asList(StructField.of("f3", Type.string()))), struct); }