Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit ef6b9f4

Browse files
Shangjie ChenDingshilun
authored andcommitted
Add transactional test
1 parent ed1a0de commit ef6b9f4

File tree

1 file changed

+103
-20
lines changed

1 file changed

+103
-20
lines changed

script/testing/junit/AlterTableTest.java

Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
public class AlterTableTest extends PLTestBase {
1919
private Connection conn;
20-
private String s_sql = "SELECT * FROM foo;";
20+
private Connection conn2;
2121

2222
private static final String SQL_DROP_TABLE =
2323
"DROP TABLE IF EXISTS foo;";
@@ -27,6 +27,12 @@ public class AlterTableTest extends PLTestBase {
2727
"id integer, " +
2828
"year integer);";
2929

30+
private static final String SQL_SELECT_STAR = "SELECT * FROM foo;";
31+
32+
private static final String SQL_RENAME_COLUMN =
33+
"ALTER TABLE foo RENAME year to month;";
34+
35+
3036
@Rule
3137
public ExpectedException thrown = ExpectedException.none();
3238

@@ -37,12 +43,16 @@ private void InitDatabase() throws SQLException {
3743
Statement stmt = conn.createStatement();
3844
stmt.execute(SQL_DROP_TABLE);
3945
stmt.execute(SQL_CREATE_TABLE);
46+
47+
String sql = "INSERT INTO foo VALUES (5, 400);";
48+
conn.createStatement().execute(sql);
4049
}
4150

4251
@Before
4352
public void Setup() throws SQLException {
4453
conn = makeDefaultConnection();
4554
conn.setAutoCommit(true);
55+
conn2 = makeDefaultConnection();
4656
InitDatabase();
4757
}
4858

@@ -57,29 +67,17 @@ public void Teardown() throws SQLException {
5767
*/
5868
@Test
5969
public void test_RenameColumn_1() throws SQLException {
60-
String sql_1 = "INSERT INTO foo VALUES (5, 400);";
61-
conn.createStatement().execute(sql_1);
62-
63-
ResultSet rs_1 = conn.createStatement().executeQuery(s_sql);
64-
rs_1.next();
65-
checkRow(rs_1,
66-
new String [] {"id", "year"},
67-
new int [] {5, 400});
68-
assertNoMoreRows(rs_1);
69-
70-
String sql_2 = "ALTER TABLE foo RENAME year to month;";
71-
conn.createStatement().execute(sql_2);
72-
ResultSet rs_2 = conn.createStatement().executeQuery(s_sql);
73-
74-
rs_2.next();
75-
checkRow(rs_2,
70+
conn.createStatement().execute(SQL_RENAME_COLUMN);
71+
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
72+
rs.next();
73+
checkRow(rs,
7674
new String [] {"id", "month"},
7775
new int [] {5, 400});
78-
assertNoMoreRows(rs_2);
76+
assertNoMoreRows(rs);
7977
}
8078

8179
/**
82-
* Rename a column that does not exist, should throw exception
80+
* Rename a column that does not exists, should throw exception
8381
*/
8482
@Test
8583
public void test_RenameColumn_2() throws SQLException {
@@ -91,7 +89,7 @@ public void test_RenameColumn_2() throws SQLException {
9189
}
9290

9391
/**
94-
* Rename a column to a name that already exists, should throw exception
92+
* Rename a column that does not exists, should throw exception
9593
*/
9694
@Test
9795
public void test_RenameColumn_3() throws SQLException {
@@ -102,4 +100,89 @@ public void test_RenameColumn_3() throws SQLException {
102100
conn.createStatement().execute(sql);
103101
}
104102

103+
/**
104+
* Two transactions try to rename at the same time, should throw exception
105+
*/
106+
@Test
107+
public void test_RenameColumn_4() throws SQLException {
108+
conn.setAutoCommit(false);
109+
conn2.setAutoCommit(false);
110+
111+
conn.createStatement().execute(SQL_RENAME_COLUMN);
112+
113+
thrown.expect(PSQLException.class);
114+
conn2.createStatement().execute(SQL_RENAME_COLUMN);
115+
116+
conn.commit();
117+
conn2.commit();
118+
}
119+
120+
// The following tests are currently broken.
121+
// @Test
122+
// public void test_RenameColumn_5() throws SQLException {
123+
// conn.setAutoCommit(false);
124+
// conn2.setAutoCommit(false);
125+
//
126+
// String sql = "ALTER TABLE foo RENAME year to month;";
127+
// conn.createStatement().execute(sql);
128+
//
129+
// ResultSet rs = conn2.createStatement().executeQuery(SQL_SELECT_STAR);
130+
// rs.next();
131+
// checkRow(rs,
132+
// new String [] {"id", "year"},
133+
// new int [] {5, 400});
134+
// assertNoMoreRows(rs);
135+
//
136+
// conn.commit();
137+
// conn2.commit();
138+
// }
139+
//
140+
// /**
141+
// * 2 transactions, t2 read the table before and after t1 change the column
142+
// * name and should not see the changes.
143+
// */
144+
// @Test
145+
// public void test_RenameColumn_6() throws SQLException {
146+
// conn.setAutoCommit(false);
147+
// conn2.setAutoCommit(false);
148+
//
149+
// ResultSet rs_1 = conn2.createStatement().executeQuery(SQL_SELECT_STAR);
150+
// rs_1.next();
151+
// checkRow(rs_1,
152+
// new String [] {"id", "year"},
153+
// new int [] {5, 400});
154+
// assertNoMoreRows(rs_1);
155+
//
156+
// conn.createStatement().execute(SQL_RENAME_COLUMN);
157+
// conn.commit();
158+
//
159+
// ResultSet rs_2 = conn2.createStatement().executeQuery(SQL_SELECT_STAR);
160+
// rs_2.next();
161+
// checkRow(rs_2,
162+
// new String [] {"id", "year"},
163+
// new int [] {5, 400});
164+
// assertNoMoreRows(rs_2);
165+
// conn2.commit();
166+
// }
167+
168+
// @Test
169+
// public void test_RenameColumn_7() throws SQLException {
170+
// conn.setAutoCommit(false);
171+
// conn2.setAutoCommit(false);
172+
//
173+
// Statement alter_statement = conn.createStatement();
174+
// Statement select_statement = conn2.createStatement();
175+
//
176+
// alter_statement.execute(SQL_RENAME_COLUMN);
177+
// conn.commit();
178+
//
179+
// ResultSet rs = select_statement.executeQuery(SQL_SELECT_STAR);
180+
// rs.next();
181+
// checkRow(rs,
182+
// new String [] {"id", "year"},
183+
// new int [] {5, 400});
184+
// assertNoMoreRows(rs);
185+
// conn2.commit();
186+
// }
187+
105188
}

0 commit comments

Comments
 (0)