-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathparser_test.go
More file actions
117 lines (100 loc) · 2.58 KB
/
parser_test.go
File metadata and controls
117 lines (100 loc) · 2.58 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ddlmaker
import (
"database/sql"
"reflect"
"testing"
"time"
"github.com/kayac/ddl-maker/dialect"
"github.com/kayac/ddl-maker/dialect/mysql"
)
type T1 struct {
ID uint64 `ddl:"auto"`
Name string
Description sql.NullString `ddl:"null,text"`
CreatedAt time.Time
Binary []byte
Ignore string `ddl:"-"`
}
func (t1 T1) Table() string {
return "test1"
}
func (t1 T1) Indexes() dialect.Indexes {
return dialect.Indexes{
mysql.AddUniqueIndex("token_idx", "token"),
}
}
func (t1 T1) PrimaryKey() dialect.PrimaryKey {
return mysql.AddPrimaryKey("id", "created_at")
}
func (t1 T1) ForeignKeys() dialect.ForeignKeys {
return dialect.ForeignKeys{
mysql.AddForeignKey([]string{"player_id"}, []string{"id"}, "player",
mysql.WithUpdateForeignKeyOption(mysql.ForeignKeyOptionNoAction),
mysql.WithDeleteForeignKeyOption(mysql.ForeignKeyOptionNoAction)),
}
}
func TestParseField(t *testing.T) {
t1 := T1{}
idColumn := column{
name: "id",
tag: "auto",
typeName: "uint64",
dialect: mysql.MySQL{},
}
nameColumn := column{
name: "name",
typeName: "string",
dialect: mysql.MySQL{},
}
descColumn := column{
name: "description",
typeName: "sql.NullString",
tag: "null,text",
dialect: mysql.MySQL{},
}
createdAtColumn := column{
name: "created_at",
typeName: "time.Time",
dialect: mysql.MySQL{},
}
binaryColumn := column{
name: "binary",
typeName: "[]uint8",
dialect: mysql.MySQL{},
}
columns := []dialect.Column{idColumn, nameColumn, descColumn, createdAtColumn, binaryColumn}
rt := reflect.TypeOf(t1)
if rt.NumField() == 0 {
t.Fatal("T1 field is 0")
}
for i := 0; i < rt.NumField(); i++ {
column, err := parseField(rt.Field(i), mysql.MySQL{})
if err != nil {
if err == ErrIgnoreField {
continue
}
t.Fatal("error parse field", err.Error())
}
if !reflect.DeepEqual(columns[i], column) {
t.Fatalf("parsed %s: %v is different \n %s: %v", column.Name(), column, columns[i].Name(), columns[i])
}
}
}
func TestParseTable(t *testing.T) {
t1 := T1{}
d := mysql.MySQL{}
var columns []dialect.Column
table := parseTable(t1, columns, d)
if table.Name() != d.Quote(t1.Table()) {
t.Fatal("error parse table name", table.Name())
}
if len(table.Indexes()) != len(t1.Indexes()) {
t.Fatal("error parse index ", len(table.Indexes()))
}
if table.PrimaryKey().ToSQL() != "PRIMARY KEY (`id`, `created_at`)" {
t.Fatal("error parse pk: ", table.PrimaryKey().ToSQL())
}
if len(table.ForeignKeys()) != len(t1.ForeignKeys()) {
t.Fatal("error parse fk: ", len(table.ForeignKeys()))
}
}