Skip to content

Commit

Permalink
Merge pull request #18 from sxzh93/alter_test
Browse files Browse the repository at this point in the history
basic test for alterTable
  • Loading branch information
DeanChensj authored May 5, 2018
2 parents 4d00de1 + bc26411 commit 32a260f
Showing 1 changed file with 206 additions and 0 deletions.
206 changes: 206 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 All @@ -32,6 +33,8 @@ public class AlterTableTest extends PLTestBase {
private static final String SQL_RENAME_COLUMN =
"ALTER TABLE foo RENAME year to month;";

private static final double EPSILON = 1e-6;


@Rule
public ExpectedException thrown = ExpectedException.none();
Expand Down Expand Up @@ -193,4 +196,207 @@ public void test_RenameCol_Concurrent() throws SQLException {
// conn2.commit();
// }

/**
* Add a column to the table, and do some insertion.
*/
@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);

String sql2 = "INSERT INTO foo VALUES (6, 500, 1);";
conn.createStatement().execute(sql2);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
checkRow(rs,
new String [] {"id", "year", "month"},
new int [] {5, 400, 0});
rs.next();
checkRow(rs,
new String [] {"id", "year", "month"},
new int [] {6, 500, 1});
assertNoMoreRows(rs);
}

/**
* Add a column to a name that already exists, should throw exception
*/
@Test
public void test_AddCol_Exist() throws SQLException {
String sql = "ALTER TABLE foo ADD year int;";

// New column already exists
thrown.expect(PSQLException.class);
conn.createStatement().execute(sql);
}

/**
* 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);

String sql2 = "INSERT INTO foo VALUES (6);";
conn.createStatement().execute(sql2);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
checkRow(rs,
new String [] {"id"},
new int [] {5});
rs.next();
checkRow(rs,
new String [] {"id"},
new int [] {6});
assertNoMoreRows(rs);
}

/**
* Drop a column that does not exists, should throw exception
*/
@Test
public void test_DropCol_NotExist() throws SQLException {
String sql = "ALTER TABLE foo DROP month;";

// Old column does not exist
thrown.expect(PSQLException.class);
conn.createStatement().execute(sql);
}

/**
* 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, EPSILON);
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, EPSILON);
rs.next();
assertEquals(rs.getInt("id"), 6);
assertEquals(rs.getFloat("year"), 3.5, EPSILON);
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(sql2);
rs = conn.createStatement().executeQuery(SQL_SELECT_STAR);
rs.next();
assertEquals(rs.getInt("id"), 5);
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 year 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, EPSILON);
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, EPSILON);
assertEquals(rs.getInt("month"), 0);
rs.next();
assertEquals(rs.getFloat("id"), 4.5, EPSILON);
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 32a260f

Please sign in to comment.