mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 03:38:35 +08:00
1284 lines
31 KiB
Go
1284 lines
31 KiB
Go
// 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 (
|
|
"fmt"
|
|
"github.com/gogf/gf/container/garray"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/database/gdb"
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
"github.com/gogf/gf/encoding/gxml"
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
"github.com/gogf/gf/os/gtime"
|
|
"github.com/gogf/gf/test/gtest"
|
|
)
|
|
|
|
func Test_DB_Ping(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
err1 := db.PingMaster()
|
|
err2 := db.PingSlave()
|
|
t.Assert(err1, nil)
|
|
t.Assert(err2, nil)
|
|
})
|
|
}
|
|
|
|
func Test_DB_Query(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
_, err := db.Query("SELECT ?", 1)
|
|
t.Assert(err, nil)
|
|
|
|
_, err = db.Query("SELECT ?+?", 1, 2)
|
|
t.Assert(err, nil)
|
|
|
|
_, err = db.Query("SELECT ?+?", g.Slice{1, 2})
|
|
t.Assert(err, nil)
|
|
|
|
_, err = db.Query("ERROR")
|
|
t.AssertNE(err, nil)
|
|
})
|
|
|
|
}
|
|
|
|
func Test_DB_Exec(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
_, err := db.Exec("SELECT ?", 1)
|
|
t.Assert(err, nil)
|
|
|
|
_, err = db.Exec("ERROR")
|
|
t.AssertNE(err, nil)
|
|
})
|
|
|
|
}
|
|
|
|
func Test_DB_Prepare(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
st, err := db.Prepare("SELECT 100")
|
|
t.Assert(err, nil)
|
|
|
|
rows, err := st.Query()
|
|
t.Assert(err, nil)
|
|
|
|
array, err := rows.Columns()
|
|
t.Assert(err, nil)
|
|
t.Assert(array[0], "100")
|
|
|
|
err = rows.Close()
|
|
t.Assert(err, nil)
|
|
})
|
|
}
|
|
|
|
func Test_DB_Insert(t *testing.T) {
|
|
table := createTable()
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
_, err := db.Insert(table, g.Map{
|
|
"id": 1,
|
|
"passport": "t1",
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
"nickname": "T1",
|
|
"create_time": gtime.Now().String(),
|
|
})
|
|
t.Assert(err, nil)
|
|
|
|
// normal map
|
|
result, err := db.Insert(table, g.Map{
|
|
"id": "2",
|
|
"passport": "t2",
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
"nickname": "name_2",
|
|
"create_time": gtime.Now().String(),
|
|
})
|
|
t.Assert(err, nil)
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
|
|
// 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,
|
|
})
|
|
t.Assert(err, nil)
|
|
n, _ = result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
|
|
one, err := db.Table(table).Where("id", 3).One()
|
|
t.Assert(err, nil)
|
|
|
|
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)
|
|
|
|
// *struct
|
|
timeStr = gtime.Now().String()
|
|
result, err = db.Insert(table, &User{
|
|
Id: 4,
|
|
Passport: "t4",
|
|
Password: "25d55ad283aa400af464c76d713c07ad",
|
|
Nickname: "name_4",
|
|
CreateTime: timeStr,
|
|
})
|
|
t.Assert(err, nil)
|
|
n, _ = result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
|
|
one, err = db.Table(table).Where("id", 4).One()
|
|
t.Assert(err, nil)
|
|
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)
|
|
|
|
// 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,
|
|
},
|
|
})
|
|
t.Assert(err, nil)
|
|
n, _ = r.RowsAffected()
|
|
t.Assert(n, 2)
|
|
|
|
one, err = db.Table(table).Where("id", 200).One()
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
}
|
|
|
|
func Test_DB_InsertIgnore(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
gtest.C(t, func(t *gtest.T) {
|
|
_, err := db.Insert(table, g.Map{
|
|
"id": 1,
|
|
"passport": "t1",
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
"nickname": "T1",
|
|
"create_time": gtime.Now().String(),
|
|
})
|
|
t.AssertNE(err, nil)
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
_, err := db.InsertIgnore(table, g.Map{
|
|
"id": 1,
|
|
"passport": "t1",
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
"nickname": "T1",
|
|
"create_time": gtime.Now().String(),
|
|
})
|
|
t.Assert(err, nil)
|
|
})
|
|
}
|
|
|
|
func Test_DB_BatchInsert(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table := createTable()
|
|
defer dropTable(table)
|
|
r, err := db.BatchInsert(table, g.List{
|
|
{
|
|
"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)
|
|
t.Assert(err, nil)
|
|
n, _ := r.RowsAffected()
|
|
t.Assert(n, 2)
|
|
|
|
n, _ = r.LastInsertId()
|
|
t.Assert(n, 3)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table := createTable()
|
|
defer dropTable(table)
|
|
// []interface{}
|
|
r, err := db.BatchInsert(table, g.Slice{
|
|
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)
|
|
t.Assert(err, nil)
|
|
n, _ := r.RowsAffected()
|
|
t.Assert(n, 2)
|
|
})
|
|
|
|
// batch insert map
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table := createTable()
|
|
defer dropTable(table)
|
|
result, err := db.BatchInsert(table, g.Map{
|
|
"id": 1,
|
|
"passport": "t1",
|
|
"password": "p1",
|
|
"nickname": "T1",
|
|
"create_time": gtime.Now().String(),
|
|
})
|
|
t.Assert(err, nil)
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
})
|
|
|
|
}
|
|
|
|
func Test_DB_BatchInsert_Struct(t *testing.T) {
|
|
// batch insert struct
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table := createTable()
|
|
defer dropTable(table)
|
|
|
|
type User struct {
|
|
Id int `c:"id"`
|
|
Passport string `c:"passport"`
|
|
Password string `c:"password"`
|
|
NickName string `c:"nickname"`
|
|
CreateTime *gtime.Time `c:"create_time"`
|
|
}
|
|
user := &User{
|
|
Id: 1,
|
|
Passport: "t1",
|
|
Password: "p1",
|
|
NickName: "T1",
|
|
CreateTime: gtime.Now(),
|
|
}
|
|
result, err := db.BatchInsert(table, user)
|
|
t.Assert(err, nil)
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
})
|
|
}
|
|
|
|
func Test_DB_Save(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
timeStr := gtime.Now().String()
|
|
_, err := db.Save(table, g.Map{
|
|
"id": 1,
|
|
"passport": "t1",
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
"nickname": "T11",
|
|
"create_time": timeStr,
|
|
})
|
|
t.Assert(err, nil)
|
|
|
|
one, err := db.Table(table).Where("id", 1).One()
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
}
|
|
|
|
func Test_DB_Replace(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
timeStr := gtime.Now().String()
|
|
_, err := db.Replace(table, g.Map{
|
|
"id": 1,
|
|
"passport": "t1",
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
"nickname": "T11",
|
|
"create_time": timeStr,
|
|
})
|
|
t.Assert(err, nil)
|
|
|
|
one, err := db.Table(table).Where("id", 1).One()
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
}
|
|
|
|
func Test_DB_Update(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.Update(table, "password='987654321'", "id=3")
|
|
t.Assert(err, nil)
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
|
|
one, err := db.Table(table).Where("id", 3).One()
|
|
t.Assert(err, nil)
|
|
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")
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetAll(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=?", table), 1)
|
|
t.Assert(err, nil)
|
|
t.Assert(len(result), 1)
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), g.Slice{1})
|
|
t.Assert(err, nil)
|
|
t.Assert(len(result), 1)
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?)", table), g.Slice{1, 2, 3})
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3})
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3}...)
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id>=? AND id <=?", table), g.Slice{1, 3})
|
|
t.Assert(err, nil)
|
|
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)
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetOne(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
gtest.C(t, func(t *gtest.T) {
|
|
record, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_1")
|
|
t.Assert(err, nil)
|
|
t.Assert(record["nickname"].String(), "name_1")
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetValue(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
gtest.C(t, func(t *gtest.T) {
|
|
value, err := db.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3")
|
|
t.Assert(err, nil)
|
|
t.Assert(value.Int(), 3)
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetCount(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
gtest.C(t, func(t *gtest.T) {
|
|
count, err := db.GetCount(fmt.Sprintf("SELECT * FROM %s", table))
|
|
t.Assert(err, nil)
|
|
t.Assert(count, SIZE)
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetStruct(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 gtime.Time
|
|
}
|
|
user := new(User)
|
|
err := db.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
|
t.Assert(err, nil)
|
|
t.Assert(user.NickName, "name_3")
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
type User struct {
|
|
Id int
|
|
Passport string
|
|
Password string
|
|
NickName string
|
|
CreateTime *gtime.Time
|
|
}
|
|
user := new(User)
|
|
err := db.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
|
t.Assert(err, nil)
|
|
t.Assert(user.NickName, "name_3")
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetStructs(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 gtime.Time
|
|
}
|
|
var users []User
|
|
err := db.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
|
t.Assert(err, nil)
|
|
t.Assert(len(users), SIZE-1)
|
|
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")
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
type User struct {
|
|
Id int
|
|
Passport string
|
|
Password string
|
|
NickName string
|
|
CreateTime *gtime.Time
|
|
}
|
|
var users []User
|
|
err := db.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
|
t.Assert(err, nil)
|
|
t.Assert(len(users), SIZE-1)
|
|
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")
|
|
})
|
|
}
|
|
|
|
func Test_DB_GetScan(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 gtime.Time
|
|
}
|
|
user := new(User)
|
|
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
|
t.Assert(err, nil)
|
|
t.Assert(user.NickName, "name_3")
|
|
})
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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)
|
|
t.Assert(err, nil)
|
|
t.Assert(user.NickName, "name_3")
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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)
|
|
t.Assert(err, nil)
|
|
t.Assert(len(users), SIZE-1)
|
|
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")
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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)
|
|
t.Assert(err, nil)
|
|
t.Assert(len(users), SIZE-1)
|
|
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")
|
|
})
|
|
}
|
|
|
|
func Test_DB_Delete(t *testing.T) {
|
|
table := createInitTable()
|
|
defer dropTable(table)
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.Delete(table, nil)
|
|
t.Assert(err, nil)
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, SIZE)
|
|
})
|
|
}
|
|
|
|
func Test_DB_Time(t *testing.T) {
|
|
table := createTable()
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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()
|
|
t.Assert(n, 1)
|
|
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 200)
|
|
t.Assert(err, nil)
|
|
t.Assert(value.String(), "t200")
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
t1 := time.Now()
|
|
result, err := db.Insert(table, g.Map{
|
|
"id": 300,
|
|
"passport": "t300",
|
|
"password": "123456",
|
|
"nickname": "T300",
|
|
"create_time": &t1,
|
|
})
|
|
if err != nil {
|
|
gtest.Error(err)
|
|
}
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, 1)
|
|
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 300)
|
|
t.Assert(err, nil)
|
|
t.Assert(value.String(), "t300")
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.Delete(table, nil)
|
|
t.Assert(err, nil)
|
|
n, _ := result.RowsAffected()
|
|
t.Assert(n, 2)
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.Table(table).Fields("*").Where("id =? ", 1).Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
type User struct {
|
|
Id int
|
|
Passport string
|
|
Password string
|
|
NickName string
|
|
CreateTime string
|
|
}
|
|
|
|
users := make([]User, 0)
|
|
|
|
err = result.Structs(users)
|
|
t.AssertNE(err, nil)
|
|
|
|
err = result.Structs(&users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
//ToJson
|
|
resultJson, err := gjson.LoadContent(result.Json())
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
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"))
|
|
|
|
result = nil
|
|
err = result.Structs(&users)
|
|
t.AssertNE(err, nil)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
result, err := db.Table(table).Fields("*").Where("id =? ", 1).One()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
type User struct {
|
|
Id int
|
|
Passport string
|
|
Password string
|
|
NickName string
|
|
CreateTime string
|
|
}
|
|
|
|
users := User{}
|
|
|
|
err = result.Struct(&users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
result = nil
|
|
err = result.Struct(&users)
|
|
t.AssertNE(err, nil)
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
record, err := db.Table(table).Fields("*").Where("id = ?", 1).One()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
type User struct {
|
|
Id int
|
|
Passport string
|
|
Password string
|
|
NickName string
|
|
CreateTime string
|
|
}
|
|
|
|
user := User{}
|
|
err = record.Struct(&user)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
result, err := gxml.Decode([]byte(record.Xml("doc")))
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultXml := result["doc"].(map[string]interface{})
|
|
if v, ok := resultXml["id"]; ok {
|
|
t.Assert(user.Id, v)
|
|
} else {
|
|
gtest.Fatal("FAIL")
|
|
}
|
|
|
|
if v, ok := resultXml["passport"]; ok {
|
|
t.Assert(user.Passport, v)
|
|
} else {
|
|
gtest.Fatal("FAIL")
|
|
}
|
|
|
|
if v, ok := resultXml["password"]; ok {
|
|
t.Assert(user.Password, v)
|
|
} else {
|
|
gtest.Fatal("FAIL")
|
|
}
|
|
|
|
if v, ok := resultXml["nickname"]; ok {
|
|
t.Assert(user.NickName, v)
|
|
} else {
|
|
gtest.Fatal("FAIL")
|
|
}
|
|
|
|
if v, ok := resultXml["create_time"]; ok {
|
|
t.Assert(user.CreateTime, v)
|
|
} 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)
|
|
gtest.Assert(err, nil)
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := "1"
|
|
result, err := db.Table(table).Fields("*").Where("id = ?", 1).Select()
|
|
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)
|
|
err = result.Structs(&t_users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultStringMap := result.MapKeyStr("id")
|
|
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"])
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := 1
|
|
result, err := db.Table(table).Fields("*").Where("id = ?", id).Select()
|
|
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)
|
|
err = result.Structs(&t_users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultIntMap := result.MapKeyInt("id")
|
|
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"])
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := 1
|
|
result, err := db.Table(table).Fields("*").Where("id = ?", id).Select()
|
|
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)
|
|
err = result.Structs(&t_users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultUintMap := result.MapKeyUint("id")
|
|
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"])
|
|
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := 1
|
|
ids := "1"
|
|
result, err := db.Table(table).Fields("*").Where("id = ?", id).Select()
|
|
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)
|
|
err = result.Structs(&t_users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultStringRecord := result.RecordKeyStr("id")
|
|
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())
|
|
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := 1
|
|
result, err := db.Table(table).Fields("*").Where("id = ?", id).Select()
|
|
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)
|
|
err = result.Structs(&t_users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultIntRecord := result.RecordKeyInt("id")
|
|
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())
|
|
|
|
})
|
|
}
|
|
|
|
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)
|
|
gtest.Assert(err, nil)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := 1
|
|
result, err := db.Table(table).Fields("*").Where("id = ?", id).Select()
|
|
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)
|
|
err = result.Structs(&t_users)
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
resultUintRecord := result.RecordKeyUint("id")
|
|
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())
|
|
})
|
|
}
|
|
|
|
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",
|
|
}
|
|
res, err := db.Table(name).Data(data).Insert()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
} else {
|
|
gtest.Assert(n, 1)
|
|
}
|
|
|
|
result, err := db.Table(name).Fields("*").Where("field_int = ?", 2).Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
gtest.Assert(result[0], data)
|
|
}
|
|
|
|
func Test_DB_Prefix(t *testing.T) {
|
|
db := dbPrefix
|
|
name := fmt.Sprintf(`%s_%d`, TABLE, gtime.TimestampNano())
|
|
table := PREFIX1 + name
|
|
createTableWithDb(db, table)
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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(),
|
|
})
|
|
t.Assert(err, nil)
|
|
|
|
n, e := result.RowsAffected()
|
|
t.Assert(e, nil)
|
|
t.Assert(n, 1)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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(),
|
|
})
|
|
t.Assert(err, nil)
|
|
|
|
n, e := result.RowsAffected()
|
|
t.Assert(e, nil)
|
|
t.Assert(n, 2)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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(),
|
|
})
|
|
t.Assert(err, nil)
|
|
|
|
n, e := result.RowsAffected()
|
|
t.Assert(e, nil)
|
|
t.Assert(n, 2)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
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)
|
|
t.Assert(err, nil)
|
|
|
|
n, e := result.RowsAffected()
|
|
t.Assert(e, nil)
|
|
t.Assert(n, 1)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
id := 10000
|
|
result, err := db.Delete(name, "id=?", id)
|
|
t.Assert(err, nil)
|
|
|
|
n, e := result.RowsAffected()
|
|
t.Assert(e, nil)
|
|
t.Assert(n, 1)
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
array := garray.New(true)
|
|
for i := 1; i <= SIZE; i++ {
|
|
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(),
|
|
})
|
|
}
|
|
|
|
result, err := db.BatchInsert(name, array.Slice())
|
|
t.Assert(err, nil)
|
|
|
|
n, e := result.RowsAffected()
|
|
t.Assert(e, nil)
|
|
t.Assert(n, SIZE)
|
|
})
|
|
|
|
}
|
|
|
|
func Test_Model_InnerJoin(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table1 := createInitTable("user1")
|
|
table2 := createInitTable("user2")
|
|
|
|
defer dropTable(table1)
|
|
defer dropTable(table2)
|
|
|
|
res, err := db.Table(table1).Where("id > ?", 5).Delete()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
t.Assert(n, 5)
|
|
|
|
result, err := db.Table(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").OrderBy("u1.id").Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
t.Assert(len(result), 5)
|
|
|
|
result, err = db.Table(table1+" u1").InnerJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > ?", 1).OrderBy("u1.id").Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
t.Assert(len(result), 4)
|
|
})
|
|
}
|
|
|
|
func Test_Model_LeftJoin(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table1 := createInitTable("user1")
|
|
table2 := createInitTable("user2")
|
|
|
|
defer dropTable(table1)
|
|
defer dropTable(table2)
|
|
|
|
res, err := db.Table(table2).Where("id > ?", 3).Delete()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
} else {
|
|
t.Assert(n, 7)
|
|
}
|
|
|
|
result, err := db.Table(table1+" u1").LeftJoin(table2+" u2", "u1.id = u2.id").Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
t.Assert(len(result), 10)
|
|
|
|
result, err = db.Table(table1+" u1").LeftJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > ? ", 2).Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
t.Assert(len(result), 8)
|
|
})
|
|
}
|
|
|
|
func Test_Model_RightJoin(t *testing.T) {
|
|
gtest.C(t, func(t *gtest.T) {
|
|
table1 := createInitTable("user1")
|
|
table2 := createInitTable("user2")
|
|
|
|
defer dropTable(table1)
|
|
defer dropTable(table2)
|
|
|
|
res, err := db.Table(table1).Where("id > ?", 3).Delete()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
|
|
t.Assert(n, 7)
|
|
|
|
result, err := db.Table(table1+" u1").RightJoin(table2+" u2", "u1.id = u2.id").Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
t.Assert(len(result), 10)
|
|
|
|
result, err = db.Table(table1+" u1").RightJoin(table2+" u2", "u1.id = u2.id").Where("u1.id > 2").Select()
|
|
if err != nil {
|
|
gtest.Fatal(err)
|
|
}
|
|
t.Assert(len(result), 1)
|
|
})
|
|
}
|