Skip to content

Commit

Permalink
test: add integration tests for large batch dml updates. (#2618)
Browse files Browse the repository at this point in the history
* test: add integration tests for large batch dml updates.

* chore: incorporate review comments.

* fix: lint issues.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
arpan14 and gcf-owl-bot[bot] committed Sep 19, 2023
1 parent 6fe132a commit 84cd62f
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -189,4 +191,65 @@ public void errorBatchDmlAlreadyExist() {
}
}
}

@Test
public void largeBatchDml() {
List<Statement> stmts = new LinkedList<>();
for (int i = 0; i < 40; i++) {
stmts.add(Statement.of("INSERT INTO T (k, v) VALUES ('boo" + i + "', " + i + ");"));
}

for (int i = 0; i < 40; i++) {
stmts.add(Statement.of("DELETE FROM T WHERE T.K = 'boo" + i + "';"));
}
long[] expectedRowCounts = new long[stmts.size()];
Arrays.fill(expectedRowCounts, 1L);

final TransactionCallable<long[]> callable = transaction -> transaction.batchUpdate(stmts);
TransactionRunner runner = client.readWriteTransaction();
long[] actualRowCounts = runner.run(callable);
assertThat(actualRowCounts.length).isEqualTo(80);
assertThat(expectedRowCounts).isEqualTo(actualRowCounts);
}

@Test
public void largeBatchDml_withParameterisedStatements() {
List<Statement> stmts = new LinkedList<>();
String insertQuery = "INSERT INTO T(k, v) VALUES(@key, @val)";
for (int i = 0; i < 80; i++) {
stmts.add(
Statement.newBuilder(insertQuery)
.bind("key")
.to("'boo" + i + "'")
.bind("val")
.to(i)
.build());
}
long[] expectedRowCounts = new long[stmts.size()];
Arrays.fill(expectedRowCounts, 1L);

final TransactionCallable<long[]> callable = transaction -> transaction.batchUpdate(stmts);
TransactionRunner runner = client.readWriteTransaction();
long[] actualRowCounts = runner.run(callable);

assertThat(actualRowCounts.length).isEqualTo(80);
assertThat(expectedRowCounts).isEqualTo(actualRowCounts);
}

@Test
public void largeBatchDml_withNonParameterisedStatements() {
List<Statement> stmts = new LinkedList<>();
for (int i = 0; i < 80; i++) {
stmts.add(Statement.of("INSERT INTO T (k, v) VALUES ('boo" + i + "', " + i + ");"));
}
long[] expectedRowCounts = new long[stmts.size()];
Arrays.fill(expectedRowCounts, 1L);

final TransactionCallable<long[]> callable = transaction -> transaction.batchUpdate(stmts);
TransactionRunner runner = client.readWriteTransaction();
long[] actualRowCounts = runner.run(callable);

assertThat(actualRowCounts.length).isEqualTo(80);
assertThat(expectedRowCounts).isEqualTo(actualRowCounts);
}
}

0 comments on commit 84cd62f

Please sign in to comment.