-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_index_test.go
91 lines (79 loc) · 2.2 KB
/
create_index_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package bob_test
import (
"testing"
"github.com/aldy505/bob"
)
func TestCreateIndex(t *testing.T) {
sql, _, err := bob.
CreateIndexIfNotExists("email_idx").
On("users").
Unique().
Spatial().
Fulltext().
Columns(bob.IndexColumn{Name: "email"}).
ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "CREATE UNIQUE FULLTEXT SPATIAL INDEX IF NOT EXISTS email_idx ON users (email);"
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
}
func TestCreateIndex_Simple(t *testing.T) {
sql, _, err := bob.
CreateIndex("idx_email").
On("users").
Columns(bob.IndexColumn{Name: "email", Collate: "DEFAULT", Extras: []string{"ASC"}}).
Columns(bob.IndexColumn{Name: "name", Extras: []string{"DESC"}}).
ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "CREATE INDEX idx_email ON users (email COLLATE DEFAULT ASC, name DESC);"
if sql != result {
t.Fatal("sql is not equal to result:", sql)
}
}
func TestCreateIndex_Error(t *testing.T) {
t.Run("without index", func(t *testing.T) {
_, _, err := bob.
CreateIndex("").
On("users").
Columns(bob.IndexColumn{Name: "email", Collate: "DEFAULT", Extras: []string{"ASC"}}).
Columns(bob.IndexColumn{Name: "name", Extras: []string{"DESC"}}).
ToSql()
if err == nil {
t.Fatal("error is nil")
}
if err.Error() != "index name is required on create index statement" {
t.Fatal("error is not equal to result:", err.Error())
}
})
t.Run("without table name", func(t *testing.T) {
_, _, err := bob.
CreateIndex("name").
On("").
Columns(bob.IndexColumn{Name: "email", Collate: "DEFAULT", Extras: []string{"ASC"}}).
Columns(bob.IndexColumn{Name: "name", Extras: []string{"DESC"}}).
ToSql()
if err == nil {
t.Fatal("error is nil")
}
if err.Error() != "a table name must be specified on create index statement" {
t.Fatal("error is not equal to result:", err.Error())
}
})
t.Run("without columns", func(t *testing.T) {
_, _, err := bob.
CreateIndex("name").
On("users").
ToSql()
if err == nil {
t.Fatal("error is nil")
}
if err.Error() != "should at least specify one column for create index statement" {
t.Fatal("error is not equal to result:", err.Error())
}
})
}