improved tests
This commit is contained in:
parent
bd195b23f7
commit
4edb3c96db
|
@ -0,0 +1,41 @@
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var me Engine
|
||||||
|
|
||||||
|
func TestMysql(t *testing.T) {
|
||||||
|
// You should drop all tables before executing this testing
|
||||||
|
me = Create("mysql", "root:@/xorm_test?charset=utf8")
|
||||||
|
me.ShowSQL = true
|
||||||
|
|
||||||
|
directCreateTable(&me, t)
|
||||||
|
mapper(&me, t)
|
||||||
|
insert(&me, t)
|
||||||
|
query(&me, t)
|
||||||
|
exec(&me, t)
|
||||||
|
insertAutoIncr(&me, t)
|
||||||
|
insertMulti(&me, t)
|
||||||
|
insertTwoTable(&me, t)
|
||||||
|
update(&me, t)
|
||||||
|
testdelete(&me, t)
|
||||||
|
get(&me, t)
|
||||||
|
cascadeGet(&me, t)
|
||||||
|
find(&me, t)
|
||||||
|
findMap(&me, t)
|
||||||
|
count(&me, t)
|
||||||
|
where(&me, t)
|
||||||
|
in(&me, t)
|
||||||
|
limit(&me, t)
|
||||||
|
order(&me, t)
|
||||||
|
join(&me, t)
|
||||||
|
having(&me, t)
|
||||||
|
transaction(&me, t)
|
||||||
|
combineTransaction(&me, t)
|
||||||
|
table(&me, t)
|
||||||
|
createMultiTables(&me, t)
|
||||||
|
tableOp(&me, t)
|
||||||
|
}
|
|
@ -26,7 +26,11 @@ func (session *Session) Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) Close() {
|
func (session *Session) Close() {
|
||||||
defer session.Db.Close()
|
defer func() {
|
||||||
|
if session.Db != nil {
|
||||||
|
session.Db.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) Where(querystring string, args ...interface{}) *Session {
|
func (session *Session) Where(querystring string, args ...interface{}) *Session {
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var se Engine
|
||||||
|
|
||||||
|
func TestSqlite(t *testing.T) {
|
||||||
|
os.Remove("./test.db")
|
||||||
|
se = Create("sqlite3", "./test.db")
|
||||||
|
se.ShowSQL = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteCreateTable(t *testing.T) {
|
||||||
|
directCreateTable(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteMapper(t *testing.T) {
|
||||||
|
mapper(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteInsert(t *testing.T) {
|
||||||
|
insert(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteQuery(t *testing.T) {
|
||||||
|
query(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteExec(t *testing.T) {
|
||||||
|
exec(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteInsertAutoIncr(t *testing.T) {
|
||||||
|
insertAutoIncr(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
type sss struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s sss) TestInsertMulti(t *testing.T) {
|
||||||
|
insertMulti(&se, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSqliteInsertMulti(t *testing.T) {
|
||||||
|
insertMulti(&se, t)
|
||||||
|
|
||||||
|
insertTwoTable(&se, t)
|
||||||
|
update(&se, t)
|
||||||
|
testdelete(&se, t)
|
||||||
|
get(&se, t)
|
||||||
|
cascadeGet(&se, t)
|
||||||
|
find(&se, t)
|
||||||
|
findMap(&se, t)
|
||||||
|
count(&se, t)
|
||||||
|
where(&se, t)
|
||||||
|
in(&se, t)
|
||||||
|
limit(&se, t)
|
||||||
|
order(&se, t)
|
||||||
|
join(&se, t)
|
||||||
|
having(&se, t)
|
||||||
|
transaction(&se, t)
|
||||||
|
combineTransaction(&se, t)
|
||||||
|
table(&se, t)
|
||||||
|
createMultiTables(&se, t)
|
||||||
|
tableOp(&se, t)
|
||||||
|
}
|
|
@ -0,0 +1,437 @@
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
CREATE TABLE `userinfo` (
|
||||||
|
`uid` INT(10) NULL AUTO_INCREMENT,
|
||||||
|
`username` VARCHAR(64) NULL,
|
||||||
|
`departname` VARCHAR(64) NULL,
|
||||||
|
`created` DATE NULL,
|
||||||
|
PRIMARY KEY (`uid`)
|
||||||
|
);
|
||||||
|
CREATE TABLE `userdeatail` (
|
||||||
|
`uid` INT(10) NULL,
|
||||||
|
`intro` TEXT NULL,
|
||||||
|
`profile` TEXT NULL,
|
||||||
|
PRIMARY KEY (`uid`)
|
||||||
|
);
|
||||||
|
*/
|
||||||
|
|
||||||
|
type Userinfo struct {
|
||||||
|
Uid int64 `xorm:"id pk not null autoincr"`
|
||||||
|
Username string
|
||||||
|
Departname string
|
||||||
|
Alias string `xorm:"-"`
|
||||||
|
Created time.Time
|
||||||
|
Detail Userdetail `xorm:"detail_id int(11)"`
|
||||||
|
Height float64
|
||||||
|
Avatar []byte
|
||||||
|
IsMan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Userdetail struct {
|
||||||
|
Id int64
|
||||||
|
Intro string `xorm:"text"`
|
||||||
|
Profile string `xorm:"varchar(2000)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func directCreateTable(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.CreateTables(&Userinfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapper(engine *Engine, t *testing.T) {
|
||||||
|
err := engine.UnMap(&Userinfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.Map(&Userinfo{}, &Userdetail{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.DropAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.CreateAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func insert(engine *Engine, t *testing.T) {
|
||||||
|
user := Userinfo{1, "xiaolunwen", "dev", "lunny", time.Now(),
|
||||||
|
Userdetail{Id: 1}, 1.78, []byte{1, 2, 3}, true}
|
||||||
|
_, err := engine.Insert(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func query(engine *Engine, t *testing.T) {
|
||||||
|
sql := "select * from userinfo"
|
||||||
|
results, err := engine.Query(sql)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(results)
|
||||||
|
}
|
||||||
|
|
||||||
|
func exec(engine *Engine, t *testing.T) {
|
||||||
|
sql := "update userinfo set username=? where id=?"
|
||||||
|
res, err := engine.Exec(sql, "xiaolun", 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertAutoIncr(engine *Engine, t *testing.T) {
|
||||||
|
// auto increment insert
|
||||||
|
user := Userinfo{Username: "xiaolunwen", Departname: "dev", Alias: "lunny", Created: time.Now(),
|
||||||
|
Detail: Userdetail{Id: 1}, Height: 1.78, Avatar: []byte{1, 2, 3}, IsMan: true}
|
||||||
|
_, err := engine.Insert(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertMulti(engine *Engine, t *testing.T) {
|
||||||
|
engine.InsertMany = true
|
||||||
|
users := []Userinfo{
|
||||||
|
{Username: "xlw", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||||
|
{Username: "xlw2", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||||
|
{Username: "xlw11", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||||
|
{Username: "xlw22", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||||
|
}
|
||||||
|
_, err := engine.Insert(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.InsertMany = false
|
||||||
|
|
||||||
|
users = []Userinfo{
|
||||||
|
{Username: "xlw9", Departname: "dev", Alias: "lunny9", Created: time.Now()},
|
||||||
|
{Username: "xlw10", Departname: "dev", Alias: "lunny10", Created: time.Now()},
|
||||||
|
{Username: "xlw99", Departname: "dev", Alias: "lunny2", Created: time.Now()},
|
||||||
|
{Username: "xlw1010", Departname: "dev", Alias: "lunny3", Created: time.Now()},
|
||||||
|
}
|
||||||
|
_, err = engine.Insert(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.InsertMany = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertTwoTable(engine *Engine, t *testing.T) {
|
||||||
|
userdetail := Userdetail{Id: 1, Intro: "I'm a very beautiful women.", Profile: "sfsaf"}
|
||||||
|
userinfo := Userinfo{Username: "xlw3", Departname: "dev", Alias: "lunny4", Created: time.Now(), Detail: userdetail}
|
||||||
|
|
||||||
|
_, err := engine.Insert(&userinfo, &userdetail)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func update(engine *Engine, t *testing.T) {
|
||||||
|
// update by id
|
||||||
|
user := Userinfo{Username: "xxx"}
|
||||||
|
_, err := engine.Id(1).Update(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = engine.Update(&Userinfo{Username: "yyy"}, &user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testdelete(engine *Engine, t *testing.T) {
|
||||||
|
user := Userinfo{Uid: 1}
|
||||||
|
_, err := engine.Delete(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func get(engine *Engine, t *testing.T) {
|
||||||
|
user := Userinfo{Uid: 2}
|
||||||
|
|
||||||
|
err := engine.Get(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cascadeGet(engine *Engine, t *testing.T) {
|
||||||
|
user := Userinfo{Uid: 11}
|
||||||
|
|
||||||
|
err := engine.Get(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func find(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
|
||||||
|
err := engine.Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func findMap(engine *Engine, t *testing.T) {
|
||||||
|
users := make(map[int64]Userinfo)
|
||||||
|
|
||||||
|
err := engine.Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func count(engine *Engine, t *testing.T) {
|
||||||
|
user := Userinfo{Departname: "dev"}
|
||||||
|
total, err := engine.Count(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Total %d records!!!", total)
|
||||||
|
}
|
||||||
|
|
||||||
|
func where(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.Where("id > ?", 2).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func in(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.In("id", 1, 2, 3).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
|
||||||
|
ids := []interface{}{1, 2, 3}
|
||||||
|
err = engine.Where("id > ?", 2).In("id", ids...).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func limit(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.Limit(2, 1).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func order(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.OrderBy("id desc").Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func join(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.Join("LEFT", "userdetail", "userinfo.id=userdetail.id").Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func having(engine *Engine, t *testing.T) {
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err := engine.GroupBy("username").Having("username='xlw'").Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Println(users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func transaction(engine *Engine, t *testing.T) {
|
||||||
|
counter := func() {
|
||||||
|
total, err := engine.Count(&Userinfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("----now total %v records\n", total)
|
||||||
|
}
|
||||||
|
|
||||||
|
counter()
|
||||||
|
defer counter()
|
||||||
|
session, err := engine.MakeSession()
|
||||||
|
defer session.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.Begin()
|
||||||
|
//session.IsAutoRollback = false
|
||||||
|
user1 := Userinfo{Username: "xiaoxiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
||||||
|
_, err = session.Insert(&user1)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user2 := Userinfo{Username: "yyy"}
|
||||||
|
_, err = session.Where("uid = ?", 0).Update(&user2)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
fmt.Println(err)
|
||||||
|
//t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = session.Delete(&user2)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = session.Commit()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func combineTransaction(engine *Engine, t *testing.T) {
|
||||||
|
counter := func() {
|
||||||
|
total, err := engine.Count(&Userinfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("----now total %v records\n", total)
|
||||||
|
}
|
||||||
|
|
||||||
|
counter()
|
||||||
|
defer counter()
|
||||||
|
session, err := engine.MakeSession()
|
||||||
|
defer session.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.Begin()
|
||||||
|
//session.IsAutoRollback = false
|
||||||
|
user1 := Userinfo{Username: "xiaoxiao2", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
||||||
|
_, err = session.Insert(&user1)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user2 := Userinfo{Username: "zzz"}
|
||||||
|
_, err = session.Where("id = ?", 0).Update(&user2)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = session.Exec("delete from userinfo where username = ?", user2.Username)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = session.Commit()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func table(engine *Engine, t *testing.T) {
|
||||||
|
engine.Table("user_user").CreateTables(&Userinfo{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func createMultiTables(engine *Engine, t *testing.T) {
|
||||||
|
session, err := engine.MakeSession()
|
||||||
|
defer session.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &Userinfo{}
|
||||||
|
session.Begin()
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
err = session.Table(fmt.Sprintf("user_%v", i)).CreateTable(user)
|
||||||
|
if err != nil {
|
||||||
|
session.Rollback()
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = session.Commit()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableOp(engine *Engine, t *testing.T) {
|
||||||
|
user := Userinfo{Username: "tablexiao", Departname: "dev", Alias: "lunny", Created: time.Now()}
|
||||||
|
tableName := fmt.Sprintf("user_%v", len(user.Username))
|
||||||
|
id, err := engine.Table(tableName).Insert(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = engine.Table(tableName).Get(&Userinfo{Username: "tablexiao"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
users := make([]Userinfo, 0)
|
||||||
|
err = engine.Table(tableName).Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = engine.Table(tableName).Id(id).Update(&Userinfo{Username: "tableda"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = engine.Table(tableName).Id(id).Delete(&Userinfo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue