Skip to content

Commit

Permalink
test: Added DbDecimal0Test
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed May 9, 2024
1 parent 9db1574 commit 38c438b
Show file tree
Hide file tree
Showing 5 changed files with 654 additions and 95 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.tsurugidb.iceaxe.test.type;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;

import java.io.IOException;
import java.time.LocalDate;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable;
import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables;
import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping;
import com.tsurugidb.iceaxe.sql.result.TgResultMapping;
import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity;
import com.tsurugidb.iceaxe.test.util.DbTestTableTester;
import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException;
import com.tsurugidb.tsubakuro.sql.SqlServiceCode;

/**
* date test
*/
class DbDateTest extends DbTestTableTester {

private static final int SIZE = 5;

@BeforeEach
void beforeEach(TestInfo info) throws Exception {
logInitStart(info);

dropTestTable();
createTable();
insert(SIZE);

logInitEnd(info);
}

private static void createTable() throws IOException, InterruptedException {
String sql = "create table " + TEST + "(" //
+ " pk int primary key," //
+ "value date" //
+ ")";
var session = getSession();
executeDdl(session, sql);
}

private static void insert(int size) throws IOException, InterruptedException {
var session = getSession();
var insertSql = "insert into " + TEST + " values(:pk, :value)";
var insertMapping = TgParameterMapping.of(TgBindVariables.of().addInt("pk").addDate("value"));
try (var ps = session.createStatement(insertSql, insertMapping)) {
var tm = createTransactionManagerOcc(session);
tm.execute(transaction -> {
for (int i = 0; i < size; i++) {
var parameter = TgBindParameters.of().addInt("pk", i).addDate("value", LocalDate.of(2024, 5, size - i));
transaction.executeAndGetCount(ps, parameter);
}
return;
});
}
}

@Test
void bindWhereEq() throws Exception {
var variable = TgBindVariable.ofDate("value");
var sql = "select * from " + TEST + " where value=" + variable;
var mapping = TgParameterMapping.of(variable);

var session = getSession();
var tm = createTransactionManagerOcc(session);
try (var ps = session.createQuery(sql, mapping)) {
var date = LocalDate.of(2024, 5, 2);
var parameter = TgBindParameters.of(variable.bind(date));
var entity = tm.executeAndFindRecord(ps, parameter).get();
assertEquals(date, entity.getDate("value"));
}
}

@Test
void bindWhereRange() throws Exception {
var start = TgBindVariable.ofDate("start");
var end = TgBindVariable.ofDate("end");
var sql = "select * from " + TEST + " where " + start + "<=value and value<=" + end + " order by value";
var mapping = TgParameterMapping.of(start, end);

var session = getSession();
var tm = createTransactionManagerOcc(session);
try (var ps = session.createQuery(sql, mapping)) {
var parameter = TgBindParameters.of(start.bind(LocalDate.of(2024, 5, 2)), end.bind(LocalDate.of(2024, 5, 3)));
var list = tm.executeAndGetList(ps, parameter);
assertEquals(2, list.size());
assertEquals(LocalDate.of(2024, 5, 2), list.get(0).getDate("value"));
assertEquals(LocalDate.of(2024, 5, 3), list.get(1).getDate("value"));
}
}

@Test
@Disabled // TODO implicit conversion: char to date
void implicitConversion() throws Exception {
var session = getSession();
String sql = "select * from " + TEST + " where value = '2024-05-02'";
var tm = createTransactionManagerOcc(session);
TsurugiResultEntity entity = tm.executeAndFindRecord(sql).get();
assertEquals(LocalDate.of(2024, 5, 2), entity.getDate("value"));
}

@Test
@Disabled // TODO cast as date
void cast() throws Exception {
var session = getSession();
String sql = "update " + TEST + " set value = cast('2024-05-07' as date)";
var tm = createTransactionManagerOcc(session);
int count = tm.executeAndGetCount(sql);
assertEquals(SIZE, count);
}

@Test
void min() throws Exception {
var session = getSession();
String sql = "select min(value) from " + TEST;
var resultMapping = TgResultMapping.ofSingle(LocalDate.class);
var tm = createTransactionManagerOcc(session);
LocalDate result = tm.executeAndFindRecord(sql, resultMapping).get();
assertEquals(LocalDate.of(2024, 5, 1), result);
}

@Test
void max() throws Exception {
var session = getSession();
String sql = "select max(value) from " + TEST;
var resultMapping = TgResultMapping.ofSingle(LocalDate.class);
var tm = createTransactionManagerOcc(session);
LocalDate result = tm.executeAndFindRecord(sql, resultMapping).get();
assertEquals(LocalDate.of(2024, 5, SIZE), result);
}

@Test
void sum() throws Exception {
var session = getSession();
String sql = "select sum(value) from " + TEST;
var resultMapping = TgResultMapping.ofSingle(LocalDate.class);
var tm = createTransactionManagerOcc(session);
var e = assertThrowsExactly(TsurugiTmIOException.class, () -> {
tm.executeAndFindRecord(sql, resultMapping);
});
assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e);
assertContains("function 'sum' is not found", e.getMessage());
}
}
Loading

0 comments on commit 38c438b

Please sign in to comment.