Unverified 提交 a715afa5 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1101 from katzyn/tests

Move some tests in better place and add an additional test for 2PC
......@@ -35,7 +35,6 @@ public class TestCompatibility extends TestBase {
public void test() throws SQLException {
deleteDb("compatibility");
testOnDuplicateKey();
testCaseSensitiveIdentifiers();
testKeyAsColumnInMySQLMode();
......@@ -56,42 +55,6 @@ public class TestCompatibility extends TestBase {
deleteDb("compatibility");
}
private void testOnDuplicateKey() throws SQLException {
Connection c = getConnection("compatibility;MODE=MYSQL");
Statement stat = c.createStatement();
stat.execute("set mode mysql");
stat.execute("create schema s2");
stat.execute("create table s2.test(id int primary key, name varchar(255))");
stat.execute("insert into s2.test(id, name) values(1, 'a')");
assertEquals(2, stat.executeUpdate("insert into s2.test(id, name) values(1, 'b') " +
"on duplicate key update name = values(name)"));
assertEquals(0, stat.executeUpdate("insert into s2.test(id, name) values(1, 'b') " +
"on duplicate key update name = values(name)"));
assertEquals(1, stat.executeUpdate("insert into s2.test(id, name) values(2, 'c') " +
"on duplicate key update name = values(name)"));
ResultSet rs = stat.executeQuery("select id, name from s2.test order by id");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals("b", rs.getString(2));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals("c", rs.getString(2));
assertFalse(rs.next());
// Check qualified names in ON UPDATE case
assertEquals(2, stat.executeUpdate("insert into s2.test(id, name) values(2, 'd') " +
"on duplicate key update test.name = values(name)"));
assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, stat)
.executeUpdate("insert into s2.test(id, name) values(2, 'd') " +
"on duplicate key update test2.name = values(name)");
assertEquals(2, stat.executeUpdate("insert into s2.test(id, name) values(2, 'e') " +
"on duplicate key update s2.test.name = values(name)"));
assertThrows(ErrorCode.SCHEMA_NAME_MUST_MATCH, stat)
.executeUpdate("insert into s2.test(id, name) values(2, 'd') " +
"on duplicate key update s3.test.name = values(name)");
stat.execute("drop schema s2 cascade");
c.close();
}
private void testKeyAsColumnInMySQLMode() throws SQLException {
Connection c = getConnection("compatibility;MODE=MYSQL");
Statement stat = c.createStatement();
......
......@@ -10,6 +10,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.ErrorCode;
import org.h2.test.TestBase;
/**
......@@ -37,6 +39,7 @@ public class TestDuplicateKeyUpdate extends TestBase {
testOnDuplicateKeyInsertBatch(conn);
testOnDuplicateKeyInsertMultiValue(conn);
testPrimaryKeyAndUniqueKey(conn);
testUpdateCountAndQualifiedNames(conn);
conn.close();
deleteDb("duplicateKeyUpdate");
}
......@@ -262,4 +265,39 @@ public class TestDuplicateKeyUpdate extends TestBase {
stat.execute("drop table test");
}
private void testUpdateCountAndQualifiedNames(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("set mode mysql");
stat.execute("create schema s2");
stat.execute("create table s2.test(id int primary key, name varchar(255))");
stat.execute("insert into s2.test(id, name) values(1, 'a')");
assertEquals(2, stat.executeUpdate("insert into s2.test(id, name) values(1, 'b') " +
"on duplicate key update name = values(name)"));
assertEquals(0, stat.executeUpdate("insert into s2.test(id, name) values(1, 'b') " +
"on duplicate key update name = values(name)"));
assertEquals(1, stat.executeUpdate("insert into s2.test(id, name) values(2, 'c') " +
"on duplicate key update name = values(name)"));
ResultSet rs = stat.executeQuery("select id, name from s2.test order by id");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertEquals("b", rs.getString(2));
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals("c", rs.getString(2));
assertFalse(rs.next());
// Check qualified names in ON UPDATE case
assertEquals(2, stat.executeUpdate("insert into s2.test(id, name) values(2, 'd') " +
"on duplicate key update test.name = values(name)"));
assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, stat)
.executeUpdate("insert into s2.test(id, name) values(2, 'd') " +
"on duplicate key update test2.name = values(name)");
assertEquals(2, stat.executeUpdate("insert into s2.test(id, name) values(2, 'e') " +
"on duplicate key update s2.test.name = values(name)"));
assertThrows(ErrorCode.SCHEMA_NAME_MUST_MATCH, stat)
.executeUpdate("insert into s2.test(id, name) values(2, 'd') " +
"on duplicate key update s3.test.name = values(name)");
stat.execute("drop schema s2 cascade");
}
}
......@@ -48,7 +48,6 @@ public class TestTransaction extends TestBase {
testReferential();
testSavepoint();
testIsolation();
testTwoPhaseCommit();
deleteDb("transaction");
}
......@@ -544,42 +543,4 @@ public class TestTransaction extends TestBase {
stat.execute(sql);
}
private void testTwoPhaseCommit() throws SQLException {
if (config.memory) {
return;
}
deleteDb("transaction2pc");
Connection conn = getConnection("transaction2pc");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST (ID INT PRIMARY KEY)");
conn.setAutoCommit(false);
stat.execute("INSERT INTO TEST VALUES (1)");
stat.execute("PREPARE COMMIT \"#1\"");
conn.commit();
stat.execute("SHUTDOWN IMMEDIATELY");
conn = getConnection("transaction2pc");
stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT TRANSACTION, STATE FROM INFORMATION_SCHEMA.IN_DOUBT");
assertFalse(rs.next());
rs = stat.executeQuery("SELECT ID FROM TEST");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
conn.setAutoCommit(false);
stat.execute("INSERT INTO TEST VALUES (2)");
stat.execute("PREPARE COMMIT \"#2\"");
conn.rollback();
stat.execute("SHUTDOWN IMMEDIATELY");
conn = getConnection("transaction2pc");
stat = conn.createStatement();
rs = stat.executeQuery("SELECT TRANSACTION, STATE FROM INFORMATION_SCHEMA.IN_DOUBT");
assertFalse(rs.next());
rs = stat.executeQuery("SELECT ID FROM TEST");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
conn.close();
deleteDb("transaction2pc");
}
}
......@@ -43,6 +43,8 @@ public class TestTwoPhaseCommit extends TestBase {
openWith(false);
test(false);
testInDoubtAfterShutdown();
if (!config.mvStore) {
testLargeTransactionName();
}
......@@ -115,4 +117,57 @@ public class TestTwoPhaseCommit extends TestBase {
stat.execute("PREPARE COMMIT XID_TEST_TRANSACTION_WITH_LONG_NAME");
crash(conn);
}
private void testInDoubtAfterShutdown() throws SQLException {
if (config.memory) {
return;
}
deleteDb("twoPhaseCommit");
Connection conn = getConnection("twoPhaseCommit");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST (ID INT PRIMARY KEY)");
conn.setAutoCommit(false);
stat.execute("INSERT INTO TEST VALUES (1)");
stat.execute("PREPARE COMMIT \"#1\"");
conn.commit();
stat.execute("SHUTDOWN IMMEDIATELY");
conn = getConnection("twoPhaseCommit");
stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT TRANSACTION, STATE FROM INFORMATION_SCHEMA.IN_DOUBT");
assertFalse(rs.next());
rs = stat.executeQuery("SELECT ID FROM TEST");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
conn.setAutoCommit(false);
stat.execute("INSERT INTO TEST VALUES (2)");
stat.execute("PREPARE COMMIT \"#2\"");
conn.rollback();
stat.execute("SHUTDOWN IMMEDIATELY");
conn = getConnection("twoPhaseCommit");
stat = conn.createStatement();
rs = stat.executeQuery("SELECT TRANSACTION, STATE FROM INFORMATION_SCHEMA.IN_DOUBT");
assertFalse(rs.next());
rs = stat.executeQuery("SELECT ID FROM TEST");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
conn.setAutoCommit(false);
stat.execute("INSERT INTO TEST VALUES (3)");
stat.execute("PREPARE COMMIT \"#3\"");
stat.execute("SHUTDOWN IMMEDIATELY");
conn = getConnection("twoPhaseCommit");
stat = conn.createStatement();
rs = stat.executeQuery("SELECT TRANSACTION, STATE FROM INFORMATION_SCHEMA.IN_DOUBT");
assertTrue(rs.next());
assertEquals("#3", rs.getString("TRANSACTION"));
assertEquals("IN_DOUBT", rs.getString("STATE"));
rs = stat.executeQuery("SELECT ID FROM TEST");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
conn.close();
deleteDb("twoPhaseCommit");
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论