Search in sources :

Example 16 with DatastoreResultsIterable

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

the class SimpleDatastoreRepositoryTests method findAllByExamplePageCursor.

@Test
void findAllByExamplePageCursor() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    Cursor cursor = Cursor.copyFrom("abc".getBytes());
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1, 2), cursor)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort).build()));
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(3, 4), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor).build()));
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)).when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
    Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(0, 2, sort));
    assertThat(result).containsExactly(1, 2);
    assertThat(result.getTotalElements()).isEqualTo(5);
    Page<Object> resultNext = this.simpleDatastoreRepository.findAll(example, result.getPageable().next());
    assertThat(resultNext).containsExactly(3, 4);
    assertThat(resultNext.getTotalElements()).isEqualTo(5);
    verify(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(0).setSort(sort).build()));
    verify(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).setCursor(cursor).build()));
    verify(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
}
Also used : DatastoreQueryOptions(com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions) Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Cursor(com.google.cloud.datastore.Cursor) Test(org.junit.jupiter.api.Test)

Example 17 with DatastoreResultsIterable

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

the class SimpleDatastoreRepositoryTests method findAllByExamplePage.

@Test
void findAllByExamplePage() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1, 2), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).build()));
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1, 2, 3, 4, 5), null)).when(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
    Page<Object> result = this.simpleDatastoreRepository.findAll(example, PageRequest.of(1, 2, sort));
    assertThat(result).containsExactly(1, 2);
    assertThat(result.getTotalElements()).isEqualTo(5);
    verify(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(2).setOffset(2).setSort(sort).build()));
    verify(this.datastoreTemplate).keyQueryByExample(same(example), isNull());
}
Also used : Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Example 18 with DatastoreResultsIterable

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

the class SimpleDatastoreRepositoryTests method findByExampleFluentQuerySortAndFirstValue.

@Test
void findByExampleFluentQuerySortAndFirstValue() {
    Example<Object> example = Example.of(new Object());
    Sort sort = Sort.by("id");
    Iterable entities = Arrays.asList(1);
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(entities, null)).when(this.datastoreTemplate).queryByExample(same(example), any());
    this.spyRepo.findBy(example, q -> q.sortBy(sort).firstValue());
    verify(this.spyRepo).findFirstSorted(same(example), same(sort));
    verify(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setSort(sort).setLimit(1).build()));
}
Also used : DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) DatastoreQueryOptions(com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions) Sort(org.springframework.data.domain.Sort) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Test(org.junit.jupiter.api.Test)

Example 19 with DatastoreResultsIterable

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

the class SimpleDatastoreRepositoryTests method findOneByExample.

@Test
void findOneByExample() {
    Example<Object> example = Example.of(new Object());
    doAnswer(invocationOnMock -> new DatastoreResultsIterable(Arrays.asList(1), null)).when(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(1).build()));
    assertThat(this.simpleDatastoreRepository.findOne(example)).contains(1);
    verify(this.datastoreTemplate).queryByExample(same(example), eq(new DatastoreQueryOptions.Builder().setLimit(1).build()));
}
Also used : 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