Skip to content

Commit

Permalink
revise tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kookubo committed Sep 1, 2023
1 parent d25270c commit 43d1cc8
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,34 +152,34 @@ 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()) {
buffer.clear();
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();
buffer.add(KEY_NAME, key1);
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()) {
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, value3);
}
// {(key1, value2), (key3, value3)}; without PutType means OVERWRITE
Expand All @@ -189,31 +189,31 @@ 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()) {
buffer.clear();
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()) {
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, value3);
}
try (var tx = kvs.beginTransaction().await()) {
buffer.clear();
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);
}
}
Expand All @@ -231,37 +231,37 @@ 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()) {
buffer.clear();
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()) {
buffer.clear();
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()) {
Expand All @@ -270,37 +270,37 @@ 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()) {
buffer.clear();
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()) {
buffer.clear();
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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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
Expand All @@ -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()) {
Expand All @@ -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();
}
}
Expand Down
Loading

0 comments on commit 43d1cc8

Please sign in to comment.