use of org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SpannerStatementQueryTests method unsupportedParamTypeTest.
@Test
public void unsupportedParamTypeTest() throws NoSuchMethodException {
this.expectedEx.expect(IllegalArgumentException.class);
this.expectedEx.expectMessage("is not a supported type: class org.springframework." + "cloud.gcp.data.spanner.repository.query.SpannerStatementQueryTests$Trade");
when(this.queryMethod.getName()).thenReturn("findTop3DistinctIdActionPriceByActionAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullOrderByIdDesc");
this.partTreeSpannerQuery = createQuery();
Method method = QueryHolder.class.getMethod("repositoryMethod2", Object.class, Object.class, Object.class, Object.class, Object.class, Trade.class, Object.class);
doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
// This parameter is an unsupported type for Spanner SQL.
Object[] params = new Object[] { "BUY", "abcd", "abc123", 8.88, 3.33, new Trade(), "ignored" };
this.partTreeSpannerQuery.execute(params);
}
use of org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SpannerStatementQueryTests method compoundNameConventionCountTest.
@Test
public void compoundNameConventionCountTest() throws NoSuchMethodException {
when(this.queryMethod.getName()).thenReturn("existsDistinctByActionIgnoreCaseAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullAndTraderIdLikeAndPriceTrueAndPriceFalse" + "AndPriceGreaterThanAndPriceLessThanEqualOrderByIdDesc");
this.partTreeSpannerQuery = spy(createQuery());
when(this.spannerTemplate.query((Function<Struct, Object>) any(), any(), any())).thenReturn(Collections.singletonList(1L));
Object[] params = new Object[] { Trade.Action.BUY, "abcd", "abc123", 8.88, 3.33, "ignored", "ignored", "blahblah", "ignored", "ignored", 1.11, 2.22 };
Method method = QueryHolder.class.getMethod("repositoryMethod3", Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class);
doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
when(this.spannerTemplate.query((Function<Struct, Object>) any(), any(), any())).thenAnswer((invocation) -> {
Statement statement = invocation.getArgument(1);
String expectedSql = "SELECT EXISTS" + "(SELECT DISTINCT shares, trader_id, ticker, price, action, id, value " + "FROM trades WHERE ( LOWER(action)=LOWER(@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 @tag7 AND price=TRUE AND price=FALSE AND " + "price>@tag10 AND price<=@tag11 ) ORDER BY id DESC LIMIT 1)";
assertThat(statement.getSql()).isEqualTo(expectedSql);
Map<String, Value> paramMap = statement.getParameters();
assertThat(paramMap.get("tag0").getString()).isEqualTo(params[0].toString());
assertThat(paramMap.get("tag1").getString()).isEqualTo(params[1]);
assertThat(paramMap.get("tag2").getString()).isEqualTo(params[2]);
assertThat(paramMap.get("tag3").getFloat64()).isEqualTo(params[3]);
assertThat(paramMap.get("tag4").getFloat64()).isEqualTo(params[4]);
assertThat(paramMap.get("tag5").getString()).isEqualTo(params[5]);
assertThat(paramMap.get("tag6").getString()).isEqualTo(params[6]);
assertThat(paramMap.get("tag7").getString()).isEqualTo(params[7]);
assertThat(paramMap.get("tag8").getString()).isEqualTo(params[8]);
assertThat(paramMap.get("tag9").getString()).isEqualTo(params[9]);
assertThat(paramMap.get("tag10").getFloat64()).isEqualTo(params[10]);
assertThat(paramMap.get("tag11").getFloat64()).isEqualTo(params[11]);
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((Function<Struct, Object>) any(), any(), any());
}
use of org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SpannerStatementQueryTests method unspecifiedParametersTest.
@Test
public void unspecifiedParametersTest() throws NoSuchMethodException {
this.expectedEx.expect(IllegalArgumentException.class);
this.expectedEx.expectMessage("The number of tags does not match the number of params.");
when(this.queryMethod.getName()).thenReturn("findTop3DistinctIdActionPriceByActionAndSymbolOrTraderIdAndPriceLessThanOrPriceGreater" + "ThanEqualAndIdIsNotNullAndTraderIdIsNullOrderByIdDesc");
this.partTreeSpannerQuery = createQuery();
Method method = QueryHolder.class.getMethod("repositoryMethod4", Object.class, Object.class, Object.class);
doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
// There are too few params specified, so the exception will occur.
Object[] params = new Object[] { "BUY", "abcd", "abc123" };
this.partTreeSpannerQuery.execute(params);
}
use of org.springframework.data.repository.query.DefaultParameters in project spring-cloud-gcp by spring-cloud.
the class SpannerStatementQueryTests method unSupportedPredicateTest.
@Test
public void unSupportedPredicateTest() throws NoSuchMethodException {
this.expectedEx.expect(UnsupportedOperationException.class);
this.expectedEx.expectMessage("The statement type: BETWEEN (2): [IsBetween, " + "Between] is not supported.");
when(this.queryMethod.getName()).thenReturn("countByTraderIdBetween");
Method method = Object.class.getMethod("toString");
doReturn(new DefaultParameters(method)).when(this.queryMethod).getParameters();
this.partTreeSpannerQuery = createQuery();
this.partTreeSpannerQuery.execute(EMPTY_PARAMETERS);
}
use of org.springframework.data.repository.query.DefaultParameters in project spring-data-jdbc by spring-projects.
the class JdbcRepositoryQueryUnitTests method setup.
@Before
public void setup() throws NoSuchMethodException {
Parameters parameters = new DefaultParameters(JdbcRepositoryQueryUnitTests.class.getDeclaredMethod("dummyMethod"));
queryMethod = mock(JdbcQueryMethod.class);
when(queryMethod.getParameters()).thenReturn(parameters);
context = mock(JdbcMappingContext.class, RETURNS_DEEP_STUBS);
defaultRowMapper = mock(RowMapper.class);
}
Aggregations