add initial keeper tests

This commit is contained in:
rhuairahrighairigh 2018-07-13 16:22:58 +01:00
parent 469dd84a32
commit 97a7f79ed8

View File

@ -3,10 +3,119 @@ package paychan
import ( import (
"testing" "testing"
//"github.com/stretchr/testify/assert" //"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
) )
// example from x/bank // GetPaychan
// - gets a paychan if it exists, and not if it doesn't
// setPaychan
// - sets a paychan
// CreatePaychan
// - creates a paychan under normal conditions
// ClosePaychan
// - closes a paychan under normal conditions
// GetPaychans
// paychanKey
func setupMultiStore() (sdk.MultiStore, *sdk.KVStoreKey, *sdk.KVStoreKey) {
// create db
db := dbm.NewMemDB()
// create keys
authKey := sdk.NewKVStoreKey("authkey")
paychanKey := sdk.NewKVStoreKey("paychankey")
// create new multistore around db
ms := store.NewCommitMultiStore(db) // DB handle plus store key maps
// register separate stores in the multistore
ms.MountStoreWithDB(authKey, sdk.StoreTypeIAVL, db) // sets store key map
ms.MountStoreWithDB(paychanKey, sdk.StoreTypeIAVL, db)
ms.LoadLatestVersion()
return ms, authKey, paychanKey
}
func setupCodec() *wire.Codec {
cdc := wire.NewCodec()
auth.RegisterBaseAccount(cdc)
// TODO might need to register paychan struct
return cdc
}
func TestKeeper(t *testing.T) { func TestKeeper(t *testing.T) {
// Setup
// create multistore and key
ms, authKey, paychanKey := setupMultiStore()
// create and initialise codec(s)
cdc := setupCodec()
// create context
ctx := sdk.NewContext(ms, abci.Header{}, false, nil, log.NewNopLogger())
// create accountMapper
accountMapper := auth.NewAccountMapper(cdc, authKey, &auth.BaseAccount{})
// create coinkeeper
coinKeeper := bank.NewKeeper(accountMapper)
// create keeper
paychanKeeper := NewKeeper(cdc, paychanKey, coinKeeper)
// Test no paychans exist
_, exists := paychanKeeper.GetPaychan(ctx, sdk.Address{}, sdk.Address{}, 0)
if exists {
t.Error("payment channel found when none exist")
}
// Test paychan can be set and get
p := Paychan{
sender: sdk.Address([]byte("senderAddress")),
receiver: sdk.Address([]byte("receiverAddress")),
id: 0,
balance: sdk.Coins{{"KVA", 100}},
}
paychanKeeper.setPaychan(ctx, p)
_, exists = paychanKeeper.GetPaychan(ctx, p.sender, p.receiver, p.id)
if !exists {
t.Error("payment channel not found")
}
// Test create paychan under normal conditions
senderAddress := sdk.Address([]byte("senderAddress"))
senderFunds := sdk.Coins{{"KVA", 100}}
receiverAddress := sdk.Address([]byte("receiverAddress"))
balance := sdk.Coins{{"KVA", 10}}
coinKeeper.SetCoins(ctx, senderAddress, senderFunds)
_, err := paychanKeeper.CreatePaychan(ctx, senderAddress, receiverAddress, balance)
if err != nil {
t.Error("unexpected error created payment channel", err)
}
p, _ = paychanKeeper.GetPaychan(ctx, senderAddress, receiverAddress, 0)
if !p.balance.IsEqual(balance) {
t.Error("payment channel balance incorrect", p.balance, balance)
}
expectedNewSenderFunds := senderFunds.Minus(balance)
if !coinKeeper.GetCoins(ctx, senderAddress).IsEqual(expectedNewSenderFunds) {
t.Error("sender has incorrect balance after paychan creation")
}
}
// example from x/bank
//func TestKeeper(t *testing.T) {
// ms, authKey := setupMultiStore() // ms, authKey := setupMultiStore()
// cdc := wire.NewCodec() // cdc := wire.NewCodec()
@ -89,4 +198,4 @@ func TestKeeper(t *testing.T) {
// assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 7}, {"foocoin", 6}})) // assert.True(t, coinKeeper.GetCoins(ctx, addr2).IsEqual(sdk.Coins{{"barcoin", 7}, {"foocoin", 6}}))
// assert.True(t, coinKeeper.GetCoins(ctx, addr3).IsEqual(sdk.Coins{{"barcoin", 2}, {"foocoin", 5}})) // assert.True(t, coinKeeper.GetCoins(ctx, addr3).IsEqual(sdk.Coins{{"barcoin", 2}, {"foocoin", 5}}))
} //}