-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtable_test.go
71 lines (65 loc) · 2.36 KB
/
table_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
package sqlbuilder
import (
"testing"
)
//
// Author: 陈永佳 [email protected], [email protected]
//
func TestCreateTable(t *testing.T) {
sbc := NewContext()
sql := sbc.CreateTable("t_users").
Column("id").Int(20).NotNull().PrimaryKey().AutoIncrement().
Column("username").VarChar(255).NotNull().Unique().
Column("password").VarChar(255).NotNull().
Column("age").Int(2).Default0().
Column("register_time").Date().DefaultNow().
ToSQL()
checkSQLMatches(sql, "CREATE TABLE IF NOT EXISTS `t_users`("+
"`id` INT(20) NOT NULL AUTO_INCREMENT, "+
"`username` VARCHAR(255) NOT NULL, "+
"`password` VARCHAR(255) NOT NULL, "+
"`age` INT(2) DEFAULT 0, "+
"`register_time` DATE DEFAULT NOW(), "+
"PRIMARY KEY(`id`), "+
"UNIQUE (`username`)"+
") DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;", t)
}
func TestTableBuilder_ForeignKey(t *testing.T) {
sbc := NewContext()
sql := sbc.CreateTable("t_user").
Column("id").Int(12).NotNull().PrimaryKey().AutoIncrement().
Column("pid").Int(12).ForeignKey("t_profile", "prof_id").
ToSQL()
checkSQLMatches(sql, "CREATE TABLE IF NOT EXISTS `t_user`("+
"`id` INT(12) NOT NULL AUTO_INCREMENT, "+
"`pid` INT(12), PRIMARY KEY(`id`), "+
"FOREIGN KEY (`pid`) REFERENCES `t_profile`(`prof_id`)"+
") DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;", t)
}
func TestTableBuilder_ForeignKeyNamed(t *testing.T) {
sbc := NewContext()
sql := sbc.CreateTable("t_user").
Column("id").Int(12).NotNull().PrimaryKey().AutoIncrement().
Column("pid").Int(12).ForeignKeyNamed("FK_PID", "t_profile", "prof_id").
ToSQL()
checkSQLMatches(sql, "CREATE TABLE IF NOT EXISTS `t_user`("+
"`id` INT(12) NOT NULL AUTO_INCREMENT, "+
"`pid` INT(12), PRIMARY KEY(`id`), "+
"CONSTRAINT `FK_PID` FOREIGN KEY (`pid`) REFERENCES `t_profile`(`prof_id`)"+
") DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;", t)
}
func TestTableBuilder_UniqueNamed(t *testing.T) {
sbc := NewContext()
sql := sbc.CreateTable("t_user").
Column("id").Int(12).NotNull().PrimaryKey().AutoIncrement().
Column("pid").Int(12).UniqueNamed("uc_Id_P").
Column("pid_bak").Int(12).UniqueNamed("uc_Id_P").
ToSQL()
checkSQLMatches(sql, "CREATE TABLE IF NOT EXISTS `t_user`("+
"`id` INT(12) NOT NULL AUTO_INCREMENT, "+
"`pid` INT(12), "+
"`pid_bak` INT(12), "+
"PRIMARY KEY(`id`), "+
"CONSTRAINT `uc_Id_P` UNIQUE (`pid`, `pid_bak`)"+
") DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;", t)
}