-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the set unique operation
- Loading branch information
1 parent
b10c298
commit 9de1261
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package migrations_test | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
|
||
"pg-roll/pkg/migrations" | ||
) | ||
|
||
func TestSetColumnsUnique(t *testing.T) { | ||
t.Parallel() | ||
|
||
ExecuteTests(t, TestCases{{ | ||
name: "set unique", | ||
migrations: []migrations.Migration{ | ||
{ | ||
Name: "01_add_table", | ||
Operations: migrations.Operations{ | ||
&migrations.OpCreateTable{ | ||
Name: "reviews", | ||
Columns: []migrations.Column{ | ||
{ | ||
Name: "id", | ||
Type: "serial", | ||
PrimaryKey: true, | ||
}, | ||
{ | ||
Name: "username", | ||
Type: "text", | ||
Nullable: false, | ||
}, | ||
{ | ||
Name: "product", | ||
Type: "text", | ||
Nullable: false, | ||
}, | ||
{ | ||
Name: "review", | ||
Type: "text", | ||
Nullable: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "02_set_unique", | ||
Operations: migrations.Operations{ | ||
&migrations.OpSetUnique{ | ||
Table: "reviews", | ||
Columns: []string{"username", "product"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
afterStart: func(t *testing.T, db *sql.DB) { | ||
// The unique index has been created on the underlying table. | ||
idxName := migrations.IndexName("reviews", []string{"username", "product"}) | ||
IndexMustExist(t, db, "public", "reviews", idxName) | ||
|
||
// Inserting values into the old schema that violate uniqueness should fail. | ||
MustInsert(t, db, "public", "01_add_table", "reviews", map[string]string{ | ||
"username": "alice", "product": "apple", "review": "good", | ||
}) | ||
MustNotInsert(t, db, "public", "01_add_table", "reviews", map[string]string{ | ||
"username": "alice", "product": "apple", "review": "bad", | ||
}) | ||
|
||
// Inserting values into the new schema that violate uniqueness should fail. | ||
MustInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{ | ||
"username": "bob", "product": "orange", "review": "good", | ||
}) | ||
MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{ | ||
"username": "bob", "product": "orange", "review": "bad", | ||
}) | ||
}, | ||
afterRollback: func(t *testing.T, db *sql.DB) { | ||
// The unique index has been dropped from the the underlying table. | ||
idxName := migrations.IndexName("reviews", []string{"username", "product"}) | ||
IndexMustNotExist(t, db, "public", "reviews", idxName) | ||
}, | ||
afterComplete: func(t *testing.T, db *sql.DB) { | ||
// The unique constraint has been created on the underlying table. | ||
constraintName := migrations.UniqueConstraintName("reviews", []string{"username", "product"}) | ||
ConstraintMustExist(t, db, "public", "reviews", constraintName) | ||
|
||
// Inserting values into the new schema that violate uniqueness should fail. | ||
MustInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{ | ||
"username": "carl", "product": "banana", "review": "good", | ||
}) | ||
MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{ | ||
"username": "carl", "product": "banana", "review": "bad", | ||
}) | ||
}, | ||
}}) | ||
} |