Skip to content

Commit a79b7b5

Browse files
committed
polish tests.
1 parent b056d7a commit a79b7b5

4 files changed

Lines changed: 128 additions & 90 deletions

File tree

databend-jdbc/src/main/java/com/databend/jdbc/FileTransferAPI.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface FileTransferAPI {
1818
* @param compressData whether to compress the data
1919
* @throws SQLException failed to upload input stream
2020
*/
21-
public void uploadStream(String stageName, String destPrefix, InputStream inputStream, String destFileName, long fileSize, boolean compressData) throws SQLException;
21+
void uploadStream(String stageName, String destPrefix, InputStream inputStream, String destFileName, long fileSize, boolean compressData) throws SQLException;
2222

2323
/**
2424
* Download a file from the databend internal stage, the data would be downloaded as one file with no split.
@@ -27,18 +27,18 @@ public interface FileTransferAPI {
2727
* @param sourceFileName the file name in the stage
2828
* @param decompress whether to decompress the data
2929
* @return the input stream of the file
30-
* @throws SQLException
30+
* @throws SQLException failed to download input stream
3131
*/
32-
public InputStream downloadStream(String stageName, String sourceFileName, boolean decompress) throws SQLException;
32+
InputStream downloadStream(String stageName, String sourceFileName, boolean decompress) throws SQLException;
3333

3434
/**
3535
* Copy into the target table from files on the internal stage
36-
* Documentation: https://databend.rs/doc/sql-commands/dml/dml-copy-into-table
36+
* Documentation: <a href="https://databend.rs/doc/sql-commands/dml/dml-copy-into-table">...</a>
3737
*
3838
* @param database the target table's database
3939
* @param tableName the target table name
4040
* @param params copy options and file options
41-
* @throws SQLException
41+
* @throws SQLException fail to copy into table
4242
*/
43-
public void copyIntoTable(String database, String tableName, DatabendCopyParams params) throws SQLException;
43+
void copyIntoTable(String database, String tableName, DatabendCopyParams params) throws SQLException;
4444
}

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,38 @@ private static void assertTableMetadata(ResultSet rs)
5757
public void setUp()
5858
throws SQLException {
5959
// create table
60-
Connection c = Utils.createConnection();
61-
c.createStatement().execute("drop table if exists test_column_meta");
62-
c.createStatement().execute("drop table if exists decimal_test");
63-
c.createStatement().execute("drop table if exists decimal_test_1");
64-
c.createStatement().execute("drop table if exists test_comment");
65-
c.createStatement().execute("drop view if exists v_test_comment");
66-
c.createStatement().execute("create table test_column_meta (nu1 uint8 null, u1 uint8, u2 uint16, u3 uint32, u4 uint64, i1 int8, i2 int16, i3 int32, i4 int64, f1 float32, f2 float64, s1 string,d1 date, d2 datetime, v1 variant, a1 array(int64), t1 Tuple(x Int64, y Int64 NULL)) engine = fuse");
67-
c.createStatement().execute("create table decimal_test (a decimal(4,2))");
68-
c.createStatement().execute("create table decimal_test_1 (a decimal(15,12))");
69-
c.createStatement().execute("create table test_comment (a int comment 'test comment')");
70-
c.createStatement().execute("create view v_test_comment as select * from test_comment");
60+
try (Connection c = Utils.createConnection()) {
61+
c.createStatement().execute("drop table if exists test_column_meta");
62+
c.createStatement().execute("drop table if exists decimal_test");
63+
c.createStatement().execute("drop table if exists decimal_test_1");
64+
c.createStatement().execute("drop table if exists test_comment");
65+
c.createStatement().execute("drop view if exists v_test_comment");
66+
c.createStatement().execute("create table test_column_meta (nu1 uint8 null, u1 uint8, u2 uint16, u3 uint32, u4 uint64, i1 int8, i2 int16, i3 int32, i4 int64, f1 float32, f2 float64, s1 string,d1 date, d2 datetime, v1 variant, a1 array(int64), t1 Tuple(x Int64, y Int64 NULL)) engine = fuse");
67+
c.createStatement().execute("create table decimal_test (a decimal(4,2))");
68+
c.createStatement().execute("create table decimal_test_1 (a decimal(15,12))");
69+
c.createStatement().execute("create table test_comment (a int comment 'test comment')");
70+
c.createStatement().execute("create view v_test_comment as select * from test_comment");
71+
}
7172
// json data
7273
}
7374

75+
@Test(groups = {"UNIT"})
76+
public void testVersion() throws SQLException {
77+
try (Connection c = Utils.createConnection()) {
78+
DatabaseMetaData metaData = c.getMetaData();
79+
int major = metaData.getDriverMajorVersion();
80+
int minor = metaData.getDriverMinorVersion();
81+
assertEquals(major, 0);
82+
assertEquals(minor, 4);
83+
}
84+
}
85+
7486
@Test(groups = {"IT"})
7587
public void testGetUrl() throws SQLException {
7688
try (Connection c = Utils.createConnection()) {
7789
DatabaseMetaData metaData = c.getMetaData();
7890
String url = metaData.getURL();
79-
Assert.assertEquals(url, "jdbc:databend://http://localhost:" + Utils.port);;
91+
Assert.assertEquals(url, "jdbc:databend://http://localhost:" + Utils.port);
8092
}
8193
}
8294

@@ -113,7 +125,6 @@ public void testGetUserName()
113125
@Test(groups = {"IT"})
114126
public void testGetTables() throws Exception {
115127
try (Connection connection = Utils.createConnection()) {
116-
DatabaseMetaData metaData = connection.getMetaData();
117128
try (ResultSet rs = connection.getMetaData().getTables(null, null, null, null)) {
118129
assertTableMetadata(rs);
119130
// test for view
@@ -133,11 +144,11 @@ public void testGetTables() throws Exception {
133144
public void testGetSchemas() throws Exception {
134145
try (Connection connection = Utils.createConnection()) {
135146
DatabaseMetaData metaData = connection.getMetaData();
136-
try (ResultSet rs = connection.getMetaData().getSchemas()) {
147+
try (ResultSet rs = metaData.getSchemas()) {
137148
ResultSetMetaData metaData1 = rs.getMetaData();
138149
assertEquals(metaData1.getColumnCount(), 2);
139150
}
140-
try (ResultSet rs = connection.getMetaData().getCatalogs()) {
151+
try (ResultSet rs = metaData.getCatalogs()) {
141152
ResultSetMetaData metaData1 = rs.getMetaData();
142153
assertEquals(metaData1.getColumnCount(), 1);
143154
}
@@ -147,7 +158,6 @@ public void testGetSchemas() throws Exception {
147158
@Test(groups = {"IT"})
148159
public void testGetColumns() throws Exception {
149160
try (Connection connection = Utils.createConnection()) {
150-
DatabaseMetaData metaData = connection.getMetaData();
151161
try (ResultSet rs = connection.getMetaData().getColumns(null, null, null, null)) {
152162
assertEquals(rs.getMetaData().getColumnCount(), 24);
153163
}
@@ -157,7 +167,6 @@ public void testGetColumns() throws Exception {
157167
@Test(groups = {"IT"})
158168
public void testComment() throws SQLException {
159169
try (Connection connection = Utils.createConnection()) {
160-
DatabaseMetaData metaData = connection.getMetaData();
161170
try (ResultSet rs = connection.getMetaData().getColumns("default", "default", "test_comment", null)) {
162171
while (rs.next()) {
163172
String tableSchem = rs.getString("table_schem");
@@ -281,7 +290,6 @@ public void testGetBigDecimal() throws Exception {
281290
@Test(groups = {"IT"})
282291
public void testGetPrimaryKeys() throws Exception {
283292
try (Connection connection = Utils.createConnection()) {
284-
DatabaseMetaData metaData = connection.getMetaData();
285293
try (ResultSet rs = connection.getMetaData().getPrimaryKeys(null, null, null)) {
286294
assertEquals(rs.getMetaData().getColumnCount(), 6);
287295
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Vector;
1212

1313
public class TestHeartbeat {
14-
@Test
14+
@Test(groups = {"IT"})
1515
public void testHeartbeat() throws SQLException {
1616
Properties p = new Properties();
1717
p.setProperty("max_rows_in_buffer", "10000");
@@ -26,7 +26,7 @@ public void testHeartbeat() throws SQLException {
2626
int n = 80000;
2727
int numQuery = 3;
2828

29-
Vector<ResultSet> rss = new Vector();
29+
Vector<ResultSet> rss = new Vector<>();
3030
for (int i = 0; i < numQuery; i++) {
3131
statement = c1.createStatement();
3232
statement.executeQuery("select * from numbers(" + n + ") order by number");
@@ -39,10 +39,10 @@ public void testHeartbeat() throws SQLException {
3939
for (int i = 0; i < numQuery; i++) {
4040
ResultSet rs = rss.get(i);
4141
for (int j = 0; j < n; j++) {
42-
Assert.assertEquals(true, rs.next());
42+
Assert.assertTrue(rs.next());
4343
Assert.assertEquals(j, rs.getInt(1));
4444
}
45-
Assert.assertEquals(false, rs.next());
45+
Assert.assertFalse(rs.next());
4646
rs.close();
4747
}
4848
statement.close();

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

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,88 +9,118 @@
99
import java.sql.SQLException;
1010
import java.sql.Statement;
1111

12+
import static org.testng.Assert.*;
13+
1214
public class TestTransaction {
1315

1416
@BeforeTest
1517
public void setUp()
1618
throws SQLException {
17-
// create table
18-
Connection c = Utils.createConnection();
19-
c.createStatement().execute("drop database if exists test_txn");
20-
c.createStatement().execute("create database test_txn");
21-
c.createStatement().execute("create table test_txn.table1(i int)");
19+
// create db
20+
try (Connection c = Utils.createConnection()) {
21+
c.createStatement().execute("create or replace database test_txn");
22+
}
2223
}
2324

24-
@Test
25-
public void testRollback()
26-
throws SQLException {
27-
Connection c = Utils.createConnection();
28-
c.createStatement().execute("delete from test_txn.table1");
29-
try (Statement statement = c.createStatement()) {
30-
statement.execute("begin");
31-
ResultSet r = statement.getResultSet();
32-
}
25+
@Test(groups = {"IT"})
26+
public void testCommit() throws SQLException {
27+
try ( Connection c1 = Utils.createConnection();
28+
Connection c2 = Utils.createConnection();
29+
Connection c3 = Utils.createConnection()
30+
) {
3331

34-
try (Statement statement = c.createStatement()) {
35-
statement.execute("select 11");
36-
// txn_state = Auto_Commit, not correct, should be Active
37-
ResultSet r = statement.getResultSet();
38-
while (r.next()) {
39-
}
40-
}
32+
c1.createStatement().execute("create or replace table test_txn.table1(i int)");
4133

42-
try (Statement statement = c.createStatement()) {
43-
statement.execute("insert into test_txn.table1 values(3)");
44-
ResultSet r = statement.getResultSet();
45-
}
46-
try (Statement statement = c.createStatement()) {
47-
statement.execute("rollback");
48-
ResultSet r = statement.getResultSet();
49-
}
34+
try (Statement statement = c1.createStatement()) {
35+
statement.execute("begin");
36+
statement.execute("insert into test_txn.table1 values(4)");
37+
statement.execute("select * from test_txn.table1");
38+
ResultSet rs = statement.getResultSet();
39+
assertTrue(rs.next());
40+
Assert.assertEquals(4, rs.getInt(1));
41+
assertFalse(rs.next());
5042

51-
try (Statement statement = c.createStatement()) {
52-
statement.execute("select * from test_txn.table1");
53-
ResultSet rs = statement.getResultSet();
54-
while (rs.next()) {
55-
Assert.assertEquals(0, rs.getInt(1));
5643
}
57-
}
58-
}
5944

60-
@Test
61-
public void testCommit() throws SQLException {
62-
Connection c1 = Utils.createConnection();
63-
Connection c2 = Utils.createConnection();
64-
c1.createStatement().execute("delete from test_txn.table1");
65-
try (Statement statement = c1.createStatement()) {
66-
statement.execute("create or replace table test_txn.table1(i int)");
67-
statement.execute("begin");
68-
statement.execute("insert into test_txn.table1 values(4)");
69-
statement.execute("select * from test_txn.table1");
70-
ResultSet rs = statement.getResultSet();
71-
while (rs.next()) {
45+
try (Statement statement = c2.createStatement()) {
46+
statement.execute("begin");
47+
statement.execute("insert into test_txn.table1 values(5)");
48+
statement.execute("select * from test_txn.table1");
49+
ResultSet rs = statement.getResultSet();
50+
assertTrue(rs.next());
51+
Assert.assertEquals(5, rs.getInt(1));
52+
assertFalse(rs.next());
53+
}
54+
c1.commit();
55+
56+
try (Statement statement = c3.createStatement()) {
57+
statement.execute("select * from test_txn.table1");
58+
ResultSet rs = statement.getResultSet();
59+
assertTrue(rs.next());
7260
Assert.assertEquals(4, rs.getInt(1));
61+
assertFalse(rs.next());
7362
}
74-
}
63+
c2.commit();
7564

76-
try (Statement statement = c2.createStatement()) {
77-
statement.execute("begin");
78-
statement.execute("insert into test_txn.table1 values(5)");
79-
statement.execute("select * from test_txn.table1");
80-
ResultSet rs = statement.getResultSet();
81-
while (rs.next()) {
65+
try (Statement statement = c3.createStatement()) {
66+
statement.execute("select * from test_txn.table1 order by i");
67+
ResultSet rs = statement.getResultSet();
68+
assertTrue(rs.next());
69+
Assert.assertEquals(4, rs.getInt(1));
70+
assertTrue(rs.next());
8271
Assert.assertEquals(5, rs.getInt(1));
72+
assertFalse(rs.next());
8373
}
8474
}
85-
c1.commit();
86-
Connection c3 = Utils.createConnection();
87-
try (Statement statement = c3.createStatement()) {
88-
statement.execute("select * from test_txn.table1");
89-
ResultSet rs = statement.getResultSet();
90-
while (rs.next()) {
91-
Assert.assertEquals(4, rs.getInt(1));
75+
}
76+
77+
@Test(groups = {"IT"})
78+
public void testRollback()
79+
throws SQLException {
80+
try (Connection c = Utils.createConnection()) {
81+
try (Statement statement = c.createStatement()) {
82+
c.createStatement().execute("create or replace table test_txn.table2(i int)");
83+
statement.execute("begin");
84+
statement.execute("select 11");
85+
statement.execute("insert into test_txn.table2 values(3)");
86+
statement.execute("rollback");
87+
statement.execute("select * from test_txn.table2");
88+
ResultSet rs = statement.getResultSet();
89+
assertFalse(rs.next());
9290
}
9391
}
9492
}
9593

94+
@Test(groups = {"IT"})
95+
public void testConflict() throws SQLException {
96+
try (Connection c1 = Utils.createConnection(); Connection c2 = Utils.createConnection()) {
97+
Statement statement1 = c1.createStatement();
98+
Statement statement2 = c2.createStatement();
99+
100+
statement1.execute("create or replace table test_txn.table3(i int, j int)");
101+
statement1.execute("insert into test_txn.table3 values (1, 11)");
102+
103+
statement1.execute("begin");
104+
statement1.execute("UPDATE test_txn.table3 set j = 111 where i=1");
105+
106+
statement2.execute("begin");
107+
statement2.execute("UPDATE test_txn.table3 set j = 222 where i=1");
108+
c2.commit();
109+
110+
java.sql.SQLException exception = Assert.expectThrows(
111+
java.sql.SQLException.class,
112+
() -> statement1.execute("commit")
113+
);
114+
// e.g. Unresolvable conflict detected for table 2249
115+
Assert.assertTrue(exception.getMessage().toLowerCase().contains("conflict"));
116+
117+
118+
statement2.execute("select j from test_txn.table3 where i = 1");
119+
ResultSet rs = statement2.getResultSet();
120+
assertTrue(rs.next());
121+
Assert.assertEquals(rs.getInt(1), 222);
122+
assertFalse(rs.next());
123+
}
124+
125+
}
96126
}

0 commit comments

Comments
 (0)