2021-01-09 21:05:47 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-05-28 21:41:00 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gdb_test
|
|
|
|
|
|
|
|
import (
|
2021-01-09 23:02:16 +08:00
|
|
|
"database/sql"
|
2021-06-26 16:23:54 +08:00
|
|
|
"github.com/gogf/gf/database/gdb"
|
2021-08-24 21:18:59 +08:00
|
|
|
"github.com/gogf/gf/errors/gcode"
|
2021-06-26 16:23:54 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2021-09-15 21:17:45 +08:00
|
|
|
"github.com/gogf/gf/text/gstr"
|
2020-07-08 19:12:48 +08:00
|
|
|
"github.com/gogf/gf/util/gconv"
|
2021-06-26 16:23:54 +08:00
|
|
|
"reflect"
|
2020-07-08 19:12:48 +08:00
|
|
|
"testing"
|
2019-05-28 21:41:00 +08:00
|
|
|
)
|
|
|
|
|
2021-05-11 20:14:06 +08:00
|
|
|
func Test_Model_Embedded_Insert(t *testing.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-05-28 21:41:00 +08:00
|
|
|
type Base struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Uid int `json:"uid"`
|
|
|
|
CreateTime string `json:"create_time"`
|
|
|
|
}
|
|
|
|
type User struct {
|
|
|
|
Base
|
2019-06-19 09:06:52 +08:00
|
|
|
Passport string `json:"passport"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Nickname string `json:"nickname"`
|
2019-05-28 21:41:00 +08:00
|
|
|
}
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err := db.Model(table).Data(User{
|
2019-06-19 09:06:52 +08:00
|
|
|
Passport: "john-test",
|
|
|
|
Password: "123456",
|
|
|
|
Nickname: "John",
|
|
|
|
Base: Base{
|
|
|
|
Id: 100,
|
|
|
|
Uid: 100,
|
|
|
|
CreateTime: gtime.Now().String(),
|
2019-05-28 21:41:00 +08:00
|
|
|
},
|
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-05-28 21:41:00 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table).Fields("passport").Where("id=100").Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.String(), "john-test")
|
2019-05-28 21:41:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:14:06 +08:00
|
|
|
func Test_Model_Embedded_MapToStruct(t *testing.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-05-28 21:41:00 +08:00
|
|
|
type Ids struct {
|
2019-06-19 09:06:52 +08:00
|
|
|
Id int `json:"id"`
|
|
|
|
Uid int `json:"uid"`
|
2019-05-28 21:41:00 +08:00
|
|
|
}
|
|
|
|
type Base struct {
|
|
|
|
Ids
|
|
|
|
CreateTime string `json:"create_time"`
|
|
|
|
}
|
|
|
|
type User struct {
|
|
|
|
Base
|
2019-06-19 09:06:52 +08:00
|
|
|
Passport string `json:"passport"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Nickname string `json:"nickname"`
|
2019-05-28 21:41:00 +08:00
|
|
|
}
|
|
|
|
data := g.Map{
|
2019-06-19 09:06:52 +08:00
|
|
|
"id": 100,
|
|
|
|
"uid": 101,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "123456",
|
|
|
|
"nickname": "T1",
|
|
|
|
"create_time": gtime.Now().String(),
|
2019-05-28 21:41:00 +08:00
|
|
|
}
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err := db.Model(table).Data(data).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-05-28 21:41:00 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-05-28 21:41:00 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id=100").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-05-28 21:41:00 +08:00
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one.Struct(user), nil)
|
|
|
|
t.Assert(user.Id, data["id"])
|
|
|
|
t.Assert(user.Passport, data["passport"])
|
|
|
|
t.Assert(user.Password, data["password"])
|
|
|
|
t.Assert(user.Nickname, data["nickname"])
|
|
|
|
t.Assert(user.CreateTime, data["create_time"])
|
2019-05-28 21:41:00 +08:00
|
|
|
|
|
|
|
})
|
2020-04-28 21:03:25 +08:00
|
|
|
}
|
|
|
|
|
2020-05-13 23:21:11 +08:00
|
|
|
func Test_Struct_Pointer_Attribute(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id *int
|
|
|
|
Passport *string
|
|
|
|
Password *string
|
|
|
|
Nickname string
|
|
|
|
}
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).FindOne(1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
user := new(User)
|
|
|
|
err = one.Struct(user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(*user.Id, 1)
|
|
|
|
t.Assert(*user.Passport, "user_1")
|
|
|
|
t.Assert(*user.Password, "pass_1")
|
|
|
|
t.Assert(user.Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Scan(user, "id=1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(*user.Id, 1)
|
|
|
|
t.Assert(*user.Passport, "user_1")
|
|
|
|
t.Assert(*user.Password, "pass_1")
|
|
|
|
t.Assert(user.Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var user *User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Scan(&user, "id=1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(*user.Id, 1)
|
|
|
|
t.Assert(*user.Passport, "user_1")
|
|
|
|
t.Assert(*user.Password, "pass_1")
|
|
|
|
t.Assert(user.Nickname, "name_1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Structs_Pointer_Attribute(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id *int
|
|
|
|
Passport *string
|
|
|
|
Password *string
|
|
|
|
Nickname string
|
|
|
|
}
|
|
|
|
// All
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).All("id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
users := make([]User, 0)
|
|
|
|
err = one.Structs(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).All("id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
users := make([]*User, 0)
|
|
|
|
err = one.Structs(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []User
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).All("id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
err = one.Structs(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []*User
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).All("id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
err = one.Structs(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
// Structs
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
users := make([]User, 0)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Scan(&users, "id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
users := make([]*User, 0)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Scan(&users, "id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Scan(&users, "id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []*User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Scan(&users, "id < 3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-05-13 23:21:11 +08:00
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(*users[0].Id, 1)
|
|
|
|
t.Assert(*users[0].Passport, "user_1")
|
|
|
|
t.Assert(*users[0].Password, "pass_1")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-28 21:03:25 +08:00
|
|
|
func Test_Struct_Empty(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
}
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=100").Scan(user)
|
2021-01-09 23:02:16 +08:00
|
|
|
t.Assert(err, sql.ErrNoRows)
|
|
|
|
t.AssertNE(user, nil)
|
2020-04-28 21:03:25 +08:00
|
|
|
})
|
2019-05-28 21:41:00 +08:00
|
|
|
|
2020-04-28 21:03:25 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id=100").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
var user *User
|
2021-01-09 21:05:47 +08:00
|
|
|
t.Assert(one.Struct(&user), nil)
|
|
|
|
t.Assert(user, nil)
|
2020-04-28 21:03:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var user *User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=100").Scan(&user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-09 23:02:16 +08:00
|
|
|
t.Assert(user, nil)
|
2020-04-28 21:03:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Structs_Empty(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
}
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id>100").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
users := make([]User, 0)
|
|
|
|
t.Assert(all.Structs(&users), nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id>100").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
users := make([]User, 10)
|
2021-02-09 18:00:43 +08:00
|
|
|
t.Assert(all.Structs(&users), nil)
|
2020-04-28 21:03:25 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id>100").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
var users []User
|
|
|
|
t.Assert(all.Structs(&users), nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id>100").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
users := make([]*User, 0)
|
|
|
|
t.Assert(all.Structs(&users), nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id>100").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
users := make([]*User, 10)
|
|
|
|
t.Assert(all.Structs(&users), nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id>100").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-28 21:03:25 +08:00
|
|
|
var users []*User
|
|
|
|
t.Assert(all.Structs(&users), nil)
|
|
|
|
})
|
2019-05-28 21:41:00 +08:00
|
|
|
}
|
2020-07-08 19:12:48 +08:00
|
|
|
|
|
|
|
type MyTime struct {
|
|
|
|
gtime.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type MyTimeSt struct {
|
|
|
|
CreateTime MyTime
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *MyTimeSt) UnmarshalValue(v interface{}) error {
|
|
|
|
m := gconv.Map(v)
|
|
|
|
t, err := gtime.StrToTime(gconv.String(m["create_time"]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
st.CreateTime = MyTime{*t}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
func Test_Model_Scan_CustomType_Time(t *testing.T) {
|
2020-07-08 19:12:48 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
st := new(MyTimeSt)
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Fields("create_time").Scan(st)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-08 19:12:48 +08:00
|
|
|
t.Assert(st.CreateTime.String(), "2018-10-24 10:00:00")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var stSlice []*MyTimeSt
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Fields("create_time").Scan(&stSlice)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(stSlice), TableSize)
|
2020-07-08 19:12:48 +08:00
|
|
|
t.Assert(stSlice[0].CreateTime.String(), "2018-10-24 10:00:00")
|
|
|
|
t.Assert(stSlice[9].CreateTime.String(), "2018-10-24 10:00:00")
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
|
2021-03-24 21:19:23 +08:00
|
|
|
func Test_Model_Scan_CustomType_String(t *testing.T) {
|
|
|
|
type MyString string
|
|
|
|
|
|
|
|
type MyStringSt struct {
|
|
|
|
Passport MyString
|
|
|
|
}
|
|
|
|
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
st := new(MyStringSt)
|
|
|
|
err := db.Model(table).Fields("Passport").WherePri(1).Scan(st)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(st.Passport, "user_1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var sts []MyStringSt
|
|
|
|
err := db.Model(table).Fields("Passport").Order("id asc").Scan(&sts)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(sts), TableSize)
|
2021-03-24 22:04:04 +08:00
|
|
|
t.Assert(sts[0].Passport, "user_1")
|
2021-03-24 21:19:23 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) UnmarshalValue(value interface{}) error {
|
2021-06-26 16:23:54 +08:00
|
|
|
if record, ok := value.(gdb.Record); ok {
|
|
|
|
*user = User{
|
|
|
|
Id: record["id"].Int(),
|
|
|
|
Passport: record["passport"].String(),
|
|
|
|
Password: "",
|
|
|
|
Nickname: record["nickname"].String(),
|
|
|
|
CreateTime: record["create_time"].GTime(),
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-24 21:18:59 +08:00
|
|
|
return gerror.NewCodef(gcode.CodeInvalidParameter, `unsupported value type for UnmarshalValue: %v`, reflect.TypeOf(value))
|
2021-02-27 23:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Scan_UnmarshalValue(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []*User
|
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(users), TableSize)
|
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[0].Passport, "user_1")
|
|
|
|
t.Assert(users[0].Password, "")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
t.Assert(users[0].CreateTime.String(), CreateTime)
|
|
|
|
|
|
|
|
t.Assert(users[9].Id, 10)
|
|
|
|
t.Assert(users[9].Passport, "user_10")
|
|
|
|
t.Assert(users[9].Password, "")
|
|
|
|
t.Assert(users[9].Nickname, "name_10")
|
|
|
|
t.Assert(users[9].CreateTime.String(), CreateTime)
|
|
|
|
})
|
|
|
|
}
|
2021-05-25 09:56:23 +08:00
|
|
|
|
|
|
|
func Test_Model_Scan_Map(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []*User
|
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(users), TableSize)
|
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[0].Passport, "user_1")
|
|
|
|
t.Assert(users[0].Password, "")
|
|
|
|
t.Assert(users[0].Nickname, "name_1")
|
|
|
|
t.Assert(users[0].CreateTime.String(), CreateTime)
|
|
|
|
|
|
|
|
t.Assert(users[9].Id, 10)
|
|
|
|
t.Assert(users[9].Passport, "user_10")
|
|
|
|
t.Assert(users[9].Password, "")
|
|
|
|
t.Assert(users[9].Nickname, "name_10")
|
|
|
|
t.Assert(users[9].CreateTime.String(), CreateTime)
|
|
|
|
})
|
|
|
|
}
|
2021-06-22 21:48:56 +08:00
|
|
|
|
|
|
|
func Test_Scan_AutoFilteringByStructAttributes(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
}
|
|
|
|
//db.SetDebug(true)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var user *User
|
|
|
|
err := db.Model(table).OrderAsc("id").Scan(&user)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(user.Id, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var users []User
|
|
|
|
err := db.Model(table).OrderAsc("id").Scan(&users)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(users), TableSize)
|
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
})
|
|
|
|
}
|
2021-09-15 21:17:45 +08:00
|
|
|
|
|
|
|
func Test_Scan_JsonAttributes(t *testing.T) {
|
|
|
|
type GiftImage struct {
|
|
|
|
Uid string `json:"uid"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GiftComment struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Field string `json:"field"`
|
|
|
|
Required bool `json:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Prop struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Values []string `json:"values"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Sku struct {
|
|
|
|
GiftId int64 `json:"gift_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ScorePrice int `json:"score_price"`
|
|
|
|
MarketPrice int `json:"market_price"`
|
|
|
|
CostPrice int `json:"cost_price"`
|
|
|
|
Stock int `json:"stock"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Covers struct {
|
|
|
|
List []GiftImage `json:"list"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GiftEntity struct {
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
StoreId int64 `json:"store_id"`
|
|
|
|
GiftType int `json:"gift_type"`
|
|
|
|
GiftName string `json:"gift_name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Covers Covers `json:"covers"`
|
|
|
|
Cover string `json:"cover"`
|
|
|
|
GiftCategoryId []int64 `json:"gift_category_id"`
|
|
|
|
HasProps bool `json:"has_props"`
|
|
|
|
OutSn string `json:"out_sn"`
|
|
|
|
IsLimitSell bool `json:"is_limit_sell"`
|
|
|
|
LimitSellType int `json:"limit_sell_type"`
|
|
|
|
LimitSellCycle string `json:"limit_sell_cycle"`
|
|
|
|
LimitSellCycleCount int `json:"limit_sell_cycle_count"`
|
|
|
|
LimitSellCustom bool `json:"limit_sell_custom"` // 只允许特定会员兑换
|
|
|
|
LimitCustomerTags []int64 `json:"limit_customer_tags"` // 允许兑换的成员
|
|
|
|
ScorePrice int `json:"score_price"`
|
|
|
|
MarketPrice float64 `json:"market_price"`
|
|
|
|
CostPrice int `json:"cost_price"`
|
|
|
|
Stock int `json:"stock"`
|
|
|
|
Props []Prop `json:"props"`
|
|
|
|
Skus []Sku `json:"skus"`
|
|
|
|
ExpressType []string `json:"express_type"`
|
|
|
|
Comments []GiftComment `json:"comments"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
AtLeastRechargeCount int `json:"at_least_recharge_count"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
table = "jfy_gift"
|
|
|
|
)
|
|
|
|
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1380.sql`), ";")
|
|
|
|
for _, v := range array {
|
|
|
|
if _, err := db.Exec(v); err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var (
|
|
|
|
entity = new(GiftEntity)
|
|
|
|
err = db.Model(table).Where("id", 17).Scan(entity)
|
|
|
|
)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(entity.Skus), 2)
|
|
|
|
|
|
|
|
t.Assert(entity.Skus[0].Name, "red")
|
|
|
|
t.Assert(entity.Skus[0].Stock, 10)
|
|
|
|
t.Assert(entity.Skus[0].GiftId, 1)
|
|
|
|
t.Assert(entity.Skus[0].CostPrice, 80)
|
|
|
|
t.Assert(entity.Skus[0].ScorePrice, 188)
|
|
|
|
t.Assert(entity.Skus[0].MarketPrice, 388)
|
|
|
|
|
|
|
|
t.Assert(entity.Skus[1].Name, "blue")
|
|
|
|
t.Assert(entity.Skus[1].Stock, 100)
|
|
|
|
t.Assert(entity.Skus[1].GiftId, 2)
|
|
|
|
t.Assert(entity.Skus[1].CostPrice, 81)
|
|
|
|
t.Assert(entity.Skus[1].ScorePrice, 200)
|
|
|
|
t.Assert(entity.Skus[1].MarketPrice, 288)
|
|
|
|
|
|
|
|
t.Assert(entity.Id, 17)
|
|
|
|
t.Assert(entity.StoreId, 100004)
|
|
|
|
t.Assert(entity.GiftType, 1)
|
|
|
|
t.Assert(entity.GiftName, "GIFT")
|
|
|
|
t.Assert(entity.Description, "支持个性定制的父亲节老师长辈的专属礼物")
|
|
|
|
t.Assert(len(entity.Covers.List), 3)
|
|
|
|
t.Assert(entity.OutSn, "259402")
|
|
|
|
t.Assert(entity.LimitCustomerTags, "[]")
|
|
|
|
t.Assert(entity.ScorePrice, 10)
|
|
|
|
t.Assert(len(entity.Props), 1)
|
|
|
|
t.Assert(len(entity.Comments), 2)
|
|
|
|
t.Assert(entity.Status, 99)
|
|
|
|
t.Assert(entity.Content, `<p>礼品详情</p>`)
|
|
|
|
})
|
|
|
|
}
|