1 Star 0 Fork 1

winie/sq

forked from unsafe-rust/sq 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
6_db_test.go 9.61 KB
一键复制 编辑 原始数据 按行查看 历史
unsafe-rust 提交于 2021-03-28 17:36 . update
package sq
import (
"context"
"encoding/json"
"errors"
"fmt"
"gitee.com/gopher2011/sq/internal/models"
_ "github.com/go-sql-driver/mysql"
"strconv"
"testing"
"time"
)
func TestShowSql(t *testing.T) {
logger.logging = false
RunWithSchema(t, func(t *testing.T) {
insert(1)
ShowSql().QueryX("select * from users")
user := &models.Users{}
Model(user).ShowSQL().Where("id = ?", 1).Take()
Table("users").ShowSQL().Where("id = ?", 1).Update(map[string]interface{}{
"name": "test2",
})
})
}
func TestExec(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
result, err := Exec("insert into users(name,status,created_at,updated_at) value(?,?,?,?)", "test", 1, time.Now(), time.Now())
if err != nil {
t.Error(err)
}
id, err := result.LastInsertId()
if err != nil {
t.Error(err)
}
if id != 1 {
t.Error("lastInsertId error")
}
Exec("update users set status = status + 1 where id = ?", 1)
result, err = Exec("update users set status = status + 1 where id = ?", 1)
if err != nil {
t.Error("update user error", err)
}
if aff, _ := result.RowsAffected(); aff == 0 {
t.Error("update set error")
}
})
}
func TestQueryX(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
rows, err := QueryX("select * from users")
if err != nil {
t.Error(err)
}
defer rows.Close()
for rows.Next() {
user := &models.Users{}
_,err = rows.ScanX(user)
if err != nil {
t.Error(err)
}
}
rows, err = QueryX("select name from users")
if err != nil {
t.Error(err)
}
defer rows.Close()
for rows.Next() {
// results := make(map[string]interface{})
// err = rows.MapScan(results)
var name string
err = rows.Scan(&name)
if err != nil {
t.Error(err)
}
fmt.Println(name)
}
})
}
func TestQueryRowX(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
user := &models.Users{}
_,err := QueryRowX("select * from users where id = 1").Scan(user)
fmt.Println(user)
if err != nil {
t.Error(err)
}
if user.Id != 1 {
t.Error("wraper QueryRowx error")
}
})
}
func TestUse(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
db := Use("db2")
_, err := db.Exec("insert into photos(moment_id,url,created_at,updated_at) value(?,?,?,?)", 1, "http://test.com", time.Now(), time.Now())
if err != nil {
t.Error(err)
}
})
}
func TestUseTable(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
post := &models.Photos{
MomentId: 1,
Url: "http://test.com/1",
}
_, err := Use("db2").Model(post).Insert()
if err != nil {
t.Error(err)
}
post.Url = "http://test.com/2"
_, err1 := Use("db2").Ctx(context.Background()).Model(post).Update()
if err1 != nil {
t.Error(err1)
}
_, err1 = Use("db2").Table("photos").Where("id = ?", 1).Update(map[string]interface{}{
"url": "http://test2.com",
})
if err1 != nil {
t.Error(err1)
}
})
}
func TestTake(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
db := Use("default")
{
user := &models.Users{}
err := db.Take(user, "select * from users where id = ?", 1)
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
}
})
}
func TestTakeSingle(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
db := Use("default")
{
var name string
err := db.Take(&name, "select name from users where id = ?", 1)
if err != nil {
t.Error(err)
}
fmt.Println(name)
}
})
}
func TestSelectSlice(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
var users []string
err := Select(&users, "select name from users")
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(users))
})
}
func TestSelect(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
db := Use("default")
user := make([]*models.Users, 0)
err := db.Select(&user, "select * from users")
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func TestQueryXIn(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(4)
insert(5)
insert(6)
rows, err := QueryX("select * from users where status = ? and id in (?)", 1, []int{1, 2, 3})
if err != nil {
t.Error(err)
}
defer rows.Close()
user := &models.Users{}
us := make([]models.Users,0)
for rows.Next() {
_,err = rows.ScanX(user)
if err != nil {
t.Error(err)
}
us = append(us,*user)
}
fmt.Println(us)
})
}
func TestSelectIn(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
insert(1)
insert(2)
insert(4)
insert(5)
insert(6)
db := Use("default")
user := make([]*models.Users, 0)
err := db.Select(&user, "select * from users where id in(?)", []int{1, 2, 3})
if err != nil {
t.Error(err)
}
fmt.Println(jsonEncode(user))
})
}
func TestTx(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
// 1
{
Tx(func(tx *DB) error {
for id := 1; id < 10; id++ {
user := &models.Users{
Id: id,
Name: "test" + strconv.Itoa(id),
}
tx.Model(user).Insert()
if id == 8 {
return errors.New("simulation terminated")
}
}
return nil
})
num, err := Model(&models.Users{}).Count()
if err != nil {
t.Error(err)
}
if num != 0 {
t.Error("transaction abort failed")
}
}
// 2
{
Tx(func(tx *DB) error {
for id := 1; id < 10; id++ {
user := &models.Users{
Id: id,
Name: "test" + strconv.Itoa(id),
}
tx.Model(user).Insert()
}
return nil
})
num, err := Model(&models.Users{}).Count()
if err != nil {
t.Error(err)
}
if num != 9 {
t.Error("transaction create failed")
}
}
})
}
func TestWithTx(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
{
err := Tx(func(tx *DB) error {
for id := 1; id < 10; id++ {
_, err := tx.Exec("INSERT INTO users(id,name,status,success_time,created_at,updated_at) VALUES(?,?,?,?,?,?)", id, "test"+strconv.Itoa(id), 1, time.Now(), time.Now(),time.Now())
if err != nil {
return err
}
}
var num int
// 这句单独拿出来执行并不会错,但放在这里就会出错。
_,err := tx.QueryRowX("select count(*) from users").Scan(&num)
// 这句执行不会出错。
u := &models.Users{}
tx.Take(u,"select * from users where id = ?",1)
fmt.Println(u)
// 这句执行不会出错。
tx.QueryRowX("select * from users where id = ?",1).Scan(u)
fmt.Println(u)
if err != nil {
return err
}
fmt.Println(num)
if num != 9 {
t.Error("with transaction create failed")
}
return nil
})
if err != nil {
t.Fatalf("with transaction failed %s", err)
}
}
})
}
func TestTxx(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
err := TxCtx(ctx, func(ctx context.Context, tx *DB) error {
for id := 1; id < 10; id++ {
user := &models.Users{
Id: id,
Name: "test" + strconv.Itoa(id),
}
tx.Model(user).Insert()
if id == 8 {
cancel()
break
}
}
return nil
})
if err == nil {
t.Fatalf("with transaction must be cancel error")
}
num, err := Model(&models.Users{}).Count()
if err != nil {
t.Error(err)
}
if num != 0 {
t.Error("transaction abort failed")
}
})
}
func TestWrapper_Relation(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
initDatas(t)
moment := &MomentList{}
err := Relation("User", func(b *Builder) {
b.Where("status = 0")
}).Take(moment, "select * from moments")
b, _ := json.MarshalIndent(moment, "", " ")
fmt.Println(string(b), err)
if err != nil {
t.Fatal(err)
}
})
}
func TestWrapper_Relation2(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
initDatas(t)
var moments = make([]*MomentList, 0)
err := Relation("User", func(b *Builder) {
b.Where("status = 1")
}).Select(&moments, "select * from moments")
if err != nil {
t.Fatal(err)
}
})
}
func TestDB_Begin(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
tx, err := Begin()
if err != nil {
t.Fatalf("with transaction begin error %s", err)
}
var fn = func() error {
for id := 1; id < 10; id++ {
_, err1 := tx.Exec("INSERT INTO users(id,name,status,created_at,updated_at) VALUES(?,?,?,?,?)", id, "test"+strconv.Itoa(id), 1, time.Now(), time.Now())
if err1 != nil {
return err
}
}
var num int
// 这个方法单独放这里会报错。
_,err = tx.QueryRowX("select count(*) from users").Scan(&num)
// 用这个代替,执行不会报错。
err = tx.Take(&num,"select count(*) from users")
fmt.Println(num)
if err != nil {
return err
}
if num != 9 {
return errors.New("with transaction create failed")
}
return nil
}
err = fn()
if err != nil {
err := tx.Rollback()
if err != nil {
t.Fatalf("with transaction rollback error %s", err)
}
}
err = tx.Commit()
if err != nil {
t.Fatalf("with transaction commit error %s", err)
}
})
}
func TestRelation(t *testing.T) {
RunWithSchema(t, func(t *testing.T) {
initDatas(t)
moment := &MomentList{}
err := Relation("User", func(b *Builder) {
// this is builder instance
b.Where("status = 1")
}).Take(moment, "select * from moments where id = 1")
if err != nil {
t.Fatalf("relation query error %s", err)
}
})
}
//func TestDB_ExecN(t *testing.T) {
// u := &models.Users{1,"张三",1,"",time.Now(),time.Now()}
// _, err := GetDB().ExecN("insert into users(id,name,status,success_time,created_at,updated_at)values(:id :name :status :success_time :created_at :updated_at)", u)
// if err != nil {
// fmt.Println(err)
// }
//
//
//
//
//}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/winie_admin/sq.git
git@gitee.com:winie_admin/sq.git
winie_admin
sq
sq
master

搜索帮助