Search in sources :

Example 6 with Date

use of com.google.cloud.Date in project google-cloud-java by GoogleCloudPlatform.

the class ITWriteTest method writeDateArray.

@Test
public void writeDateArray() {
    Date d1 = Date.parseDate("2016-09-18");
    Date d2 = Date.parseDate("2016-09-19");
    write(baseInsert().set("DateArrayValue").toDateArray(Arrays.asList(d1, null, d2)).build());
    Struct row = readLastRow("DateArrayValue");
    assertThat(row.isNull(0)).isFalse();
    assertThat(row.getDateList(0)).containsExactly(d1, null, d2).inOrder();
}
Also used : Date(com.google.cloud.Date) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 7 with Date

use of com.google.cloud.Date in project google-cloud-java by GoogleCloudPlatform.

the class ValueTest method date.

@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);
}
Also used : Date(com.google.cloud.Date) Test(org.junit.Test)

Example 8 with Date

use of com.google.cloud.Date in project google-cloud-java by GoogleCloudPlatform.

the class KeyTest method equalsAndHashCode.

@Test
public void equalsAndHashCode() {
    EqualsTester tester = new EqualsTester();
    tester.addEqualityGroup(Key.of(), Key.newBuilder().build());
    // All types of null are considered equal.
    tester.addEqualityGroup(Key.of((Object) null), Key.newBuilder().append((Boolean) null).build(), Key.newBuilder().append((Long) null).build(), Key.newBuilder().append((Double) null).build(), Key.newBuilder().append((String) null).build(), Key.newBuilder().append((ByteArray) null).build(), Key.newBuilder().append((Timestamp) null).build(), Key.newBuilder().append((Date) null).build(), Key.newBuilder().appendObject(null).build());
    tester.addEqualityGroup(Key.of(true), Key.newBuilder().append(true).build());
    tester.addEqualityGroup(Key.of(false), Key.newBuilder().append(false).build());
    tester.addEqualityGroup(Key.of(1), Key.of(1L), Key.newBuilder().append(1).build());
    tester.addEqualityGroup(Key.of(2), Key.of(2L), Key.newBuilder().append(2).build());
    tester.addEqualityGroup(Key.of(1, 2));
    tester.addEqualityGroup(Key.of(1.0f), Key.of(1.0d), Key.newBuilder().append(1.0).build());
    tester.addEqualityGroup(Key.of(2.0f), Key.of(2.0d), Key.newBuilder().append(2.0).build());
    tester.addEqualityGroup(Key.of("a"), Key.newBuilder().append("a").build());
    tester.addEqualityGroup(Key.of("a", "b", "c"));
    tester.addEqualityGroup(Key.of(ByteArray.copyFrom("a")), Key.newBuilder().append(ByteArray.copyFrom("a")).build());
    Timestamp t = Timestamp.parseTimestamp("2015-09-15T00:00:00Z");
    tester.addEqualityGroup(Key.of(t), Key.newBuilder().append(t).build());
    Date d = Date.parseDate("2016-09-15");
    tester.addEqualityGroup(Key.of(d), Key.newBuilder().append(d).build());
    tester.addEqualityGroup(Key.of("a", 2, null));
    tester.testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) Timestamp(com.google.cloud.Timestamp) Date(com.google.cloud.Date) Test(org.junit.Test)

Example 9 with Date

use of com.google.cloud.Date in project beam by apache.

the class MutationSizeEstimatorTest method dates.

@Test
public void dates() throws Exception {
    Mutation timestamp = Mutation.newInsertOrUpdateBuilder("test").set("one").to(Timestamp.now()).build();
    Mutation nullTimestamp = Mutation.newInsertOrUpdateBuilder("test").set("one").to((Timestamp) null).build();
    Mutation date = Mutation.newInsertOrUpdateBuilder("test").set("one").to(Date.fromYearMonthDay(2017, 10, 10)).build();
    Mutation nullDate = Mutation.newInsertOrUpdateBuilder("test").set("one").to((Date) null).build();
    Mutation timestampArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toTimestampArray(Arrays.asList(Timestamp.now(), null)).build();
    Mutation dateArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toDateArray(Arrays.asList(null, Date.fromYearMonthDay(2017, 1, 1), null, Date.fromYearMonthDay(2017, 1, 2))).build();
    Mutation nullTimestampArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toTimestampArray(null).build();
    Mutation nullDateArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toDateArray(null).build();
    assertThat(MutationSizeEstimator.sizeOf(timestamp), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(date), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(nullTimestamp), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(nullDate), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(timestampArray), is(24L));
    assertThat(MutationSizeEstimator.sizeOf(dateArray), is(48L));
    assertThat(MutationSizeEstimator.sizeOf(nullTimestampArray), is(0L));
    assertThat(MutationSizeEstimator.sizeOf(nullDateArray), is(0L));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Timestamp(com.google.cloud.Timestamp) Date(com.google.cloud.Date) Test(org.junit.Test)

Example 10 with Date

use of com.google.cloud.Date in project beam by apache.

the class MutationKeyEncoder method encodeKey.

private void encodeKey(OrderedCode orderedCode, String tableName, Key key) {
    List<SpannerSchema.KeyPart> parts = schema.getKeyParts(tableName);
    Iterator<Object> it = key.getParts().iterator();
    for (SpannerSchema.KeyPart part : parts) {
        Object value = it.next();
        if (value == null) {
            if (part.isDesc()) {
                orderedCode.writeInfinityDecreasing();
            } else {
                orderedCode.writeInfinity();
            }
        } else {
            if (value instanceof Boolean) {
                writeNumber(orderedCode, part, (long) ((Boolean) value ? 0 : 1));
            } else if (value instanceof Long) {
                writeNumber(orderedCode, part, (long) value);
            } else if (value instanceof Double) {
                writeNumber(orderedCode, part, Double.doubleToLongBits((double) value));
            } else if (value instanceof String) {
                writeString(orderedCode, part, (String) value);
            } else if (value instanceof ByteArray) {
                writeBytes(orderedCode, part, (ByteArray) value);
            } else if (value instanceof Timestamp) {
                writeTimestamp(orderedCode, part, (Timestamp) value);
            } else if (value instanceof Date) {
                writeNumber(orderedCode, part, encodeDate((Date) value));
            } else {
                throw new IllegalArgumentException("Unknown key part " + value);
            }
        }
    }
}
Also used : KeyPart(org.apache.beam.sdk.io.gcp.spanner.SpannerSchema.KeyPart) Timestamp(com.google.cloud.Timestamp) Date(com.google.cloud.Date) KeyPart(org.apache.beam.sdk.io.gcp.spanner.SpannerSchema.KeyPart) ByteArray(com.google.cloud.ByteArray)

Aggregations

Date (com.google.cloud.Date)10 Test (org.junit.Test)9 IntegrationTest (com.google.cloud.spanner.IntegrationTest)6 Struct (com.google.cloud.spanner.Struct)6 Timestamp (com.google.cloud.Timestamp)3 ByteArray (com.google.cloud.ByteArray)1 Mutation (com.google.cloud.spanner.Mutation)1 EqualsTester (com.google.common.testing.EqualsTester)1 KeyPart (org.apache.beam.sdk.io.gcp.spanner.SpannerSchema.KeyPart)1