-
Notifications
You must be signed in to change notification settings - Fork 26
/
constraint_test.go
46 lines (39 loc) · 1.55 KB
/
constraint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package qb
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestConstraints(t *testing.T) {
dialect := NewDialect("default")
assert.Equal(t, Constraint("NULL"), Null())
assert.Equal(t, Constraint("NOT NULL"), NotNull())
assert.Equal(t, Constraint("DEFAULT '5'"), Default(5))
assert.Equal(t, Constraint("UNIQUE"), Unique())
assert.Equal(t, ConstraintElem{"CHECK id > 5"}, Constraint("CHECK id > 5"))
assert.Equal(t, "NOT NULL", NotNull().String())
assert.Equal(t, "PRIMARY KEY(id)", PrimaryKey("id").String(dialect))
assert.Equal(t, "PRIMARY KEY(id, email)", PrimaryKey("id", "email").String(dialect))
assert.Contains(t,
ForeignKey("user_id").References("users", "id").String(dialect),
"FOREIGN KEY(user_id) REFERENCES users(id)")
assert.Contains(t,
ForeignKey("user_id", "user_email").References("users", "id", "email").String(dialect),
"FOREIGN KEY(user_id, user_email) REFERENCES users(id, email)")
assert.Panics(t, func() {
ForeignKey().OnUpdate("invalid")
})
assert.Panics(t, func() {
ForeignKey().OnDelete("invalid")
})
assert.Equal(t,
"\tFOREIGN KEY(user_id) REFERENCES users(id) ON DELETE SET NULL",
ForeignKey("user_id").References("users", "id").OnDelete("SET NULL").String(dialect),
)
assert.Equal(t,
"\tFOREIGN KEY(user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE",
ForeignKey("user_id").References("users", "id").OnUpdate("CASCADE").OnDelete("CASCADE").String(dialect),
)
assert.Equal(t,
"CONSTRAINT u_users_id_email UNIQUE(id, email)",
UniqueKey("id", "email").Table("users").String(dialect))
}