|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Peloton |
| 4 | +// |
| 5 | +// AlterTableTest.java |
| 6 | +// |
| 7 | +// Identification: script/testing/junit/AlterTableTest.java |
| 8 | +// |
| 9 | +// Copyright (c) 2015-2018, Carnegie Mellon University Database Group |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import java.sql.*; |
| 14 | +import org.junit.*; |
| 15 | +import org.junit.rules.ExpectedException; |
| 16 | +import org.postgresql.util.PSQLException; |
| 17 | + |
| 18 | +public class AlterTableTest extends PLTestBase { |
| 19 | + private Connection conn; |
| 20 | + private String s_sql = "SELECT * FROM foo;"; |
| 21 | + |
| 22 | + private static final String SQL_DROP_TABLE = |
| 23 | + "DROP TABLE IF EXISTS foo;"; |
| 24 | + |
| 25 | + private static final String SQL_CREATE_TABLE = |
| 26 | + "CREATE TABLE foo (" + |
| 27 | + "id integer, " + |
| 28 | + "year integer);"; |
| 29 | + |
| 30 | + @Rule |
| 31 | + public ExpectedException thrown = ExpectedException.none(); |
| 32 | + |
| 33 | + /** |
| 34 | + * Initialize the database and table for testing |
| 35 | + */ |
| 36 | + private void InitDatabase() throws SQLException { |
| 37 | + Statement stmt = conn.createStatement(); |
| 38 | + stmt.execute(SQL_DROP_TABLE); |
| 39 | + stmt.execute(SQL_CREATE_TABLE); |
| 40 | + } |
| 41 | + |
| 42 | + @Before |
| 43 | + public void Setup() throws SQLException { |
| 44 | + conn = makeDefaultConnection(); |
| 45 | + conn.setAutoCommit(true); |
| 46 | + InitDatabase(); |
| 47 | + } |
| 48 | + |
| 49 | + @After |
| 50 | + public void Teardown() throws SQLException { |
| 51 | + Statement stmt = conn.createStatement(); |
| 52 | + stmt.execute(SQL_DROP_TABLE); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Insert 1 tuple, rename the column, and see if the change is visible. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + 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, |
| 76 | + new String [] {"id", "month"}, |
| 77 | + new int [] {5, 400}); |
| 78 | + assertNoMoreRows(rs_2); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Rename a column that does not exist, should throw exception |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void test_RenameColumn_2() throws SQLException { |
| 86 | + String sql = "ALTER TABLE foo RENAME a to b;"; |
| 87 | + |
| 88 | + // Old column does not exist |
| 89 | + thrown.expect(PSQLException.class); |
| 90 | + conn.createStatement().execute(sql); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Rename a column to a name that already exists, should throw exception |
| 95 | + */ |
| 96 | + @Test |
| 97 | + public void test_RenameColumn_3() throws SQLException { |
| 98 | + String sql = "ALTER TABLE foo RENAME year to id;"; |
| 99 | + |
| 100 | + // New column already exists |
| 101 | + thrown.expect(PSQLException.class); |
| 102 | + conn.createStatement().execute(sql); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments