Search in sources :

Example 11 with ByteArray

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

the class ITWriteTest method writeBytesArray.

@Test
public void writeBytesArray() {
    List<ByteArray> data = Arrays.asList(ByteArray.copyFrom("a"), ByteArray.copyFrom("b"), null);
    write(baseInsert().set("BytesArrayValue").toBytesArray(data).build());
    Struct row = readLastRow("BytesArrayValue");
    assertThat(row.isNull(0)).isFalse();
    assertThat(row.getBytesList(0)).isEqualTo(data);
}
Also used : ByteArray(com.google.cloud.ByteArray) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 12 with ByteArray

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

the class ITWriteTest method writeBytesRandom.

@Test
public void writeBytesRandom() {
    // Pseudo-random test for byte encoding.  We explicitly set a random seed so that multiple
    // test runs cover more data, but any failing test run can be reproduced easily.
    Random rnd = new Random();
    long seed = rnd.nextLong();
    rnd.setSeed(seed);
    Map<String, ByteArray> expected = new HashMap<>();
    boolean pass = false;
    try {
        for (int length : new int[] { 1, 2, 5, 11 }) {
            byte[] data = new byte[length];
            for (int i = 0; i < 3; ++i) {
                rnd.nextBytes(data);
                String key = uniqueString();
                ByteArray value = ByteArray.copyFrom(data);
                expected.put(key, value);
                write(Mutation.newInsertOrUpdateBuilder("T").set("K").to(key).set("BytesValue").to(value).build());
            }
        }
        KeySet.Builder keys = KeySet.newBuilder();
        for (String key : expected.keySet()) {
            keys.addKey(Key.of(key));
        }
        ResultSet resultSet = client.singleUse(TimestampBound.strong()).read("T", keys.build(), Arrays.asList("K", "BytesValue"));
        while (resultSet.next()) {
            String key = resultSet.getString(0);
            ByteArray value = resultSet.getBytes(1);
            assertThat(expected).containsKey(key);
            ByteArray expectedValue = expected.remove(key);
            assertThat(value).isEqualTo(expectedValue);
        }
        assertThat(expected).isEmpty();
        pass = true;
    } finally {
        if (!pass) {
            System.out.println("To reproduce failure, use seed " + seed);
        }
    }
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Random(java.util.Random) HashMap(java.util.HashMap) ResultSet(com.google.cloud.spanner.ResultSet) ByteArray(com.google.cloud.ByteArray) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 13 with ByteArray

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

the class ITQueryTest method bindBytesArray.

@Test
public void bindBytesArray() {
    ByteArray e1 = ByteArray.copyFrom("x");
    ByteArray e2 = ByteArray.copyFrom("y");
    ByteArray e3 = null;
    Struct row = execute(Statement.newBuilder("SELECT @v").bind("v").toBytesArray(Arrays.asList(e1, e2, e3)), Type.array(Type.bytes()));
    assertThat(row.isNull(0)).isFalse();
    assertThat(row.getBytesList(0)).containsExactly(e1, e2, e3).inOrder();
}
Also used : ByteArray(com.google.cloud.ByteArray) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 14 with ByteArray

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

the class ITQueryTest method bindBytesNull.

@Test
public void bindBytesNull() {
    Struct row = execute(Statement.newBuilder("SELECT @v").bind("v").to((ByteArray) null), Type.bytes());
    assertThat(row.isNull(0)).isTrue();
}
Also used : ByteArray(com.google.cloud.ByteArray) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 15 with ByteArray

use of com.google.cloud.ByteArray in project spanner-jdbc by olavloite.

the class CloudSpannerConversionUtilTest method testToCloudSpannerBytes.

@Test
public void testToCloudSpannerBytes() {
    byte[][] input = new byte[][] { "AA".getBytes(), "BB".getBytes() };
    List<ByteArray> output = CloudSpannerConversionUtil.toCloudSpannerBytes(input);
    ByteArray inp1 = ByteArray.copyFrom("AA".getBytes());
    ByteArray inp2 = ByteArray.copyFrom("BB".getBytes());
    assertArrayEquals(new ByteArray[] { inp1, inp2 }, output.toArray());
}
Also used : ByteArray(com.google.cloud.ByteArray) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Aggregations

ByteArray (com.google.cloud.ByteArray)15 Test (org.junit.Test)14 IntegrationTest (com.google.cloud.spanner.IntegrationTest)6 Struct (com.google.cloud.spanner.Struct)5 UnitTest (nl.topicus.jdbc.test.category.UnitTest)2 KeySet (com.google.cloud.spanner.KeySet)1 Mutation (com.google.cloud.spanner.Mutation)1 ResultSet (com.google.cloud.spanner.ResultSet)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1