use of org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions 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());
}
Aggregations