Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed May 27, 2017
1 parent 45f23ff commit 5d75270
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 9 deletions.
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ test:
override:
# './...' is a relative pattern which means all subdirectories
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
- go test -v -race -db=mysql -conn_str="root:@/xorm_test"
- go test -v -race -db=postgres -conn_str="dbname=xorm_test sslmode=disable"
- go test -v -race -db=mysql -conn_str="root:@/xorm_test" -coverprofile=coverage.txt -covermode=atomic
- go test -v -race -db=postgres -conn_str="dbname=xorm_test sslmode=disable" -coverprofile=coverage.txt -covermode=atomic
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./sqlite3.sh
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./mysql.sh
- cd /home/ubuntu/.go_workspace/src/github.com/go-xorm/tests && ./postgres.sh
Expand Down
41 changes: 41 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRows(t *testing.T) {
assert.NoError(t, prepareEngine())

type UserRows struct {
Id int64
IsMan bool
}

assert.NoError(t, testEngine.Sync2(new(UserRows)))

cnt, err := testEngine.Insert(&UserRows{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

rows, err := testEngine.Rows(new(UserRows))
assert.NoError(t, err)
defer rows.Close()

cnt = 0
user := new(UserRows)
for rows.Next() {
err = rows.Scan(user)
assert.NoError(t, err)
cnt++
}
assert.EqualValues(t, 1, cnt)
}
20 changes: 18 additions & 2 deletions session_cols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@

package xorm

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSetExpr(t *testing.T) {
assert.NoError(t, prepareEngine())

type User struct {
Id int64
Show bool
}

testEngine.SetExpr("show", "NOT show").Id(1).Update(new(User))
assert.NoError(t, testEngine.Sync2(new(User)))

cnt, err := testEngine.Insert(&User{
Show: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt, err = testEngine.SetExpr("show", "NOT show").Id(1).Update(new(User))
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}
38 changes: 38 additions & 0 deletions session_iterate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIterate(t *testing.T) {
assert.NoError(t, prepareEngine())

type UserIterate struct {
Id int64
IsMan bool
}

assert.NoError(t, testEngine.Sync2(new(UserIterate)))

cnt, err := testEngine.Insert(&UserIterate{
IsMan: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)

cnt = 0
err = testEngine.Iterate(new(UserIterate), func(i int, bean interface{}) error {
user := bean.(*UserIterate)
assert.EqualValues(t, 1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.EqualValues(t, 1, cnt)
}
29 changes: 24 additions & 5 deletions xorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"testing"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -14,6 +15,7 @@ import (

var (
testEngine *Engine
dbType string
connString string

db = flag.String("db", "sqlite3", "the tested database")
Expand Down Expand Up @@ -47,12 +49,13 @@ func createEngine(dbType, connStr string) error {
}

func prepareEngine() error {
return createEngine(*db, connString)
return createEngine(dbType, connString)
}

func TestMain(m *testing.M) {
flag.Parse()

dbType = *db
if *db == "sqlite3" {
if ptrConnStr == nil {
connString = "./test.db"
Expand All @@ -67,11 +70,27 @@ func TestMain(m *testing.M) {
connString = *ptrConnStr
}

if err := prepareEngine(); err != nil {
fmt.Println(err)
return
dbs := strings.Split(*db, ";")
conns := strings.Split(connString, ";")

var res int
for i := 0; i < len(dbs); i++ {
dbType = dbs[i]
connString = conns[i]
testEngine = nil

if err := prepareEngine(); err != nil {
fmt.Println(err)
return
}

code := m.Run()
if code > 0 {
res = code
}
}
os.Exit(m.Run())

os.Exit(res)
}

func TestPing(t *testing.T) {
Expand Down

0 comments on commit 5d75270

Please sign in to comment.