Search in sources :

Example 11 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQueryTests method streamResultTest.

@Test
void streamResultTest() {
    Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
    Parameters parameters = mock(Parameters.class);
    when(this.queryMethod.getParameters()).thenReturn(parameters);
    when(parameters.getNumberOfParameters()).thenReturn(0);
    when(this.queryMethod.isStreamQuery()).thenReturn(true);
    Trade tradeA = new Trade();
    tradeA.id = "a";
    Trade tradeB = new Trade();
    tradeB.id = "b";
    doAnswer(invocation -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString()).isEqualTo("unusedGqlString");
        Cursor cursor = Cursor.copyFrom("abc".getBytes());
        DatastoreResultsIterable datastoreResultsIterable = new DatastoreResultsIterable(Arrays.asList(tradeA, tradeB), cursor);
        return datastoreResultsIterable;
    }).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
    GqlDatastoreQuery gqlDatastoreQuery = createQuery("unusedGqlString", false, false);
    Object result = gqlDatastoreQuery.execute(new Parameters[0]);
    assertThat(result).isInstanceOf(Stream.class);
    assertThat((Stream) result).hasSize(2).containsExactly(tradeA, tradeB);
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Test(org.junit.jupiter.api.Test)

Example 12 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQueryTests method pageableTestPage.

@Test
void pageableTestPage() {
    String gql = "SELECT * FROM trades WHERE price=@price";
    String expected = "SELECT * FROM trades WHERE price=@price LIMIT @limit OFFSET @offset";
    Object[] paramVals = new Object[] { 1, PageRequest.of(0, 2) };
    String[] paramNames = new String[] { "price", null };
    Parameters parameters = buildParameters(paramVals, paramNames);
    Mockito.<Class>when(this.queryMethod.getReturnedObjectType()).thenReturn(Trade.class);
    when(parameters.hasPageableParameter()).thenReturn(true);
    when(parameters.getPageableIndex()).thenReturn(1);
    GqlDatastoreQuery gqlDatastoreQuery = createQuery(gql, true, true);
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    doAnswer(invocation -> {
        GqlQuery statement = invocation.getArgument(0);
        assertThat(statement.getQueryString().equals(gql) || statement.getQueryString().equals(expected)).isTrue();
        Map<String, Value> paramMap = statement.getNamedBindings();
        if (statement.getQueryString().equals(expected)) {
            assertThat(paramMap).hasSize(3);
            assertThat(paramMap.get("price").get()).isEqualTo(1L);
            assertThat(paramMap.get("limit").get()).isEqualTo(2L);
            assertThat(paramMap.get("offset").get()).isEqualTo(0L);
            return new DatastoreResultsIterable(Collections.emptyList(), cursor);
        } else if (statement.getQueryString().equals(gql)) {
            assertThat(paramMap).hasSize(1);
            assertThat(paramMap.get("price").get()).isEqualTo(1L);
            return new DatastoreResultsIterable(Arrays.asList(1L, 2L), cursor);
        }
        return null;
    }).when(this.datastoreTemplate).queryKeysOrEntities(any(), eq(Trade.class));
    doReturn(false).when(gqlDatastoreQuery).isNonEntityReturnedType(any());
    doAnswer(invocation -> invocation.getArgument(0)).when(gqlDatastoreQuery).processRawObjectForProjection(any());
    Slice result = (Page) gqlDatastoreQuery.execute(paramVals);
    assertThat(((DatastorePageable) result.getPageable()).toCursor()).isEqualTo(cursor);
    assertThat(((DatastorePageable) result.getPageable()).getTotalCount()).isEqualTo(2L);
    assertThat(((Page) result).getTotalElements()).isEqualTo(2L);
    verify(this.datastoreTemplate, times(2)).queryKeysOrEntities(any(), eq(Trade.class));
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Page(org.springframework.data.domain.Page) Cursor(com.google.cloud.datastore.Cursor) GqlQuery(com.google.cloud.datastore.GqlQuery) Slice(org.springframework.data.domain.Slice) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Example 13 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQueryTests method prepareSliceResults.

private void prepareSliceResults(int offset, Integer queryLimit, Boolean hasNext) {
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
    when(this.datastoreTemplate.queryEntitiesSlice(isA(EntityQuery.class), any(), any())).thenAnswer(invocation -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOffset(offset).setOrderBy(OrderBy.desc("__key__")).setLimit(queryLimit).build();
        assertThat(statement).isEqualTo(expected);
        return new SliceImpl(new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor).toList(), Pageable.unpaged(), hasNext);
    });
}
Also used : DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor) EntityQuery(com.google.cloud.datastore.EntityQuery) SliceImpl(org.springframework.data.domain.SliceImpl)

Example 14 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQueryTests method nonCollectionReturnType.

@Test
void nonCollectionReturnType() throws NoSuchMethodException {
    Trade trade = new Trade();
    queryWithMockResult("findByAction", null, getClass().getMethod("findByAction", String.class), true, null);
    Object[] params = new Object[] { "BUY" };
    when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer(invocation -> {
        EntityQuery statement = invocation.getArgument(0);
        EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(PropertyFilter.eq("action", "BUY")).setKind("trades").setLimit(1).build();
        assertThat(statement).isEqualTo(expected);
        List<Trade> results = Collections.singletonList(trade);
        return new DatastoreResultsIterable(results.iterator(), null);
    });
    assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade);
}
Also used : DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.jupiter.api.Test)

Example 15 with DatastoreResultsIterable

use of com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable in project spring-cloud-gcp by GoogleCloudPlatform.

the class SimpleDatastoreRepositoryTests method findByExampleFluentQueryOneValue.

@Test
void findByExampleFluentQueryOneValue() {
    Example<Object> example = Example.of(new Object());
    Iterable entities = Arrays.asList();
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(entities, null)).when(this.datastoreTemplate).queryByExample(same(example), any());
    this.spyRepo.findBy(example, FetchableFluentQuery::oneValue);
    verify(this.spyRepo).findOne(same(example));
}
Also used : FetchableFluentQuery(org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Aggregations

DatastoreResultsIterable (com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable)19 Test (org.junit.jupiter.api.Test)13 Cursor (com.google.cloud.datastore.Cursor)7 GqlQuery (com.google.cloud.datastore.GqlQuery)5 Sort (org.springframework.data.domain.Sort)5 EntityQuery (com.google.cloud.datastore.EntityQuery)4 Parameters (org.springframework.data.repository.query.Parameters)4 DoubleValue (com.google.cloud.datastore.DoubleValue)3 KeyValue (com.google.cloud.datastore.KeyValue)3 LongValue (com.google.cloud.datastore.LongValue)3 Value (com.google.cloud.datastore.Value)3 Slice (org.springframework.data.domain.Slice)3 DatastoreQueryOptions (com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions)2 Page (org.springframework.data.domain.Page)2 FetchableFluentQuery (org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery)2 KeyQuery (com.google.cloud.datastore.KeyQuery)1 StructuredQuery (com.google.cloud.datastore.StructuredQuery)1 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)1 DiscriminatorField (com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorField)1 DatastorePageable (com.google.cloud.spring.data.datastore.repository.query.DatastorePageable)1