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");
}
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")));
}
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();
}
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());
}
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();
}
Aggregations