Skip to content

Commit

Permalink
add alter table tests, change unsupported type, multiple operation in…
Browse files Browse the repository at this point in the history
… a statement
  • Loading branch information
sxzh93 committed May 4, 2018
1 parent 5e454d4 commit 9ef6289
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion script/testing/junit/AlterTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void test_RenameCol_Concurrent() throws SQLException {
*/
@Test
public void test_AddCol_Basic() throws SQLException {
String sql = "ALTER TABLE foo add month int;";
String sql = "ALTER TABLE foo ADD month INT;";
conn.createStatement().execute(sql);
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
Expand Down Expand Up @@ -307,4 +307,75 @@ public void test_AlterType_Varchar() throws SQLException {
assertEquals(rs.getInt("year"), 400);
assertNoMoreRows(rs);
}


/**
* Alter type to column that does not exist
*/
@Test
public void test_AlterType_NonExist() throws SQLException {
String sql = "ALTER TABLE foo ALTER a TYPE int;";

thrown.expect(PSQLException.class);
conn.createStatement().execute(sql);
}


/**
* Alter to an unsupported column type
*/
@Test
public void test_AlterType_UnSupported() throws SQLException {
String sql = "ALTER TABLE foo ALTER a TYPE non;";

thrown.expect(PSQLException.class);
conn.createStatement().execute(sql);
}


/**
* Add columns, drop columns, change column type in one sql statement
*/
@Test
public void test_MultiOperation() throws SQLException {
String sql =
"ALTER TABLE foo ADD month INT, DROP year, ALTER id TYPE float;";
conn.createStatement().execute(sql);
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getFloat("id"), 5, 1e-3);
assertEquals(rs.getInt("month"), 0);
assertNoMoreRows(rs);

String sql2 = "INSERT INTO foo VALUES (4.5, 3);";
conn.createStatement().execute(sql2);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getFloat("id"), 5, 1e-3);
assertEquals(rs.getInt("month"), 0);
rs.next();
assertEquals(rs.getFloat("id"), 4.5, 1e-3);
assertEquals(rs.getInt("month"), 3);
assertNoMoreRows(rs);
}


/**
* If multiple operations in one statement failed, schema will not change
*/
@Test
public void test_MultiOperationFailed() throws SQLException {
String sql =
"ALTER TABLE foo ADD month INT, DROP month, ALTER year TYPE float;";

thrown.expect(PSQLException.class);
conn.createStatement().execute(sql);

ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
checkRow(rs,
new String [] {"id", "year"},
new int [] {5, 400});
assertNoMoreRows(rs);
}
}

0 comments on commit 9ef6289

Please sign in to comment.