Search in sources :

Example 1 with WriteBuilder

use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.

the class SpannerObjectMapperImplTests method writeUnsupportedTypeIterableTest.

@Test(expected = SpannerDataException.class)
public void writeUnsupportedTypeIterableTest() {
    FaultyTestEntity2 ft = new FaultyTestEntity2();
    ft.listWithUnsupportedInnerType = new ArrayList<TestEntity>();
    WriteBuilder writeBuilder = Mutation.newInsertBuilder("faulty_test_table_2");
    this.objectMapper.write(ft, writeBuilder);
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Test(org.junit.Test)

Example 2 with WriteBuilder

use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.

the class SpannerObjectMapperImplTests method writeUnannotatedIterableTest.

@Test(expected = SpannerDataException.class)
public void writeUnannotatedIterableTest() {
    FaultyTestEntity ft = new FaultyTestEntity();
    ft.doubleList = new ArrayList<>();
    WriteBuilder writeBuilder = Mutation.newInsertBuilder("faulty_test_table");
    this.objectMapper.write(ft, writeBuilder);
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Test(org.junit.Test)

Example 3 with WriteBuilder

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

the class ValueBinderExpressionVisitorAdapterTest method create.

private ValueBinderExpressionVisitorAdapter<WriteBuilder> create() {
    ParameterStore parameterStore = new ParameterStore();
    WriteBuilder builder = Mutation.newInsertBuilder("INSERT INTO FOO (ID, COL1, COL2) VALUES (?, ?, ?)");
    ValueBinder<WriteBuilder> binder = builder.set("COL1");
    ValueBinderExpressionVisitorAdapter<WriteBuilder> res = new ValueBinderExpressionVisitorAdapter<>(parameterStore, binder, "COL1");
    return res;
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder)

Example 4 with WriteBuilder

use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.

the class SpannerObjectMapperImplTests method writeTest.

@Test
public void writeTest() throws ClassNotFoundException {
    TestEntity t = new TestEntity();
    t.id = "key1";
    t.stringField = "string";
    t.booleanField = true;
    t.intField = 123;
    t.longField = 3L;
    t.doubleField = 3.33;
    t.doubleArray = new double[] { 3.33, 3.33, 3.33 };
    t.doubleList = new ArrayList<>();
    t.doubleList.add(3.33);
    t.stringList = new ArrayList<>();
    t.stringList.add("stringstringstring");
    t.dateField = Date.fromYearMonthDay(2018, 11, 22);
    t.timestampField = Timestamp.ofTimeMicroseconds(333);
    t.bytes = ByteArray.copyFrom("333");
    t.booleanList = new ArrayList<>();
    t.booleanList.add(t.booleanField);
    t.longList = new ArrayList<>();
    t.longList.add(t.longField);
    t.dateList = new ArrayList<>();
    t.dateList.add(t.dateField);
    t.timestampList = new ArrayList<>();
    t.timestampList.add(t.timestampField);
    t.bytesList = new ArrayList<>();
    t.bytesList.add(t.bytes);
    WriteBuilder writeBuilder = mock(WriteBuilder.class);
    ValueBinder<WriteBuilder> idBinder = mock(ValueBinder.class);
    when(idBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id"))).thenReturn(idBinder);
    ValueBinder<WriteBuilder> stringFieldBinder = mock(ValueBinder.class);
    when(stringFieldBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("custom_col"))).thenReturn(stringFieldBinder);
    ValueBinder<WriteBuilder> booleanFieldBinder = mock(ValueBinder.class);
    when(booleanFieldBinder.to((Boolean) any())).thenReturn(null);
    when(writeBuilder.set(eq("booleanField"))).thenReturn(booleanFieldBinder);
    ValueBinder<WriteBuilder> intFieldBinder = mock(ValueBinder.class);
    when(intFieldBinder.to(anyLong())).thenReturn(null);
    when(writeBuilder.set(eq("intField"))).thenReturn(intFieldBinder);
    ValueBinder<WriteBuilder> longFieldBinder = mock(ValueBinder.class);
    when(longFieldBinder.to(anyLong())).thenReturn(null);
    when(writeBuilder.set(eq("longField"))).thenReturn(longFieldBinder);
    ValueBinder<WriteBuilder> doubleFieldBinder = mock(ValueBinder.class);
    when(doubleFieldBinder.to(anyDouble())).thenReturn(null);
    when(writeBuilder.set(eq("doubleField"))).thenReturn(doubleFieldBinder);
    ValueBinder<WriteBuilder> doubleArrayFieldBinder = mock(ValueBinder.class);
    when(doubleArrayFieldBinder.toFloat64Array((double[]) any())).thenReturn(null);
    when(writeBuilder.set(eq("doubleArray"))).thenReturn(doubleArrayFieldBinder);
    ValueBinder<WriteBuilder> doubleListFieldBinder = mock(ValueBinder.class);
    when(doubleListFieldBinder.toFloat64Array((Iterable<Double>) any())).thenReturn(null);
    when(writeBuilder.set(eq("doubleList"))).thenReturn(doubleListFieldBinder);
    ValueBinder<WriteBuilder> stringListFieldBinder = mock(ValueBinder.class);
    when(stringListFieldBinder.toStringArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("stringList"))).thenReturn(stringListFieldBinder);
    ValueBinder<WriteBuilder> booleanListFieldBinder = mock(ValueBinder.class);
    when(booleanListFieldBinder.toBoolArray((Iterable<Boolean>) any())).thenReturn(null);
    when(writeBuilder.set(eq("booleanList"))).thenReturn(booleanListFieldBinder);
    ValueBinder<WriteBuilder> longListFieldBinder = mock(ValueBinder.class);
    when(longListFieldBinder.toInt64Array((Iterable<Long>) any())).thenReturn(null);
    when(writeBuilder.set(eq("longList"))).thenReturn(longListFieldBinder);
    ValueBinder<WriteBuilder> timestampListFieldBinder = mock(ValueBinder.class);
    when(timestampListFieldBinder.toTimestampArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("timestampList"))).thenReturn(timestampListFieldBinder);
    ValueBinder<WriteBuilder> dateListFieldBinder = mock(ValueBinder.class);
    when(dateListFieldBinder.toDateArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("dateList"))).thenReturn(dateListFieldBinder);
    ValueBinder<WriteBuilder> bytesListFieldBinder = mock(ValueBinder.class);
    when(bytesListFieldBinder.toDateArray(any())).thenReturn(null);
    when(writeBuilder.set(eq("bytesList"))).thenReturn(bytesListFieldBinder);
    ValueBinder<WriteBuilder> dateFieldBinder = mock(ValueBinder.class);
    when(dateFieldBinder.to((Date) any())).thenReturn(null);
    when(writeBuilder.set(eq("dateField"))).thenReturn(dateFieldBinder);
    ValueBinder<WriteBuilder> timestampFieldBinder = mock(ValueBinder.class);
    when(timestampFieldBinder.to((Timestamp) any())).thenReturn(null);
    when(writeBuilder.set(eq("timestampField"))).thenReturn(timestampFieldBinder);
    ValueBinder<WriteBuilder> bytesFieldBinder = mock(ValueBinder.class);
    when(bytesFieldBinder.to((ByteArray) any())).thenReturn(null);
    when(writeBuilder.set(eq("bytes"))).thenReturn(bytesFieldBinder);
    this.objectMapper.write(t, writeBuilder);
    verify(idBinder, times(1)).to(eq(t.id));
    verify(stringFieldBinder, times(1)).to(eq(t.stringField));
    verify(booleanFieldBinder, times(1)).to(eq(Boolean.valueOf(t.booleanField)));
    verify(intFieldBinder, times(1)).to(eq(Long.valueOf(t.intField)));
    verify(longFieldBinder, times(1)).to(eq(Long.valueOf(t.longField)));
    verify(doubleFieldBinder, times(1)).to(eq(Double.valueOf(t.doubleField)));
    verify(doubleArrayFieldBinder, times(1)).toFloat64Array(eq(t.doubleArray));
    verify(doubleListFieldBinder, times(1)).toFloat64Array(eq(t.doubleList));
    verify(stringListFieldBinder, times(1)).toStringArray(eq(t.stringList));
    verify(booleanListFieldBinder, times(1)).toBoolArray(eq(t.booleanList));
    verify(longListFieldBinder, times(1)).toInt64Array(eq(t.longList));
    verify(timestampListFieldBinder, times(1)).toTimestampArray(eq(t.timestampList));
    verify(dateListFieldBinder, times(1)).toDateArray(eq(t.dateList));
    verify(bytesListFieldBinder, times(1)).toBytesArray(eq(t.bytesList));
    verify(dateFieldBinder, times(1)).to(eq(t.dateField));
    verify(timestampFieldBinder, times(1)).to(eq(t.timestampField));
    verify(bytesFieldBinder, times(1)).to(eq(t.bytes));
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.anyDouble(org.mockito.ArgumentMatchers.anyDouble) Test(org.junit.Test)

Example 5 with WriteBuilder

use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.

the class SpannerObjectMapperImplTests method writeSomeColumnsTest.

@Test
public void writeSomeColumnsTest() throws ClassNotFoundException {
    TestEntity t = new TestEntity();
    t.id = "key1";
    t.stringField = "string";
    WriteBuilder writeBuilder = mock(WriteBuilder.class);
    ValueBinder<WriteBuilder> idBinder = mock(ValueBinder.class);
    when(idBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("id"))).thenReturn(idBinder);
    ValueBinder<WriteBuilder> stringFieldBinder = mock(ValueBinder.class);
    when(stringFieldBinder.to(anyString())).thenReturn(null);
    when(writeBuilder.set(eq("custom_col"))).thenReturn(stringFieldBinder);
    ValueBinder<WriteBuilder> booleanFieldBinder = mock(ValueBinder.class);
    when(booleanFieldBinder.to((Boolean) any())).thenReturn(null);
    when(writeBuilder.set(eq("booleanField"))).thenReturn(booleanFieldBinder);
    this.objectMapper.write(t, writeBuilder, new HashSet<>(Arrays.asList(new String[] { "id", "custom_col" })));
    verify(idBinder, times(1)).to(eq(t.id));
    verify(stringFieldBinder, times(1)).to(eq(t.stringField));
    verifyZeroInteractions(booleanFieldBinder);
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Test(org.junit.Test)

Aggregations

WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)11 Test (org.junit.Test)5 Mutation (com.google.cloud.spanner.Mutation)2 Expression (net.sf.jsqlparser.expression.Expression)2 Column (net.sf.jsqlparser.schema.Column)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2 ByteArray (com.google.cloud.ByteArray)1 Date (com.google.cloud.Date)1 Timestamp (com.google.cloud.Timestamp)1 ValueBinder (com.google.cloud.spanner.ValueBinder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Set (java.util.Set)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)1 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)1