Skip to content

Commit

Permalink
basic test for alterTable
Browse files Browse the repository at this point in the history
  • Loading branch information
DeanChensj committed May 4, 2018
1 parent 4d00de1 commit d97b321
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions script/testing/junit/AlterTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.*;
import org.junit.rules.ExpectedException;
import org.postgresql.util.PSQLException;
import static org.junit.Assert.assertEquals;

public class AlterTableTest extends PLTestBase {
private Connection conn;
Expand Down Expand Up @@ -193,4 +194,93 @@ public void test_RenameCol_Concurrent() throws SQLException {
// conn2.commit();
// }

/**
* Add a column to the table.
*/
@Test
public void test_AddCol_Basic() throws SQLException {
String sql = "ALTER TABLE foo add month int;";
conn.createStatement().execute(sql);
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
checkRow(rs,
new String [] {"id", "year", "month"},
new int [] {5, 400, 0});
assertNoMoreRows(rs);
}

/**
* Drop a column from the table.
*/
@Test
public void test_DropCol_Basic() throws SQLException {
String sql = "ALTER TABLE foo drop year;";
conn.createStatement().execute(sql);
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
checkRow(rs,
new String [] {"id"},
new int [] {5});
assertNoMoreRows(rs);
}

/**
* Alter column type from int to float and then alter to int again.
*/
@Test
public void test_AlterType_Basic() throws SQLException {
String sql = "ALTER TABLE foo alter year type float;";
conn.createStatement().execute(sql);
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getInt("id"), 5);
assertEquals(rs.getFloat("year"), 400, 1e-3);
assertNoMoreRows(rs);

String sql2 = "INSERT INTO foo VALUES (6, 3.5);";
conn.createStatement().execute(sql2);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getInt("id"), 5);
assertEquals(rs.getFloat("year"), 400, 1e-3);
rs.next();
assertEquals(rs.getInt("id"), 6);
assertEquals(rs.getFloat("year"), 3.5, 1e-3);
assertNoMoreRows(rs);


String sql3 = "ALTER TABLE foo alter year type int;";
conn.createStatement().execute(sql3);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getInt("id"), 5);
assertEquals(rs.getInt("year"), 400);
rs.next();
assertEquals(rs.getInt("id"), 6);
assertEquals(rs.getInt("year"), 3);
assertNoMoreRows(rs);
}


/**
* Alter column type from int to varchar and backwards.
*/
@Test
public void test_AlterType_Varchar() throws SQLException {
String sql = "ALTER TABLE foo alter year type varchar;";
conn.createStatement().execute(sql);
ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getInt("id"), 5);
assertEquals(rs.getString("year"), Integer.toString(400));
assertNoMoreRows(rs);

String sql2 = "ALTER TABLE foo alter year type int;";
conn.createStatement().execute(sql);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getInt("id"), 5);
assertEquals(rs.getInt("year"), 400);
assertNoMoreRows(rs);
}
}

0 comments on commit d97b321

Please sign in to comment.