001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.structure.DatabaseSnapshot;
005: import liquibase.exception.JDBCException;
006: import liquibase.test.DatabaseTestTemplate;
007: import liquibase.test.SqlStatementDatabaseTest;
008: import liquibase.test.TestContext;
009: import static org.junit.Assert.*;
010: import org.junit.Test;
011:
012: public class AddPrimaryKeyStatementTest extends
013: AbstractSqlStatementTest {
014:
015: private static final String TABLE_NAME = "AddPKTest";
016: private static final String COLUMN_NAME = "id";
017: private static final String COLUMN2_NAME = "id2";
018:
019: protected void setupDatabase(Database database) throws Exception {
020: dropAndCreateTable(
021: new CreateTableStatement(null, TABLE_NAME).addColumn(
022: COLUMN_NAME, "int", new NotNullConstraint())
023: .addColumn(COLUMN2_NAME, "int",
024: new NotNullConstraint()), database);
025:
026: dropAndCreateTable(new CreateTableStatement(
027: TestContext.ALT_SCHEMA, TABLE_NAME).addColumn(
028: COLUMN_NAME, "int", new NotNullConstraint()).addColumn(
029: COLUMN2_NAME, "int", new NotNullConstraint()), database);
030: }
031:
032: protected SqlStatement generateTestStatement() {
033: return new AddPrimaryKeyStatement(null, null, null, null);
034: }
035:
036: @Test
037: public void execute_noSchema() throws Exception {
038: new DatabaseTestTemplate()
039: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
040: null,
041: new AddPrimaryKeyStatement(null, TABLE_NAME,
042: COLUMN_NAME, "PK_addpktest")) {
043:
044: protected void preExecuteAssert(
045: DatabaseSnapshot snapshot) {
046: assertFalse(snapshot.getTable(TABLE_NAME)
047: .getColumn(COLUMN_NAME).isPrimaryKey());
048: assertFalse(snapshot.getTable(TABLE_NAME)
049: .getColumn(COLUMN2_NAME).isPrimaryKey());
050: }
051:
052: protected void postExecuteAssert(
053: DatabaseSnapshot snapshot) {
054: assertTrue(snapshot.getTable(TABLE_NAME)
055: .getColumn(COLUMN_NAME).isPrimaryKey());
056: assertFalse(snapshot.getTable(TABLE_NAME)
057: .getColumn(COLUMN2_NAME).isPrimaryKey());
058: }
059:
060: });
061: }
062:
063: @Test
064: public void execute_altSchema() throws Exception {
065: new DatabaseTestTemplate()
066: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
067: TestContext.ALT_SCHEMA,
068: new AddPrimaryKeyStatement(
069: TestContext.ALT_SCHEMA, TABLE_NAME,
070: COLUMN_NAME, "PK_addpktest")) {
071: protected void preExecuteAssert(
072: DatabaseSnapshot snapshot) {
073: assertFalse(snapshot.getTable(TABLE_NAME)
074: .getColumn(COLUMN_NAME).isPrimaryKey());
075: assertFalse(snapshot.getTable(TABLE_NAME)
076: .getColumn(COLUMN2_NAME).isPrimaryKey());
077: }
078:
079: protected void postExecuteAssert(
080: DatabaseSnapshot snapshot) {
081: assertTrue(snapshot.getTable(TABLE_NAME)
082: .getColumn(COLUMN_NAME).isPrimaryKey());
083: assertFalse(snapshot.getTable(TABLE_NAME)
084: .getColumn(COLUMN2_NAME).isPrimaryKey());
085: }
086:
087: });
088: }
089:
090: @Test
091: public void execute_compundPKNoSchema() throws Exception {
092: new DatabaseTestTemplate()
093: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
094: null, new AddPrimaryKeyStatement(null,
095: TABLE_NAME, COLUMN_NAME + ","
096: + COLUMN2_NAME, "PK_addpktest")) {
097: protected void preExecuteAssert(
098: DatabaseSnapshot snapshot) {
099: assertFalse(snapshot.getTable(TABLE_NAME)
100: .getColumn(COLUMN_NAME).isPrimaryKey());
101: assertFalse(snapshot.getTable(TABLE_NAME)
102: .getColumn(COLUMN2_NAME).isPrimaryKey());
103: }
104:
105: protected void postExecuteAssert(
106: DatabaseSnapshot snapshot) {
107: assertTrue(snapshot.getTable(TABLE_NAME)
108: .getColumn(COLUMN_NAME).isPrimaryKey());
109: assertTrue(snapshot.getTable(TABLE_NAME)
110: .getColumn(COLUMN2_NAME).isPrimaryKey());
111: }
112:
113: });
114: }
115:
116: @Test
117: public void execute_compundPKAltSchema() throws Exception {
118: new DatabaseTestTemplate()
119: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
120: TestContext.ALT_SCHEMA,
121: new AddPrimaryKeyStatement(
122: TestContext.ALT_SCHEMA, TABLE_NAME,
123: COLUMN_NAME + "," + COLUMN2_NAME,
124: "PK_addpktest")) {
125: protected void preExecuteAssert(
126: DatabaseSnapshot snapshot) {
127: assertFalse(snapshot.getTable(TABLE_NAME)
128: .getColumn(COLUMN_NAME).isPrimaryKey());
129: assertFalse(snapshot.getTable(TABLE_NAME)
130: .getColumn(COLUMN2_NAME).isPrimaryKey());
131: }
132:
133: protected void postExecuteAssert(
134: DatabaseSnapshot snapshot) {
135: assertTrue(snapshot.getTable(TABLE_NAME)
136: .getColumn(COLUMN_NAME).isPrimaryKey());
137: assertTrue(snapshot.getTable(TABLE_NAME)
138: .getColumn(COLUMN2_NAME).isPrimaryKey());
139: }
140: });
141: }
142:
143: @Test
144: public void execute_withTablespace() throws Exception {
145:
146: new DatabaseTestTemplate()
147: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
148: null,
149: new AddPrimaryKeyStatement(null, TABLE_NAME,
150: COLUMN_NAME, "PK_addpktest")
151: .setTablespace(TestContext.ALT_TABLESPACE)) {
152:
153: protected boolean expectedException(
154: Database database, JDBCException exception) {
155: return !database.supportsTablespaces();
156: }
157:
158: protected void preExecuteAssert(
159: DatabaseSnapshot snapshot) {
160: assertFalse(snapshot.getTable(TABLE_NAME)
161: .getColumn(COLUMN_NAME).isPrimaryKey());
162: assertFalse(snapshot.getTable(TABLE_NAME)
163: .getColumn(COLUMN2_NAME).isPrimaryKey());
164: }
165:
166: protected void postExecuteAssert(
167: DatabaseSnapshot snapshot) {
168: assertTrue(snapshot.getTable(TABLE_NAME)
169: .getColumn(COLUMN_NAME).isPrimaryKey());
170: assertFalse(snapshot.getTable(TABLE_NAME)
171: .getColumn(COLUMN2_NAME).isPrimaryKey());
172: }
173:
174: });
175: }
176: }
|