1 Star 0 Fork 15

落舞者/gosql

forked from fifsky/gosql 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
builder_test.go 3.74 KB
一键复制 编辑 原始数据 按行查看 历史
fifsky 提交于 2018-07-12 16:40 . split sql builder
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)
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mlboy/gosql.git
git@gitee.com:mlboy/gosql.git
mlboy
gosql
gosql
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385