From 43d1cc851eecc6976803e1deb5ff61a381ba2b1a Mon Sep 17 00:00:00 2001 From: Katsuhiko Ohkubo Date: Fri, 1 Sep 2023 15:35:45 +0900 Subject: [PATCH] revise tests --- .../kvs/basic/BasicPutGetRemoveTest.java | 38 ++++----- .../tsubakuro/kvs/basic/DataTypesTest.java | 24 +++--- .../tsubakuro/kvs/basic/GetTest.java | 77 +++++++++++++++++++ .../tsubakuro/kvs/basic/PutTest.java | 19 ++--- .../tsubakuro/kvs/basic/RemoveTest.java | 76 ++++++++++++++++++ .../tsubakuro/kvs/basic/RollbackTest.java | 25 +++--- .../tsubakuro/kvs/util/TestBase.java | 34 +++++--- 7 files changed, 227 insertions(+), 66 deletions(-) create mode 100644 modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/GetTest.java create mode 100644 modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RemoveTest.java diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/BasicPutGetRemoveTest.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/BasicPutGetRemoveTest.java index 586bf5f8..bd5e11de 100644 --- a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/BasicPutGetRemoveTest.java +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/BasicPutGetRemoveTest.java @@ -152,7 +152,7 @@ public void putTypes() throws Exception { buffer.add(VALUE_NAME, value3); var put = kvs.put(tx, TABLE_NAME, buffer, PutType.IF_ABSENT).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } // {(key1, value2), (key3, value3)} try (var tx = kvs.beginTransaction().await()) { @@ -160,10 +160,10 @@ public void putTypes() throws Exception { buffer.add(KEY_NAME, key3); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); checkRecord(get.asRecord(), key3, value3); } - assertEquals(PutType.DEFAULT_BEHAVIOR, PutType.OVERWRITE); + assertEquals(PutType.OVERWRITE, PutType.DEFAULT_BEHAVIOR); // {(key1, value2), (key3, value3)}; without PutType means OVERWRITE try (var tx = kvs.beginTransaction().await()) { buffer.clear(); @@ -171,7 +171,7 @@ public void putTypes() throws Exception { buffer.add(VALUE_NAME, value3); var put = kvs.put(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } // {(key1, value3), (key3, value3)} try (var tx = kvs.beginTransaction().await()) { @@ -179,7 +179,7 @@ public void putTypes() throws Exception { buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); checkRecord(get.asRecord(), key1, value3); } // {(key1, value2), (key3, value3)}; without PutType means OVERWRITE @@ -189,7 +189,7 @@ public void putTypes() throws Exception { buffer.add(VALUE_NAME, value2); var put = kvs.put(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } // {(key1, value3), (key3, value3), (key2, value2)} try (var tx = kvs.beginTransaction().await()) { @@ -197,7 +197,7 @@ public void putTypes() throws Exception { buffer.add(KEY_NAME, key2); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); checkRecord(get.asRecord(), key2, value2); } try (var tx = kvs.beginTransaction().await()) { @@ -205,7 +205,7 @@ public void putTypes() throws Exception { buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); checkRecord(get.asRecord(), key1, value3); } try (var tx = kvs.beginTransaction().await()) { @@ -213,7 +213,7 @@ public void putTypes() throws Exception { buffer.add(KEY_NAME, key3); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); checkRecord(get.asRecord(), key3, value3); } } @@ -231,7 +231,7 @@ public void removeTypes() throws Exception { buffer.add(VALUE_NAME, value); var put = kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } // {(key1, 100)}; remove non-exist key try (var tx = kvs.beginTransaction().await()) { @@ -239,14 +239,14 @@ public void removeTypes() throws Exception { buffer.add(KEY_NAME, key2); var rem = kvs.remove(tx, TABLE_NAME, buffer, RemoveType.COUNTING).await(); kvs.commit(tx).await(); - assertEquals(rem.size(), 0); // means no record removed + assertEquals(0, rem.size()); // means no record removed } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); // (key1, value) exists + assertEquals(1, get.size()); // (key1, value) exists } // {(key1, 100)}; remove exists key try (var tx = kvs.beginTransaction().await()) { @@ -254,14 +254,14 @@ public void removeTypes() throws Exception { buffer.add(KEY_NAME, key1); var rem = kvs.remove(tx, TABLE_NAME, buffer, RemoveType.COUNTING).await(); kvs.commit(tx).await(); - assertEquals(rem.size(), 1); // means 1 record removed + assertEquals(1, rem.size()); // means 1 record removed } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 0); // (key1, value) doesn't exist + assertEquals(0, get.size()); // (key1, value) doesn't exist } // insert again try (var tx = kvs.beginTransaction().await()) { @@ -270,7 +270,7 @@ public void removeTypes() throws Exception { buffer.add(VALUE_NAME, value); var put = kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } // {(key1, 100)}; remove non-exist key try (var tx = kvs.beginTransaction().await()) { @@ -278,14 +278,14 @@ public void removeTypes() throws Exception { buffer.add(KEY_NAME, key2); var rem = kvs.remove(tx, TABLE_NAME, buffer, RemoveType.INSTANT).await(); kvs.commit(tx).await(); - assertEquals(rem.size(), 1); // means 1 remove operation called + assertEquals(1, rem.size()); // means 1 remove operation called } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); // (key1, value) exists + assertEquals(1, get.size()); // (key1, value) exists } // {(key1, 100)}; remove exists key try (var tx = kvs.beginTransaction().await()) { @@ -293,14 +293,14 @@ public void removeTypes() throws Exception { buffer.add(KEY_NAME, key1); var rem = kvs.remove(tx, TABLE_NAME, buffer, RemoveType.INSTANT).await(); kvs.commit(tx).await(); - assertEquals(rem.size(), 1); // means 1 remove operation called + assertEquals(1, rem.size()); // means 1 remove operation called } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 0); // (key1, value) doesn't exist + assertEquals(0, get.size()); // (key1, value) doesn't exist } } } diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/DataTypesTest.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/DataTypesTest.java index c7dc8b2e..54a3d059 100644 --- a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/DataTypesTest.java +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/DataTypesTest.java @@ -20,19 +20,13 @@ public class DataTypesTest extends TestBase { private static final String KEY_NAME = "k1"; private static final String VALUE_NAME = "v1"; - private void createTable(String typeName) throws Exception { - // see jogasaki/docs/value_limit.md - String schema = String.format("%s %s PRIMARY KEY, %s %s", KEY_NAME, typeName, VALUE_NAME, typeName); - createTable(TABLE_NAME, schema); - } - private void checkRecord(Record record, Object key1, Object value1) throws Exception { final int idxKey = 0; // TODO maybe change final int idxValue = 1; - assertEquals(record.getName(idxKey), KEY_NAME); - assertEquals(record.getName(idxValue), VALUE_NAME); - assertEquals(record.getValue(idxKey), key1); - assertEquals(record.getValue(idxValue), value1); + assertEquals(KEY_NAME, record.getName(idxKey)); + assertEquals(VALUE_NAME, record.getName(idxValue)); + assertEquals(key1, record.getValue(idxKey)); + assertEquals(value1, record.getValue(idxValue)); } private void checkPutGet(Object key1, Object value1) throws Exception { @@ -43,22 +37,25 @@ private void checkPutGet(Object key1, Object value1) throws Exception { buffer.add(VALUE_NAME, value1); var put = kvs.put(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); checkRecord(get.asRecord(), key1, value1); } } } private void checkDataType(String typeName, Object key1, Object value1) throws Exception { - createTable(typeName); + // see jogasaki/docs/value_limit.md + String schema = String.format("%s %s PRIMARY KEY, %s %s", KEY_NAME, typeName, VALUE_NAME, typeName); + createTable(TABLE_NAME, schema); checkPutGet(key1, value1); + dropTable(TABLE_NAME); } @Test @@ -111,6 +108,7 @@ public void decimalScaleTest() throws Exception { String schema = String.format("%s %s PRIMARY KEY, %s %s", KEY_NAME, type, VALUE_NAME, type); createTable(TABLE_NAME, schema); checkPutGet(key1, value1); + dropTable(TABLE_NAME); } @Test diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/GetTest.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/GetTest.java new file mode 100644 index 00000000..f568c8e5 --- /dev/null +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/GetTest.java @@ -0,0 +1,77 @@ +package com.tsurugidb.tsubakuro.kvs.basic; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import com.tsurugidb.tsubakuro.kvs.KvsClient; +import com.tsurugidb.tsubakuro.kvs.KvsServiceCode; +import com.tsurugidb.tsubakuro.kvs.KvsServiceException; +import com.tsurugidb.tsubakuro.kvs.RecordBuffer; +import com.tsurugidb.tsubakuro.kvs.util.TestBase; + +public class GetTest extends TestBase { + + private static final String TABLE_NAME = "table" + GetTest.class.getSimpleName(); + private static final String KEY_NAME = "k1"; + private static final String VALUE_NAME = "v1"; + + public GetTest() throws Exception { + String schema = String.format("%s BIGINT PRIMARY KEY, %s BIGINT", KEY_NAME, VALUE_NAME); + createTable(TABLE_NAME, schema); + } + + @Test + public void invalidRequests() throws Exception { + final long key1 = 1L; + final long value1 = 100L; + RecordBuffer key = new RecordBuffer(); + try (var session = getNewSession(); var kvs = KvsClient.attach(session)) { + // COLUMN_TYPE_MISMATCH + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add(KEY_NAME, (int)key1); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.get(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add(KEY_NAME, "aaa"); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.get(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + // COLUMN_NOT_FOUND + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add("hoge", key1); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.get(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_NOT_FOUND, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add(KEY_NAME, key1); + key.add("hoge", key1); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.get(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_NOT_FOUND, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + // INVALID_ARGUMENT + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + // empty key + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.get(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.INVALID_ARGUMENT, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + } + } +} diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/PutTest.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/PutTest.java index 24a67d77..4787c29c 100644 --- a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/PutTest.java +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/PutTest.java @@ -39,7 +39,7 @@ public void invalidRequests() throws Exception { buffer.add(VALUE_NAME, value1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.COLUMN_TYPE_MISMATCH); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } try (var tx = kvs.beginTransaction().await()) { @@ -48,7 +48,7 @@ public void invalidRequests() throws Exception { buffer.add(VALUE_NAME, (int)value1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.COLUMN_TYPE_MISMATCH); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } try (var tx = kvs.beginTransaction().await()) { @@ -57,7 +57,7 @@ public void invalidRequests() throws Exception { buffer.add(VALUE_NAME, (int)value1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.COLUMN_TYPE_MISMATCH); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } try (var tx = kvs.beginTransaction().await()) { @@ -66,24 +66,25 @@ public void invalidRequests() throws Exception { buffer.add(VALUE_NAME, value1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.COLUMN_TYPE_MISMATCH); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } - // COLUMN_NOT_FOUND + // MISMATCH_KEY try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(VALUE_NAME, value1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.COLUMN_NOT_FOUND); + assertEquals(KvsServiceCode.MISMATCH_KEY, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } + // INCOMPLETE_COLUMNS try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.COLUMN_NOT_FOUND); + assertEquals(KvsServiceCode.INCOMPLETE_COLUMNS, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } // INVALID_ARGUMENT @@ -92,7 +93,7 @@ public void invalidRequests() throws Exception { // empty record KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.INVALID_ARGUMENT); + assertEquals(KvsServiceCode.INVALID_ARGUMENT, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } try (var tx = kvs.beginTransaction().await()) { @@ -102,7 +103,7 @@ public void invalidRequests() throws Exception { buffer.add("v2", value1); KvsServiceException ex = assertThrows(KvsServiceException.class, () -> kvs.put(tx, TABLE_NAME, buffer, PutType.OVERWRITE).await()); - assertEquals(ex.getDiagnosticCode(), KvsServiceCode.INVALID_ARGUMENT); + assertEquals(KvsServiceCode.INVALID_ARGUMENT, ex.getDiagnosticCode()); kvs.rollback(tx).await(); } } diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RemoveTest.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RemoveTest.java new file mode 100644 index 00000000..487941a5 --- /dev/null +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RemoveTest.java @@ -0,0 +1,76 @@ +package com.tsurugidb.tsubakuro.kvs.basic; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import com.tsurugidb.tsubakuro.kvs.KvsClient; +import com.tsurugidb.tsubakuro.kvs.KvsServiceCode; +import com.tsurugidb.tsubakuro.kvs.KvsServiceException; +import com.tsurugidb.tsubakuro.kvs.RecordBuffer; +import com.tsurugidb.tsubakuro.kvs.util.TestBase; + +public class RemoveTest extends TestBase { + + private static final String TABLE_NAME = "table" + RemoveTest.class.getSimpleName(); + private static final String KEY_NAME = "k1"; + private static final String VALUE_NAME = "v1"; + + public RemoveTest() throws Exception { + String schema = String.format("%s BIGINT PRIMARY KEY, %s BIGINT", KEY_NAME, VALUE_NAME); + createTable(TABLE_NAME, schema); + } + + @Test + public void invalidRequests() throws Exception { + final long key1 = 1L; + RecordBuffer key = new RecordBuffer(); + try (var session = getNewSession(); var kvs = KvsClient.attach(session)) { + // COLUMN_TYPE_MISMATCH + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add(KEY_NAME, (int)key1); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.remove(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add(KEY_NAME, "aaa"); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.remove(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_TYPE_MISMATCH, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + // COLUMN_NOT_FOUND + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add("hoge", key1); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.remove(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_NOT_FOUND, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + key.add(KEY_NAME, key1); + key.add("hoge", key1); + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.remove(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.COLUMN_NOT_FOUND, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + // INVALID_ARGUMENT + try (var tx = kvs.beginTransaction().await()) { + key.clear(); + // empty key + KvsServiceException ex = assertThrows(KvsServiceException.class, + () -> kvs.remove(tx, TABLE_NAME, key).await()); + assertEquals(KvsServiceCode.INVALID_ARGUMENT, ex.getDiagnosticCode()); + kvs.rollback(tx).await(); + } + } + } +} diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RollbackTest.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RollbackTest.java index 1ab7ccfe..df779144 100644 --- a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RollbackTest.java +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/basic/RollbackTest.java @@ -35,17 +35,17 @@ public void rollbackUpdateAndRemove() throws Exception { buffer.add(VALUE_NAME, value1); var put = kvs.put(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); var record = get.asRecord(); - assertEquals(record.getLong(KEY_NAME), key1); - assertEquals(record.getLong(VALUE_NAME), value1); + assertEquals(key1, record.getLong(KEY_NAME)); + assertEquals(value1, record.getLong(VALUE_NAME)); } // update and abort try (var tx = kvs.beginTransaction().await()) { @@ -54,36 +54,35 @@ var record = get.asRecord(); buffer.add(VALUE_NAME, value2); var put = kvs.put(tx, TABLE_NAME, buffer).await(); kvs.rollback(tx).await(); - assertEquals(put.size(), 1); + assertEquals(1, put.size()); } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); var record = get.asRecord(); - assertEquals(record.getLong(KEY_NAME), key1); - assertEquals(record.getLong(VALUE_NAME), value1); // not value2 + assertEquals(key1, record.getLong(KEY_NAME)); + assertEquals(value1, record.getLong(VALUE_NAME)); // not value2 } // remove and abort try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); - buffer.add(VALUE_NAME, value2); var remove = kvs.remove(tx, TABLE_NAME, buffer).await(); kvs.rollback(tx).await(); - assertEquals(remove.size(), 1); + assertEquals(1, remove.size()); } try (var tx = kvs.beginTransaction().await()) { buffer.clear(); buffer.add(KEY_NAME, key1); var get = kvs.get(tx, TABLE_NAME, buffer).await(); kvs.commit(tx).await(); - assertEquals(get.size(), 1); + assertEquals(1, get.size()); var record = get.asRecord(); - assertEquals(record.getLong(KEY_NAME), key1); // found key1, not removed - assertEquals(record.getLong(VALUE_NAME), value1); // not value2 + assertEquals(key1, record.getLong(KEY_NAME)); // found key1, not removed + assertEquals(value1, record.getLong(VALUE_NAME)); // not value2 } } } diff --git a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/util/TestBase.java b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/util/TestBase.java index 31920584..873fc350 100644 --- a/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/util/TestBase.java +++ b/modules/kvs/src/inttest/java/com/tsurugidb/tsubakuro/kvs/util/TestBase.java @@ -26,20 +26,30 @@ public Session getNewSession() throws IOException, ServerException, InterruptedE return SessionBuilder.connect(ENDPOINT).withCredential(NullCredential.INSTANCE).create(); } + private void dropTable(SqlClient client, String tableName) throws Exception { + try { + try (var tx = client.createTransaction().await()) { + String sql = String.format("DROP TABLE %s", tableName); + tx.executeStatement(sql).await(); + tx.commit().await(); + } + } catch (Exception e) { + var msg = e.getMessage(); + if (!msg.contains("table_not_found") && !msg.contains("not found")) { + throw e; + } + } + } + + public void dropTable(String tableName) throws Exception { + try (var session = getNewSession(); var client = SqlClient.attach(session)) { + dropTable(client, tableName); + } + } + public void createTable(String tableName, String schema) throws Exception { try (var session = getNewSession(); var client = SqlClient.attach(session)) { - try { - try (var tx = client.createTransaction().await()) { - String sql = String.format("DROP TABLE %s", tableName); - tx.executeStatement(sql).await(); - tx.commit().await(); - } - } catch (Exception e) { - var msg = e.getMessage(); - if (!msg.contains("table_not_found") && !msg.contains("not found")) { - throw e; - } - } + dropTable(client, tableName); try (var tx = client.createTransaction().await()) { String sql = String.format("CREATE TABLE %s (%s)", tableName, schema); tx.executeStatement(sql).await();