001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.structure.DatabaseSnapshot;
005: import liquibase.test.DatabaseTestTemplate;
006: import liquibase.test.SqlStatementDatabaseTest;
007: import liquibase.test.TestContext;
008: import static org.junit.Assert.*;
009: import org.junit.Test;
010:
011: public class AddUniqueConstraintStatementTest extends
012: AbstractSqlStatementTest {
013: private static final String TABLE_NAME = "AddUQTest";
014: private static final String COLUMN_NAME = "colToMakeUQ";
015:
016: protected void setupDatabase(Database database) throws Exception {
017: dropAndCreateTable(
018: new CreateTableStatement(null, TABLE_NAME).addColumn(
019: "id", "int", new NotNullConstraint())
020: .addColumn(COLUMN_NAME, "int",
021: new NotNullConstraint()), database);
022:
023: dropAndCreateTable(new CreateTableStatement(
024: TestContext.ALT_SCHEMA, TABLE_NAME).addColumn("id",
025: "int", new NotNullConstraint()).addColumn(COLUMN_NAME,
026: "int", new NotNullConstraint()), database);
027: }
028:
029: protected SqlStatement generateTestStatement() {
030: return new AddUniqueConstraintStatement(null, null, null, null);
031: }
032:
033: @Test
034: public void execute_noSchema() throws Exception {
035: new DatabaseTestTemplate()
036: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
037: null,
038: new AddUniqueConstraintStatement(null,
039: TABLE_NAME, COLUMN_NAME, "uq_adduqtest")) {
040:
041: protected void preExecuteAssert(
042: DatabaseSnapshot snapshot) {
043: assertFalse(snapshot.getTable(TABLE_NAME)
044: .getColumn(COLUMN_NAME).isUnique());
045: }
046:
047: protected void postExecuteAssert(
048: DatabaseSnapshot snapshot) {
049: //todo: enable snapshot and assertion when snapshot can check for unique constraints
050: // snapshot = new DatabaseSnapshot(database);
051: // assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
052: }
053: });
054: }
055:
056: @Test
057: public void execute_withSchema() throws Exception {
058: new DatabaseTestTemplate()
059: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
060: TestContext.ALT_SCHEMA,
061: new AddUniqueConstraintStatement(
062: TestContext.ALT_SCHEMA, TABLE_NAME,
063: COLUMN_NAME, "uq_adduqtest")) {
064: protected void preExecuteAssert(
065: DatabaseSnapshot snapshot) {
066: assertFalse(snapshot.getTable(TABLE_NAME)
067: .getColumn(COLUMN_NAME).isUnique());
068: }
069:
070: protected void postExecuteAssert(
071: DatabaseSnapshot snapshot) {
072: //todo: enable snapshot and assertion when snapshot can check for unique constraints
073: // snapshot = new DatabaseSnapshot(database, TestContext.ALT_SCHEMA);
074: // assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
075: }
076:
077: });
078: }
079:
080: @Test
081: public void execute_withTablespace() throws Exception {
082: new DatabaseTestTemplate()
083: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
084: null,
085: new AddUniqueConstraintStatement(null,
086: TABLE_NAME, COLUMN_NAME, "uq_adduqtest")
087: .setTablespace(TestContext.ALT_TABLESPACE)) {
088: protected void preExecuteAssert(
089: DatabaseSnapshot snapshot) {
090: assertFalse(snapshot.getTable(TABLE_NAME)
091: .getColumn(COLUMN_NAME).isUnique());
092: }
093:
094: protected void postExecuteAssert(
095: DatabaseSnapshot snapshot) {
096: //todo: enable snapshot and assertion when snapshot can check for unique constraints
097: // snapshot = new DatabaseSnapshot(database);
098: // assertTrue(snapshot.getTable(TABLE_NAME).getColumn(COLUMN_NAME).isUnique());
099: }
100: });
101: }
102:
103: }
|