Search in sources :

Example 6 with SpannerQueryOptions

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());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Statement(com.google.cloud.spanner.Statement) Method(java.lang.reflect.Method) SpannerQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions) DefaultParameters(org.springframework.data.repository.query.DefaultParameters) Value(com.google.cloud.spanner.Value) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Aggregations

Statement (com.google.cloud.spanner.Statement)6 Method (java.lang.reflect.Method)6 Test (org.junit.Test)6 SpannerQueryOptions (org.springframework.cloud.gcp.data.spanner.core.SpannerQueryOptions)6 DefaultParameters (org.springframework.data.repository.query.DefaultParameters)6 EvaluationContext (org.springframework.expression.EvaluationContext)6 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)6 Value (com.google.cloud.spanner.Value)5