|
| 1 | +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the MIT License. |
| 4 | +// If a copy of the MIT was not distributed with this file, |
| 5 | +// You can obtain one at https://github.com/gogf/gf. |
| 6 | + |
| 7 | +package mariadb_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gogf/gf/v2/frame/g" |
| 13 | + "github.com/gogf/gf/v2/test/gtest" |
| 14 | + "github.com/gogf/gf/v2/text/gstr" |
| 15 | + "github.com/gogf/gf/v2/util/gconv" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_Model_Insert_Data_DO(t *testing.T) { |
| 19 | + table := createTable() |
| 20 | + defer dropTable(table) |
| 21 | + |
| 22 | + gtest.C(t, func(t *gtest.T) { |
| 23 | + type User struct { |
| 24 | + g.Meta `orm:"do:true"` |
| 25 | + Id any |
| 26 | + Passport any |
| 27 | + Password any |
| 28 | + Nickname any |
| 29 | + CreateTime any |
| 30 | + } |
| 31 | + data := User{ |
| 32 | + Id: 1, |
| 33 | + Passport: "user_1", |
| 34 | + Password: "pass_1", |
| 35 | + } |
| 36 | + result, err := db.Model(table).Data(data).Insert() |
| 37 | + t.AssertNil(err) |
| 38 | + n, _ := result.LastInsertId() |
| 39 | + t.Assert(n, 1) |
| 40 | + |
| 41 | + one, err := db.Model(table).WherePri(1).One() |
| 42 | + t.AssertNil(err) |
| 43 | + t.Assert(one[`id`], `1`) |
| 44 | + t.Assert(one[`passport`], `user_1`) |
| 45 | + t.Assert(one[`password`], `pass_1`) |
| 46 | + t.Assert(one[`nickname`], ``) |
| 47 | + t.Assert(one[`create_time`], ``) |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func Test_Model_Insert_Data_List_DO(t *testing.T) { |
| 52 | + table := createTable() |
| 53 | + defer dropTable(table) |
| 54 | + |
| 55 | + gtest.C(t, func(t *gtest.T) { |
| 56 | + type User struct { |
| 57 | + g.Meta `orm:"do:true"` |
| 58 | + Id any |
| 59 | + Passport any |
| 60 | + Password any |
| 61 | + Nickname any |
| 62 | + CreateTime any |
| 63 | + } |
| 64 | + data := g.Slice{ |
| 65 | + User{ |
| 66 | + Id: 1, |
| 67 | + Passport: "user_1", |
| 68 | + Password: "pass_1", |
| 69 | + }, |
| 70 | + User{ |
| 71 | + Id: 2, |
| 72 | + Passport: "user_2", |
| 73 | + Password: "pass_2", |
| 74 | + }, |
| 75 | + } |
| 76 | + result, err := db.Model(table).Data(data).Insert() |
| 77 | + t.AssertNil(err) |
| 78 | + n, _ := result.LastInsertId() |
| 79 | + t.Assert(n, 2) |
| 80 | + |
| 81 | + one, err := db.Model(table).WherePri(1).One() |
| 82 | + t.AssertNil(err) |
| 83 | + t.Assert(one[`id`], `1`) |
| 84 | + t.Assert(one[`passport`], `user_1`) |
| 85 | + t.Assert(one[`password`], `pass_1`) |
| 86 | + t.Assert(one[`nickname`], ``) |
| 87 | + t.Assert(one[`create_time`], ``) |
| 88 | + |
| 89 | + one, err = db.Model(table).WherePri(2).One() |
| 90 | + t.AssertNil(err) |
| 91 | + t.Assert(one[`id`], `2`) |
| 92 | + t.Assert(one[`passport`], `user_2`) |
| 93 | + t.Assert(one[`password`], `pass_2`) |
| 94 | + t.Assert(one[`nickname`], ``) |
| 95 | + t.Assert(one[`create_time`], ``) |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +func Test_Model_Update_Data_DO(t *testing.T) { |
| 100 | + table := createInitTable() |
| 101 | + defer dropTable(table) |
| 102 | + |
| 103 | + gtest.C(t, func(t *gtest.T) { |
| 104 | + type User struct { |
| 105 | + g.Meta `orm:"do:true"` |
| 106 | + Id any |
| 107 | + Passport any |
| 108 | + Password any |
| 109 | + Nickname any |
| 110 | + CreateTime any |
| 111 | + } |
| 112 | + data := User{ |
| 113 | + Id: 1, |
| 114 | + Passport: "user_100", |
| 115 | + Password: "pass_100", |
| 116 | + } |
| 117 | + _, err := db.Model(table).Data(data).WherePri(1).Update() |
| 118 | + t.AssertNil(err) |
| 119 | + |
| 120 | + one, err := db.Model(table).WherePri(1).One() |
| 121 | + t.AssertNil(err) |
| 122 | + t.Assert(one[`id`], `1`) |
| 123 | + t.Assert(one[`passport`], `user_100`) |
| 124 | + t.Assert(one[`password`], `pass_100`) |
| 125 | + t.Assert(one[`nickname`], `name_1`) |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func Test_Model_Update_Pointer_Data_DO(t *testing.T) { |
| 130 | + table := createInitTable() |
| 131 | + defer dropTable(table) |
| 132 | + |
| 133 | + gtest.C(t, func(t *gtest.T) { |
| 134 | + type NN string |
| 135 | + type Req struct { |
| 136 | + Id int |
| 137 | + Passport *string |
| 138 | + Password *string |
| 139 | + Nickname *NN |
| 140 | + } |
| 141 | + type UserDo struct { |
| 142 | + g.Meta `orm:"do:true"` |
| 143 | + Id any |
| 144 | + Passport any |
| 145 | + Password any |
| 146 | + Nickname any |
| 147 | + CreateTime any |
| 148 | + } |
| 149 | + var ( |
| 150 | + nickname = NN("nickname_111") |
| 151 | + req = Req{ |
| 152 | + Password: gconv.PtrString("12345678"), |
| 153 | + Nickname: &nickname, |
| 154 | + } |
| 155 | + data = UserDo{ |
| 156 | + Passport: req.Passport, |
| 157 | + Password: req.Password, |
| 158 | + Nickname: req.Nickname, |
| 159 | + } |
| 160 | + ) |
| 161 | + |
| 162 | + _, err := db.Model(table).Data(data).WherePri(1).Update() |
| 163 | + t.AssertNil(err) |
| 164 | + |
| 165 | + one, err := db.Model(table).WherePri(1).One() |
| 166 | + t.AssertNil(err) |
| 167 | + t.Assert(one[`id`], `1`) |
| 168 | + t.Assert(one[`password`], `12345678`) |
| 169 | + t.Assert(one[`nickname`], `nickname_111`) |
| 170 | + }) |
| 171 | +} |
| 172 | + |
| 173 | +func Test_Model_Where_DO(t *testing.T) { |
| 174 | + table := createInitTable() |
| 175 | + defer dropTable(table) |
| 176 | + |
| 177 | + gtest.C(t, func(t *gtest.T) { |
| 178 | + type User struct { |
| 179 | + g.Meta `orm:"do:true"` |
| 180 | + Id any |
| 181 | + Passport any |
| 182 | + Password any |
| 183 | + Nickname any |
| 184 | + CreateTime any |
| 185 | + } |
| 186 | + where := User{ |
| 187 | + Id: 1, |
| 188 | + Passport: "user_1", |
| 189 | + Password: "pass_1", |
| 190 | + } |
| 191 | + one, err := db.Model(table).Where(where).One() |
| 192 | + t.AssertNil(err) |
| 193 | + t.Assert(one[`id`], `1`) |
| 194 | + t.Assert(one[`passport`], `user_1`) |
| 195 | + t.Assert(one[`password`], `pass_1`) |
| 196 | + t.Assert(one[`nickname`], `name_1`) |
| 197 | + }) |
| 198 | +} |
| 199 | + |
| 200 | +func Test_Model_Insert_Data_ForDao(t *testing.T) { |
| 201 | + table := createTable() |
| 202 | + defer dropTable(table) |
| 203 | + |
| 204 | + gtest.C(t, func(t *gtest.T) { |
| 205 | + type UserForDao struct { |
| 206 | + Id any |
| 207 | + Passport any |
| 208 | + Password any |
| 209 | + Nickname any |
| 210 | + CreateTime any |
| 211 | + } |
| 212 | + data := UserForDao{ |
| 213 | + Id: 1, |
| 214 | + Passport: "user_1", |
| 215 | + Password: "pass_1", |
| 216 | + } |
| 217 | + result, err := db.Model(table).Data(data).Insert() |
| 218 | + t.AssertNil(err) |
| 219 | + n, _ := result.LastInsertId() |
| 220 | + t.Assert(n, 1) |
| 221 | + |
| 222 | + one, err := db.Model(table).WherePri(1).One() |
| 223 | + t.AssertNil(err) |
| 224 | + t.Assert(one[`id`], `1`) |
| 225 | + t.Assert(one[`passport`], `user_1`) |
| 226 | + t.Assert(one[`password`], `pass_1`) |
| 227 | + t.Assert(one[`nickname`], ``) |
| 228 | + t.Assert(one[`create_time`], ``) |
| 229 | + }) |
| 230 | +} |
| 231 | + |
| 232 | +func Test_Model_Insert_Data_List_ForDao(t *testing.T) { |
| 233 | + table := createTable() |
| 234 | + defer dropTable(table) |
| 235 | + |
| 236 | + gtest.C(t, func(t *gtest.T) { |
| 237 | + type UserForDao struct { |
| 238 | + Id any |
| 239 | + Passport any |
| 240 | + Password any |
| 241 | + Nickname any |
| 242 | + CreateTime any |
| 243 | + } |
| 244 | + data := g.Slice{ |
| 245 | + UserForDao{ |
| 246 | + Id: 1, |
| 247 | + Passport: "user_1", |
| 248 | + Password: "pass_1", |
| 249 | + }, |
| 250 | + UserForDao{ |
| 251 | + Id: 2, |
| 252 | + Passport: "user_2", |
| 253 | + Password: "pass_2", |
| 254 | + }, |
| 255 | + } |
| 256 | + result, err := db.Model(table).Data(data).Insert() |
| 257 | + t.AssertNil(err) |
| 258 | + n, _ := result.LastInsertId() |
| 259 | + t.Assert(n, 2) |
| 260 | + |
| 261 | + one, err := db.Model(table).WherePri(1).One() |
| 262 | + t.AssertNil(err) |
| 263 | + t.Assert(one[`id`], `1`) |
| 264 | + t.Assert(one[`passport`], `user_1`) |
| 265 | + t.Assert(one[`password`], `pass_1`) |
| 266 | + t.Assert(one[`nickname`], ``) |
| 267 | + t.Assert(one[`create_time`], ``) |
| 268 | + |
| 269 | + one, err = db.Model(table).WherePri(2).One() |
| 270 | + t.AssertNil(err) |
| 271 | + t.Assert(one[`id`], `2`) |
| 272 | + t.Assert(one[`passport`], `user_2`) |
| 273 | + t.Assert(one[`password`], `pass_2`) |
| 274 | + t.Assert(one[`nickname`], ``) |
| 275 | + t.Assert(one[`create_time`], ``) |
| 276 | + }) |
| 277 | +} |
| 278 | + |
| 279 | +func Test_Model_Update_Data_ForDao(t *testing.T) { |
| 280 | + table := createInitTable() |
| 281 | + defer dropTable(table) |
| 282 | + |
| 283 | + gtest.C(t, func(t *gtest.T) { |
| 284 | + type UserForDao struct { |
| 285 | + Id any |
| 286 | + Passport any |
| 287 | + Password any |
| 288 | + Nickname any |
| 289 | + CreateTime any |
| 290 | + } |
| 291 | + data := UserForDao{ |
| 292 | + Id: 1, |
| 293 | + Passport: "user_100", |
| 294 | + Password: "pass_100", |
| 295 | + } |
| 296 | + _, err := db.Model(table).Data(data).WherePri(1).Update() |
| 297 | + t.AssertNil(err) |
| 298 | + |
| 299 | + one, err := db.Model(table).WherePri(1).One() |
| 300 | + t.AssertNil(err) |
| 301 | + t.Assert(one[`id`], `1`) |
| 302 | + t.Assert(one[`passport`], `user_100`) |
| 303 | + t.Assert(one[`password`], `pass_100`) |
| 304 | + t.Assert(one[`nickname`], `name_1`) |
| 305 | + }) |
| 306 | +} |
| 307 | + |
| 308 | +func Test_Model_Where_ForDao(t *testing.T) { |
| 309 | + table := createInitTable() |
| 310 | + defer dropTable(table) |
| 311 | + |
| 312 | + gtest.C(t, func(t *gtest.T) { |
| 313 | + type UserForDao struct { |
| 314 | + Id any |
| 315 | + Passport any |
| 316 | + Password any |
| 317 | + Nickname any |
| 318 | + CreateTime any |
| 319 | + } |
| 320 | + where := UserForDao{ |
| 321 | + Id: 1, |
| 322 | + Passport: "user_1", |
| 323 | + Password: "pass_1", |
| 324 | + } |
| 325 | + one, err := db.Model(table).Where(where).One() |
| 326 | + t.AssertNil(err) |
| 327 | + t.Assert(one[`id`], `1`) |
| 328 | + t.Assert(one[`passport`], `user_1`) |
| 329 | + t.Assert(one[`password`], `pass_1`) |
| 330 | + t.Assert(one[`nickname`], `name_1`) |
| 331 | + }) |
| 332 | +} |
| 333 | + |
| 334 | +func Test_Model_Where_FieldPrefix(t *testing.T) { |
| 335 | + gtest.C(t, func(t *gtest.T) { |
| 336 | + array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";") |
| 337 | + for _, v := range array { |
| 338 | + if _, err := db.Exec(ctx, v); err != nil { |
| 339 | + gtest.Error(err) |
| 340 | + } |
| 341 | + } |
| 342 | + defer dropTable("instance") |
| 343 | + |
| 344 | + type Instance struct { |
| 345 | + ID int `orm:"f_id"` |
| 346 | + Name string |
| 347 | + } |
| 348 | + |
| 349 | + type InstanceDo struct { |
| 350 | + g.Meta `orm:"table:instance, do:true"` |
| 351 | + ID any `orm:"f_id"` |
| 352 | + } |
| 353 | + var instance *Instance |
| 354 | + err := db.Model("instance").Where(InstanceDo{ |
| 355 | + ID: 1, |
| 356 | + }).Scan(&instance) |
| 357 | + t.AssertNil(err) |
| 358 | + t.AssertNE(instance, nil) |
| 359 | + t.Assert(instance.ID, 1) |
| 360 | + t.Assert(instance.Name, "john") |
| 361 | + }) |
| 362 | + // With omitempty. |
| 363 | + gtest.C(t, func(t *gtest.T) { |
| 364 | + array := gstr.SplitAndTrim(gtest.DataContent(`table_with_prefix.sql`), ";") |
| 365 | + for _, v := range array { |
| 366 | + if _, err := db.Exec(ctx, v); err != nil { |
| 367 | + gtest.Error(err) |
| 368 | + } |
| 369 | + } |
| 370 | + defer dropTable("instance") |
| 371 | + |
| 372 | + type Instance struct { |
| 373 | + ID int `orm:"f_id,omitempty"` |
| 374 | + Name string |
| 375 | + } |
| 376 | + |
| 377 | + type InstanceDo struct { |
| 378 | + g.Meta `orm:"table:instance, do:true"` |
| 379 | + ID any `orm:"f_id,omitempty"` |
| 380 | + } |
| 381 | + var instance *Instance |
| 382 | + err := db.Model("instance").Where(InstanceDo{ |
| 383 | + ID: 1, |
| 384 | + }).Scan(&instance) |
| 385 | + t.AssertNil(err) |
| 386 | + t.AssertNE(instance, nil) |
| 387 | + t.Assert(instance.ID, 1) |
| 388 | + t.Assert(instance.Name, "john") |
| 389 | + }) |
| 390 | +} |
0 commit comments