Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.

Commit

Permalink
add tests for Find ints
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 3, 2017
1 parent 26d7a18 commit 3443507
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 155 deletions.
155 changes: 2 additions & 153 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,151 +82,6 @@ type Article struct {
Status int8 `xorm:"TINYINT(4)"`
}

func in(engine *xorm.Engine, t *testing.T) {
var usrs []Userinfo
err := engine.Limit(3).Find(&usrs)
if err != nil {
t.Error(err)
panic(err)
}

if len(usrs) != 3 {
err = errors.New("there are not 3 records")
t.Error(err)
panic(err)
}

var ids []int64
var idsStr string
for _, u := range usrs {
ids = append(ids, u.Uid)
idsStr = fmt.Sprintf("%d,", u.Uid)
}
idsStr = idsStr[:len(idsStr)-1]

users := make([]Userinfo, 0)
err = engine.In("(id)", ids[0], ids[1], ids[2]).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)
if len(users) != 3 {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}

users = make([]Userinfo, 0)
err = engine.In("(id)", ids).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)
if len(users) != 3 {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}

for _, user := range users {
if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}
}

users = make([]Userinfo, 0)
var idsInterface []interface{}
for _, id := range ids {
idsInterface = append(idsInterface, id)
}

department := "`" + engine.ColumnMapper.Obj2Table("Departname") + "`"
err = engine.Where(department+" = ?", "dev").In("(id)", idsInterface...).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)

if len(users) != 3 {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}

for _, user := range users {
if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}
}

dev := engine.ColumnMapper.Obj2Table("Dev")

err = engine.In("(id)", 1).In("(id)", 2).In(department, dev).Find(&users)

if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)

cnt, err := engine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev-"})
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("update records not 1")
t.Error(err)
panic(err)
}

user := new(Userinfo)
has, err := engine.Id(ids[0]).Get(user)
if err != nil {
t.Error(err)
panic(err)
}
if !has {
err = errors.New("get record not 1")
t.Error(err)
panic(err)
}
if user.Departname != "dev-" {
err = errors.New("update not success")
t.Error(err)
panic(err)
}

cnt, err = engine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev"})
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("update records not 1")
t.Error(err)
panic(err)
}

cnt, err = engine.In("(id)", ids[1]).Delete(&Userinfo{})
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("deleted records not 1")
t.Error(err)
panic(err)
}
}

type Limit struct {
Id int64
Name string
Expand Down Expand Up @@ -625,14 +480,8 @@ func BaseTestAll(engine *xorm.Engine, t *testing.T) {
get(engine, t)
fmt.Println("-------------- testCascade --------------")
testCascade(engine, t)
fmt.Println("-------------- find --------------")
find(engine, t)
fmt.Println("-------------- find2 --------------")
find2(engine, t)
fmt.Println("-------------- findMap --------------")
findMap(engine, t)
fmt.Println("-------------- findMap2 --------------")
findMap2(engine, t)
fmt.Println("-------------- testFind --------------")
testFind(engine, t)
fmt.Println("-------------- count --------------")
count(engine, t)
fmt.Println("-------------- where --------------")
Expand Down
33 changes: 31 additions & 2 deletions testFind.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import (
"github.com/go-xorm/xorm"
)

func testFind(engine *xorm.Engine, t *testing.T) {
fmt.Println("-------------- find --------------")
find(engine, t)
fmt.Println("-------------- find2 --------------")
find2(engine, t)
fmt.Println("-------------- findMap --------------")
findMap(engine, t)
fmt.Println("-------------- findMap2 --------------")
findMap2(engine, t)
fmt.Println("-------------- findInts --------------")
testFindInts(engine, t)
}

func where(engine *xorm.Engine, t *testing.T) {
users := make([]Userinfo, 0)
err := engine.Where("(id) > ?", 2).Find(&users)
Expand Down Expand Up @@ -204,8 +217,24 @@ func havingSameMapper(engine *xorm.Engine, t *testing.T) {
users := make([]Userinfo, 0)
err := engine.GroupBy("`Username`").Having("`Username`='xlw'").Find(&users)
if err != nil {
t.Error(err)
panic(err)
t.Fatal(err)
}
fmt.Println(users)
}

func testFindInts(engine *xorm.Engine, t *testing.T) {
userinfo := engine.TableMapper.Obj2Table("Userinfo")
var idsInt64 []int64
err := engine.Table(userinfo).Cols("id").Desc("id").Find(&idsInt64)
if err != nil {
t.Fatal(err)
}
fmt.Println(idsInt64)

var idsInt32 []int32
err = engine.Table(userinfo).Cols("id").Desc("id").Find(&idsInt32)
if err != nil {
t.Fatal(err)
}
fmt.Println(idsInt32)
}
154 changes: 154 additions & 0 deletions testIn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package tests

import (
"errors"
"fmt"
"testing"

"github.com/go-xorm/xorm"
)

func in(engine *xorm.Engine, t *testing.T) {
var usrs []Userinfo
err := engine.Limit(3).Find(&usrs)
if err != nil {
t.Error(err)
panic(err)
}

if len(usrs) != 3 {
err = errors.New("there are not 3 records")
t.Error(err)
panic(err)
}

var ids []int64
var idsStr string
for _, u := range usrs {
ids = append(ids, u.Uid)
idsStr = fmt.Sprintf("%d,", u.Uid)
}
idsStr = idsStr[:len(idsStr)-1]

users := make([]Userinfo, 0)
err = engine.In("(id)", ids[0], ids[1], ids[2]).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)
if len(users) != 3 {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}

users = make([]Userinfo, 0)
err = engine.In("(id)", ids).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)
if len(users) != 3 {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}

for _, user := range users {
if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}
}

users = make([]Userinfo, 0)
var idsInterface []interface{}
for _, id := range ids {
idsInterface = append(idsInterface, id)
}

department := "`" + engine.ColumnMapper.Obj2Table("Departname") + "`"
err = engine.Where(department+" = ?", "dev").In("(id)", idsInterface...).Find(&users)
if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)

if len(users) != 3 {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}

for _, user := range users {
if user.Uid != ids[0] && user.Uid != ids[1] && user.Uid != ids[2] {
err = errors.New("in uses should be " + idsStr + " total 3")
t.Error(err)
panic(err)
}
}

dev := engine.ColumnMapper.Obj2Table("Dev")

err = engine.In("(id)", 1).In("(id)", 2).In(department, dev).Find(&users)

if err != nil {
t.Error(err)
panic(err)
}
fmt.Println(users)

cnt, err := engine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev-"})
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("update records not 1")
t.Error(err)
panic(err)
}

user := new(Userinfo)
has, err := engine.Id(ids[0]).Get(user)
if err != nil {
t.Error(err)
panic(err)
}
if !has {
err = errors.New("get record not 1")
t.Error(err)
panic(err)
}
if user.Departname != "dev-" {
err = errors.New("update not success")
t.Error(err)
panic(err)
}

cnt, err = engine.In("(id)", ids[0]).Update(&Userinfo{Departname: "dev"})
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("update records not 1")
t.Error(err)
panic(err)
}

cnt, err = engine.In("(id)", ids[1]).Delete(&Userinfo{})
if err != nil {
t.Error(err)
panic(err)
}
if cnt != 1 {
err = errors.New("deleted records not 1")
t.Error(err)
panic(err)
}
}

0 comments on commit 3443507

Please sign in to comment.