75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
|
package tdb
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func setupUniqueTestDb() {
|
||
|
tdb = &testDb{}
|
||
|
|
||
|
db := setupCustomTestDb(tdb, func(db DBSetup) error {
|
||
|
db.SetDebug(debug)
|
||
|
db.AddTableOrPanic(&TEST_Main{}, func(t TableSetup) error {
|
||
|
t.AddIndexOrPanic(SimpleIndexOptions{
|
||
|
ConstraintOptions: ConstraintOptions{
|
||
|
Field: "Guarantee",
|
||
|
},
|
||
|
Unique: true,
|
||
|
})
|
||
|
return nil
|
||
|
})
|
||
|
return nil
|
||
|
})
|
||
|
|
||
|
tdb.db = db
|
||
|
|
||
|
if tdb.TEST_Main == nil {
|
||
|
panic(errors.New("tdb.TEST_Main was not set"))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestUniqueConstraint(t *testing.T) {
|
||
|
setupUniqueTestDb()
|
||
|
defer cleanupTestDb()
|
||
|
|
||
|
_, err := tdb.TEST_Main.Create(&TEST_Main{Guarantee: "asdf"})
|
||
|
|
||
|
if assertNilEnd(t, err, "Unable to insert first (unique) record") {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
id, err := tdb.TEST_Main.Create(&TEST_Main{Guarantee: "asdf"})
|
||
|
|
||
|
if assertNotNilEnd(t, err, "Expected constraint error") {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
assertEqual(t, id, uint64(0), "Expected 0 (zero value) for inserted id")
|
||
|
}
|
||
|
|
||
|
func TestEmptyStringUniqueConstraint(t *testing.T) {
|
||
|
setupUniqueTestDb()
|
||
|
defer cleanupTestDb()
|
||
|
|
||
|
_, err := tdb.TEST_Main.Create(&TEST_Main{Guarantee: "0"})
|
||
|
|
||
|
if assertNilEnd(t, err, "Unable to insert non-null record") {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
_, err = tdb.TEST_Main.Create(&TEST_Main{Guarantee: ""})
|
||
|
|
||
|
if assertNilEnd(t, err, "Unable to insert second (unique test) record") {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
id, err := tdb.TEST_Main.Create(&TEST_Main{Guarantee: ""})
|
||
|
|
||
|
if assertNotNilEnd(t, err, "Expected constraint error") {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
assertEqual(t, id, uint64(0), "Expected 0 (zero value) for inserted id")
|
||
|
}
|