Commit c914326a authored by mdj33's avatar mdj33

fix ut

parent 3b91705f
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testnode
import (
"testing"
"github.com/33cn/chain33/util/testnode"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
ticketwallet "github.com/33cn/plugin/plugin/dapp/ticket/wallet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/33cn/chain33/system"
"github.com/33cn/chain33/types"
_ "github.com/33cn/plugin/plugin"
)
func Test_WalletTicket(t *testing.T) {
minerAddr := "12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
t.Log("Begin wallet ticket test")
cfg, sub := testnode.GetDefaultConfig()
cfg.Consensus.Name = "ticket"
mock33 := testnode.NewWithConfig(cfg, sub, nil)
defer mock33.Close()
err := mock33.WaitHeight(0)
assert.Nil(t, err)
msg, err := mock33.GetAPI().Query(ty.TicketX, "TicketList", &ty.TicketList{Addr: minerAddr, Status: 1})
assert.Nil(t, err)
ticketList := msg.(*ty.ReplyTicketList)
assert.NotNil(t, ticketList)
//return
ticketwallet.FlushTicket(mock33.GetAPI())
err = mock33.WaitHeight(2)
assert.Nil(t, err)
header, err := mock33.GetAPI().GetLastHeader()
require.Equal(t, err, nil)
require.Equal(t, header.Height >= 2, true)
in := &ty.TicketClose{MinerAddress: minerAddr}
msg, err = mock33.GetAPI().ExecWalletFunc(ty.TicketX, "CloseTickets", in)
assert.Nil(t, err)
hashes := msg.(*types.ReplyHashes)
assert.NotNil(t, hashes)
in = &ty.TicketClose{}
msg, err = mock33.GetAPI().ExecWalletFunc(ty.TicketX, "CloseTickets", in)
assert.Nil(t, err)
hashes = msg.(*types.ReplyHashes)
assert.NotNil(t, hashes)
t.Log("End wallet ticket test")
}
......@@ -16,7 +16,6 @@ import (
wcom "github.com/33cn/chain33/wallet/common"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "github.com/33cn/chain33/system"
"github.com/33cn/chain33/types"
......@@ -27,40 +26,14 @@ import (
"github.com/33cn/chain33/common/db"
)
func Test_WalletTicket(t *testing.T) {
minerAddr := "12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"
t.Log("Begin wallet ticket test")
const (
sendhash = "sendhash"
)
cfg, sub := testnode.GetDefaultConfig()
func init() {
cfg, _ := testnode.GetDefaultConfig()
cfg.Consensus.Name = "ticket"
mock33 := testnode.NewWithConfig(cfg, sub, nil)
defer mock33.Close()
err := mock33.WaitHeight(0)
assert.Nil(t, err)
msg, err := mock33.GetAPI().Query(ty.TicketX, "TicketList", &ty.TicketList{Addr: minerAddr, Status: 1})
assert.Nil(t, err)
ticketList := msg.(*ty.ReplyTicketList)
assert.NotNil(t, ticketList)
//return
FlushTicket(mock33.GetAPI())
err = mock33.WaitHeight(2)
assert.Nil(t, err)
header, err := mock33.GetAPI().GetLastHeader()
require.Equal(t, err, nil)
require.Equal(t, header.Height >= 2, true)
in := &ty.TicketClose{MinerAddress: minerAddr}
msg, err = mock33.GetAPI().ExecWalletFunc(ty.TicketX, "CloseTickets", in)
assert.Nil(t, err)
hashes := msg.(*types.ReplyHashes)
assert.NotNil(t, hashes)
in = &ty.TicketClose{}
msg, err = mock33.GetAPI().ExecWalletFunc(ty.TicketX, "CloseTickets", in)
assert.Nil(t, err)
hashes = msg.(*types.ReplyHashes)
assert.NotNil(t, hashes)
t.Log("End wallet ticket test")
types.Init(cfg.Title, cfg)
}
func Test_ForceCloseTicketList(t *testing.T) {
......@@ -73,7 +46,7 @@ func Test_ForceCloseTicketList(t *testing.T) {
tlist := []*ty.Ticket{t1, t2, t3}
r1, r2 := ticket.forceCloseTicketList(0, nil, tlist)
assert.Nil(t, r1)
assert.Equal(t, []byte(sendhash), r1)
assert.Nil(t, r2)
}
......@@ -99,7 +72,9 @@ func Test_CloseTicketsByAddr(t *testing.T) {
tlist := &ty.ReplyTicketList{Tickets: []*ty.Ticket{t1, t2, t3}}
qapi.On("Query", ty.TicketX, "TicketList", mock.Anything).Return(tlist, nil)
ticket.closeTicketsByAddr(0, priKey)
r1, r2 := ticket.closeTicketsByAddr(0, priKey)
assert.Equal(t, []byte(sendhash), r1)
assert.Nil(t, r2)
}
......@@ -113,7 +88,10 @@ func Test_BuyTicketOne(t *testing.T) {
assert.Nil(t, err)
priKey, err := secp.PrivKeyFromBytes(pk)
assert.Nil(t, err)
ticket.buyTicketOne(0, priKey)
hash, r1, r2 := ticket.buyTicketOne(0, priKey)
assert.Equal(t, []byte(sendhash), hash)
assert.Equal(t, 10, r1)
assert.Nil(t, r2)
}
......@@ -126,6 +104,8 @@ func Test_BuyMinerAddrTicketOne(t *testing.T) {
assert.Nil(t, err)
ticket := &ticketPolicy{mtx: &sync.Mutex{}}
ticket.cfg = &subConfig{}
ticket.initMinerWhiteList(nil)
wallet := new(walletOperateMock)
qapi := new(mocks.QueueProtocolAPI)
wallet.api = qapi
......@@ -134,7 +114,10 @@ func Test_BuyMinerAddrTicketOne(t *testing.T) {
tlist := &types.ReplyStrings{Datas: []string{"12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"}}
qapi.On("Query", ty.TicketX, "MinerSourceList", mock.Anything).Return(tlist, nil)
ticket.buyMinerAddrTicketOne(0, priKey)
hashs, r2, r3 := ticket.buyMinerAddrTicketOne(0, priKey)
assert.Equal(t, [][]byte{[]byte(sendhash)}, hashs)
assert.Equal(t, 10, r2)
assert.Nil(t, r3)
}
......@@ -164,7 +147,7 @@ func (_m *walletOperateMock) GetAllPrivKeys() ([]crypto.PrivKey, error) {
// GetBalance provides a mock function with given fields: addr, execer
func (_m *walletOperateMock) GetBalance(addr string, execer string) (*types.Account, error) {
return &types.Account{Balance: 10}, nil
return &types.Account{Balance: 10000000000000, Addr: "12qyocayNF7Lv6C9qW4avxs2E7U41fKSfv"}, nil
}
// GetBlockHeight provides a mock function with given fields:
......@@ -259,12 +242,12 @@ func (_m *walletOperateMock) RegisterMineStatusReporter(reporter wcom.MineStatus
// SendToAddress provides a mock function with given fields: priv, addrto, amount, note, Istoken, tokenSymbol
func (_m *walletOperateMock) SendToAddress(priv crypto.PrivKey, addrto string, amount int64, note string, Istoken bool, tokenSymbol string) (*types.ReplyHash, error) {
return nil, nil
return &types.ReplyHash{Hash: []byte(sendhash)}, nil
}
// SendTransaction provides a mock function with given fields: payload, execer, priv, to
func (_m *walletOperateMock) SendTransaction(payload types.Message, execer []byte, priv crypto.PrivKey, to string) ([]byte, error) {
return nil, nil
return []byte(sendhash), nil
}
// WaitTx provides a mock function with given fields: hash
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment