代码拉取完成,页面将自动刷新
同步操作将从 fifsky/gosql 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package gosql
import (
"encoding/json"
"fmt"
"strconv"
"testing"
"time"
)
var (
createSchema = `
CREATE TABLE users (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL DEFAULT '',
email varchar(100) NOT NULL DEFAULT '',
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
PRIMARY KEY (id)
)ENGINE=InnoDB CHARSET=utf8;
`
dropSchema = `
drop table users
`
)
type Users struct {
Id int `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
func (u *Users) DbName() string {
return "default"
}
func (u *Users) TableName() string {
return "users"
}
func (u *Users) PK() string {
return "id"
}
func RunWithSchema(t *testing.T, test func(t *testing.T)) {
db := DB()
defer func() {
_, err := db.Exec(dropSchema)
if err != nil {
t.Error(err)
}
}()
_, err := db.Exec(createSchema)
if err != nil {
t.Fatalf("create schema error:%s", err)
}
test(t)
}
func insert(id int) {
user := &Users{
Id: id,
Name: "test" + strconv.Itoa(id),
Email: "test" + strconv.Itoa(id) + "@test.com",
}
Model(user).Create()
}
func TestBuilder_Get(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
user := &Users{}
err := Model(user).Where("id = ?", 1).Get()
if err != nil {
t.Error(err)
}
//fmt.Println(user)
})
}
func jsonEncode(i interface{}) string {
ret, _ := json.Marshal(i)
return string(ret)
}
func TestBuilder_All(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
user := make([]*Users, 0)
err := Model(&user).All()
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func TestBuilder_Update(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
user := &Users{
Name: "test2",
}
_, err := Model(user).Where("id=1").Update()
if err != nil {
t.Error("update user error", err)
}
})
}
func TestBuilder_Delete(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
_, err := Model(&Users{}).Where("id=1").Delete()
if err != nil {
t.Error("delete user error", err)
}
})
}
func TestBuilder_Count(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
num, err := Model(&Users{}).Count()
if err != nil {
t.Error(err)
}
if num != 1 {
t.Error("count user error")
}
})
}
func TestBuilder_Create(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
user := &Users{
Id: 1,
Name: "test",
Email: "test@test.com",
}
id, err := Model(user).Create()
if err != nil {
t.Error(err)
}
if id != 1 {
t.Error("lastInsertId error", id)
}
})
}
func TestBuilder_Limit(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := &Users{}
err := Model(user).Limit(1).Get()
if err != nil {
t.Error(err)
}
})
}
func TestBuilder_Offset(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := &Users{}
err := Model(user).Limit(1).Offset(1).Get()
if err != nil {
t.Error(err)
}
})
}
func TestBuilder_OrderBy(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := &Users{}
err := Model(user).OrderBy("id desc").Limit(1).Offset(1).Get()
if err != nil {
t.Error(err)
}
if user.Id != 2 {
t.Error("order by error")
}
//fmt.Println(user)
})
}
func TestBuilder_Where(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(3)
user := make([]*Users, 0)
err := Model(&user).Where("id in(?,?)", 2, 3).OrderBy("id desc").All()
if err != nil {
t.Error(err)
}
if len(user) != 2 {
t.Error("where error")
}
//fmt.Println(user)
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。