This is an automated email from the ASF dual-hosted git repository. hui pushed a commit to branch lmh/addQueryIT in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 2ca0c71f24030e053e360f2d56d21daeb6cb8199 Author: Minghui Liu <[email protected]> AuthorDate: Tue Jun 21 11:08:19 2022 +0800 refactor test --- .../org/apache/iotdb/db/it/query/IoTDBAliasIT.java | 131 +++++--------- .../iotdb/db/it/query/IoTDBPaginationIT.java | 189 +++++++-------------- .../apache/iotdb/db/it/query/IoTDBResultSetIT.java | 93 +++++----- .../org/apache/iotdb/db/it/query/TestUtils.java | 42 +++++ 4 files changed, 189 insertions(+), 266 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAliasIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAliasIT.java index 83e3a20ce8..a907266706 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAliasIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBAliasIT.java @@ -25,7 +25,6 @@ import org.apache.iotdb.itbase.category.ClusterIT; import org.apache.iotdb.itbase.category.LocalStandaloneIT; import org.junit.AfterClass; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -39,13 +38,16 @@ import java.util.Arrays; import java.util.List; import static org.apache.iotdb.db.it.query.TestUtils.assertResultSetEqual; +import static org.apache.iotdb.db.it.query.TestUtils.assertTestFail; +import static org.apache.iotdb.db.it.query.TestUtils.prepareData; +import static org.apache.iotdb.db.it.query.TestUtils.resultSetEqualTest; import static org.junit.Assert.fail; @RunWith(IoTDBTestRunner.class) @Category({LocalStandaloneIT.class, ClusterIT.class}) public class IoTDBAliasIT { - private static final String[] sqls = + private static final String[] SQLs = new String[] { "SET STORAGE GROUP TO root.sg", "CREATE TIMESERIES root.sg.d1.s1(speed) WITH DATATYPE=FLOAT, ENCODING=RLE", @@ -88,7 +90,7 @@ public class IoTDBAliasIT { @BeforeClass public static void setUp() throws Exception { EnvFactory.getEnv().initBeforeClass(); - insertData(); + prepareData(SQLs); } @AfterClass @@ -96,18 +98,6 @@ public class IoTDBAliasIT { EnvFactory.getEnv().cleanAfterClass(); } - private static void insertData() { - try (Connection connection = EnvFactory.getEnv().getConnection(); - Statement statement = connection.createStatement()) { - for (String sql : sqls) { - statement.execute(sql); - } - } catch (Exception e) { - e.printStackTrace(); - Assert.fail(); - } - } - // ---------------------------------- Use timeseries alias --------------------------------- @Test @@ -183,7 +173,16 @@ public class IoTDBAliasIT { // ---------------------------------------- Use AS ----------------------------------------- @Test - public void selectWithAsTest() { + public void rawDataQueryAsTest1() { + String expectedHeader = "Time,power,"; + String[] retArray = new String[] {"100,80.0,", "200,81.0,", "300,82.0,", "400,83.0,"}; + + // root.sg.*.s3 matches root.sg.d2.s3 exactly + resultSetEqualTest("select s3 as power from root.sg2.*", expectedHeader, retArray); + } + + @Test + public void rawDataQueryAsTest2() { String expectedHeader = "Time,speed,temperature,"; String[] retArray = new String[] {"100,10.1,20.7,", "200,15.2,22.9,", "300,30.3,25.1,", "400,50.4,28.3,"}; @@ -193,7 +192,7 @@ public class IoTDBAliasIT { } @Test - public void selectWithAsMixedTest() { + public void rawDataQueryAsTest3() { String expectedHeader = "Time,speed,root.sg2.d1.s2,"; String[] retArray = new String[] {"100,10.1,20.7,", "200,15.2,22.9,", "300,30.3,25.1,", "400,50.4,28.3,"}; @@ -202,23 +201,14 @@ public class IoTDBAliasIT { } @Test - public void selectWithAsFailTest() { + public void rawDataQueryAsFailTest() { assertTestFail( "select s1 as speed from root.sg2.*", "alias 'speed' can only be matched with one time series"); } @Test - public void selectWithAsSingleTest() { - String expectedHeader = "Time,power,"; - String[] retArray = new String[] {"100,80.0,", "200,81.0,", "300,82.0,", "400,83.0,"}; - - // root.sg.*.s3 matches root.sg.d2.s3 exactly - resultSetEqualTest("select s3 as power from root.sg2.*", expectedHeader, retArray); - } - - @Test - public void aggregationWithAsTest() { + public void aggregationQueryAsTest() { String expectedHeader = "s1_num,s2_max,"; String[] retArray = new String[] { @@ -232,7 +222,7 @@ public class IoTDBAliasIT { } @Test - public void aggregationWithAsFailTest() { + public void aggregationQueryAsFailTest() { // root.sg2.*.s1 matches root.sg2.d1.s1 and root.sg2.d2.s1 both assertTestFail( "select count(s1) as s1_num from root.sg2.*", @@ -240,7 +230,7 @@ public class IoTDBAliasIT { } @Test - public void groupByWithAsTest() { + public void groupByQueryAsTest() { String expectedHeader = "Time,s1_num,"; String[] retArray = new String[] { @@ -254,7 +244,7 @@ public class IoTDBAliasIT { } @Test - public void alignByDeviceWithAsTest() { + public void alignByDeviceQueryAsTest1() { String expectedHeader = "Time,Device,speed,temperature,"; String[] retArray = new String[] { @@ -271,7 +261,7 @@ public class IoTDBAliasIT { } @Test - public void alignByDeviceWithAsMixedTest() { + public void alignByDeviceQueryAsTest2() { String expectedHeader = "Time,Device,speed,s2,"; String[] retArray = new String[] { @@ -290,15 +280,7 @@ public class IoTDBAliasIT { } @Test - public void alignByDeviceWithAsFailTest() { - // root.sg.*.s1 matches root.sg.d1.s1 and root.sg.d2.s1 both - assertTestFail( - "select * as speed from root.sg2.d1 align by device", - "alias 'speed' can only be matched with one time series"); - } - - @Test - public void alignByDeviceWithAsDuplicatedTest() { + public void alignByDeviceQueryAsTest3() { String expectedHeader = "Time,Device,speed,s1,"; String[] retArray = new String[] { @@ -313,12 +295,9 @@ public class IoTDBAliasIT { } @Test - public void alignByDeviceWithAsAggregationTest() { + public void alignByDeviceQueryAsTest4() { String expectedHeader = "Device,s1_num,count(s2),s3_num,"; - String[] retArray = - new String[] { - "root.sg2.d2,4,4,4,", - }; + String[] retArray = new String[] {"root.sg2.d2,4,4,4,"}; resultSetEqualTest( "select count(s1) as s1_num, count(s2), count(s3) as s3_num from root.sg2.d2 align by device", @@ -326,9 +305,17 @@ public class IoTDBAliasIT { retArray); } + @Test + public void alignByDeviceQueryAsFailTest() { + // root.sg.*.s1 matches root.sg.d1.s1 and root.sg.d2.s1 both + assertTestFail( + "select * as speed from root.sg2.d1 align by device", + "alias 'speed' can only be matched with one time series"); + } + @Test @Ignore // TODO: remove @Ignore after support alias in last query - public void lastWithAsTest() { + public void lastQueryAsTest() { String[] retArray = new String[] {"400,speed,50.4,FLOAT,", "400,root.sg2.d1.s2,28.3,FLOAT,"}; resultSetEqualTest("select last s1 as speed, s2 from root.sg2.d1", LAST_QUERY_HEADER, retArray); @@ -336,7 +323,7 @@ public class IoTDBAliasIT { @Test @Ignore // TODO: remove @Ignore after support alias in last query - public void lastWithAsDuplicatedTest() { + public void lastQueryAsTest2() { String[] retArray = new String[] { "400,speed,50.4,FLOAT,", "400,root.sg2.d1.s1,50.4,FLOAT,", "400,temperature,28.3,FLOAT," @@ -350,7 +337,7 @@ public class IoTDBAliasIT { @Test @Ignore // TODO: remove @Ignore after support alias in last query - public void lastWithAsFailTest() { + public void lastQueryAsFailTest() { // root.sg2.*.s1 matches root.sg2.d1.s1 and root.sg2.d2.s1 both assertTestFail( "select last s1 as speed from root.sg2.*", @@ -359,17 +346,19 @@ public class IoTDBAliasIT { @Test @Ignore // TODO: remove @Ignore after support UDF - public void UDFAliasTest() { + public void UDFQueryAsTest() { List<String> sqls = Arrays.asList( "select -s1, sin(cos(tan(s1))) as a, cos(s2), top_k(s1 + s1, 'k'='1') as b from root.sg1.d1 WHERE time >= 1509466140000", "select -s1, sin(cos(tan(s1))) as a, cos(s2), top_k(s1 + s1, 'k'='1') as b from root.sg1.d1", - "select -s1, -s1, sin(cos(tan(s1))) as a, sin(cos(tan(s1))), cos(s2), top_k(s1 + s1, 'k'='1') as b, cos(s2) from root.sg1.d1"); + "select -s1, -s1, sin(cos(tan(s1))) as a, sin(cos(tan(s1))), cos(s2), top_k(s1 + s1, 'k'='1') as b, cos(s2) from root.sg1.d1", + "select s1, s2, sin(s1+s2) as a from root.sg1.d1"); List<String> expectHeaders = Arrays.asList( "Time,-root.sg1.d1.s1,a,cos(root.sg1.d1.s2),b,", "Time,-root.sg1.d1.s1,a,cos(root.sg1.d1.s2),b,", - "Time,-root.sg1.d1.s1,-root.sg1.d1.s1,a,sin(cos(tan(root.sg1.d1.s1))),cos(root.sg1.d1.s2),b,cos(root.sg1.d1.s2),"); + "Time,-root.sg1.d1.s1,-root.sg1.d1.s1,a,sin(cos(tan(root.sg1.d1.s1))),cos(root.sg1.d1.s2),b,cos(root.sg1.d1.s2),", + "Time,root.sg1.d1.s1,root.sg1.d1.s2,a,"); List<String[]> retArrays = Arrays.asList( new String[] {}, @@ -382,45 +371,13 @@ public class IoTDBAliasIT { "0,1,1,0.013387802193205699,0.013387802193205699,0.5403023058681398,-2.0,0.5403023058681398,", "1,2,2,-0.5449592372801408,-0.5449592372801408,-0.4161468365471424,null,-0.4161468365471424,", "2,3,3,0.8359477452180156,0.8359477452180156,-0.9899924966004454,null,-0.9899924966004454," + }, + new String[] { + "0,-1,1,0.0", "1,-2,2,0.0", "2,-3,3,0.0", }); for (int i = 0; i < sqls.size(); i++) { resultSetEqualTest(sqls.get(i), expectHeaders.get(i), retArrays.get(i)); } } - - @Test - @Ignore // TODO: remove @Ignore after support UDF - public void UDFAliasResultTest() { - String expectHeader = "Time,root.sg1.d1.s1,root.sg1.d1.s2,a,"; - String[] retArray = { - "0,-1,1,0.0", "1,-2,2,0.0", "2,-3,3,0.0", - }; - - resultSetEqualTest("select s1, s2, sin(s1+s2) as a from root.sg1.d1", expectHeader, retArray); - } - - // --------------------------------------- Utilities --------------------------------------- - - public void resultSetEqualTest(String sql, String expectedHeader, String[] expectedRetArray) { - try (Connection connection = EnvFactory.getEnv().getConnection(); - Statement statement = connection.createStatement()) { - try (ResultSet resultSet = statement.executeQuery(sql)) { - assertResultSetEqual(resultSet, expectedHeader, expectedRetArray); - } - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - } - - public void assertTestFail(String sql, String errMsg) { - try (Connection connection = EnvFactory.getEnv().getConnection(); - Statement statement = connection.createStatement()) { - statement.executeQuery(sql); - fail(); - } catch (Exception e) { - Assert.assertTrue(e.getMessage(), e.getMessage().contains(errMsg)); - } - } } diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBPaginationIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBPaginationIT.java index 100363a540..ba88ecdd7b 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBPaginationIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBPaginationIT.java @@ -24,29 +24,25 @@ import org.apache.iotdb.it.env.EnvFactory; import org.apache.iotdb.it.env.IoTDBTestRunner; import org.apache.iotdb.itbase.category.ClusterIT; import org.apache.iotdb.itbase.category.LocalStandaloneIT; -import org.apache.iotdb.itbase.constant.TestConstant; import org.junit.AfterClass; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.Statement; -import java.util.Objects; +import java.util.Arrays; +import java.util.List; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.apache.iotdb.db.it.query.TestUtils.assertTestFail; +import static org.apache.iotdb.db.it.query.TestUtils.prepareData; +import static org.apache.iotdb.db.it.query.TestUtils.resultSetEqualTest; @RunWith(IoTDBTestRunner.class) @Category({LocalStandaloneIT.class, ClusterIT.class}) public class IoTDBPaginationIT { - private static final String[] insertSqls = + private static final String[] SQLs = new String[] { "SET STORAGE GROUP TO root.vehicle", "CREATE TIMESERIES root.vehicle.d0.s0 WITH DATATYPE=INT32, ENCODING=RLE", @@ -82,141 +78,80 @@ public class IoTDBPaginationIT { "insert into root.vehicle.d0(timestamp,s2) values(102,10.00)", "insert into root.vehicle.d0(timestamp,s2) values(105,11.11)", "insert into root.vehicle.d0(timestamp,s2) values(1000,1000.11)", - "insert into root.vehicle.d0(timestamp,s1) values(2000-01-01T08:00:00+08:00, 100)", + "insert into root.vehicle.d0(timestamp,s1) values(2000-01-01T08:00:00+08:00, 100)" }; + private static int maxQueryDeduplicatedPathNum; + @BeforeClass public static void setUp() throws InterruptedException { + maxQueryDeduplicatedPathNum = ConfigFactory.getConfig().getMaxQueryDeduplicatedPathNum(); + ConfigFactory.getConfig().setMaxQueryDeduplicatedPathNum(2); EnvFactory.getEnv().initBeforeClass(); + prepareData(SQLs); } @AfterClass public static void tearDown() throws Exception { EnvFactory.getEnv().cleanAfterClass(); - } - - private static void insertData() { - try (Connection connection = EnvFactory.getEnv().getConnection(); - Statement statement = connection.createStatement()) { - - for (String sql : insertSqls) { - statement.execute(sql); - } - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } + ConfigFactory.getConfig().setMaxQueryDeduplicatedPathNum(maxQueryDeduplicatedPathNum); } @Test - public void Test() throws ClassNotFoundException { - insertData(); - SelectTest(); - } - - public void SelectTest() { - String[] sqlS = { - "SELECT s1 FROM root.vehicle.d0 WHERE time<200 limit 3", - "1,1101,\n" + "2,40000,\n" + "50,50000,\n", - "SELECT s0 FROM root.vehicle.d0 WHERE s1 > 190 limit 3", - "1,101,\n" + "2,10000,\n" + "50,10000,\n", - "SELECT s1,s2 FROM root.vehicle.d0 where s1>190 or s2<10.0 limit 3 offset 2", - "3,null,3.33,\n" + "4,null,4.44,\n" + "50,50000,null,\n", - "select * from root.vehicle.d0 slimit 1", - "1,101,\n" - + "2,10000,\n" - + "50,10000,\n" - + "100,99,\n" - + "101,99,\n" - + "102,80,\n" - + "103,99,\n" - + "104,90,\n" - + "105,99,\n" - + "106,99,\n" - + "1000,22222,\n", - "select * from root.vehicle.d0 slimit 1 soffset 2", - "2,2.22,\n" + "3,3.33,\n" + "4,4.44,\n" + "102,10.0,\n" + "105,11.11,\n" + "1000,1000.11,\n", - "select d0 from root.vehicle slimit 1 soffset 2", - "2,2.22,\n" + "3,3.33,\n" + "4,4.44,\n" + "102,10.0,\n" + "105,11.11,\n" + "1000,1000.11,\n", - "select * from root.vehicle.d0 where s1>190 or s2 < 10.0 limit 3 offset 1 slimit 1 soffset 2 ", - "3,3.33,\n" + "4,4.44,\n" + "105,11.11,\n" - }; - executeSQL(sqlS); - } - - private void executeSQL(String[] sqls) { - - try (Connection connection = EnvFactory.getEnv().getConnection(); - Statement statement = connection.createStatement()) { - StringBuilder result = new StringBuilder(); - long now_start = 0L; - boolean cmp = false; - - for (String sql : sqls) { - // System.out.println("----" + sql); - if (cmp) { - Assert.assertEquals(sql, result.toString()); - cmp = false; - } else { - if (sql.contains("NOW()") && now_start == 0L) { - now_start = System.currentTimeMillis(); - } - - if (sql.split(" ")[0].equals("SELECT") | sql.split(" ")[0].equals("select")) { - try (ResultSet resultSet = statement.executeQuery(sql)) { - ResultSetMetaData metaData = resultSet.getMetaData(); - int count = metaData.getColumnCount(); - String[] column = new String[count]; - for (int i = 0; i < count; i++) { - column[i] = metaData.getColumnName(i + 1); - } - result = new StringBuilder(); - while (resultSet.next()) { - for (int i = 1; i <= count; i++) { - if (now_start > 0L && Objects.equals(column[i - 1], TestConstant.TIMESTAMP_STR)) { - String timestr = resultSet.getString(i); - long tn = Long.parseLong(timestr); - long now = System.currentTimeMillis(); - if (tn >= now_start && tn <= now) { - timestr = "NOW()"; - } - result.append(timestr).append(','); - } else { - result.append(resultSet.getString(i)).append(','); - } - } - result.append('\n'); - } - cmp = true; + public void rawDataQueryTest() { + // TODO: remove comments after support value filter + List<String> querySQLs = + Arrays.asList( + "SELECT s1 FROM root.vehicle.d0 WHERE time<200 limit 3", + // "SELECT s0 FROM root.vehicle.d0 WHERE s1 > 190 limit 3", + // "SELECT s1,s2 FROM root.vehicle.d0 WHERE s1 > 190 or s2 < 10.0 limit 3 offset 2", + "SELECT * FROM root.vehicle.d0 slimit 1", + "SELECT * FROM root.vehicle.d0 slimit 1 soffset 2" + // "SELECT * FROM root.vehicle.d0 WHERE s1 > 190 or s2 < 10.0 limit 3 offset 1 slimit 1 + // soffset 2" + ); + List<String> expectHeaders = + Arrays.asList( + "Time,root.vehicle.d0.s1,", + // "Time,root.vehicle.d0.s0,", + // "Time,root.vehicle.d0.s1,root.vehicle.d0.s2,", + "Time,root.vehicle.d0.s0,", + "Time,root.vehicle.d0.s2," + // "Time,root.vehicle.d0.s2," + ); + List<String[]> retArrays = + Arrays.asList( + new String[] {"1,1101,", "2,40000,", "50,50000,"}, + // new String[] {"1,101,", "2,10000,", "50,10000,"}, + // new String[] {"3,null,3.33,", "4,null,4.44,", "50,50000,null,"}, + new String[] { + "1,101,", + "2,10000,", + "50,10000,", + "100,99,", + "101,99,", + "102,80,", + "103,99,", + "104,90,", + "105,99,", + "106,99,", + "1000,22222," + }, + new String[] { + "2,2.22,", "3,3.33,", "4,4.44,", "102,10.0,", "105,11.11,", "1000,1000.11," } - } - } - } - } catch (Exception e) { - e.printStackTrace(); + // new String[] {"3,3.33,", "4,4.44,", "105,11.11,"} + ); + + for (int i = 0; i < querySQLs.size(); i++) { + resultSetEqualTest(querySQLs.get(0), expectHeaders.get(0), retArrays.get(0)); } } - /** Test path num over limit, there is supposed to throw pathNumOverLimitException. */ @Test public void pathNumOverLimitTest() { - final int maxQueryDeduplicatedPathNum = - ConfigFactory.getConfig().getMaxQueryDeduplicatedPathNum(); - ConfigFactory.getConfig().setMaxQueryDeduplicatedPathNum(2); - try (Connection connection = EnvFactory.getEnv().getConnection(); - Statement statement = connection.createStatement()) { - - statement.execute("insert into root.sg.d1(time, s1, s2, s3) values(1, 1, 1, 1)"); - - // fail - statement.executeQuery("select ** from root"); - fail(); - } catch (Exception e) { - assertTrue( - e.getMessage() - .contains( - "Too many paths in one query! Currently allowed max deduplicated path number is 2.")); - } - ConfigFactory.getConfig().setMaxQueryDeduplicatedPathNum(maxQueryDeduplicatedPathNum); + assertTestFail( + "select * from root.vehicle.d0", + "Too many paths in one query! Currently allowed max deduplicated path number is 2."); } } diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBResultSetIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBResultSetIT.java index 6590c5561c..7e367db1a4 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBResultSetIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/query/IoTDBResultSetIT.java @@ -36,9 +36,8 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.sql.Types; -import java.util.Arrays; -import java.util.List; +import static org.apache.iotdb.db.it.query.TestUtils.prepareData; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -46,20 +45,21 @@ import static org.junit.Assert.fail; @Category({LocalStandaloneIT.class, ClusterIT.class}) public class IoTDBResultSetIT { - private static final List<String> SQLs = - Arrays.asList( - "SET STORAGE GROUP TO root.t1", - "CREATE TIMESERIES root.t1.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN", - "CREATE TIMESERIES root.t1.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=RLE", - "CREATE TIMESERIES root.t1.wf01.wt01.type WITH DATATYPE=INT32, ENCODING=RLE", - "CREATE TIMESERIES root.t1.wf01.wt01.grade WITH DATATYPE=INT64, ENCODING=RLE", - "CREATE TIMESERIES root.sg.dev.status WITH DATATYPE=text,ENCODING=PLAIN", - "insert into root.sg.dev(time,status) values(1,3.14)"); + private static final String[] SQLs = + new String[] { + "SET STORAGE GROUP TO root.t1", + "CREATE TIMESERIES root.t1.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN", + "CREATE TIMESERIES root.t1.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=RLE", + "CREATE TIMESERIES root.t1.wf01.wt01.type WITH DATATYPE=INT32, ENCODING=RLE", + "CREATE TIMESERIES root.t1.wf01.wt01.grade WITH DATATYPE=INT64, ENCODING=RLE", + "CREATE TIMESERIES root.sg.dev.status WITH DATATYPE=text,ENCODING=PLAIN", + "insert into root.sg.dev(time,status) values(1,3.14)" + }; @BeforeClass public static void setUp() throws Exception { EnvFactory.getEnv().initBeforeClass(); - insertData(); + prepareData(SQLs); } @AfterClass @@ -67,51 +67,40 @@ public class IoTDBResultSetIT { EnvFactory.getEnv().cleanAfterClass(); } - private static void insertData() { + @Test + public void intAndLongConversionTest() { try (Connection connection = EnvFactory.getEnv().getConnection(); Statement statement = connection.createStatement()) { + statement.execute( + "insert into root.t1.wf01.wt01(timestamp, status, type, grade) values (1000, true, 1, 1000)"); + statement.execute( + "insert into root.t1.wf01.wt01(timestamp, status, type, grade) values (2000, false, 2, 2000)"); - for (String sql : SQLs) { - statement.execute(sql); + try (ResultSet resultSet1 = + statement.executeQuery("select count(status) from root.t1.wf01.wt01"); ) { + resultSet1.next(); + // type of r1 is INT64(long), test long convert to int + int countStatus = resultSet1.getInt(1); + Assert.assertEquals(2L, countStatus); } - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - } - @Test - public void testIntAndLongConversion() { - try (Connection connection = EnvFactory.getEnv().getConnection()) { - Statement st0 = connection.createStatement(); - st0.execute( - "insert into root.t1.wf01.wt01(timestamp, status, type, grade) values (1000, true, 1, 1000)"); - st0.execute( - "insert into root.t1.wf01.wt01(timestamp, status, type, grade) values (2000, false, 2, 2000)"); - st0.close(); - - Statement st1 = connection.createStatement(); - ResultSet rs1 = st1.executeQuery("select count(status) from root.t1.wf01.wt01"); - rs1.next(); - // type of r1 is INT64(long), test long convert to int - int countStatus = rs1.getInt(1); - Assert.assertEquals(2L, countStatus); - - ResultSet rs2 = - st1.executeQuery("select type from root.t1.wf01.wt01 where time = 1000 limit 1"); - rs2.next(); - // type of r2 is INT32(int), test int convert to long - long type = rs2.getLong(2); - Assert.assertEquals(1, type); - - ResultSet rs3 = - st1.executeQuery("select grade from root.t1.wf01.wt01 where time = 1000 limit 1"); - rs3.next(); - // type of r3 is INT64(long), test long convert to int - int grade = rs3.getInt(2); - Assert.assertEquals(1000, grade); - - st1.close(); + try (ResultSet resultSet2 = + statement.executeQuery( + "select type from root.t1.wf01.wt01 where time = 1000 limit 1"); ) { + resultSet2.next(); + // type of r2 is INT32(int), test int convert to long + long type = resultSet2.getLong(2); + Assert.assertEquals(1, type); + } + + try (ResultSet resultSet3 = + statement.executeQuery( + "select grade from root.t1.wf01.wt01 where time = 1000 limit 1"); ) { + resultSet3.next(); + // type of r3 is INT64(long), test long convert to int + int grade = resultSet3.getInt(2); + Assert.assertEquals(1000, grade); + } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/query/TestUtils.java b/integration-test/src/test/java/org/apache/iotdb/db/it/query/TestUtils.java index 56c0b1c0ec..0fb2b6a88f 100644 --- a/integration-test/src/test/java/org/apache/iotdb/db/it/query/TestUtils.java +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/query/TestUtils.java @@ -19,14 +19,56 @@ package org.apache.iotdb.db.it.query; +import org.apache.iotdb.it.env.EnvFactory; + +import org.junit.Assert; + +import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; +import java.sql.Statement; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class TestUtils { + public static void prepareData(String[] SQLs) { + try (Connection connection = EnvFactory.getEnv().getConnection(); + Statement statement = connection.createStatement()) { + for (String sql : SQLs) { + statement.execute(sql); + } + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + public static void resultSetEqualTest( + String sql, String expectedHeader, String[] expectedRetArray) { + try (Connection connection = EnvFactory.getEnv().getConnection(); + Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(sql)) { + assertResultSetEqual(resultSet, expectedHeader, expectedRetArray); + } + } catch (Exception e) { + e.printStackTrace(); + fail(e.getMessage()); + } + } + + public static void assertTestFail(String sql, String errMsg) { + try (Connection connection = EnvFactory.getEnv().getConnection(); + Statement statement = connection.createStatement()) { + statement.executeQuery(sql); + fail(); + } catch (Exception e) { + Assert.assertTrue(e.getMessage(), e.getMessage().contains(errMsg)); + } + } + public static void assertResultSetEqual( ResultSet actualResultSet, String expectedHeader, String[] expectedRetArray) throws SQLException {
