Commit b90a3a53 authored by Son of Odin's avatar Son of Odin

add unit tests

parent e8ec1083
Pipeline #130164659 passed with stages
in 31 minutes and 33 seconds
......@@ -73,6 +73,7 @@ type TestMigrateKeeperHappyPath struct {
newVault Vault
retireVault Vault
txout *TxOut
pool Pool
}
func (k *TestMigrateKeeperHappyPath) GetTxOut(ctx sdk.Context, blockHeight int64) (*TxOut, error) {
......@@ -90,6 +91,28 @@ func (k *TestMigrateKeeperHappyPath) SetTxOut(ctx sdk.Context, blockOut *TxOut)
return kaboom
}
func (k *TestMigrateKeeperHappyPath) GetNodeAccountByPubKey(_ sdk.Context, _ common.PubKey) (NodeAccount, error) {
return k.activeNodeAccount, nil
}
func (k *TestMigrateKeeperHappyPath) SetNodeAccount(_ sdk.Context, na NodeAccount) error {
k.activeNodeAccount = na
return nil
}
func (k *TestMigrateKeeperHappyPath) GetPool(_ sdk.Context, _ common.Asset) (Pool, error) {
return k.pool, nil
}
func (k *TestMigrateKeeperHappyPath) SetPool(_ sdk.Context, p Pool) error {
k.pool = p
return nil
}
func (k *TestMigrateKeeperHappyPath) UpsertEvent(_ sdk.Context, _ Event) error {
return nil
}
func (HandlerMigrateSuite) TestMigrateHappyPath(c *C) {
ctx, _ := setupKeeperForTest(c)
retireVault := GetRandomVault()
......@@ -132,3 +155,46 @@ func (HandlerMigrateSuite) TestMigrateHappyPath(c *C) {
c.Assert(result.Code, Equals, sdk.CodeOK)
c.Assert(keeper.txout.TxArray[0].OutHash.Equals(tx.Tx.ID), Equals, true)
}
func (HandlerMigrateSuite) TestSlash(c *C) {
ctx, _ := setupKeeperForTest(c)
retireVault := GetRandomVault()
newVault := GetRandomVault()
txout := NewTxOut(1)
newVaultAddr, err := newVault.PubKey.GetAddress(common.BNBChain)
c.Assert(err, IsNil)
pool := NewPool()
pool.Asset = common.BNBAsset
pool.BalanceAsset = sdk.NewUint(100 * common.One)
pool.BalanceRune = sdk.NewUint(100 * common.One)
na := GetRandomNodeAccount(NodeActive)
na.Bond = sdk.NewUint(100 * common.One)
keeper := &TestMigrateKeeperHappyPath{
activeNodeAccount: na,
newVault: newVault,
retireVault: retireVault,
txout: txout,
pool: pool,
}
addr, err := keeper.retireVault.PubKey.GetAddress(common.BNBChain)
c.Assert(err, IsNil)
handler := NewMigrateHandler(keeper)
tx := NewObservedTx(common.Tx{
ID: GetRandomTxHash(),
Chain: common.BNBChain,
Coins: common.Coins{
common.NewCoin(common.BNBAsset, sdk.NewUint(1024)),
},
Memo: NewMigrateMemo(1).String(),
FromAddress: addr,
ToAddress: newVaultAddr,
Gas: common.BNBGasFeeSingleton,
}, 1, retireVault.PubKey)
msgMigrate := NewMsgMigrate(tx, 1, keeper.activeNodeAccount.NodeAddress)
result := handler.handleV1(ctx, msgMigrate)
c.Assert(result.Code, Equals, sdk.CodeOK, Commentf("%s", result.Log))
c.Assert(keeper.activeNodeAccount.Bond.Equal(sdk.NewUint(9999998464)), Equals, true, Commentf("%d", keeper.activeNodeAccount.Bond.Uint64()))
}
......@@ -73,6 +73,7 @@ type TestRagnarokKeeperHappyPath struct {
newVault Vault
retireVault Vault
txout *TxOut
pool Pool
}
func (k *TestRagnarokKeeperHappyPath) GetTxOut(ctx sdk.Context, blockHeight int64) (*TxOut, error) {
......@@ -90,6 +91,28 @@ func (k *TestRagnarokKeeperHappyPath) SetTxOut(ctx sdk.Context, blockOut *TxOut)
return kaboom
}
func (k *TestRagnarokKeeperHappyPath) GetNodeAccountByPubKey(_ sdk.Context, _ common.PubKey) (NodeAccount, error) {
return k.activeNodeAccount, nil
}
func (k *TestRagnarokKeeperHappyPath) SetNodeAccount(_ sdk.Context, na NodeAccount) error {
k.activeNodeAccount = na
return nil
}
func (k *TestRagnarokKeeperHappyPath) GetPool(_ sdk.Context, _ common.Asset) (Pool, error) {
return k.pool, nil
}
func (k *TestRagnarokKeeperHappyPath) SetPool(_ sdk.Context, p Pool) error {
k.pool = p
return nil
}
func (k *TestRagnarokKeeperHappyPath) UpsertEvent(_ sdk.Context, _ Event) error {
return nil
}
func (HandlerRagnarokSuite) TestRagnarokHappyPath(c *C) {
ctx, _ := setupKeeperForTest(c)
retireVault := GetRandomVault()
......@@ -132,3 +155,46 @@ func (HandlerRagnarokSuite) TestRagnarokHappyPath(c *C) {
c.Assert(result.Code, Equals, sdk.CodeOK)
c.Assert(keeper.txout.TxArray[0].OutHash.Equals(tx.Tx.ID), Equals, true)
}
func (HandlerRagnarokSuite) TestSlash(c *C) {
ctx, _ := setupKeeperForTest(c)
retireVault := GetRandomVault()
newVault := GetRandomVault()
txout := NewTxOut(1)
newVaultAddr, err := newVault.PubKey.GetAddress(common.BNBChain)
c.Assert(err, IsNil)
pool := NewPool()
pool.Asset = common.BNBAsset
pool.BalanceAsset = sdk.NewUint(100 * common.One)
pool.BalanceRune = sdk.NewUint(100 * common.One)
na := GetRandomNodeAccount(NodeActive)
na.Bond = sdk.NewUint(100 * common.One)
keeper := &TestRagnarokKeeperHappyPath{
activeNodeAccount: na,
newVault: newVault,
retireVault: retireVault,
txout: txout,
pool: pool,
}
addr, err := keeper.retireVault.PubKey.GetAddress(common.BNBChain)
c.Assert(err, IsNil)
handler := NewRagnarokHandler(keeper)
tx := NewObservedTx(common.Tx{
ID: GetRandomTxHash(),
Chain: common.BNBChain,
Coins: common.Coins{
common.NewCoin(common.BNBAsset, sdk.NewUint(1024)),
},
Memo: NewRagnarokMemo(1).String(),
FromAddress: addr,
ToAddress: newVaultAddr,
Gas: common.BNBGasFeeSingleton,
}, 1, retireVault.PubKey)
msgRagnarok := NewMsgRagnarok(tx, 1, keeper.activeNodeAccount.NodeAddress)
result := handler.handleV1(ctx, msgRagnarok)
c.Assert(result.Code, Equals, sdk.CodeOK, Commentf("%s", result.Log))
c.Assert(keeper.activeNodeAccount.Bond.Equal(sdk.NewUint(9999998464)), Equals, true, Commentf("%d", keeper.activeNodeAccount.Bond.Uint64()))
}
......@@ -37,6 +37,10 @@ func (k yggdrasilTestKeeper) GetNodeAccountByPubKey(ctx sdk.Context, pk common.P
return k.Keeper.GetNodeAccountByPubKey(ctx, pk)
}
func (k *yggdrasilTestKeeper) SetNodeAccount(ctx sdk.Context, na NodeAccount) error {
return k.Keeper.SetNodeAccount(ctx, na)
}
func (k yggdrasilTestKeeper) GetPool(ctx sdk.Context, asset common.Asset) (Pool, error) {
if k.errGetPool {
return Pool{}, kaboom
......@@ -44,6 +48,14 @@ func (k yggdrasilTestKeeper) GetPool(ctx sdk.Context, asset common.Asset) (Pool,
return k.Keeper.GetPool(ctx, asset)
}
func (k *yggdrasilTestKeeper) SetPool(ctx sdk.Context, p Pool) error {
return k.Keeper.SetPool(ctx, p)
}
func (k *yggdrasilTestKeeper) UpsertEvent(ctx sdk.Context, evt Event) error {
return k.Keeper.UpsertEvent(ctx, evt)
}
func (k yggdrasilTestKeeper) GetVault(ctx sdk.Context, pk common.PubKey) (Vault, error) {
if k.errGetVault {
return Vault{}, kaboom
......
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