Search in sources :

Example 11 with Key

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

the class SpannerMutationFactoryImplTests method deleteKeyTest.

@Test
public void deleteKeyTest() {
    Key key = Key.of("key1");
    Mutation mutation = this.spannerMutationFactory.delete(TestEntity.class, key);
    assertThat(mutation.getTable()).isEqualTo("custom_test_table");
    assertThat(mutation.getOperation()).isEqualTo(Op.DELETE);
    List<String> keys = new ArrayList<>();
    mutation.getKeySet().getKeys().forEach((k) -> {
        keys.add((String) (k.getParts().iterator().next()));
    });
    assertThat(keys).containsExactlyInAnyOrder("key1");
}
Also used : ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 12 with Key

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

the class SpannerTemplateTests method existsByIdEmbeddedKeyTest.

@Test
public void existsByIdEmbeddedKeyTest() {
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenReturn(false);
    when(this.readContext.read(any(), any(), any(), any())).thenReturn(results);
    when(this.databaseClient.singleUse(any())).thenReturn(this.readContext);
    Key key = Key.of("key");
    KeySet keySet = KeySet.singleKey(key);
    assertThat(this.spannerTemplate.existsById(TestEntityEmbeddedPK.class, key)).isFalse();
    verify(this.databaseClient, times(1)).singleUse();
    verify(this.readContext, times(1)).read(eq("test_table_embedded_pk"), eq(keySet), eq(Collections.singleton("stringId")));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 13 with Key

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

the class SpannerTemplateTests method findSingleKeyNullTest.

@Test
public void findSingleKeyNullTest() {
    when(this.readContext.read(any(), any(), any())).thenReturn(null);
    Key key = Key.of("key");
    KeySet keys = KeySet.newBuilder().addKey(key).build();
    verifyAfterEvents(new AfterReadEvent(Collections.emptyList(), keys, null), () -> assertThat(this.spannerTemplate.read(TestEntity.class, key)).isNull(), x -> {
    });
    verify(this.databaseClient, times(1)).singleUse();
}
Also used : KeySet(com.google.cloud.spanner.KeySet) AfterReadEvent(org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterReadEvent) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 14 with Key

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

the class SpannerPersistentEntityImplTests method testEmbeddedParentKeys.

@Test
public void testEmbeddedParentKeys() {
    GrandParentEmbedded grandParentEmbedded = new GrandParentEmbedded();
    grandParentEmbedded.id = "1";
    ParentEmbedded parentEmbedded = new ParentEmbedded();
    parentEmbedded.grandParentEmbedded = grandParentEmbedded;
    parentEmbedded.id2 = 2;
    parentEmbedded.id3 = 3L;
    ChildEmbedded childEmbedded = new ChildEmbedded();
    childEmbedded.parentEmbedded = parentEmbedded;
    childEmbedded.id4 = "4";
    // intentionally null, which is a supported key component type.
    childEmbedded.id5 = null;
    Key key = (Key) this.spannerMappingContext.getPersistentEntity(ChildEmbedded.class).getIdentifierAccessor(childEmbedded).getIdentifier();
    assertThat(key).isEqualTo(Key.newBuilder().append("1").append("2").append("3").append("4").appendObject(null).build());
}
Also used : Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Example 15 with Key

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

the class SpannerRepositoryIntegrationTests method existsTest.

@Test
public void existsTest() {
    Trade trade = Trade.aTrade();
    this.tradeRepository.save(trade);
    SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(Trade.class);
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(trade);
    PersistentProperty idProperty = persistentEntity.getIdProperty();
    Key key = (Key) accessor.getProperty(idProperty);
    assertThat(this.tradeRepository.existsById(key)).isTrue();
    this.tradeRepository.delete(trade);
    assertThat(this.tradeRepository.existsById(key)).isFalse();
}
Also used : SubTrade(org.springframework.cloud.gcp.data.spanner.test.domain.SubTrade) Trade(org.springframework.cloud.gcp.data.spanner.test.domain.Trade) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PersistentProperty(org.springframework.data.mapping.PersistentProperty) Key(com.google.cloud.spanner.Key) AbstractSpannerIntegrationTest(org.springframework.cloud.gcp.data.spanner.test.AbstractSpannerIntegrationTest) 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