-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcreate_test.go
226 lines (213 loc) · 7 KB
/
create_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package oracle
import (
"encoding/json"
"strings"
"testing"
"time"
)
func TestMergeCreate(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}
model := TestTableUser{}
migrator := db.Set("gorm:table_comments", "用户信息表").Migrator()
if migrator.HasTable(model) {
if err = migrator.DropTable(model); err != nil {
t.Fatalf("DropTable() error = %v", err)
}
}
if err = migrator.AutoMigrate(model); err != nil {
t.Fatalf("AutoMigrate() error = %v", err)
} else {
t.Log("AutoMigrate() success!")
}
data := []TestTableUser{
{
UID: "U1",
Name: "Lisa",
Account: "lisa",
Password: "H6aLDNr",
PhoneNumber: "+8616666666666",
Sex: "0",
UserType: 1,
Enabled: true,
},
{
UID: "U1",
Name: "Lisa",
Account: "lisa",
Password: "H6aLDNr",
PhoneNumber: "+8616666666666",
Sex: "0",
UserType: 1,
Enabled: true,
},
{
UID: "U2",
Name: "Daniela",
Account: "daniela",
Password: "Si7l1sRIC79",
PhoneNumber: "+8619999999999",
Sex: "1",
UserType: 1,
Enabled: true,
},
}
t.Run("MergeCreate", func(t *testing.T) {
tx := db.Create(&data)
if err = tx.Error; err != nil {
t.Fatal(err)
}
dataJsonBytes, _ := json.MarshalIndent(data, "", " ")
t.Logf("result: %s", dataJsonBytes)
})
}
type TestTableUserUnique struct {
ID uint64 `gorm:"column:id;size:64;not null;autoIncrement:true;autoIncrementIncrement:1;primaryKey;comment:自增 ID" json:"id"`
UID string `gorm:"column:uid;type:varchar(50);comment:用户身份标识;unique" json:"uid"`
Name string `gorm:"column:name;size:50;comment:用户姓名" json:"name"`
Account string `gorm:"column:account;type:varchar(50);comment:登录账号" json:"account"`
Password string `gorm:"column:password;type:varchar(512);comment:登录密码(密文)" json:"password"`
Email string `gorm:"column:email;type:varchar(128);comment:邮箱地址" json:"email"`
PhoneNumber string `gorm:"column:phone_number;type:varchar(15);comment:E.164" json:"phoneNumber"`
Sex string `gorm:"column:sex;type:char(1);comment:性别" json:"sex"`
Birthday *time.Time `gorm:"column:birthday;->:false;<-:create;comment:生日" json:"birthday,omitempty"`
UserType int `gorm:"column:user_type;size:8;comment:用户类型" json:"userType"`
Enabled bool `gorm:"column:enabled;comment:是否可用" json:"enabled"`
Remark string `gorm:"column:remark;size:1024;comment:备注信息" json:"remark"`
}
func (TestTableUserUnique) TableName() string {
return "test_user_unique"
}
func TestMergeCreateUnique(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}
model := TestTableUserUnique{}
migrator := db.Set("gorm:table_comments", "用户信息表").Migrator()
if migrator.HasTable(model) {
if err = migrator.DropTable(model); err != nil {
t.Fatalf("DropTable() error = %v", err)
}
}
if err = migrator.AutoMigrate(model); err != nil {
t.Fatalf("AutoMigrate() error = %v", err)
} else {
t.Log("AutoMigrate() success!")
}
data := []TestTableUserUnique{
{
UID: "U1",
Name: "Lisa",
Account: "lisa",
Password: "H6aLDNr",
PhoneNumber: "+8616666666666",
Sex: "0",
UserType: 1,
Enabled: true,
},
{
UID: "U2",
Name: "Daniela",
Account: "daniela",
Password: "Si7l1sRIC79",
PhoneNumber: "+8619999999999",
Sex: "1",
UserType: 1,
Enabled: true,
},
{
UID: "U2",
Name: "Daniela",
Account: "daniela",
Password: "Si7l1sRIC79",
PhoneNumber: "+8619999999999",
Sex: "1",
UserType: 1,
Enabled: true,
},
}
t.Run("MergeCreateUnique", func(t *testing.T) {
tx := db.Create(&data)
if err = tx.Error; err != nil {
if strings.Contains(err.Error(), "ORA-00001") {
t.Log(err) // ORA-00001: 违反唯一约束条件
var gotData []TestTableUserUnique
tx = db.Where(map[string]interface{}{"uid": []string{"U1", "U2"}}).Find(&gotData)
if err = tx.Error; err != nil {
t.Fatal(err)
} else {
if len(gotData) > 0 {
t.Error("Unique constraint violation, but some data was inserted!")
} else {
t.Log("Unique constraint violation, rolled back!")
}
}
} else {
t.Fatal(err)
}
return
}
dataJsonBytes, _ := json.MarshalIndent(data, "", " ")
t.Logf("result: %s", dataJsonBytes)
})
}
type testModelOra03146TTC struct {
Id int64 `gorm:"primaryKey;autoIncrement:false;column:SL_ID;type:uint;size:20;default:0;comment:id" json:"SL_ID"`
ApiName string `gorm:"column:SL_API_NAME;type:VARCHAR2;size:100;default:null;comment:接口名称" json:"SL_API_NAME"`
RawReceive string `gorm:"column:SL_RAW_RECEIVE_JSON;type:VARCHAR2;size:4000;default:null;comment:原始请求参数" json:"SL_RAW_RECEIVE_JSON"`
RawSend string `gorm:"column:SL_RAW_SEND_JSON;type:VARCHAR2;size:4000;default:null;comment:原始响应参数" json:"SL_RAW_SEND_JSON"`
DealReceive string `gorm:"column:SL_DEAL_RECEIVE_JSON;type:VARCHAR2;size:4000;default:null;comment:处理请求参数" json:"SL_DEAL_RECEIVE_JSON"`
DealSend string `gorm:"column:SL_DEAL_SEND_JSON;type:VARCHAR2;size:4000;default:null;comment:处理响应参数" json:"SL_DEAL_SEND_JSON"`
Code string `gorm:"column:SL_CODE;type:VARCHAR2;size:16;default:null;comment:http状态" json:"SL_CODE"`
CreatedTime time.Time `gorm:"column:SL_CREATED_TIME;type:date;default:null;comment:创建时间" json:"SL_CREATED_TIME"`
}
func TestOra03146TTC(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}
model := testModelOra03146TTC{}
migrator := db.Set("gorm:table_comments", "TTC 字段的缓冲区长度无效问题测试表").Migrator()
if migrator.HasTable(model) {
if err = migrator.DropTable(model); err != nil {
t.Fatalf("DropTable() error = %v", err)
}
}
if err = migrator.AutoMigrate(model); err != nil {
t.Fatalf("AutoMigrate() error = %v", err)
} else {
t.Log("AutoMigrate() success!")
}
// INSERT INTO "T100_SCPTOAPI_LOG" ("SL_ID","SL_API_NAME","SL_RAW_RECEIVE_JSON","SL_RAW_SEND_JSON","SL_DEAL_RECEIVE_JSON","SL_DEAL_SEND_JSON","SL_CODE","SL_CREATED_TIME")
// VALUES (9578529926701056,'/v1/t100/packingNum','11111','11111','11111','11111','111','2024-08-27 18:21:39.495')
data := testModelOra03146TTC{
Id: 9578529926701056,
ApiName: "/v1/t100/packingNum",
RawReceive: "11111",
RawSend: "11111",
DealReceive: "11111",
DealSend: "11111",
Code: "111",
CreatedTime: time.Now(),
}
result := db.Create(&data)
if err = result.Error; err != nil {
t.Fatalf("执行失败:%v", err)
}
t.Log("执行成功,影响行数:", result.RowsAffected)
}