use of org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method compoundNameConventionTest.
@Test
public void compoundNameConventionTest() throws NoSuchMethodException {
String sql = "SELECT DISTINCT * FROM " + ":org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQueryTests$Trade:" + "@{index=fakeindex}" + " WHERE price=#{#tag3 * -1} AND price<>#{#tag3 * -1} OR " + "price<>#{#tag4 * -1} AND " + "( action=@tag0 AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND " + "struct_val = @tag8 AND struct_val = @tag9 " + "price>@tag6 AND price<=@tag7 and price in unnest(@tag10)) ORDER BY id DESC LIMIT 3;";
// @formatter:off
String entityResolvedSql = "SELECT *, " + "ARRAY (SELECT AS STRUCT disabled, id, childId, value, " + "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 children WHERE (children.id = trades.id) AND (disabled = false)) AS children FROM " + "(SELECT DISTINCT * FROM trades@{index=fakeindex}" + " WHERE price=@SpELtag1 AND price<>@SpELtag1 OR price<>@SpELtag2 AND " + "( action=@tag0 AND ticker=@tag1 ) OR " + "( trader_id=@tag2 AND price<@tag3 ) OR ( price>=@tag4 AND id<>NULL AND " + "trader_id=NULL AND trader_id LIKE %@tag5 AND price=TRUE AND price=FALSE AND " + "struct_val = @tag8 AND struct_val = @tag9 " + "price>@tag6 AND price<=@tag7 and price in unnest(@tag10)) ORDER BY id DESC LIMIT 3) trades " + "ORDER BY COLA ASC , COLB DESC LIMIT 10 OFFSET 30";
// @formatter:on
Object[] params = new Object[] { "BUY", this.pageable, "abcd", "abc123", 8.88, 3.33, "blahblah", 1.11, 2.22, Struct.newBuilder().set("symbol").to("ABCD").set("action").to("BUY").build(), new SymbolAction("ABCD", "BUY"), Arrays.asList("a", "b") };
String[] paramNames = new String[] { "tag0", "ignoredPageable", "tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tag10" };
when(queryMethod.isCollectionQuery()).thenReturn(false);
when(queryMethod.getReturnedObjectType()).thenReturn((Class) Trade.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, Trade.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("tag0").getString()).isEqualTo(params[0]);
// params[1] is this.pageable that is ignored, hence no synthetic tag is created for it
assertThat(paramMap.get("tag1").getString()).isEqualTo(params[2]);
assertThat(paramMap.get("tag2").getString()).isEqualTo(params[3]);
assertThat(paramMap.get("tag3").getFloat64()).isEqualTo(params[4]);
assertThat(paramMap.get("tag4").getFloat64()).isEqualTo(params[5]);
assertThat(paramMap.get("tag5").getString()).isEqualTo(params[6]);
assertThat(paramMap.get("tag6").getFloat64()).isEqualTo(params[7]);
assertThat(paramMap.get("tag7").getFloat64()).isEqualTo(params[8]);
assertThat(paramMap.get("tag8").getStruct()).isEqualTo(params[9]);
assertThat(paramMap.get("tag10").getStringArray()).isEqualTo(params[11]);
verify(this.spannerEntityProcessor, times(1)).write(same(params[10]), any());
assertThat(paramMap.get("SpELtag1").getFloat64()).isEqualTo(-8.88, DELTA);
assertThat(paramMap.get("SpELtag2").getFloat64()).isEqualTo(-3.33, DELTA);
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("dummyMethod", Object.class, Pageable.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, List.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 org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method sortParamQueryTest.
@Test
public void sortParamQueryTest() 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";
// @formatter:on
Object[] params = new Object[] { "ID", "TRADER_ID", Sort.by(Order.asc("trader_id")) };
String[] paramNames = new String[] { "id", "trader_id", "ignoredSort" };
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();
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("dummyMethod5", String.class, String.class, Sort.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 org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method pageableParamQueryTest.
@Test
public void pageableParamQueryTest() 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 10 OFFSET 30";
// @formatter:on
Object[] params = new Object[] { "ID", "TRADER_ID", PageRequest.of(3, 10, Sort.by(Order.asc("trader_id"))) };
String[] paramNames = new String[] { "id", "trader_id", "ignoredPageable" };
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("ignoredPageable")).isNull();
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("dummyMethod4", String.class, String.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 org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SqlSpannerQueryTests method dmlTest.
@Test
public void dmlTest() throws NoSuchMethodException {
String sql = "dml statement here";
TransactionContext context = mock(TransactionContext.class);
TransactionRunner transactionRunner = mock(TransactionRunner.class);
when(this.databaseClient.readWriteTransaction()).thenReturn(transactionRunner);
when(transactionRunner.run(any())).thenAnswer((invocation) -> {
TransactionRunner.TransactionCallable transactionCallable = invocation.getArgument(0);
return transactionCallable.run(context);
});
Method method = QueryHolder.class.getMethod("noParamMethod");
Mockito.<Parameters>when(this.queryMethod.getParameters()).thenReturn(new DefaultParameters(method));
SqlSpannerQuery sqlSpannerQuery = spy(createQuery(sql, Trade.class, true));
doReturn(long.class).when(sqlSpannerQuery).getReturnedSimpleConvertableItemType();
doReturn(null).when(sqlSpannerQuery).convertToSimpleReturnType(any(), any());
sqlSpannerQuery.execute(new Object[] {});
verify(this.spannerTemplate, times(1)).executeDmlStatement(any());
}
use of org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SpannerStatementQueryTests method runPageableOrSortTest.
private void runPageableOrSortTest(Object[] params, Method method, String expectedSql) {
when(this.queryMethod.getName()).thenReturn("findByPriceLessThan");
this.partTreeSpannerQuery = spy(createQuery());
when(this.spannerTemplate.query((Function<Struct, Object>) any(), any(), any())).thenReturn(Collections.singletonList(1L));
doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
when(this.spannerTemplate.query((Class) any(), any(), any())).thenAnswer((invocation) -> {
Statement statement = invocation.getArgument(1);
assertThat(statement.getSql()).isEqualTo(expectedSql);
Map<String, Value> paramMap = statement.getParameters();
assertThat(paramMap.get("tag0").getFloat64()).isEqualTo(params[0]);
assertThat(paramMap).hasSize(1);
return null;
});
doReturn(Object.class).when(this.partTreeSpannerQuery).getReturnedSimpleConvertableItemType();
doReturn(null).when(this.partTreeSpannerQuery).convertToSimpleReturnType(any(), any());
this.partTreeSpannerQuery.execute(params);
verify(this.spannerTemplate, times(1)).query((Class) any(), any(), any());
}
Aggregations