@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 boolArrayRange() { Value v = Value.boolArray(new boolean[] {true, false, false, true, false}, 1, 3); assertThat(v.isNull()).isFalse(); assertThat(v.getBoolArray()).containsExactly(false, false, true).inOrder(); assertThat(v.toString()).isEqualTo("[false,false,true]"); }
@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 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 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 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 float64ArrayWrapper() { Value v = Value.float64Array(Arrays.asList(.1, null, .3)); assertThat(v.isNull()).isFalse(); assertThat(v.getFloat64Array()).containsExactly(.1d, null, .3d).inOrder(); assertThat(v.toString()).isEqualTo("[0.1,NULL,0.3]"); }
@Test public void int64ArrayWrapper() { Value v = Value.int64Array(Arrays.asList(1L, null, 3L)); assertThat(v.isNull()).isFalse(); assertThat(v.getInt64Array()).containsExactly(1L, null, 3L).inOrder(); assertThat(v.toString()).isEqualTo("[1,NULL,3]"); }
@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 bytesArray() { ByteArray a = newByteArray("a"); ByteArray c = newByteArray("c"); Value v = Value.bytesArray(Arrays.asList(a, null, c)); assertThat(v.isNull()).isFalse(); assertThat(v.getBytesArray()).containsExactly(a, null, c).inOrder(); assertThat(v.toString()).isEqualTo(String.format("[%s,NULL,%s]", a, c)); }
@Test public void int64ArrayWrapperNull() { Value v = Value.int64Array((Iterable<Long>) null); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getInt64Array(); }
@Test public void boolArrayFromListNull() { Value v = Value.boolArray((Iterable<Boolean>) null); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getBoolArray(); }
@Test public void float64ArrayWrapperNull() { Value v = Value.float64Array((Iterable<Double>) null); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getFloat64Array(); }
@Test public void dateArray() { String d1 = "2016-09-15"; String d2 = "2016-09-14"; Value v = Value.dateArray(Arrays.asList(Date.parseDate(d1), null, Date.parseDate(d2))); assertThat(v.isNull()).isFalse(); assertThat(v.getDateArray()) .containsExactly(Date.parseDate(d1), null, Date.parseDate(d2)) .inOrder(); assertThat(v.toString()).isEqualTo("[" + d1 + ",NULL," + d2 + "]"); }
@Test public void dateArrayNull() { Value v = Value.dateArray(null); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getDateArray(); }
@Test public void bytesArrayNull() { Value v = Value.bytesArray(null); assertThat(v.isNull()).isTrue(); assertThat(v.toString()).isEqualTo(NULL_STRING); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("null value"); v.getBytesArray(); }
@Test public void boolArrayFromPlainIterable() { // Test to ensure that PrimitiveArrayFactory understands how to create an appropriate backing // arrays from various sizes of plain Iterable input. This test also covers the code paths // used by int64Array() and float64Array(). for (int i = 0; i < 50; ++i) { Boolean[] data = new Boolean[i]; for (int j = 0; j < data.length; ++j) { data[j] = (j % 3 == 2) ? null : ((j % 3) == 1); } String name = "boolArray() of length " + i; Value v = Value.boolArray(plainIterable(data)); assertThat(v.isNull()).named(name).isFalse(); assertThat(v.getBoolArray()).named(name).containsExactly((Object[]) data).inOrder(); } }
@Test public void bytesUnprintable() { ByteArray bytes = ByteArray.copyFrom(new byte[] {'a', 0, 15, -1, 'e'}); Value v = Value.bytes(bytes); assertThat(v.getBytes()).isSameAs(bytes); assertThat(v.toString()).isEqualTo(bytes.toString()); }
private static long estimateArrayValue(Value v) { if (v.isNull()) { return 0; switch (v.getType().getArrayElementType().getCode()) { case BOOL: return v.getBoolArray().size(); case INT64: return 8L * v.getInt64Array().size(); case FLOAT64: return 8L * v.getFloat64Array().size(); case STRING: long totalLength = 0; for (String s : v.getStringArray()) { if (s == null) { continue; case BYTES: totalLength = 0; for (ByteArray bytes : v.getBytesArray()) { if (bytes == null) { continue; return 12L * v.getDateArray().size(); case TIMESTAMP: return 12L * v.getTimestampArray().size(); default: throw new IllegalArgumentException("Unsupported type " + v.getType());
for (SpannerSchema.KeyPart part : schema.getKeyParts(m.getTable())) { Value val = mutationMap.get(part.getField()); if (val.isNull()) { if (part.isDesc()) { orderedCode.writeInfinityDecreasing(); Type.Code code = val.getType().getCode(); switch (code) { case BOOL: writeNumber(orderedCode, part, (long) (val.getBool() ? 0 : 1)); break; case INT64: writeNumber(orderedCode, part, val.getInt64()); break; case FLOAT64: writeNumber(orderedCode, part, Double.doubleToLongBits(val.getFloat64())); break; case STRING: writeString(orderedCode, part, val.getString()); break; case BYTES: writeBytes(orderedCode, part, val.getBytes()); break; case TIMESTAMP: writeTimestamp(orderedCode, part, val.getTimestamp()); break; case DATE: writeNumber(orderedCode, part, encodeDate(val.getDate())); break;