|
14 | 14 | import org.junit.*;
|
15 | 15 | import org.junit.rules.ExpectedException;
|
16 | 16 | import org.postgresql.util.PSQLException;
|
| 17 | +import static org.junit.Assert.assertEquals; |
17 | 18 |
|
18 | 19 | public class AlterTableTest extends PLTestBase {
|
19 | 20 | private Connection conn;
|
@@ -193,4 +194,117 @@ public void test_RenameCol_Concurrent() throws SQLException {
|
193 | 194 | // conn2.commit();
|
194 | 195 | // }
|
195 | 196 |
|
| 197 | + /** |
| 198 | + * Add a column to the table. |
| 199 | + */ |
| 200 | + @Test |
| 201 | + public void test_AddCol_Basic() throws SQLException { |
| 202 | + String sql = "ALTER TABLE foo add month int;"; |
| 203 | + conn.createStatement().execute(sql); |
| 204 | + ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 205 | + rs.next(); |
| 206 | + checkRow(rs, |
| 207 | + new String [] {"id", "year", "month"}, |
| 208 | + new int [] {5, 400, 0}); |
| 209 | + assertNoMoreRows(rs); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * Add a column to a name that already exists, should throw exception |
| 214 | + */ |
| 215 | + @Test |
| 216 | + public void test_AddCol_Exist() throws SQLException { |
| 217 | + String sql = "ALTER TABLE foo ADD year int;"; |
| 218 | + |
| 219 | + // New column already exists |
| 220 | + thrown.expect(PSQLException.class); |
| 221 | + conn.createStatement().execute(sql); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Drop a column from the table. |
| 226 | + */ |
| 227 | + @Test |
| 228 | + public void test_DropCol_Basic() throws SQLException { |
| 229 | + String sql = "ALTER TABLE foo DROP year;"; |
| 230 | + conn.createStatement().execute(sql); |
| 231 | + ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 232 | + rs.next(); |
| 233 | + checkRow(rs, |
| 234 | + new String [] {"id"}, |
| 235 | + new int [] {5}); |
| 236 | + assertNoMoreRows(rs); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Drop a column that does not exists, should throw exception |
| 241 | + */ |
| 242 | + @Test |
| 243 | + public void test_DropCol_NotExist() throws SQLException { |
| 244 | + String sql = "ALTER TABLE foo DROP month;"; |
| 245 | + |
| 246 | + // Old column does not exist |
| 247 | + thrown.expect(PSQLException.class); |
| 248 | + conn.createStatement().execute(sql); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Alter column type from int to float and then alter to int again. |
| 253 | + */ |
| 254 | + @Test |
| 255 | + public void test_AlterType_Basic() throws SQLException { |
| 256 | + String sql = "ALTER TABLE foo ALTER year TYPE float;"; |
| 257 | + conn.createStatement().execute(sql); |
| 258 | + ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 259 | + rs.next(); |
| 260 | + assertEquals(rs.getInt("id"), 5); |
| 261 | + assertEquals(rs.getFloat("year"), 400, 1e-3); |
| 262 | + assertNoMoreRows(rs); |
| 263 | + |
| 264 | + String sql2 = "INSERT INTO foo VALUES (6, 3.5);"; |
| 265 | + conn.createStatement().execute(sql2); |
| 266 | + rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 267 | + rs.next(); |
| 268 | + assertEquals(rs.getInt("id"), 5); |
| 269 | + assertEquals(rs.getFloat("year"), 400, 1e-3); |
| 270 | + rs.next(); |
| 271 | + assertEquals(rs.getInt("id"), 6); |
| 272 | + assertEquals(rs.getFloat("year"), 3.5, 1e-3); |
| 273 | + assertNoMoreRows(rs); |
| 274 | + |
| 275 | + |
| 276 | + String sql3 = "ALTER TABLE foo ALTER year TYPE int;"; |
| 277 | + conn.createStatement().execute(sql3); |
| 278 | + rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 279 | + rs.next(); |
| 280 | + assertEquals(rs.getInt("id"), 5); |
| 281 | + assertEquals(rs.getInt("year"), 400); |
| 282 | + rs.next(); |
| 283 | + assertEquals(rs.getInt("id"), 6); |
| 284 | + assertEquals(rs.getInt("year"), 3); |
| 285 | + assertNoMoreRows(rs); |
| 286 | + } |
| 287 | + |
| 288 | + |
| 289 | + /** |
| 290 | + * Alter column type from int to varchar and backwards. |
| 291 | + */ |
| 292 | + @Test |
| 293 | + public void test_AlterType_Varchar() throws SQLException { |
| 294 | + String sql = "ALTER TABLE foo ALTER year TYPE varchar;"; |
| 295 | + conn.createStatement().execute(sql); |
| 296 | + ResultSet rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 297 | + rs.next(); |
| 298 | + assertEquals(rs.getInt("id"), 5); |
| 299 | + assertEquals(rs.getString("year"), Integer.toString(400)); |
| 300 | + assertNoMoreRows(rs); |
| 301 | + |
| 302 | + String sql2 = "ALTER TABLE foo ALTER year TYPE int;"; |
| 303 | + conn.createStatement().execute(sql2); |
| 304 | + rs = conn.createStatement().executeQuery(SQL_SELECT_STAR); |
| 305 | + rs.next(); |
| 306 | + assertEquals(rs.getInt("id"), 5); |
| 307 | + assertEquals(rs.getInt("year"), 400); |
| 308 | + assertNoMoreRows(rs); |
| 309 | + } |
196 | 310 | }
|
0 commit comments