Search in sources :

Example 6 with Mutation

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

the class SpannerMutationFactoryImplTests method upsertTest.

@Test
public void upsertTest() {
    Mutation mutation = this.spannerMutationFactory.upsert(new TestEntity(), null);
    assertEquals("custom_test_table", mutation.getTable());
    assertEquals(Op.INSERT_OR_UPDATE, mutation.getOperation());
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 7 with Mutation

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

the class SpannerMutationFactoryImplTests method deleteEntitiesTest.

@Test
public void deleteEntitiesTest() {
    TestEntity t1 = new TestEntity();
    t1.id = "key1";
    TestEntity t2 = new TestEntity();
    t2.id = "key2";
    Mutation mutation = this.spannerMutationFactory.delete(TestEntity.class, Arrays.asList(t1, t2));
    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(t1.id, t2.id));
}
Also used : ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 8 with Mutation

use of com.google.cloud.spanner.Mutation 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);
    assertEquals("custom_test_table", mutation.getTable());
    assertEquals(Op.DELETE, mutation.getOperation());
    List<String> keys = new ArrayList<>();
    mutation.getKeySet().getKeys().forEach((k) -> {
        keys.add((String) (k.getParts().iterator().next()));
    });
    assertThat(keys, containsInAnyOrder("key1"));
}
Also used : ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Key(com.google.cloud.spanner.Key) Test(org.junit.Test)

Example 9 with Mutation

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

the class SpannerMutationFactoryImplTests method deleteEntityTest.

@Test
public void deleteEntityTest() {
    TestEntity t1 = new TestEntity();
    t1.id = "key1";
    Mutation mutation = this.spannerMutationFactory.delete(t1);
    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(t1.id));
}
Also used : ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 10 with Mutation

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

the class SpannerTemplateTests method upsertColumnsSetTest.

@Test
public void upsertColumnsSetTest() {
    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.upsert(same(entity), eq(Optional.of(cols)))).thenReturn(mutation);
    this.spannerTemplate.upsert(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)

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