Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/test_artifacts/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func CleanupPostgresDocker(_ *testing.M, pool *dockertest.Pool, resource *docker
os.Exit(0)
}

// TODO(drewsky): Remove this in favor of a golang specific solution
// TODO: Remove this in favor of a golang specific solution for after test teardown
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be part of test teardown?

func CleanupTest(u utility.UtilityContext) {
u.Context.Release()
}
90 changes: 67 additions & 23 deletions utility/test/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,32 @@ import (
)

func TestUtilityContext_AddAccountAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
acc := GetAllTestingAccounts(t, ctx)[0]
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

acc := getAllTestingAccounts(t, ctx)[0]
initialAmount, err := types.StringToBigInt(acc.GetAmount())
require.NoError(t, err)

addAmount := big.NewInt(1)
addrBz, er := hex.DecodeString(acc.GetAddress())
require.NoError(t, er)
require.NoError(t, ctx.AddAccountAmount(addrBz, addAmount), "add account amount")

afterAmount, err := ctx.GetAccountAmount(addrBz)
require.NoError(t, err)

expected := initialAmount.Add(initialAmount, addAmount)
require.Equal(t, expected, afterAmount)
// RESEARCH a golang specific solution for after test teardown

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_AddAccountAmountString(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
acc := GetAllTestingAccounts(t, ctx)[0]
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

acc := getAllTestingAccounts(t, ctx)[0]
initialAmount, err := types.StringToBigInt(acc.GetAmount())
require.NoError(t, err)

Expand All @@ -46,34 +49,41 @@ func TestUtilityContext_AddAccountAmountString(t *testing.T) {
addrBz, er := hex.DecodeString(acc.GetAddress())
require.NoError(t, er)
require.NoError(t, ctx.AddAccountAmountString(addrBz, addAmountString), "add account amount string")

afterAmount, err := ctx.GetAccountAmount(addrBz)
require.NoError(t, err)

expected := initialAmount.Add(initialAmount, addAmount)
require.Equal(t, expected, afterAmount)

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_AddPoolAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
pool := GetAllTestingPools(t, ctx)[0]
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBasePoolMock(t, runTimeMgr))

pool := getAllTestingPools(t, ctx)[0]
initialAmount, err := types.StringToBigInt(pool.GetAmount())
require.NoError(t, err)

addAmount := big.NewInt(1)
require.NoError(t, ctx.AddPoolAmount(pool.GetAddress(), addAmount), "add pool amount")

afterAmount, err := ctx.GetPoolAmount(pool.GetAddress())
require.NoError(t, err)

expected := initialAmount.Add(initialAmount, addAmount)
require.Equal(t, expected, afterAmount, "amounts are not equal")

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_HandleMessageSend(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
accs := GetAllTestingAccounts(t, ctx)
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

accs := getAllTestingAccounts(t, ctx)

sendAmount := big.NewInt(1000000)
sendAmountString := types.BigIntToString(sendAmount)
Expand All @@ -82,45 +92,57 @@ func TestUtilityContext_HandleMessageSend(t *testing.T) {

recipientBalanceBefore, err := types.StringToBigInt(accs[1].GetAmount())
require.NoError(t, err)

addrBz, er := hex.DecodeString(accs[0].GetAddress())
require.NoError(t, er)

addrBz2, er := hex.DecodeString(accs[1].GetAddress())
require.NoError(t, er)

msg := NewTestingSendMessage(t, addrBz, addrBz2, sendAmountString)
err = ctx.HandleMessageSend(&msg)
require.NoError(t, err, "handle message send")

accs = GetAllTestingAccounts(t, ctx)
accs = getAllTestingAccounts(t, ctx)
senderBalanceAfter, err := types.StringToBigInt(accs[0].GetAmount())
require.NoError(t, err)

recipientBalanceAfter, err := types.StringToBigInt(accs[1].GetAmount())
require.NoError(t, err)
require.Equal(t, sendAmount, big.NewInt(0).Sub(senderBalanceBefore, senderBalanceAfter))
require.Equal(t, sendAmount, big.NewInt(0).Sub(recipientBalanceAfter, recipientBalanceBefore))

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_GetMessageSendSignerCandidates(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
accs := GetAllTestingAccounts(t, ctx)
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

accs := getAllTestingAccounts(t, ctx)

sendAmount := big.NewInt(1000000)
sendAmountString := types.BigIntToString(sendAmount)

addrBz, er := hex.DecodeString(accs[0].GetAddress())
require.NoError(t, er)

addrBz2, er := hex.DecodeString(accs[1].GetAddress())
require.NoError(t, er)

msg := NewTestingSendMessage(t, addrBz, addrBz2, sendAmountString)
candidates, err := ctx.GetMessageSendSignerCandidates(&msg)
require.NoError(t, err)
require.Equal(t, 1, len(candidates))
require.Equal(t, addrBz, candidates[0])

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_InsertPool(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBasePoolMock(t, runTimeMgr))

testPoolName := "TEST_POOL"

addr, err := crypto.GenerateAddress()
Expand All @@ -129,79 +151,98 @@ func TestUtilityContext_InsertPool(t *testing.T) {
amount := types.BigIntToString(big.NewInt(1000))
err = ctx.InsertPool(testPoolName, addr, amount)
require.NoError(t, err, "insert pool")

gotAmount, err := ctx.GetPoolAmount(testPoolName)
require.NoError(t, err)

gotAmountString := types.BigIntToString(gotAmount)
require.Equal(t, amount, gotAmountString)

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_SetAccountAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

addr, err := crypto.GenerateAddress()
require.NoError(t, err)

amount := big.NewInt(100)
require.NoError(t, ctx.SetAccountAmount(addr, amount), "set account amount")

gotAmount, err := ctx.GetAccountAmount(addr)
require.NoError(t, err)
require.Equal(t, amount, gotAmount)

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_SetAccountWithAmountString(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

addr, err := crypto.GenerateAddress()
require.NoError(t, err)

amount := big.NewInt(100)
amountString := types.BigIntToString(amount)
require.NoError(t, ctx.SetAccountWithAmountString(addr, amountString), "set account amount string")

gotAmount, err := ctx.GetAccountAmount(addr)
require.NoError(t, err)
require.Equal(t, amount, gotAmount)

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_SetPoolAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
pool := GetAllTestingPools(t, ctx)[0]
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBasePoolMock(t, runTimeMgr))

pool := getAllTestingPools(t, ctx)[0]
beforeAmount := pool.GetAmount()
beforeAmountBig, err := types.StringToBigInt(beforeAmount)
require.NoError(t, err)

expectedAfterAmount := big.NewInt(100)
require.NoError(t, ctx.SetPoolAmount(pool.GetAddress(), expectedAfterAmount), "set pool amount")

amount, err := ctx.GetPoolAmount(pool.GetAddress())
require.NoError(t, err)
require.NotEqual(t, beforeAmountBig, amount)
require.Equal(t, amount, expectedAfterAmount)

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_SubPoolAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
pool := GetAllTestingPools(t, ctx)[0]
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBasePoolMock(t, runTimeMgr))

pool := getAllTestingPools(t, ctx)[0]

beforeAmountBig := big.NewInt(1000000000000000)
ctx.SetPoolAmount(pool.GetAddress(), beforeAmountBig)
subAmountBig := big.NewInt(100)
subAmount := types.BigIntToString(subAmountBig)
require.NoError(t, ctx.SubPoolAmount(pool.GetAddress(), subAmount), "sub pool amount")

amount, err := ctx.GetPoolAmount(pool.GetAddress())
require.NoError(t, err)
require.NotEqual(t, beforeAmountBig, amount)

expected := beforeAmountBig.Sub(beforeAmountBig, subAmountBig)
require.Equal(t, expected, amount)

test_artifacts.CleanupTest(ctx)
}

func TestUtilityContext_SubtractAccountAmount(t *testing.T) {
ctx := NewTestingUtilityContext(t, 0)
acc := GetAllTestingAccounts(t, ctx)[0]
runTimeMgr := persistenceRuntimeMgr(t)
ctx := NewTestUtilityContext(t, 0, withBaseAccountMock(t, runTimeMgr))

acc := getAllTestingAccounts(t, ctx)[0]

beforeAmount := acc.GetAmount()
beforeAmountBig, err := types.StringToBigInt(beforeAmount)
Expand All @@ -211,15 +252,18 @@ func TestUtilityContext_SubtractAccountAmount(t *testing.T) {
addrBz, er := hex.DecodeString(acc.GetAddress())
require.NoError(t, er)
require.NoError(t, ctx.SubtractAccountAmount(addrBz, subAmountBig), "sub account amount")

amount, err := ctx.GetAccountAmount(addrBz)
require.NoError(t, err)
require.NotEqual(t, beforeAmountBig, amount)

expected := beforeAmountBig.Sub(beforeAmountBig, subAmountBig)
require.Equal(t, expected, amount)

test_artifacts.CleanupTest(ctx)
}

func GetAllTestingAccounts(t *testing.T, ctx utility.UtilityContext) []modules.Account {
func getAllTestingAccounts(t *testing.T, ctx utility.UtilityContext) []modules.Account {
accs, err := (ctx.Context.PersistenceRWContext).GetAllAccounts(0)
require.NoError(t, err)
sort.Slice(accs, func(i, j int) bool {
Expand All @@ -228,7 +272,7 @@ func GetAllTestingAccounts(t *testing.T, ctx utility.UtilityContext) []modules.A
return accs
}

func GetAllTestingPools(t *testing.T, ctx utility.UtilityContext) []modules.Account {
func getAllTestingPools(t *testing.T, ctx utility.UtilityContext) []modules.Account {
accs, err := (ctx.Context.PersistenceRWContext).GetAllPools(0)
require.NoError(t, err)
sort.Slice(accs, func(i, j int) bool {
Expand Down
8 changes: 4 additions & 4 deletions utility/test/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/pokt-network/pocket/persistence"
"github.com/pokt-network/pocket/runtime/test_artifacts"
"github.com/pokt-network/pocket/shared/modules"
mock_modules "github.com/pokt-network/pocket/shared/modules/mocks"
mockModules "github.com/pokt-network/pocket/shared/modules/mocks"
"github.com/pokt-network/pocket/utility"
utilTypes "github.com/pokt-network/pocket/utility/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -77,15 +77,15 @@ func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext
func newTestPersistenceModule(t *testing.T, databaseUrl string) modules.PersistenceModule {
ctrl := gomock.NewController(t)

mockPersistenceConfig := mock_modules.NewMockPersistenceConfig(ctrl)
mockPersistenceConfig := mockModules.NewMockPersistenceConfig(ctrl)
mockPersistenceConfig.EXPECT().GetPostgresUrl().Return(databaseUrl).AnyTimes()
mockPersistenceConfig.EXPECT().GetNodeSchema().Return(testSchema).AnyTimes()
mockPersistenceConfig.EXPECT().GetBlockStorePath().Return("").AnyTimes()

mockRuntimeConfig := mock_modules.NewMockConfig(ctrl)
mockRuntimeConfig := mockModules.NewMockConfig(ctrl)
mockRuntimeConfig.EXPECT().GetPersistenceConfig().Return(mockPersistenceConfig).AnyTimes()

mockRuntimeMgr := mock_modules.NewMockRuntimeMgr(ctrl)
mockRuntimeMgr := mockModules.NewMockRuntimeMgr(ctrl)
mockRuntimeMgr.EXPECT().GetConfig().Return(mockRuntimeConfig).AnyTimes()

genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1)
Expand Down
Loading