Skip to content

Commit

Permalink
调整db测试例子
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Jun 12, 2019
1 parent 2eb9724 commit 279ff97
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
43 changes: 8 additions & 35 deletions tests/db/db_test.go → tests/db/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
package db

import (
"database/sql"
"fmt"
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/peer"
"github.com/davyxu/cellnet/peer/mysql"
_ "github.com/davyxu/cellnet/peer/redix"
"github.com/mediocregopher/radix.v2/redis"
"testing"
"time"
)

func TestMySQL(t *testing.T) {
// 从地址中选择mysql数据库,这里选择mysql系统库
p := peer.NewGenericPeer("mysql.Connector", "mysqldemo", "root:123456@(localhost:3306)/mysql", nil)
p.(cellnet.MySQLConnector).SetConnectionCount(3)

// 阻塞
p.Start()

op := p.(cellnet.MySQLOperator)

op.Operate(func(rawClient interface{}) interface{} {

client := rawClient.(*sql.DB)

// 查找默认用户
mysql.NewWrapper(client).Query("select User from user").Each(func(wrapper *mysql.Wrapper) bool {

var name string
wrapper.Scan(&name)
fmt.Println(name)
return true
})

return nil
})

}

func TestRedix(t *testing.T) {

func prepareOperator(t *testing.T) cellnet.RedisPoolOperator {
p := peer.NewGenericPeer("redix.Connector", "redis", "127.0.0.1:6379", nil)
p.(cellnet.RedisConnector).SetConnectionCount(1)
p.Start()
Expand All @@ -59,9 +27,12 @@ func TestRedix(t *testing.T) {
t.FailNow()
}

op := p.(cellnet.RedisPoolOperator)
return p.(cellnet.RedisPoolOperator)
}

func TestRedix(t *testing.T) {

op.Operate(func(rawClient interface{}) interface{} {
prepareOperator(t).Operate(func(rawClient interface{}) interface{} {

client := rawClient.(*redis.Client)
client.Cmd("SET", "mykey", "myvalue")
Expand All @@ -75,6 +46,8 @@ func TestRedix(t *testing.T) {
t.FailNow()
}

client.Cmd("DEL", "mykey")

return nil
})

Expand Down
38 changes: 38 additions & 0 deletions tests/db/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package db

import (
"database/sql"
"fmt"
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/peer"
"github.com/davyxu/cellnet/peer/mysql"
"testing"
)

func TestMySQL(t *testing.T) {
// 从地址中选择mysql数据库,这里选择mysql系统库
p := peer.NewGenericPeer("mysql.Connector", "mysqldemo", "root:123456@(localhost:3306)/mysql", nil)
p.(cellnet.MySQLConnector).SetConnectionCount(3)

// 阻塞
p.Start()

op := p.(cellnet.MySQLOperator)

op.Operate(func(rawClient interface{}) interface{} {

client := rawClient.(*sql.DB)

// 查找默认用户
mysql.NewWrapper(client).Query("select User from user").Each(func(wrapper *mysql.Wrapper) bool {

var name string
wrapper.Scan(&name)
fmt.Println(name)
return true
})

return nil
})

}

0 comments on commit 279ff97

Please sign in to comment.