Skip to content

Commit c0e2c34

Browse files
committed
prepare for test compatibility
1 parent db3663c commit c0e2c34

11 files changed

Lines changed: 182 additions & 87 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ target/
77
/databend/
88
.databend/
99
tests/data
10+
tests/compatibility/*.jar
11+
test-output
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.databend.jdbc;
2+
3+
import com.vdurmont.semver4j.Semver;
4+
5+
import java.sql.Connection;
6+
import java.sql.DriverManager;
7+
import java.sql.SQLException;
8+
import java.util.Properties;
9+
10+
public class Compatibility {
11+
public static class Capability {
12+
boolean streamingLoad;
13+
14+
public Capability() {
15+
this.streamingLoad = true;
16+
}
17+
public Capability(boolean streamingLoad) {
18+
this.streamingLoad = streamingLoad;
19+
}
20+
21+
public static Capability fromServerVersion(Semver ver) {
22+
boolean streamingLoad = ver.isGreaterThan(new Semver("1.2.781"));
23+
return new Capability(streamingLoad);
24+
}
25+
26+
public static Capability fromDriverVersion(Semver ver) {
27+
boolean streamingLoad = ver.isGreaterThan(new Semver("0.4.1"));
28+
return new Capability(streamingLoad);
29+
}
30+
}
31+
32+
static String port = System.getenv("DATABEND_TEST_CONN_PORT") != null ? System.getenv("DATABEND_TEST_CONN_PORT").trim() : "8000";
33+
public static Semver driverVersion = getDriverVersion();
34+
public static Semver serverVersion = getServerVersion();
35+
public static Capability driverCapability = driverVersion==null? new Capability(): Capability.fromDriverVersion(driverVersion);
36+
public static Capability serverCapability = serverVersion==null? new Capability(): Capability.fromServerVersion(serverVersion);
37+
38+
private static Semver getDriverVersion() {
39+
String env = System.getenv("DATABEND_TEST_DRIVER_VERSION");
40+
if (env == null) {
41+
return null;
42+
}
43+
return new Semver(env);
44+
}
45+
private static Semver getServerVersion() {
46+
String env = System.getenv("DATABEND_TEST_SERVER_VERSION");
47+
if (env == null) {
48+
return null;
49+
}
50+
return new Semver(env);
51+
}
52+
53+
public static boolean skipDriverBug(String version) {
54+
if (driverVersion.isLowerThan(new Semver(version))) {
55+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
56+
String callerName = stackTrace[2].getMethodName();
57+
System.out.println("SkipDriverBug version=" + version + ", method=" + callerName);
58+
return true;
59+
}
60+
return false;
61+
}
62+
}

databend-jdbc/src/test/java/com/databend/jdbc/TestBasicDriver.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.databend.client.DatabendSession;
44
import com.databend.client.PaginationOptions;
5+
import com.vdurmont.semver4j.Semver;
56
import org.testng.Assert;
67
import org.testng.annotations.BeforeTest;
78
import org.testng.annotations.Test;
@@ -32,8 +33,6 @@ public void setUp()
3233
c.createStatement().execute("create table test_basic_driver.table1(i int)");
3334
c.createStatement().execute("insert into test_basic_driver.table1 values(1)");
3435
c.createStatement().execute("create database test_basic_driver_2");
35-
c.createStatement().execute("create table test_basic_driver.table_with_null(a int,b varchar default null, c varchar, d varchar)");
36-
c.createStatement().execute("insert into test_basic_driver.table_with_null(a,b,c,d) values(1,null,'null','NULL')");
3736
}
3837

3938
@Test(groups = {"IT"})
@@ -153,8 +152,8 @@ public void TestMergeInto() throws SQLException {
153152

154153
Assert.assertTrue(r.next());
155154
Assert.assertEquals(3, statement.getUpdateCount());
156-
} catch (SQLException throwables) {
157-
throwables.printStackTrace();
155+
} catch (SQLException throwable) {
156+
throwable.printStackTrace();
158157
}
159158
}
160159

@@ -169,7 +168,7 @@ public void testWriteDouble() throws SQLException {
169168
" City VARCHAR(50),\n" +
170169
" Score DOUBLE\n" +
171170
");");
172-
Double infDouble = Double.POSITIVE_INFINITY;
171+
double infDouble = Double.POSITIVE_INFINITY;
173172

174173
String sql = "INSERT INTO test_basic_driver.table_double (ID, Name, Age, City, Score) values";
175174
PreparedStatement prepareStatement = connection.prepareStatement(sql);
@@ -192,17 +191,25 @@ public void testWriteDouble() throws SQLException {
192191

193192
@Test(groups = {"IT"})
194193
public void testDefaultSelectNullValue() throws SQLException {
195-
try (Connection connection = Utils.createConnection()) {
196-
DatabendStatement statement = (DatabendStatement) connection.createStatement();
197-
statement.executeQuery("SELECT a,b,c,d from test_basic_driver.table_with_null");
194+
try (Connection connection = Utils.createConnection();
195+
Statement statement = connection.createStatement()
196+
) {
197+
statement.execute("create table test_basic_driver.table_with_null(a int,b varchar default null, c varchar, d varchar)");
198+
statement.execute("insert into test_basic_driver.table_with_null(a,b,c,d) values(1,null,'null','NULL')");
199+
statement.execute("SELECT a,b,c,d from test_basic_driver.table_with_null");
198200
ResultSet r = statement.getResultSet();
199201
r.next();
200202
Assert.assertEquals(r.getInt(1), 1);
201203
Assert.assertNull(r.getObject(2));
202204
Assert.assertEquals(r.getObject(3), "null");
203-
Assert.assertEquals(r.getObject(4), "NULL");
204-
} catch (SQLException throwables) {
205-
throwables.printStackTrace();
205+
if (Compatibility.driverVersion.isGreaterThanOrEqualTo(new Semver("0.3.9"))) {
206+
Assert.assertEquals(r.getObject(4), "NULL");
207+
} else {
208+
// bug
209+
Assert.assertNull(r.getObject(4));
210+
}
211+
} catch (SQLException throwable) {
212+
throwable.printStackTrace();
206213
}
207214
}
208215

@@ -293,7 +300,7 @@ public void testUpdateSession()
293300
public void testResultException() {
294301
try (Connection connection = Utils.createConnection()) {
295302
Statement statement = connection.createStatement();
296-
ResultSet r = statement.executeQuery("SELECT 1e189he 198h");
303+
statement.execute("SELECT 1e189he 198h");
297304
} catch (SQLException e) {
298305
Assert.assertTrue(e.getMessage().contains("Query failed"));
299306
}

databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public void testVersion() throws SQLException {
7878
DatabaseMetaData metaData = c.getMetaData();
7979
int major = metaData.getDriverMajorVersion();
8080
int minor = metaData.getDriverMinorVersion();
81-
assertEquals(major, 0);
82-
assertEquals(minor, 4);
81+
assertEquals(major, (int) Compatibility.driverVersion.getMajor());
82+
assertEquals(minor, (int) Compatibility.driverVersion.getMinor());
8383
}
8484
}
8585

@@ -111,7 +111,7 @@ public void testGetDatabaseProductVersion()
111111
String checkVersion = String.format("v%.1f.%d", majorVersion, minorVersion);
112112
Assert.assertTrue(metaData.getDatabaseProductVersion().contains(checkVersion));
113113

114-
if (Utils.serverCapability.streamingLoad && Utils.driverCapability.streamingLoad) {
114+
if (Compatibility.serverCapability.streamingLoad && Compatibility.driverCapability.streamingLoad) {
115115
DatabendConnection conn = connection.unwrap(DatabendConnection.class);
116116
if (conn.getServerVersion() != null) {
117117
String semver = "v" + conn.getServerVersion().toString();

databend-jdbc/src/test/java/com/databend/jdbc/TestFileTransfer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,13 @@ public void testLoadStreamToTableWithStreaming() {
191191
}
192192

193193
public void testLoadStreamToTableInner(String method) {
194-
if (!Utils.driverCapability.streamingLoad) {
195-
throw new SkipException("driver version too low");
194+
if (!Compatibility.driverCapability.streamingLoad) {
195+
System.out.println("Skip testLoadStreamToTableInner: driver version too low");
196+
return;
196197
}
197-
if (!Utils.serverCapability.streamingLoad) {
198-
throw new SkipException("server version too low");
198+
if (!Compatibility.serverCapability.streamingLoad) {
199+
System.out.println("Skip testLoadStreamToTableInner: server version too low");
200+
return;
199201
}
200202
System.out.println("testLoadStreamToTableInner " + method);
201203
String filePath = generateRandomCSVComplex(10);

databend-jdbc/src/test/java/com/databend/jdbc/TestMultiHost.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public void testAutoDiscoveryUriParsing() throws SQLException {
148148
Assert.assertEquals(uris.size(), 3);
149149
Assert.assertEquals(uris2.size(), 3);
150150
Assert.assertEquals(uris2, uris);
151-
152151
}
153152

154153
private HashMap<Integer, Integer> get_hosts_used(String dsn) throws SQLException {

databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ public void TestBatchDelete() throws SQLException {
162162
deletePs.addBatch();
163163
int[] ansDel = deletePs.executeBatch();
164164
Assert.assertEquals(ansDel.length, 1);
165-
// todo: fix this
166-
Assert.assertEquals(ansDel[0], 0);
165+
// todo: fix this, currently == 0
166+
// Assert.assertEquals(ansDel[0], 1);
167167

168168
System.out.println("execute select");
169169
statement.execute("SELECT * from test_batch_delete");
@@ -611,44 +611,49 @@ public void testExecuteUpdate() throws SQLException {
611611

612612
@Test(groups = "IT")
613613
public void testInsertWithSelect() throws SQLException {
614+
if (Compatibility.skipDriverBug("0.3.9")) {
615+
return;
616+
}
614617
Connection conn = Utils.createConnection();
615-
conn.createStatement().execute("delete from test_prepare_statement");
616-
617-
String insertSql = "insert into test_prepare_statement select a, b from test_prepare_statement where b = ?";
618-
try (PreparedStatement statement = conn.prepareStatement(insertSql)) {
619-
statement.setString(1, "a");
620-
int insertedRows = statement.executeUpdate();
618+
Statement statement = conn.createStatement();
619+
statement.execute("use test_prepare_statement");
620+
statement.execute("create or replace table insert_with_select (a int, b string)");
621+
622+
String insertSql = "insert into insert_with_select select a, b from insert_with_select where b = ?";
623+
try (PreparedStatement ps = conn.prepareStatement(insertSql)) {
624+
ps.setString(1, "a");
625+
int insertedRows = ps.executeUpdate();
621626
Assert.assertEquals(0, insertedRows, "should not insert any rows as the table is empty");
622627
}
623628

624629
// Insert some data
625-
String insertDataSql = "insert into test_prepare_statement values (?,?)";
626-
try (PreparedStatement statement = conn.prepareStatement(insertDataSql)) {
627-
statement.setInt(1, 1);
628-
statement.setString(2, "a");
629-
statement.executeUpdate();
630+
String insertDataSql = "insert into insert_with_select values (?,?)";
631+
try (PreparedStatement ps = conn.prepareStatement(insertDataSql)) {
632+
ps.setInt(1, 1);
633+
ps.setString(2, "a");
634+
int insertedRows = ps.executeUpdate();
635+
Assert.assertEquals(1, insertedRows, "should insert 1 rows");
630636

631-
statement.setInt(1, 2);
632-
statement.setString(2, "b");
633-
statement.executeUpdate();
637+
ps.setInt(1, 2);
638+
ps.setString(2, "b");
639+
insertedRows = ps.executeUpdate();
640+
Assert.assertEquals(1, insertedRows, "should insert 1 rows");
634641
}
635642

636643
// Now try to insert again with select
637-
try (PreparedStatement statement = conn.prepareStatement(insertSql)) {
638-
statement.setString(1, "a");
639-
int insertedRows = statement.executeUpdate();
640-
Assert.assertEquals(1, insertedRows, "should insert two rows from the select");
644+
try (PreparedStatement ps = conn.prepareStatement(insertSql)) {
645+
ps.setString(1, "a");
646+
int insertedRows = ps.executeUpdate();
647+
Assert.assertEquals(1, insertedRows, "should insert 1 row from the select");
641648
}
642649

643-
ResultSet rs = conn.createStatement().executeQuery("select * from test_prepare_statement order by a");
650+
ResultSet rs = conn.createStatement().executeQuery("select * from insert_with_select order by a");
644651
int count = 0;
645652
while (rs.next()) {
646653
count++;
647654
}
648-
Assert.assertEquals(3, count, "should have four rows in the table after insert with select");
649-
650-
// Clean up
651-
conn.createStatement().execute("delete from test_prepare_statement");
655+
Assert.assertEquals(3, count, "should have 3 rows in the table after insert with select");
656+
conn.close();
652657
}
653658

654659
}

databend-jdbc/src/test/java/com/databend/jdbc/TestStageAttachment.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ public void TestStageAttachment() {
1515
StageAttachment attachment = new StageAttachment.Builder().setLocation(stagePath)
1616
.build();
1717
assertEquals("StageAttachment{location=@~/prefix/uuid/test, file_format_options={type=CSV}, copy_options=null}", attachment.toString());
18-
1918
}
2019
}

databend-jdbc/src/test/java/com/databend/jdbc/Utils.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,19 @@
88
import java.util.Properties;
99

1010
public class Utils {
11-
public static class Capability {
12-
boolean streamingLoad;
13-
14-
public Capability() {
15-
this.streamingLoad = true;
16-
}
17-
public Capability(boolean streamingLoad) {
18-
this.streamingLoad = streamingLoad;
19-
}
20-
21-
public static Capability fromServerVersion(Semver ver) {
22-
boolean streamingLoad = ver.isGreaterThan(new Semver("1.2.781"));
23-
return new Capability(streamingLoad);
24-
}
25-
26-
public static Capability fromDriverVersion(Semver ver) {
27-
boolean streamingLoad = ver.isGreaterThan(new Semver("0.4.1"));
28-
return new Capability(streamingLoad);
29-
}
30-
}
31-
3211

3312
static String port = System.getenv("DATABEND_TEST_CONN_PORT") != null ? System.getenv("DATABEND_TEST_CONN_PORT").trim() : "8000";
34-
public static Semver driverVersion = getDriverVersion();
35-
public static Semver serverVersion = getServerVersion();
36-
public static Capability driverCapability = driverVersion==null? new Capability(): Capability.fromDriverVersion(driverVersion);
37-
public static Capability serverCapability = serverVersion==null? new Capability(): Capability.fromServerVersion(serverVersion);
38-
3913
static String username = "databend";
4014
static String password = "databend";
15+
4116
public static String baseURL() {
4217
return "jdbc:databend://localhost:" + port;
4318
}
4419

4520
public static String getUsername() {
4621
return username;
4722
}
23+
4824
public static String getPassword() {
4925
return password;
5026
}
@@ -70,19 +46,5 @@ public static Connection createConnectionWithPresignedUrlDisable() throws SQLExc
7046
String url = baseURL() + "?presigned_url_disabled=true";
7147
return DriverManager.getConnection(url, "databend", "databend");
7248
}
73-
74-
private static Semver getDriverVersion() {
75-
String env = System.getenv("DATABEND_TEST_DRIVER_VERSION");
76-
if (env == null) {
77-
return null;
78-
}
79-
return new Semver(env);
80-
}
81-
private static Semver getServerVersion() {
82-
String env = System.getenv("DATABEND_TEST_SERVER_VERSION");
83-
if (env == null) {
84-
return null;
85-
}
86-
return new Semver(env);
87-
}
8849
}
50+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
set -exo pipefail
4+
5+
#curl -sSLfo ./testng.jar https://repo.maven.apache.org/maven2/org/testng/testng/7.11.0/testng-7.11.0.jar
6+
#curl -sSLfo ./semver4j.jar https://repo1.maven.org/maven2/com/vdurmont/semver4j/3.1.0/semver4j-3.1.0.jar
7+
#curl -sSLfo ./jcommander.jar https://repo1.maven.org/maven2/org/jcommander/jcommander/1.83/jcommander-1.83.jar
8+
#curl -sSLfo ./jts-core.jar https://repo1.maven.org/maven2/org/locationtech/jts/jts-core/1.19.0/jts-core-1.19.0.jar
9+
#curl -sSLfo ./slf4j-api.jar https://repo1.maven.org/maven2/org/slf4j/slf4j-api/2.0.16/slf4j-api-2.0.16.jar
10+
11+
TEST_VER=0.4.0
12+
JDBC_VER=${DATABEND_TEST_DRIVER_VERSION:-0.4.0}
13+
14+
# for dev
15+
# 1. run `mvn clean package -DskipTests`
16+
# 2. set TEST_DEV=1
17+
# 3. unset DATABEND_TEST_DRIVER_VERSION
18+
19+
TEST_DEV=${TEST_DEV:-0}
20+
21+
if [ "$TEST_DEV" = "1" ]; then
22+
curl -sSLfo ./databend-jdbc-tests.jar "https://github.com/databendlabs/databend-jdbc/releases/download/v${TEST_VER}/databend-jdbc-${TEST_VER}-tests.jar"
23+
else
24+
cp ../../databend-jdbc/target/databend-jdbc-${TEST_VER}-tests.jar databend-jdbc-tests.jar
25+
fi
26+
27+
if [ -z "$DATABEND_TEST_DRIVER_VERSION" ]; then
28+
cp ../../databend-jdbc/target/databend-jdbc-${JDBC_VER}.jar databend-jdbc.jar
29+
else
30+
curl -sSLfo "./databend-jdbc-${JDBC_VER}.jar" "https://github.com/databendlabs/databend-jdbc/releases/download/v${JDBC_VER}/databend-jdbc-${JDBC_VER}.jar"
31+
fi
32+
33+
export DATABEND_TEST_DRIVER_VERSION=$JDBC_VER
34+
java -Dlogback.logger.root=INFO -cp "testng.jar:slf4j-api.jar:databend-jdbc-${JDBC_VER}.jar:databend-jdbc-tests.jar:jcommander.jar:semver4j.jar" org.testng.TestNG testng.xml

0 commit comments

Comments
 (0)