use of com.google.cloud.spanner.Value in project spring-cloud-gcp by spring-cloud.
the class ConverterAwareMappingSpannerEntityWriterTests method testCommitTimestampsType.
@Test
public void testCommitTimestampsType() {
CommitTimestamps entity = new CommitTimestamps();
doWithFields(CommitTimestamps.class, f -> setField(f, entity, CommitTimestamp.of(f.getType())), ff -> !ff.isSynthetic() && Objects.isNull(ff.getAnnotation(PrimaryKey.class)));
WriteBuilder writeBuilder = Mutation.newInsertBuilder("commit_timestamps_table");
this.spannerEntityWriter.write(entity, writeBuilder::set);
Mutation mutation = writeBuilder.build();
assertThat(mutation.asMap().entrySet().stream().filter(e -> !"id".equals(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList())).allMatch(Value::isCommitTimestamp);
}
use of com.google.cloud.spanner.Value in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method sortAndPageableQueryTest.
@Test
public void sortAndPageableQueryTest() throws NoSuchMethodException {
String sql = "SELECT * FROM :org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Child:" + " WHERE id = @id AND trader_id = @trader_id";
// @formatter:off
String entityResolvedSql = "SELECT *, " + "ARRAY (SELECT AS STRUCT canceled, documentId, id, childId, content " + "FROM documents WHERE (documents.id = children.id AND documents.childId = children.childId) " + "AND (canceled = false)) AS documents " + "FROM (SELECT * FROM children WHERE id = @id AND trader_id = @trader_id) children " + "WHERE disabled = false ORDER BY trader_id ASC LIMIT 2 OFFSET 2";
// @formatter:on
Object[] params = new Object[] { "ID", "TRADER_ID", Sort.by(Order.asc("trader_id")), PageRequest.of(1, 2) };
String[] paramNames = new String[] { "id", "trader_id", "ignoredSort", "pageable" };
when(queryMethod.isCollectionQuery()).thenReturn(false);
when(queryMethod.getReturnedObjectType()).thenReturn((Class) Child.class);
EvaluationContext evaluationContext = new StandardEvaluationContext();
for (int i = 0; i < params.length; i++) {
evaluationContext.setVariable(paramNames[i], params[i]);
}
when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
SqlSpannerQuery sqlSpannerQuery = createQuery(sql, Child.class, false);
doAnswer((invocation) -> {
Statement statement = invocation.getArgument(0);
SpannerQueryOptions queryOptions = invocation.getArgument(1);
assertThat(queryOptions.isAllowPartialRead()).isTrue();
assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
Map<String, Value> paramMap = statement.getParameters();
assertThat(paramMap.get("id").getString()).isEqualTo(params[0]);
assertThat(paramMap.get("trader_id").getString()).isEqualTo(params[1]);
assertThat(paramMap.get("ignoredSort")).isNull();
assertThat(paramMap.get("pageable")).isNull();
return null;
}).when(this.spannerTemplate).executeQuery(any(), any());
Method method = QueryHolder.class.getMethod("sortAndPageable", String.class, String.class, Sort.class, Pageable.class);
when(this.queryMethod.getMethod()).thenReturn(method);
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
sqlSpannerQuery.execute(params);
verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
use of com.google.cloud.spanner.Value in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method sqlCountWithWhereTest.
@Test
public void sqlCountWithWhereTest() throws NoSuchMethodException {
String sql = "SELECT count(1) FROM :org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Child:" + " WHERE id = @id AND trader_id = @trader_id";
String entityResolvedSql = "SELECT count(1) FROM children WHERE id = @id AND trader_id = @trader_id";
Object[] params = new Object[] { "ID", "TRADER_ID" };
String[] paramNames = new String[] { "id", "trader_id" };
when(queryMethod.isCollectionQuery()).thenReturn(false);
when(queryMethod.getReturnedObjectType()).thenReturn((Class) long.class);
EvaluationContext evaluationContext = new StandardEvaluationContext();
for (int i = 0; i < params.length; i++) {
evaluationContext.setVariable(paramNames[i], params[i]);
}
when(this.evaluationContextProvider.getEvaluationContext(any(), any())).thenReturn(evaluationContext);
SqlSpannerQuery sqlSpannerQuery = createQuery(sql, long.class, false);
doAnswer((invocation) -> {
Statement statement = invocation.getArgument(0);
SpannerQueryOptions queryOptions = invocation.getArgument(1);
assertThat(queryOptions.isAllowPartialRead()).isTrue();
assertThat(statement.getSql()).isEqualTo(entityResolvedSql);
Map<String, Value> paramMap = statement.getParameters();
assertThat(paramMap.get("id").getString()).isEqualTo(params[0]);
assertThat(paramMap.get("trader_id").getString()).isEqualTo(params[1]);
return null;
}).when(this.spannerTemplate).executeQuery(any(), any());
// This dummy method was created so the metadata for the ARRAY param inner type is
// provided.
Method method = QueryHolder.class.getMethod("dummyMethod3", String.class, String.class);
when(this.queryMethod.getMethod()).thenReturn(method);
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
sqlSpannerQuery.execute(params);
verify(this.spannerTemplate, times(1)).executeQuery(any(), any());
}
use of com.google.cloud.spanner.Value in project spring-cloud-gcp by spring-cloud.
the class SpannerWriteMethodCoverageTests method allKnownMappingTypesTest.
// Checks that the converter is aware of all Cloud Spanner mutation binder types
@Test
public void allKnownMappingTypesTest() throws NoSuchFieldException {
for (Method method : ValueBinder.class.getMethods()) {
String methodName = method.getName();
// ignoring non-public and non "to" named binder methods
if (!Modifier.isPublic(method.getModifiers()) || !methodName.startsWith("to") || method.getParameterCount() != 1) {
continue;
}
Class<?> paramType = ConversionUtils.boxIfNeeded(method.getParameterTypes()[0]);
if (paramType.equals(Struct.class) || paramType.equals(Value.class)) {
/*
* 1. there is a method for binding a Struct value, but because Struct
* values cannot be written to table columns we will ignore it. 2. there
* is a method for binding a Value value. However, the purpose of the
* converters is to wrap java types into the Value for the user.
* Furthermore, the Cloud Spanner client lib does not give a way to read a
* Value back from a Struct, so we will ignore this method.
*/
continue;
} else if (ConversionUtils.isIterableNonByteArrayType(paramType)) {
Class<?> innerParamType = (Class) ((ParameterizedType) method.getGenericParameterTypes()[0]).getActualTypeArguments()[0];
assertThat(ConverterAwareMappingSpannerEntityWriter.iterablePropertyTypeToMethodMap.keySet()).contains(innerParamType);
} else {
assertThat(ConverterAwareMappingSpannerEntityWriter.singleItemTypeValueBinderMethodMap.keySet()).contains(paramType);
}
}
}
Aggregations