Search in sources :

Example 21 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryImplTests method findAllPageableTest.

@Test
public void findAllPageableTest() {
    Pageable pageable = mock(Pageable.class);
    Sort sort = mock(Sort.class);
    when(pageable.getSort()).thenReturn(sort);
    when(pageable.getOffset()).thenReturn(3L);
    when(pageable.getPageSize()).thenReturn(5);
    when(this.template.queryAll(eq(Object.class), any())).thenAnswer((invocation) -> {
        SpannerPageableQueryOptions spannerQueryOptions = invocation.getArgument(1);
        assertThat(spannerQueryOptions.getSort()).isSameAs(sort);
        assertThat(spannerQueryOptions.getOffset()).isEqualTo(3);
        assertThat(spannerQueryOptions.getLimit()).isEqualTo(5);
        return new ArrayList<>();
    });
    new SimpleSpannerRepository<Object, Key>(this.template, Object.class).findAll(pageable);
    verify(this.template, times(1)).queryAll(eq(Object.class), any());
}
Also used : Pageable(org.springframework.data.domain.Pageable) ArrayList(java.util.ArrayList) Sort(org.springframework.data.domain.Sort) Key(com.google.cloud.spanner.Key) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) Test(org.junit.Test)

Example 22 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryImplTests method findAllSortTest.

@Test
public void findAllSortTest() {
    Sort sort = mock(Sort.class);
    when(this.template.queryAll(eq(Object.class), any())).thenAnswer((invocation) -> {
        SpannerPageableQueryOptions spannerQueryOptions = invocation.getArgument(1);
        assertThat(spannerQueryOptions.getSort()).isSameAs(sort);
        return null;
    });
    new SimpleSpannerRepository<Object, Key>(this.template, Object.class).findAll(sort);
    verify(this.template, times(1)).queryAll(eq(Object.class), any());
}
Also used : Sort(org.springframework.data.domain.Sort) Key(com.google.cloud.spanner.Key) SpannerPageableQueryOptions(org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions) Test(org.junit.Test)

Example 23 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerRepositoryImplTests method findAllByIdTest.

@Test
public void findAllByIdTest() {
    List<Key> unconvertedKey = Arrays.asList(Key.of("key1"), Key.of("key2"));
    when(this.entityProcessor.convertToKey(eq(Key.of("key1")))).thenReturn(Key.of("key1"));
    when(this.entityProcessor.convertToKey(eq(Key.of("key2")))).thenReturn(Key.of("key2"));
    when(this.template.read(eq(Object.class), (KeySet) any())).thenAnswer((invocation) -> {
        KeySet keys = invocation.getArgument(1);
        assertThat(keys.getKeys()).containsExactlyInAnyOrder(Key.of("key2"), Key.of("key1"));
        return null;
    });
    new SimpleSpannerRepository<Object, Key>(this.template, Object.class).findAllById(unconvertedKey);
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Example 24 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class SpannerSchemaUtilsTests method getIdTest.

@Test
public void getIdTest() {
    TestEntity t = new TestEntity();
    t.id = "aaa";
    t.embeddedColumns = new EmbeddedColumns();
    t.embeddedColumns.id2 = "2";
    t.id3 = 3L;
    Key expectedKey = Key.newBuilder().append(t.id).appendObject(t.embeddedColumns.id2).append(t.id3).build();
    assertThat(this.spannerSchemaUtils.getKey(t)).isEqualTo(expectedKey);
}
Also used : Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 25 with Key

use of com.google.cloud.spanner.Key in project spring-cloud-gcp by spring-cloud.

the class KeyConversionTests method keyWritingTestCase.

@Test
public void keyWritingTestCase() {
    try {
        Key key = this.spannerEntityWriter.convertToKey(this.objectToTest);
        assertThat(key).isEqualTo(this.expectedResult);
    } catch (Exception ex) {
        assertThat(ex.getClass()).as("Unexpected exception: " + ex + "\nexpected: " + this.expectedResult).isEqualTo(this.expectedResult);
    }
}
Also used : Key(com.google.cloud.spanner.Key) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Test(org.junit.Test)

Aggregations

Key (com.google.cloud.spanner.Key)26 Test (org.junit.Test)16 KeySet (com.google.cloud.spanner.KeySet)8 PrimaryKey (org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey)7 ArrayList (java.util.ArrayList)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 Mutation (com.google.cloud.spanner.Mutation)3 ResultSet (com.google.cloud.spanner.ResultSet)3 SpannerOperations (org.springframework.cloud.gcp.data.spanner.core.SpannerOperations)3 SpannerPageableQueryOptions (org.springframework.cloud.gcp.data.spanner.core.SpannerPageableQueryOptions)3 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)3 Sort (org.springframework.data.domain.Sort)3 PersistentProperty (org.springframework.data.mapping.PersistentProperty)3 Statement (com.google.cloud.spanner.Statement)2 Struct (com.google.cloud.spanner.Struct)2 List (java.util.List)2 StringJoiner (java.util.StringJoiner)2 Pageable (org.springframework.data.domain.Pageable)2 ApiFuture (com.google.api.core.ApiFuture)1 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)1