Search in sources :

Example 36 with Mutation

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

the class SpannerMutationFactoryImplTests method deleteKeysTest.

@Test
public void deleteKeysTest() {
    KeySet keySet = KeySet.newBuilder().addKey(Key.of("key1")).addKey(Key.of("key2")).build();
    Mutation mutation = this.spannerMutationFactory.delete(TestEntity.class, keySet);
    assertEquals("custom_test_table", mutation.getTable());
    assertEquals(Op.DELETE, mutation.getOperation());
    List<String> keys = new ArrayList<>();
    mutation.getKeySet().getKeys().forEach((key) -> {
        keys.add((String) (key.getParts().iterator().next()));
    });
    assertThat(keys, containsInAnyOrder("key1", "key2"));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 37 with Mutation

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

the class SpannerTemplateTests method updateColumnsSetTest.

@Test
public void updateColumnsSetTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    Set<String> cols = new HashSet<>(Arrays.asList(new String[] { "a", "b" }));
    when(this.mutationFactory.update(same(entity), eq(Optional.of(cols)))).thenReturn(mutation);
    this.spannerTemplate.update(entity, Optional.of(cols));
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 38 with Mutation

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

the class SpannerTemplateTests method deleteKeysTest.

@Test
public void deleteKeysTest() {
    KeySet keys = KeySet.newBuilder().addKey(Key.of("key1")).addKey(Key.of("key2")).build();
    Mutation mutation = Mutation.delete("custom_test_table", keys);
    when(this.mutationFactory.delete(eq(TestEntity.class), same(keys))).thenReturn(mutation);
    this.spannerTemplate.delete(TestEntity.class, keys);
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 39 with Mutation

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

the class SpannerTemplateTests method updateColumnsArrayTest.

@Test
public void updateColumnsArrayTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    when(this.mutationFactory.update(same(entity), eq(Optional.of(new HashSet<>(Arrays.asList(new String[] { "a", "b" })))))).thenReturn(mutation);
    this.spannerTemplate.update(entity, "a", "b");
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 40 with Mutation

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

the class SpannerTemplateTests method deleteByKeyTest.

@Test
public void deleteByKeyTest() {
    Key key = Key.of("key");
    Mutation mutation = Mutation.delete("custom_test_table", key);
    when(this.mutationFactory.delete(eq(TestEntity.class), same(key))).thenReturn(mutation);
    this.spannerTemplate.delete(TestEntity.class, key);
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Aggregations

Mutation (com.google.cloud.spanner.Mutation)70 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)11 CommitResponse (com.google.cloud.spanner.CommitResponse)4 KeySet (com.google.cloud.spanner.KeySet)4 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)4 Timestamp (com.google.cloud.Timestamp)3 DatabaseClient (com.google.cloud.spanner.DatabaseClient)3 ResultSet (com.google.cloud.spanner.ResultSet)3 TransactionContext (com.google.cloud.spanner.TransactionContext)3 BatchableMutationFilterFn (org.apache.beam.sdk.io.gcp.spanner.SpannerIO.BatchableMutationFilterFn)3 Sleeper (org.apache.beam.sdk.util.Sleeper)3 Database (com.google.cloud.spanner.Database)2 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)2 Key (com.google.cloud.spanner.Key)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 SpannerException (com.google.cloud.spanner.SpannerException)2 TransactionRunner (com.google.cloud.spanner.TransactionRunner)2 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)2 BigDecimal (java.math.BigDecimal)2