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

Commit 02b0fdc

Browse files
sxzh93Dean Chen
authored andcommitted
add alter table tests, change unsupported type, multiple operation in a statement
1 parent 4924383 commit 02b0fdc

File tree

1 file changed

+99
-7
lines changed

1 file changed

+99
-7
lines changed

script/testing/junit/AlterTableTest.java

Lines changed: 99 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class AlterTableTest extends PLTestBase {
3333
private static final String SQL_RENAME_COLUMN =
3434
"ALTER TABLE foo RENAME year to month;";
3535

36+
private static final double EPSILON = 1e-6;
37+
3638

3739
@Rule
3840
public ExpectedException thrown = ExpectedException.none();
@@ -195,18 +197,31 @@ public void test_RenameCol_Concurrent() throws SQLException {
195197
// }
196198

197199
/**
198-
* Add a column to the table.
200+
* Add a column to the table, and do some insertion.
199201
*/
200202
@Test
201203
public void test_AddCol_Basic() throws SQLException {
202-
String sql = "ALTER TABLE foo add month int;";
204+
String sql = "ALTER TABLE foo ADD month int;";
203205
conn.createStatement().execute(sql);
204206
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
205207
rs.next();
206208
checkRow(rs,
207209
new String [] {"id", "year", "month"},
208210
new int [] {5, 400, 0});
209211
assertNoMoreRows(rs);
212+
213+
String sql2 = "INSERT INTO foo VALUES (6, 500, 1);";
214+
conn.createStatement().execute(sql2);
215+
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
216+
rs.next();
217+
checkRow(rs,
218+
new String [] {"id", "year", "month"},
219+
new int [] {5, 400, 0});
220+
rs.next();
221+
checkRow(rs,
222+
new String [] {"id", "year", "month"},
223+
new int [] {6, 500, 1});
224+
assertNoMoreRows(rs);
210225
}
211226

212227
/**
@@ -234,6 +249,19 @@ public void test_DropCol_Basic() throws SQLException {
234249
new String [] {"id"},
235250
new int [] {5});
236251
assertNoMoreRows(rs);
252+
253+
String sql2 = "INSERT INTO foo VALUES (6);";
254+
conn.createStatement().execute(sql2);
255+
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
256+
rs.next();
257+
checkRow(rs,
258+
new String [] {"id"},
259+
new int [] {5});
260+
rs.next();
261+
checkRow(rs,
262+
new String [] {"id"},
263+
new int [] {6});
264+
assertNoMoreRows(rs);
237265
}
238266

239267
/**
@@ -258,21 +286,20 @@ public void test_AlterType_Basic() throws SQLException {
258286
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
259287
rs.next();
260288
assertEquals(rs.getInt("id"), 5);
261-
assertEquals(rs.getFloat("year"), 400, 1e-3);
289+
assertEquals(rs.getFloat("year"), 400, EPSILON);
262290
assertNoMoreRows(rs);
263291

264292
String sql2 = "INSERT INTO foo VALUES (6, 3.5);";
265293
conn.createStatement().execute(sql2);
266294
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
267295
rs.next();
268296
assertEquals(rs.getInt("id"), 5);
269-
assertEquals(rs.getFloat("year"), 400, 1e-3);
297+
assertEquals(rs.getFloat("year"), 400, EPSILON);
270298
rs.next();
271299
assertEquals(rs.getInt("id"), 6);
272-
assertEquals(rs.getFloat("year"), 3.5, 1e-3);
300+
assertEquals(rs.getFloat("year"), 3.5, EPSILON);
273301
assertNoMoreRows(rs);
274302

275-
276303
String sql3 = "ALTER TABLE foo ALTER year TYPE int;";
277304
conn.createStatement().execute(sql3);
278305
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
@@ -285,7 +312,6 @@ public void test_AlterType_Basic() throws SQLException {
285312
assertNoMoreRows(rs);
286313
}
287314

288-
289315
/**
290316
* Alter column type from int to varchar and backwards.
291317
*/
@@ -307,4 +333,70 @@ public void test_AlterType_Varchar() throws SQLException {
307333
assertEquals(rs.getInt("year"), 400);
308334
assertNoMoreRows(rs);
309335
}
336+
337+
/**
338+
* Alter type to column that does not exist
339+
*/
340+
@Test
341+
public void test_AlterType_NonExist() throws SQLException {
342+
String sql = "ALTER TABLE foo ALTER a TYPE int;";
343+
344+
thrown.expect(PSQLException.class);
345+
conn.createStatement().execute(sql);
346+
}
347+
348+
/**
349+
* Alter to an unsupported column type
350+
*/
351+
@Test
352+
public void test_AlterType_UnSupported() throws SQLException {
353+
String sql = "ALTER TABLE foo ALTER year TYPE non;";
354+
thrown.expect(PSQLException.class);
355+
conn.createStatement().execute(sql);
356+
}
357+
358+
/**
359+
* Add columns, drop columns, change column type in one sql statement
360+
*/
361+
@Test
362+
public void test_MultiOperation() throws SQLException {
363+
String sql =
364+
"ALTER TABLE foo ADD month INT, DROP year, ALTER id TYPE float;";
365+
conn.createStatement().execute(sql);
366+
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
367+
rs.next();
368+
assertEquals(rs.getFloat("id"), 5, EPSILON);
369+
assertEquals(rs.getInt("month"), 0);
370+
assertNoMoreRows(rs);
371+
372+
String sql2 = "INSERT INTO foo VALUES (4.5, 3);";
373+
conn.createStatement().execute(sql2);
374+
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
375+
rs.next();
376+
assertEquals(rs.getFloat("id"), 5, EPSILON);
377+
assertEquals(rs.getInt("month"), 0);
378+
rs.next();
379+
assertEquals(rs.getFloat("id"), 4.5, EPSILON);
380+
assertEquals(rs.getInt("month"), 3);
381+
assertNoMoreRows(rs);
382+
}
383+
384+
/**
385+
* If multiple operations in one statement failed, schema will not change
386+
*/
387+
@Test
388+
public void test_MultiOperationFailed() throws SQLException {
389+
String sql =
390+
"ALTER TABLE foo ADD month int, DROP month, ALTER year TYPE float;";
391+
392+
thrown.expect(PSQLException.class);
393+
conn.createStatement().execute(sql);
394+
395+
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
396+
rs.next();
397+
checkRow(rs,
398+
new String [] {"id", "year"},
399+
new int [] {5, 400});
400+
assertNoMoreRows(rs);
401+
}
310402
}

0 commit comments

Comments
 (0)