-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaid_test.go
199 lines (172 loc) · 4.94 KB
/
aid_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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package rapier
import (
"log"
"os"
"reflect"
"strings"
"sync"
"testing"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
)
/******************* test type ***********************************************/
type TestInteger int32
type TestFloat float64
type TestString string
type TestBytes []byte
type TestTime time.Time
type TestDict struct {
Id int64
Pid int64
Name string
DictItem []*TestDictItem `gorm:"foreignKey:DictId"`
}
type TestDictItem struct {
Id int64
DictId int64
Name string
}
/******************* test function *******************************************/
var db, _ = gorm.Open(tests.DummyDialector{}, nil)
func newDb() *gorm.DB {
return db.Session(&gorm.Session{DryRun: true})
}
func newDbWithLog() *gorm.DB {
newDB := db.Session(&gorm.Session{DryRun: true})
newDB.Logger = logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: 200 * time.Millisecond,
Colorful: true,
IgnoreRecordNotFoundError: false,
LogLevel: logger.Info,
})
return newDB
}
func NewStatement() *gorm.Statement {
dd, _ := schema.Parse(&Dict{}, &sync.Map{}, db.NamingStrategy)
return &gorm.Statement{
DB: db,
Table: dd.Table,
Schema: dd,
Clauses: map[string]clause.Clause{},
}
}
// BuildExpr return sql and vars
func BuildExpr(e Expr) (string, []any) {
stmt := NewStatement()
sql, vars := e.BuildWithArgs(stmt)
return sql, vars
}
func ReviewBuildExpr(t *testing.T, e Expr, wantSQL string, wantVars []any) {
gotSQL, gotVars := BuildExpr(e)
if got := strings.TrimSpace(gotSQL); got != wantSQL {
t.Errorf("\nSQL:\n\twant: %v\n\tgot %v", wantSQL, gotSQL)
}
if !reflect.DeepEqual(gotVars, wantVars) {
t.Errorf("\nVars:\n\twant %+v\n\tgot %v", wantVars, gotVars)
}
}
func ReviewBuildDb(t *testing.T, db *gorm.DB, wantSQL string, wantVars []any) {
stmt := db.Statement
if gotSQL := stmt.SQL.String(); gotSQL != wantSQL {
t.Errorf("\nSQL:\n\twant: %v\n\tgot: %v", wantSQL, gotSQL)
}
if !reflect.DeepEqual(stmt.Vars, wantVars) {
t.Errorf("\nVars:\n\twant: %+v\n\tgot: %+v", wantVars, stmt.Vars)
}
}
/******************* test model **********************************************/
type Dict struct {
Id int64 `gorm:"autoIncrement:true;not null;primaryKey"`
Pid int64
Name string
Score float64
IsPin bool
Sort uint16
CreatedAt time.Time
}
func (*Dict) TableName() string {
return "dict"
}
var refDict = New_Dict("dict")
type Dict_Active struct {
refAlias string
refTableName string
ALL Asterisk
Id Int64
Pid Int64
Name String
Score Float64
Sort Uint16
IsPin Bool
CreatedAt Time
}
func new_Dict(tableName, alias string) Dict_Active {
return Dict_Active{
refAlias: alias,
refTableName: tableName,
ALL: NewAsterisk(alias),
Id: NewInt64(alias, "id"),
Pid: NewInt64(alias, "pid"),
Name: NewString(alias, "name"),
Score: NewFloat64(alias, "score"),
Sort: NewUint16(alias, "sort"),
IsPin: NewBool(alias, "is_pin"),
CreatedAt: NewTime(alias, "created_at"),
}
}
func Ref_Dict() Dict_Active { return refDict }
func New_Dict(tableName string) Dict_Active {
return new_Dict(tableName, tableName)
}
func (x *Dict_Active) As(alias string) Dict_Active { return new_Dict(x.refTableName, alias) }
func (x *Dict_Active) TableName() string { return x.refTableName }
func (x *Dict_Active) Alias() string { return x.refAlias }
func (*Dict_Active) New_Executor(db *gorm.DB) *Executor[Dict] {
return NewExecutor[Dict](db)
}
var refDictItem = New_DictItem("dict_item")
type DictItem struct {
Id int64 `gorm:"autoIncrement:true;not null;primaryKey"`
DictId int64
Name string
Sort uint32
IsEnabled bool
}
type DictItem_Active struct {
refAlias string
refTableName string
ALL Asterisk
Id Int64
DictId Int64
Name String
Sort Uint32
IsEnabled Bool
}
func new_DictItem(tableName, alias string) DictItem_Active {
return DictItem_Active{
refAlias: alias,
refTableName: tableName,
ALL: NewAsterisk(alias),
Id: NewInt64(alias, "id"),
DictId: NewInt64(alias, "dict_id"),
Name: NewString(alias, "name"),
Sort: NewUint32(alias, "sort"),
IsEnabled: NewBool(alias, "is_enabled"),
}
}
func Ref_DictItem() DictItem_Active { return refDictItem }
func New_DictItem(tableName string) DictItem_Active {
return new_DictItem(tableName, tableName)
}
func (x *DictItem_Active) As(alias string) DictItem_Active {
return new_DictItem(x.refTableName, alias)
}
func (x *DictItem_Active) TableName() string { return x.refTableName }
func (x *DictItem_Active) Alias() string { return x.refAlias }
func (*DictItem_Active) New_Executor(db *gorm.DB) *Executor[DictItem] {
return NewExecutor[DictItem](db)
}