提交 32fa2782 authored 作者: Bekrenev Dmitry's avatar Bekrenev Dmitry

Append test for string_agg function

上级 b1991f7f
...@@ -148,6 +148,7 @@ import org.h2.test.synth.TestNestedJoins; ...@@ -148,6 +148,7 @@ import org.h2.test.synth.TestNestedJoins;
import org.h2.test.synth.TestOuterJoins; import org.h2.test.synth.TestOuterJoins;
import org.h2.test.synth.TestRandomCompare; import org.h2.test.synth.TestRandomCompare;
import org.h2.test.synth.TestRandomSQL; import org.h2.test.synth.TestRandomSQL;
import org.h2.test.synth.TestStringAggCompatibility;
import org.h2.test.synth.TestTimer; import org.h2.test.synth.TestTimer;
import org.h2.test.synth.sql.TestSynth; import org.h2.test.synth.sql.TestSynth;
import org.h2.test.synth.thread.TestMulti; import org.h2.test.synth.thread.TestMulti;
...@@ -759,6 +760,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -759,6 +760,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
addTest(new TestMultiThreaded()); addTest(new TestMultiThreaded());
addTest(new TestOuterJoins()); addTest(new TestOuterJoins());
addTest(new TestNestedJoins()); addTest(new TestNestedJoins());
addTest(new TestStringAggCompatibility());
runAddedTests(); runAddedTests();
......
package org.h2.test.synth;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.h2.test.TestBase;
/**
* Test for check compatibility with posgresql function string_agg()
*
* */
public class TestStringAggCompatibility extends TestBase {
public static final String STRING_AGG_DB = "stringAgg";
private Connection conn;
public static void main(String[] args) throws Exception {
TestBase.createCaller().init().test();
}
@Override
public void test() throws Exception {
deleteDb(STRING_AGG_DB);
conn = getConnection(STRING_AGG_DB);
prepareDb();
testWhenOrderByMissing();
testWithOrderBy();
}
private void testWithOrderBy() throws SQLException {
ResultSet result = query("select string_agg(b, ', ' order by b desc) from stringAgg group by a; ");
assertTrue(result.next());
assertEquals("3, 2, 1", result.getString(1));
}
private void testWhenOrderByMissing() throws SQLException {
ResultSet result = query("select string_agg(b, ', ') from stringAgg group by a; ");
assertTrue(result.next());
assertEquals("1, 2, 3", result.getString(1));
}
private ResultSet query(String q) throws SQLException {
PreparedStatement st = conn.prepareStatement(q);
st.execute();
return st.getResultSet();
}
private void prepareDb() throws SQLException {
exec("create table stringAgg(\n" +
" a int not null,\n" +
" b varchar(50) not null\n" +
");");
exec("insert into stringAgg values(1, '1')");
exec("insert into stringAgg values(1, '2')");
exec("insert into stringAgg values(1, '3')");
}
private void exec(String sql) throws SQLException {
conn.prepareStatement(sql).execute();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论