2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-17 23:24:27 +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 (
|
2020-12-29 00:01:27 +08:00
|
|
|
"context"
|
2019-07-17 23:24:27 +08:00
|
|
|
"fmt"
|
2019-12-23 23:14:54 +08:00
|
|
|
"github.com/gogf/gf/container/garray"
|
2020-10-10 17:29:38 +08:00
|
|
|
"github.com/gogf/gf/encoding/gparser"
|
2020-12-29 00:01:27 +08:00
|
|
|
"github.com/gogf/gf/text/gstr"
|
2019-08-19 19:21:27 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-08-12 16:50:57 +08:00
|
|
|
"github.com/gogf/gf/database/gdb"
|
|
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/encoding/gxml"
|
2019-07-17 23:24:27 +08:00
|
|
|
|
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-07-17 23:24:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_DB_Ping(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
err1 := db.PingMaster()
|
|
|
|
err2 := db.PingSlave()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, nil)
|
|
|
|
t.Assert(err2, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Query(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
_, err := db.Query("SELECT ?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
_, err = db.Query("SELECT ?+?", 1, 2)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
_, err = db.Query("SELECT ?+?", g.Slice{1, 2})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
_, err = db.Query("ERROR")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Exec(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
_, err := db.Exec("SELECT ?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
_, err = db.Exec("ERROR")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Prepare(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
st, err := db.Prepare("SELECT 100")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
rows, err := st.Query()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
array, err := rows.Columns()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array[0], "100")
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
err = rows.Close()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Insert(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
_, err := db.Insert(table, g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
// normal map
|
|
|
|
result, err := db.Insert(table, g.Map{
|
|
|
|
"id": "2",
|
|
|
|
"passport": "t2",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_2",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
// struct
|
|
|
|
type User struct {
|
|
|
|
Id int `gconv:"id"`
|
|
|
|
Passport string `json:"passport"`
|
|
|
|
Password string `gconv:"password"`
|
|
|
|
Nickname string `gconv:"nickname"`
|
|
|
|
CreateTime string `json:"create_time"`
|
|
|
|
}
|
|
|
|
timeStr := gtime.Now().String()
|
|
|
|
result, err = db.Insert(table, User{
|
|
|
|
Id: 3,
|
|
|
|
Passport: "user_3",
|
|
|
|
Password: "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
Nickname: "name_3",
|
|
|
|
CreateTime: timeStr,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["id"].Int(), 3)
|
|
|
|
t.Assert(one["passport"].String(), "user_3")
|
|
|
|
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
t.Assert(one["nickname"].String(), "name_3")
|
|
|
|
t.Assert(one["create_time"].GTime().String(), timeStr)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
// *struct
|
|
|
|
timeStr = gtime.Now().String()
|
|
|
|
result, err = db.Insert(table, &User{
|
|
|
|
Id: 4,
|
|
|
|
Passport: "t4",
|
|
|
|
Password: "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
Nickname: "name_4",
|
|
|
|
CreateTime: timeStr,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err = db.Model(table).Where("id", 4).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["id"].Int(), 4)
|
|
|
|
t.Assert(one["passport"].String(), "t4")
|
|
|
|
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
t.Assert(one["nickname"].String(), "name_4")
|
|
|
|
t.Assert(one["create_time"].GTime().String(), timeStr)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
// batch with Insert
|
|
|
|
timeStr = gtime.Now().String()
|
|
|
|
r, err := db.Insert(table, g.Slice{
|
|
|
|
g.Map{
|
|
|
|
"id": 200,
|
|
|
|
"passport": "t200",
|
|
|
|
"password": "25d55ad283aa400af464c76d71qw07ad",
|
|
|
|
"nickname": "T200",
|
|
|
|
"create_time": timeStr,
|
|
|
|
},
|
|
|
|
g.Map{
|
|
|
|
"id": 300,
|
|
|
|
"passport": "t300",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T300",
|
|
|
|
"create_time": timeStr,
|
|
|
|
},
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err = db.Model(table).Where("id", 200).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["id"].Int(), 200)
|
|
|
|
t.Assert(one["passport"].String(), "t200")
|
|
|
|
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d71qw07ad")
|
|
|
|
t.Assert(one["nickname"].String(), "T200")
|
|
|
|
t.Assert(one["create_time"].GTime().String(), timeStr)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:16:13 +08:00
|
|
|
// Fix issue: https://github.com/gogf/gf/issues/819
|
2020-10-10 17:29:38 +08:00
|
|
|
func Test_DB_Insert_WithStructAndSliceAttribute(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Password struct {
|
|
|
|
Salt string `json:"salt"`
|
|
|
|
Pass string `json:"pass"`
|
|
|
|
}
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": &Password{"123", "456"},
|
|
|
|
"nickname": []string{"A", "B", "C"},
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}
|
|
|
|
_, err := db.Insert(table, data)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-10 17:29:38 +08:00
|
|
|
|
|
|
|
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-10 17:29:38 +08:00
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["create_time"], data["create_time"])
|
|
|
|
t.Assert(one["nickname"], gparser.MustToJson(data["nickname"]))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:16:13 +08:00
|
|
|
func Test_DB_Insert_KeyFieldNameMapping(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
data := User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "user_1",
|
|
|
|
Password: "pass_1",
|
|
|
|
Nickname: "name_1",
|
|
|
|
CreateTime: "2020-10-10 12:00:01",
|
|
|
|
}
|
|
|
|
_, err := db.Insert(table, data)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
|
|
|
|
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
t.Assert(one["passport"], data.Passport)
|
|
|
|
t.Assert(one["create_time"], data.CreateTime)
|
|
|
|
t.Assert(one["nickname"], data.Nickname)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Upadte_KeyFieldNameMapping(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
data := User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "user_10",
|
|
|
|
Password: "pass_10",
|
|
|
|
Nickname: "name_10",
|
|
|
|
CreateTime: "2020-10-10 12:00:01",
|
|
|
|
}
|
|
|
|
_, err := db.Update(table, data, "id=1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
|
|
|
|
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
t.Assert(one["passport"], data.Passport)
|
|
|
|
t.Assert(one["create_time"], data.CreateTime)
|
|
|
|
t.Assert(one["nickname"], data.Nickname)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-02 22:35:47 +08:00
|
|
|
// This is no longer used as the filter feature is automatically enabled from GoFrame v1.16.0.
|
|
|
|
//func Test_DB_Insert_KeyFieldNameMapping_Error(t *testing.T) {
|
|
|
|
// table := createTable()
|
|
|
|
// defer dropTable(table)
|
|
|
|
//
|
|
|
|
// gtest.C(t, func(t *gtest.T) {
|
|
|
|
// type User struct {
|
|
|
|
// Id int
|
|
|
|
// Passport string
|
|
|
|
// Password string
|
|
|
|
// Nickname string
|
|
|
|
// CreateTime string
|
|
|
|
// NoneExistField string
|
|
|
|
// }
|
|
|
|
// data := User{
|
|
|
|
// Id: 1,
|
|
|
|
// Passport: "user_1",
|
|
|
|
// Password: "pass_1",
|
|
|
|
// Nickname: "name_1",
|
|
|
|
// CreateTime: "2020-10-10 12:00:01",
|
|
|
|
// }
|
|
|
|
// _, err := db.Insert(table, data)
|
|
|
|
// t.AssertNE(err, nil)
|
|
|
|
// })
|
|
|
|
//}
|
2020-10-17 18:16:13 +08:00
|
|
|
|
2020-02-08 23:46:10 +08:00
|
|
|
func Test_DB_InsertIgnore(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-08 23:46:10 +08:00
|
|
|
_, err := db.Insert(table, g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2020-02-08 23:46:10 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-08 23:46:10 +08:00
|
|
|
_, err := db.InsertIgnore(table, g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-02-08 23:46:10 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_DB_BatchInsert(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-06-08 20:32:34 +08:00
|
|
|
r, err := db.Insert(table, g.List{
|
2019-07-17 23:24:27 +08:00
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"passport": "t2",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_2",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"passport": "user_3",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_3",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
}, 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
n, _ = r.LastInsertId()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
// []interface{}
|
2021-06-08 20:32:34 +08:00
|
|
|
r, err := db.Insert(table, g.Slice{
|
2019-07-17 23:24:27 +08:00
|
|
|
g.Map{
|
|
|
|
"id": 2,
|
|
|
|
"passport": "t2",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_2",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
g.Map{
|
|
|
|
"id": 3,
|
|
|
|
"passport": "user_3",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_3",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
}, 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// batch insert map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-06-08 20:32:34 +08:00
|
|
|
result, err := db.Insert(table, g.Map{
|
2019-07-17 23:24:27 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "p1",
|
|
|
|
"nickname": "T1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-01-03 20:23:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_BatchInsert_Struct(t *testing.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
// batch insert struct
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type User struct {
|
2020-01-03 20:23:10 +08:00
|
|
|
Id int `c:"id"`
|
|
|
|
Passport string `c:"passport"`
|
|
|
|
Password string `c:"password"`
|
|
|
|
NickName string `c:"nickname"`
|
|
|
|
CreateTime *gtime.Time `c:"create_time"`
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
|
|
|
user := &User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "t1",
|
|
|
|
Password: "p1",
|
|
|
|
NickName: "T1",
|
|
|
|
CreateTime: gtime.Now(),
|
|
|
|
}
|
2021-06-08 20:32:34 +08:00
|
|
|
result, err := db.Insert(table, user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Save(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
timeStr := gtime.Now().String()
|
|
|
|
_, err := db.Save(table, g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T11",
|
|
|
|
"create_time": timeStr,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["id"].Int(), 1)
|
|
|
|
t.Assert(one["passport"].String(), "t1")
|
|
|
|
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
t.Assert(one["nickname"].String(), "T11")
|
|
|
|
t.Assert(one["create_time"].GTime().String(), timeStr)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Replace(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
timeStr := gtime.Now().String()
|
|
|
|
_, err := db.Replace(table, g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T11",
|
|
|
|
"create_time": timeStr,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["id"].Int(), 1)
|
|
|
|
t.Assert(one["passport"].String(), "t1")
|
|
|
|
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
|
|
|
t.Assert(one["nickname"].String(), "T11")
|
|
|
|
t.Assert(one["create_time"].GTime().String(), timeStr)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Update(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.Update(table, "password='987654321'", "id=3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["id"].Int(), 3)
|
|
|
|
t.Assert(one["passport"].String(), "user_3")
|
|
|
|
t.Assert(one["password"].String(), "987654321")
|
|
|
|
t.Assert(one["nickname"].String(), "name_3")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetAll(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), g.Slice{1})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?)", table), g.Slice{1, 2, 3})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 2)
|
|
|
|
t.Assert(result[2]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 2)
|
|
|
|
t.Assert(result[2]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3}...)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 2)
|
|
|
|
t.Assert(result[2]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id>=? AND id <=?", table), g.Slice{1, 3})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 2)
|
|
|
|
t.Assert(result[2]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetOne(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
record, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(record["nickname"].String(), "name_1")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetValue(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
value, err := db.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetCount(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
count, err := db.GetCount(fmt.Sprintf("SELECT * FROM %s", table))
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(count, TableSize)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetStruct(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-06-26 18:20:55 +08:00
|
|
|
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_3")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-06-26 18:20:55 +08:00
|
|
|
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_3")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetStructs(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
var users []User
|
2021-06-26 18:20:55 +08:00
|
|
|
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize-1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 2)
|
|
|
|
t.Assert(users[1].Id, 3)
|
|
|
|
t.Assert(users[2].Id, 4)
|
|
|
|
t.Assert(users[0].NickName, "name_2")
|
|
|
|
t.Assert(users[1].NickName, "name_3")
|
|
|
|
t.Assert(users[2].NickName, "name_4")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var users []User
|
2021-06-26 18:20:55 +08:00
|
|
|
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize-1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 2)
|
|
|
|
t.Assert(users[1].Id, 3)
|
|
|
|
t.Assert(users[2].Id, 4)
|
|
|
|
t.Assert(users[0].NickName, "name_2")
|
|
|
|
t.Assert(users[1].NickName, "name_3")
|
|
|
|
t.Assert(users[2].NickName, "name_4")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_GetScan(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
|
|
|
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_3")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
|
|
|
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_3")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
var users []User
|
|
|
|
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize-1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 2)
|
|
|
|
t.Assert(users[1].Id, 3)
|
|
|
|
t.Assert(users[2].Id, 4)
|
|
|
|
t.Assert(users[0].NickName, "name_2")
|
|
|
|
t.Assert(users[1].NickName, "name_3")
|
|
|
|
t.Assert(users[2].NickName, "name_4")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var users []User
|
|
|
|
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize-1)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 2)
|
|
|
|
t.Assert(users[1].Id, 3)
|
|
|
|
t.Assert(users[2].Id, 4)
|
|
|
|
t.Assert(users[0].NickName, "name_2")
|
|
|
|
t.Assert(users[1].NickName, "name_3")
|
|
|
|
t.Assert(users[2].NickName, "name_4")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Delete(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-06 00:00:41 +08:00
|
|
|
result, err := db.Delete(table, 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Time(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.Insert(table, g.Map{
|
|
|
|
"id": 200,
|
|
|
|
"passport": "t200",
|
|
|
|
"password": "123456",
|
|
|
|
"nickname": "T200",
|
|
|
|
"create_time": time.Now(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 200)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.String(), "t200")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-19 23:53:03 +08:00
|
|
|
t1 := time.Now()
|
2019-07-17 23:24:27 +08:00
|
|
|
result, err := db.Insert(table, g.Map{
|
|
|
|
"id": 300,
|
|
|
|
"passport": "t300",
|
|
|
|
"password": "123456",
|
|
|
|
"nickname": "T300",
|
2020-03-19 23:53:03 +08:00
|
|
|
"create_time": &t1,
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 300)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.String(), "t300")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-06 00:00:41 +08:00
|
|
|
result, err := db.Delete(table, 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
func Test_DB_ToJson(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id =? ", 1).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
users := make([]User, 0)
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(users)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ToJson
|
2019-10-01 16:03:18 +08:00
|
|
|
resultJson, err := gjson.LoadContent(result.Json())
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, resultJson.GetInt("0.id"))
|
|
|
|
t.Assert(users[0].Passport, resultJson.GetString("0.passport"))
|
|
|
|
t.Assert(users[0].Password, resultJson.GetString("0.password"))
|
|
|
|
t.Assert(users[0].NickName, resultJson.GetString("0.nickname"))
|
|
|
|
t.Assert(users[0].CreateTime, resultJson.GetString("0.create_time"))
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
result = nil
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&users)
|
2021-02-09 18:00:43 +08:00
|
|
|
t.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id =? ", 1).One()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
users := User{}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Struct(&users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = nil
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Struct(&users)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToXml(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
record, err := db.Model(table).Fields("*").Where("id = ?", 1).One()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
user := User{}
|
2019-10-01 16:03:18 +08:00
|
|
|
err = record.Struct(&user)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
result, err := gxml.Decode([]byte(record.Xml("doc")))
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultXml := result["doc"].(map[string]interface{})
|
|
|
|
if v, ok := resultXml["id"]; ok {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.Id, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
} else {
|
|
|
|
gtest.Fatal("FAIL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := resultXml["passport"]; ok {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.Passport, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
} else {
|
|
|
|
gtest.Fatal("FAIL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := resultXml["password"]; ok {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.Password, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
} else {
|
|
|
|
gtest.Fatal("FAIL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := resultXml["nickname"]; ok {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
} else {
|
|
|
|
gtest.Fatal("FAIL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := resultXml["create_time"]; ok {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.CreateTime, v)
|
2019-08-12 16:50:57 +08:00
|
|
|
} else {
|
|
|
|
gtest.Fatal("FAIL")
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToStringMap(t *testing.T) {
|
|
|
|
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
id := "1"
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id = ?", 1).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type t_user struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
t_users := make([]t_user, 0)
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&t_users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
resultStringMap := result.MapKeyStr("id")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(t_users[0].Id, resultStringMap[id]["id"])
|
|
|
|
t.Assert(t_users[0].Passport, resultStringMap[id]["passport"])
|
|
|
|
t.Assert(t_users[0].Password, resultStringMap[id]["password"])
|
|
|
|
t.Assert(t_users[0].NickName, resultStringMap[id]["nickname"])
|
|
|
|
t.Assert(t_users[0].CreateTime, resultStringMap[id]["create_time"])
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToIntMap(t *testing.T) {
|
|
|
|
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
id := 1
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id = ?", id).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type t_user struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
t_users := make([]t_user, 0)
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&t_users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
resultIntMap := result.MapKeyInt("id")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(t_users[0].Id, resultIntMap[id]["id"])
|
|
|
|
t.Assert(t_users[0].Passport, resultIntMap[id]["passport"])
|
|
|
|
t.Assert(t_users[0].Password, resultIntMap[id]["password"])
|
|
|
|
t.Assert(t_users[0].NickName, resultIntMap[id]["nickname"])
|
|
|
|
t.Assert(t_users[0].CreateTime, resultIntMap[id]["create_time"])
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToUintMap(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
id := 1
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id = ?", id).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type t_user struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
t_users := make([]t_user, 0)
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&t_users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
resultUintMap := result.MapKeyUint("id")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(t_users[0].Id, resultUintMap[uint(id)]["id"])
|
|
|
|
t.Assert(t_users[0].Passport, resultUintMap[uint(id)]["passport"])
|
|
|
|
t.Assert(t_users[0].Password, resultUintMap[uint(id)]["password"])
|
|
|
|
t.Assert(t_users[0].NickName, resultUintMap[uint(id)]["nickname"])
|
|
|
|
t.Assert(t_users[0].CreateTime, resultUintMap[uint(id)]["create_time"])
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToStringRecord(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
id := 1
|
|
|
|
ids := "1"
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id = ?", id).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type t_user struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
t_users := make([]t_user, 0)
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&t_users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
resultStringRecord := result.RecordKeyStr("id")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(t_users[0].Id, resultStringRecord[ids]["id"].Int())
|
|
|
|
t.Assert(t_users[0].Passport, resultStringRecord[ids]["passport"].String())
|
|
|
|
t.Assert(t_users[0].Password, resultStringRecord[ids]["password"].String())
|
|
|
|
t.Assert(t_users[0].NickName, resultStringRecord[ids]["nickname"].String())
|
|
|
|
t.Assert(t_users[0].CreateTime, resultStringRecord[ids]["create_time"].String())
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToIntRecord(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
id := 1
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id = ?", id).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type t_user struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
t_users := make([]t_user, 0)
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&t_users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
resultIntRecord := result.RecordKeyInt("id")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(t_users[0].Id, resultIntRecord[id]["id"].Int())
|
|
|
|
t.Assert(t_users[0].Passport, resultIntRecord[id]["passport"].String())
|
|
|
|
t.Assert(t_users[0].Password, resultIntRecord[id]["password"].String())
|
|
|
|
t.Assert(t_users[0].NickName, resultIntRecord[id]["nickname"].String())
|
|
|
|
t.Assert(t_users[0].CreateTime, resultIntRecord[id]["create_time"].String())
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_ToUintRecord(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
id := 1
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("*").Where("id = ?", id).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type t_user struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
t_users := make([]t_user, 0)
|
2019-10-01 16:03:18 +08:00
|
|
|
err = result.Structs(&t_users)
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:03:18 +08:00
|
|
|
resultUintRecord := result.RecordKeyUint("id")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(t_users[0].Id, resultUintRecord[uint(id)]["id"].Int())
|
|
|
|
t.Assert(t_users[0].Passport, resultUintRecord[uint(id)]["passport"].String())
|
|
|
|
t.Assert(t_users[0].Password, resultUintRecord[uint(id)]["password"].String())
|
|
|
|
t.Assert(t_users[0].NickName, resultUintRecord[uint(id)]["nickname"].String())
|
|
|
|
t.Assert(t_users[0].CreateTime, resultUintRecord[uint(id)]["create_time"].String())
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_TableField(t *testing.T) {
|
|
|
|
name := "field_test"
|
|
|
|
dropTable(name)
|
|
|
|
defer dropTable(name)
|
|
|
|
_, err := db.Exec(fmt.Sprintf(`
|
|
|
|
CREATE TABLE %s (
|
|
|
|
field_tinyint tinyint(8) NULL ,
|
|
|
|
field_int int(8) NULL ,
|
|
|
|
field_integer integer(8) NULL ,
|
|
|
|
field_bigint bigint(8) NULL ,
|
|
|
|
field_bit bit(3) NULL ,
|
|
|
|
field_real real(8,0) NULL ,
|
|
|
|
field_double double(12,2) NULL ,
|
|
|
|
field_varchar varchar(10) NULL ,
|
|
|
|
field_varbinary varbinary(255) NULL
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`, name))
|
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := gdb.Map{
|
|
|
|
"field_tinyint": 1,
|
|
|
|
"field_int": 2,
|
|
|
|
"field_integer": 3,
|
|
|
|
"field_bigint": 4,
|
|
|
|
"field_bit": 6,
|
|
|
|
"field_real": 123,
|
|
|
|
"field_double": 123.25,
|
|
|
|
"field_varchar": "abc",
|
|
|
|
"field_varbinary": "aaa",
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
res, err := db.Model(name).Data(data).Insert()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
} else {
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.Assert(n, 1)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(name).Fields("*").Where("field_int = ?", 2).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:53:03 +08:00
|
|
|
gtest.Assert(result[0], data)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 23:14:54 +08:00
|
|
|
func Test_DB_Prefix(t *testing.T) {
|
|
|
|
db := dbPrefix
|
2021-02-27 23:58:36 +08:00
|
|
|
name := fmt.Sprintf(`%s_%d`, TableName, gtime.TimestampNano())
|
|
|
|
table := TableNamePrefix1 + name
|
2019-12-23 23:14:54 +08:00
|
|
|
createTableWithDb(db, table)
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-23 23:14:54 +08:00
|
|
|
id := 10000
|
|
|
|
result, err := db.Insert(name, g.Map{
|
|
|
|
"id": id,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, id),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, id),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, id),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-23 23:14:54 +08:00
|
|
|
|
|
|
|
n, e := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
|
|
|
t.Assert(n, 1)
|
2019-12-23 23:14:54 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-23 23:14:54 +08:00
|
|
|
id := 10000
|
|
|
|
result, err := db.Replace(name, g.Map{
|
|
|
|
"id": id,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, id),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, id),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, id),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:01").String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-23 23:14:54 +08:00
|
|
|
|
|
|
|
n, e := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
|
|
|
t.Assert(n, 2)
|
2019-12-23 23:14:54 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-23 23:14:54 +08:00
|
|
|
id := 10000
|
|
|
|
result, err := db.Save(name, g.Map{
|
|
|
|
"id": id,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, id),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, id),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, id),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:02").String(),
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-23 23:14:54 +08:00
|
|
|
|
|
|
|
n, e := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
|
|
|
t.Assert(n, 2)
|
2019-12-23 23:14:54 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-23 23:14:54 +08:00
|
|
|
id := 10000
|
|
|
|
result, err := db.Update(name, g.Map{
|
|
|
|
"id": id,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, id),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, id),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, id),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:03").String(),
|
|
|
|
}, "id=?", id)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-23 23:14:54 +08:00
|
|
|
|
|
|
|
n, e := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
|
|
|
t.Assert(n, 1)
|
2019-12-23 23:14:54 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-23 23:14:54 +08:00
|
|
|
id := 10000
|
|
|
|
result, err := db.Delete(name, "id=?", id)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-23 23:14:54 +08:00
|
|
|
|
|
|
|
n, e := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
|
|
|
t.Assert(n, 1)
|
2019-12-23 23:14:54 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-23 23:14:54 +08:00
|
|
|
array := garray.New(true)
|
2021-02-27 23:58:36 +08:00
|
|
|
for i := 1; i <= TableSize; i++ {
|
2019-12-23 23:14:54 +08:00
|
|
|
array.Append(g.Map{
|
|
|
|
"id": i,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, i),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, i),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, i),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-08 20:32:34 +08:00
|
|
|
result, err := db.Insert(name, array.Slice())
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-12-23 23:14:54 +08:00
|
|
|
|
|
|
|
n, e := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize)
|
2019-12-23 23:14:54 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-12 16:50:57 +08:00
|
|
|
func Test_Model_InnerJoin(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
table1 := createInitTable("user1")
|
|
|
|
table2 := createInitTable("user2")
|
|
|
|
|
|
|
|
defer dropTable(table1)
|
|
|
|
defer dropTable(table2)
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
res, err := db.Model(table1).Where("id > ?", 5).Delete()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 5)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").OrderBy("u1.id").Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 5)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err = db.Model(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > ?", 1).OrderBy("u1.id").Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 4)
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_LeftJoin(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
table1 := createInitTable("user1")
|
|
|
|
table2 := createInitTable("user2")
|
|
|
|
|
|
|
|
defer dropTable(table1)
|
|
|
|
defer dropTable(table2)
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
res, err := db.Model(table2).Where("id > ?", 3).Delete()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
} else {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 7)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table1+" u1").LeftJoin(table2+" u2", "u1.id = u2.id").Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 10)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err = db.Model(table1+" u1").LeftJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > ? ", 2).Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 8)
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_RightJoin(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
table1 := createInitTable("user1")
|
|
|
|
table2 := createInitTable("user2")
|
|
|
|
|
|
|
|
defer dropTable(table1)
|
|
|
|
defer dropTable(table2)
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
res, err := db.Model(table1).Where("id > ?", 3).Delete()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 7)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table1+" u1").RightJoin(table2+" u2", "u1.id = u2.id").Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 10)
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err = db.Model(table1+" u1").RightJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > 2").Select()
|
2019-08-12 16:50:57 +08:00
|
|
|
if err != nil {
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Fatal(err)
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
|
|
|
}
|
2020-07-11 09:53:16 +08:00
|
|
|
|
|
|
|
func Test_Empty_Slice_Argument(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.GetAll(fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
}
|
2020-11-28 13:52:17 +08:00
|
|
|
|
|
|
|
// update counter test
|
|
|
|
func Test_DB_UpdateCounter(t *testing.T) {
|
2020-11-29 22:26:16 +08:00
|
|
|
tableName := "gf_update_counter_test_" + gtime.TimestampNanoStr()
|
2020-11-28 13:52:17 +08:00
|
|
|
_, err := db.Exec(fmt.Sprintf(`
|
|
|
|
CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
id int(10) unsigned NOT NULL,
|
|
|
|
views int(8) unsigned DEFAULT '0' NOT NULL ,
|
|
|
|
updated_time int(10) unsigned DEFAULT '0' NOT NULL
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`, tableName))
|
|
|
|
if err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
2020-11-29 22:26:16 +08:00
|
|
|
defer dropTable(tableName)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
insertData := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"views": 0,
|
|
|
|
"updated_time": 0,
|
|
|
|
}
|
|
|
|
_, err = db.Insert(tableName, insertData)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-29 22:26:16 +08:00
|
|
|
})
|
|
|
|
|
2020-11-28 13:52:17 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
gdbCounter := &gdb.Counter{
|
2021-07-13 19:37:16 +08:00
|
|
|
Field: "id",
|
2020-11-28 13:52:17 +08:00
|
|
|
Value: 1,
|
|
|
|
}
|
|
|
|
updateData := g.Map{
|
2020-12-28 23:03:13 +08:00
|
|
|
"views": gdbCounter,
|
2020-11-28 13:52:17 +08:00
|
|
|
}
|
2020-11-29 22:26:16 +08:00
|
|
|
result, err := db.Update(tableName, updateData, "id", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-29 22:26:16 +08:00
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(tableName).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-29 22:26:16 +08:00
|
|
|
t.Assert(one["id"].Int(), 1)
|
2021-07-13 19:37:16 +08:00
|
|
|
t.Assert(one["views"].Int(), 2)
|
2020-11-29 22:26:16 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
gdbCounter := &gdb.Counter{
|
|
|
|
Field: "views",
|
|
|
|
Value: -1,
|
|
|
|
}
|
|
|
|
updateData := g.Map{
|
|
|
|
"views": gdbCounter,
|
|
|
|
"updated_time": gtime.Now().Unix(),
|
|
|
|
}
|
|
|
|
result, err := db.Update(tableName, updateData, "id", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-28 13:52:17 +08:00
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(tableName).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-28 14:56:21 +08:00
|
|
|
t.Assert(one["id"].Int(), 1)
|
2021-07-13 19:37:16 +08:00
|
|
|
t.Assert(one["views"].Int(), 1)
|
2020-11-28 13:52:17 +08:00
|
|
|
})
|
|
|
|
}
|
2020-12-29 00:01:27 +08:00
|
|
|
|
|
|
|
func Test_DB_Ctx(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := db.Ctx(ctx).Query("SELECT SLEEP(10)")
|
|
|
|
t.Assert(gstr.Contains(err.Error(), "deadline"), true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_DB_Ctx_Logger(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
defer db.SetDebug(db.GetDebug())
|
|
|
|
db.SetDebug(true)
|
|
|
|
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
|
|
|
|
_, err := db.Ctx(ctx).Query("SELECT 1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-12-29 00:01:27 +08:00
|
|
|
})
|
|
|
|
}
|