2019-05-28 21:41:00 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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 (
|
2019-07-17 23:24:27 +08:00
|
|
|
"testing"
|
|
|
|
|
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"
|
2019-05-28 21:41:00 +08:00
|
|
|
)
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Inherit_Insert(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2019-05-28 21:41:00 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
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
|
|
|
}
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.Table(table).Filter().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()
|
|
|
|
gtest.Assert(err, nil)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
gtest.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
value, err := db.Table(table).Fields("passport").Where("id=100").Value()
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-05-28 21:41:00 +08:00
|
|
|
gtest.Assert(value.String(), "john-test")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Inherit_MapToStruct(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2019-05-28 21:41:00 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
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
|
|
|
}
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.Table(table).Filter().Data(data).Insert()
|
2019-05-28 21:41:00 +08:00
|
|
|
gtest.Assert(err, nil)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
gtest.Assert(n, 1)
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
one, err := db.Table(table).Where("id=100").One()
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Assert(err, nil)
|
2019-05-28 21:41:00 +08:00
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
gtest.Assert(one.Struct(user), nil)
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Assert(user.Id, data["id"])
|
|
|
|
gtest.Assert(user.Passport, data["passport"])
|
|
|
|
gtest.Assert(user.Password, data["password"])
|
|
|
|
gtest.Assert(user.Nickname, data["nickname"])
|
2019-05-28 21:41:00 +08:00
|
|
|
gtest.Assert(user.CreateTime, data["create_time"])
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|