Skip to content

Commit a4f09d9

Browse files
committed
added more tests with other types. extended nested arrays tests
1 parent edae181 commit a4f09d9

File tree

2 files changed

+109
-7
lines changed

2 files changed

+109
-7
lines changed

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
import org.testng.Assert;
1616
import org.testng.annotations.DataProvider;
1717
import org.testng.annotations.Test;
18+
import wiremock.org.eclipse.jetty.util.log.Log;
1819

20+
import java.math.BigDecimal;
21+
import java.net.Inet4Address;
22+
import java.net.Inet6Address;
1923
import java.nio.charset.StandardCharsets;
2024
import java.sql.Array;
2125
import java.sql.Connection;
2226
import java.sql.DatabaseMetaData;
27+
import java.sql.Date;
2328
import java.sql.JDBCType;
2429
import java.sql.PreparedStatement;
2530
import java.sql.ResultSet;
@@ -28,6 +33,7 @@
2833
import java.sql.Statement;
2934
import java.sql.Struct;
3035
import java.sql.Timestamp;
36+
import java.time.LocalDate;
3137
import java.time.LocalDateTime;
3238
import java.time.ZoneId;
3339
import java.time.temporal.TemporalAccessor;
@@ -36,6 +42,10 @@
3642
import java.util.List;
3743
import java.util.Properties;
3844
import java.util.UUID;
45+
import java.util.function.BiConsumer;
46+
import java.util.function.BiFunction;
47+
import java.util.function.Function;
48+
import java.util.function.Supplier;
3949

4050
import static org.testng.Assert.assertEquals;
4151
import static org.testng.Assert.assertFalse;
@@ -434,6 +444,70 @@ public void testCreateArray() throws SQLException {
434444
}
435445
}
436446

447+
@Test(groups = {"integration"})
448+
public void testCreateArrayDifferentTypes() throws Exception {
449+
try (Connection conn = getJdbcConnection()) {
450+
451+
BiConsumer<String, Object[]> verification = (type, arr) -> {
452+
Array array;
453+
try {
454+
array = conn.createArrayOf(type, arr);
455+
Object[] wrappedArray = (Object[]) array.getArray();
456+
assertEquals(wrappedArray.length, arr.length);
457+
assertEquals(wrappedArray, arr);
458+
} catch (SQLException e) {
459+
fail("Failed to create array of type " + type + " with " + Arrays.toString(arr), e);
460+
throw new RuntimeException(e);
461+
}
462+
};
463+
464+
465+
verification.accept("Int8", new Byte[] {1, 2, 3});
466+
verification.accept("Int16", new Short[] {Short.MIN_VALUE, -1, 0, 1, Short.MAX_VALUE});
467+
verification.accept("Int32", new Integer[] {Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE});
468+
verification.accept("Int64", new Long[] {Long.MIN_VALUE, -1L, 0L, 1L, Long.MAX_VALUE});
469+
verification.accept("UInt8", new Byte[] {0, 1, Byte.MAX_VALUE});
470+
verification.accept("UInt16", new Short[] {0, 1, Short.MAX_VALUE});
471+
verification.accept("UInt32", new Long[] {0L, 1L, (long)Integer.MAX_VALUE});
472+
verification.accept("UInt64", new Long[] {0L, 1L, Long.MAX_VALUE});
473+
verification.accept("Float32", new Float[] {-1.0F, 0.0F, 1.0F});
474+
verification.accept("Float64", new Double[] {-1.0D, 0.0D, 1.0D});
475+
verification.accept("Date", new Date[] {
476+
Date.valueOf(LocalDate.now()),
477+
Date.valueOf(LocalDate.of(2022, 1, 1)),
478+
Date.valueOf(LocalDate.of(2021, 12, 31))
479+
});
480+
verification.accept("DateTime", new Timestamp[] {
481+
Timestamp.valueOf(LocalDateTime.now()),
482+
Timestamp.valueOf(LocalDateTime.of(2022, 1, 1, 0, 0, 0)),
483+
Timestamp.valueOf(LocalDateTime.of(2021, 12, 31, 23, 59, 59))
484+
});
485+
verification.accept("Decimal(10, 2)", new BigDecimal[] {
486+
new BigDecimal("123.45"),
487+
new BigDecimal("-12345.67"),
488+
new BigDecimal("0.00")
489+
});
490+
verification.accept("String", new String[] {
491+
"",
492+
"Hello",
493+
" hello "
494+
});
495+
verification.accept("FixedString(5)", new String[] {
496+
"12345",
497+
"abcde",
498+
" 123"
499+
});
500+
verification.accept("IPv4", new Inet4Address[] {
501+
(Inet4Address) Inet4Address.getByName("127.0.0.1"),
502+
(Inet4Address) Inet4Address.getByName("192.168.0.1"),
503+
});
504+
verification.accept("IPv6", new Inet6Address[] {
505+
(Inet6Address) Inet6Address.getByName("::1"),
506+
(Inet6Address) Inet6Address.getByName("2001:db8::1"),
507+
});
508+
}
509+
}
510+
437511
@Test(groups = { "integration" })
438512
public void testCreateStruct() throws SQLException {
439513
try (Connection conn = this.getJdbcConnection()) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/DataTypeTests.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,15 +1005,43 @@ public void testArrayTypes() throws SQLException {
10051005
@Test(groups = { "integration" })
10061006
public void testNestedArrays() throws Exception {
10071007
try (Connection conn = getJdbcConnection()) {
1008-
try (Statement stmt = conn.createStatement()) {
1009-
try (ResultSet rs = stmt.executeQuery("SELECT [['a', 'b'], ['c', 'd']] as value")) {
1008+
try (PreparedStatement stmt = conn.prepareStatement("SELECT ?::Array(Array(Int32)) as value")) {
1009+
Integer[][] srcArray = new Integer[][] {
1010+
{1, 2, 3},
1011+
{4, 5, 6}
1012+
};
1013+
Array array = conn.createArrayOf("Int32", srcArray);
1014+
stmt.setArray(1, array);
1015+
1016+
try (ResultSet rs = stmt.executeQuery()) {
1017+
assertTrue(rs.next());
1018+
Array arrayHolder = (Array) rs.getObject(1);
1019+
Object[] dbArray = (Object[]) arrayHolder.getArray();
1020+
for (int i = 0; i < dbArray.length; i++) {
1021+
Object[] nestedArray = (Object[]) dbArray[i];
1022+
for (int j = 0; j < nestedArray.length; j++) {
1023+
assertEquals((Integer) nestedArray[j], (Integer)srcArray[i][j]);
1024+
}
1025+
}
1026+
}
1027+
1028+
Integer[] simpleArray = new Integer[] {1, 2, 3};
1029+
Array array1 = conn.createArrayOf("Int32", simpleArray);
1030+
Array array2 = conn.createArrayOf("Int32", simpleArray);
1031+
1032+
Array[] multiLevelArray = new Array[] {array1, array2};
1033+
Array array3 = conn.createArrayOf("Int32", multiLevelArray);
1034+
stmt.setArray(1, array3);
1035+
try (ResultSet rs = stmt.executeQuery()) {
10101036
assertTrue(rs.next());
10111037
Array arrayHolder = (Array) rs.getObject(1);
1012-
Object[] array = (Object[]) arrayHolder.getArray();
1013-
Object[] subArray1 = (Object[]) array[0];
1014-
assertEquals(subArray1, new Object[]{ "a", "b"} );
1015-
Object[] subArray2 = (Object[]) array[1];
1016-
assertEquals(subArray2, new Object[]{ "c", "d"} );
1038+
Object[] dbArray = (Object[]) arrayHolder.getArray();
1039+
for (int i = 0; i < dbArray.length; i++) {
1040+
Object[] nestedArray = (Object[]) dbArray[i];
1041+
for (int j = 0; j < nestedArray.length; j++) {
1042+
assertEquals((Integer) nestedArray[j], (Integer)simpleArray[j]);
1043+
}
1044+
}
10171045
}
10181046
}
10191047
}

0 commit comments

Comments
 (0)