Skip to content

Commit f4631db

Browse files
committed
test: DbModTest
1 parent fb2663a commit f4631db

File tree

1 file changed

+200
-0
lines changed
  • modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/function

1 file changed

+200
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package com.tsurugidb.iceaxe.test.function;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.IOException;
8+
import java.math.BigDecimal;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.ValueSource;
13+
14+
import com.tsurugidb.iceaxe.test.util.DbTestTableTester;
15+
16+
/**
17+
* mod function test
18+
*/
19+
class DbModTest extends DbTestTableTester {
20+
21+
private static void createTable(String type) throws IOException, InterruptedException {
22+
dropTestTable();
23+
24+
String sql = "create table " + TEST + "(" //
25+
+ " pk int primary key," //
26+
+ " value1 " + type + ", " //
27+
+ " value2 " + type //
28+
+ ")";
29+
executeDdl(getSession(), sql);
30+
31+
int i = 0;
32+
switch (type) {
33+
case "int":
34+
case "bigint":
35+
case "decimal(10)":
36+
insert(i++, "null");
37+
insert(i++, "10");
38+
insert(i++, "0");
39+
insert(i++, "-10");
40+
break;
41+
case "real":
42+
case "double":
43+
case "decimal(10, 1)":
44+
insert(i++, "null");
45+
insert(i++, "10");
46+
insert(i++, "0");
47+
insert(i++, "-10");
48+
insert(i++, "10.5");
49+
insert(i++, "0");
50+
insert(i++, "-10.5");
51+
break;
52+
default:
53+
throw new AssertionError(type);
54+
}
55+
}
56+
57+
private static void insert(int pk, String value) throws IOException, InterruptedException {
58+
var session = getSession();
59+
var insertSql = "insert or replace into " + TEST + " values(" + pk + ", " + value + ", 3)";
60+
try (var ps = session.createStatement(insertSql)) {
61+
var tm = createTransactionManagerOcc(session);
62+
tm.execute(transaction -> {
63+
transaction.executeAndGetCount(ps);
64+
});
65+
}
66+
}
67+
68+
@Test
69+
void nullTest2() throws Exception {
70+
createTable("int");
71+
72+
var sql = "select mod(null, null) from " + TEST;
73+
74+
var session = getSession();
75+
var tm = createTransactionManagerOcc(session);
76+
var list = tm.executeAndGetList(sql);
77+
for (var entity : list) {
78+
Object value = entity.getValueOrNull(0);
79+
assertNull(value);
80+
}
81+
}
82+
83+
@ParameterizedTest
84+
@ValueSource(strings = { "int", "bigint", "real", "double", "decimal(10)", "decimal(10, 1)" })
85+
void nullTest1(String type) throws Exception {
86+
createTable("int");
87+
88+
var session = getSession();
89+
var tm = createTransactionManagerOcc(session);
90+
{
91+
var sql = "select mod(value1, null) from " + TEST;
92+
var list = tm.executeAndGetList(sql);
93+
for (var entity : list) {
94+
Object value = entity.getValueOrNull(0);
95+
assertNull(value);
96+
}
97+
}
98+
{
99+
var sql = "select mod(null, value2) from " + TEST;
100+
var list = tm.executeAndGetList(sql);
101+
for (var entity : list) {
102+
Object value = entity.getValueOrNull(0);
103+
assertNull(value);
104+
}
105+
}
106+
}
107+
108+
@ParameterizedTest
109+
@ValueSource(strings = { "int", "bigint", /* "real", "double", */ "decimal(10)", "decimal(10, 1)" })
110+
void test(String type) throws Exception {
111+
createTable(type);
112+
113+
var sql = "select value1, value2, mod(value1, value2), mod(value1, 3), value1 % value2 from " + TEST;
114+
115+
var tm = createTransactionManagerOcc(getSession());
116+
tm.executeAndForEach(sql, entity -> {
117+
switch (type) {
118+
case "int": {
119+
var expected = mod(entity.getIntOrNull(0), entity.getIntOrNull(1));
120+
assertEquals(expected, entity.getIntOrNull(2));
121+
assertEquals(expected, entity.getIntOrNull(3));
122+
assertEquals(expected, entity.getIntOrNull(4));
123+
break;
124+
}
125+
case "bigint": {
126+
var expected = mod(entity.getLongOrNull(0), entity.getLongOrNull(1));
127+
assertEquals(expected, entity.getLongOrNull(2));
128+
assertEquals(expected, entity.getLongOrNull(3));
129+
assertEquals(expected, entity.getLongOrNull(4));
130+
break;
131+
}
132+
case "real": {
133+
var expected = mod(entity.getFloatOrNull(0), entity.getFloatOrNull(1));
134+
assertEquals(expected, entity.getFloatOrNull(2));
135+
assertEquals(expected, entity.getFloatOrNull(3));
136+
assertEquals(expected, entity.getFloatOrNull(4));
137+
break;
138+
}
139+
case "double": {
140+
var expected = mod(entity.getDoubleOrNull(0), entity.getDoubleOrNull(1));
141+
assertEquals(expected, entity.getDoubleOrNull(2));
142+
assertEquals(expected, entity.getDoubleOrNull(3));
143+
assertEquals(expected, entity.getDoubleOrNull(4));
144+
break;
145+
}
146+
case "decimal(10)":
147+
case "decimal(10, 1)": {
148+
var expected = mod(entity.getDecimalOrNull(0), entity.getDecimalOrNull(1));
149+
if (expected == null) {
150+
assertNull(entity.getDecimalOrNull(2));
151+
assertNull(entity.getDecimalOrNull(3));
152+
assertNull(entity.getDecimalOrNull(4));
153+
} else {
154+
assertTrue(expected.compareTo(entity.getDecimalOrNull(2)) == 0);
155+
assertTrue(expected.compareTo(entity.getDecimalOrNull(3)) == 0);
156+
assertTrue(expected.compareTo(entity.getDecimalOrNull(4)) == 0);
157+
}
158+
break;
159+
}
160+
default:
161+
throw new AssertionError(type);
162+
}
163+
});
164+
}
165+
166+
private Integer mod(Integer value1, Integer value2) {
167+
if (value1 == null || value2 == null) {
168+
return null;
169+
}
170+
return value1 % value2;
171+
}
172+
173+
private Long mod(Long value1, Long value2) {
174+
if (value1 == null || value2 == null) {
175+
return null;
176+
}
177+
return value1 % value2;
178+
}
179+
180+
private Float mod(Float value1, Float value2) {
181+
if (value1 == null || value2 == null) {
182+
return null;
183+
}
184+
return value1 % value2;
185+
}
186+
187+
private Double mod(Double value1, Double value2) {
188+
if (value1 == null || value2 == null) {
189+
return null;
190+
}
191+
return value1 % value2;
192+
}
193+
194+
private BigDecimal mod(BigDecimal value1, BigDecimal value2) {
195+
if (value1 == null || value2 == null) {
196+
return null;
197+
}
198+
return BigDecimal.valueOf(value1.doubleValue() % value2.doubleValue());
199+
}
200+
}

0 commit comments

Comments
 (0)