Skip to content

Commit

Permalink
GSW-1838 fix: test errors are fixed (#422)
Browse files Browse the repository at this point in the history
* GSW-1838 fix: test errors are fixed

- Integrate helper functions for tests
- Change file extensions to prevent test code in the test folder from being executed
- Fixing failure errors due to code integration
- Known issue : Fixed additional test failure case related to getter

* fix: remove time compare in unit test

* fix: do not setup data in init

* test: pool manger testcase

---------

Co-authored-by: n3wbie <[email protected]>
  • Loading branch information
onlyhyde and r3v4s committed Dec 10, 2024
1 parent 5d3efd2 commit 23a3bed
Show file tree
Hide file tree
Showing 10 changed files with 482 additions and 255 deletions.
127 changes: 113 additions & 14 deletions pool/_helper_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"gno.land/r/onbloc/foo"
"gno.land/r/onbloc/obl"
"gno.land/r/onbloc/qux"
"gno.land/r/onbloc/usdc"

"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"
pusers "gno.land/p/demo/users"
"gno.land/r/demo/users"
"gno.land/r/gnoswap/v1/consts"
Expand All @@ -37,6 +39,12 @@ const (
max_timeout int64 = 9999999999
)

const (
// define addresses to use in tests
addr01 = testutils.TestAddress("addr01")
addr02 = testutils.TestAddress("addr02")
)

type WugnotToken struct{}

func (WugnotToken) Transfer() func(to pusers.AddressOrName, amount uint64) {
Expand Down Expand Up @@ -155,20 +163,22 @@ func init() {
}

var (
newAdmin = pusers.AddressOrName("g17290cwvmrapvp869xfnhhawa8sm9edpufzat7d")
admin = pusers.AddressOrName(consts.ADMIN)
alice = pusers.AddressOrName(testutils.TestAddress("alice"))
pool = pusers.AddressOrName(consts.POOL_ADDR)
protocolFee = pusers.AddressOrName(consts.PROTOCOL_FEE_ADDR)
adminRealm = std.NewUserRealm(users.Resolve(admin))
posRealm = std.NewCodeRealm(consts.POSITION_PATH)

// addresses used in tests
addrUsedInTest = []std.Address{addr01, addr02}
)

func InitialisePoolTest(t *testing.T) {
t.Helper()

ugnotFaucet(t, users.Resolve(newAdmin), 100_000_000_000_000)
ugnotDeposit(t, users.Resolve(newAdmin), 100_000_000_000_000)
TokenFaucet(t, wugnotPath, admin)
TokenFaucet(t, gnsPath, admin)
ugnotFaucet(t, users.Resolve(admin), 100_000_000_000_000)
ugnotDeposit(t, users.Resolve(admin), 100_000_000_000_000)

std.TestSetOrigCaller(users.Resolve(admin))
TokenApprove(t, gnsPath, admin, pool, maxApprove)
Expand Down Expand Up @@ -202,7 +212,7 @@ func InitialisePoolTest(t *testing.T) {

func TokenFaucet(t *testing.T, tokenPath string, to pusers.AddressOrName) {
t.Helper()
std.TestSetOrigCaller(users.Resolve(newAdmin))
std.TestSetOrigCaller(users.Resolve(admin))
defaultAmount := uint64(5_000_000_000)

switch tokenPath {
Expand Down Expand Up @@ -367,43 +377,43 @@ func quxApprove(t *testing.T, owner, spender pusers.AddressOrName, amount uint64

func wugnotTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
wugnot.Transfer(to, amount)
}

func gnsTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
gns.Transfer(to, amount)
}

func barTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
bar.Transfer(to, amount)
}

func bazTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
baz.Transfer(to, amount)
}

func fooTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
foo.Transfer(to, amount)
}

func oblTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
obl.Transfer(to, amount)
}

func quxTransfer(t *testing.T, to pusers.AddressOrName, amount uint64) {
t.Helper()
std.TestSetRealm(std.NewUserRealm(users.Resolve(newAdmin)))
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
qux.Transfer(to, amount)
}

Expand Down Expand Up @@ -446,7 +456,7 @@ func ugnotBurn(t *testing.T, addr std.Address, denom string, amount int64) {

func ugnotFaucet(t *testing.T, to std.Address, amount uint64) {
t.Helper()
faucetAddress := users.Resolve(newAdmin)
faucetAddress := users.Resolve(admin)
std.TestSetOrigCaller(faucetAddress)

if ugnotBalanceOf(t, faucetAddress) < amount {
Expand All @@ -465,3 +475,92 @@ func ugnotDeposit(t *testing.T, addr std.Address, amount uint64) {
banker.SendCoins(addr, wugnotAddr, std.Coins{{ugnotDenom, int64(amount)}})
wugnot.Deposit()
}

// resetObject resets the object state(clear or make it default values)
func resetObject(t *testing.T) {
pools = make(poolMap)
slot0FeeProtocol = 0
poolCreationFee = 100_000_000
withdrawalFeeBPS = 100
}

func burnTokens(t *testing.T) {
t.Helper()

// burn tokens
for _, addr := range addrUsedInTest {
uAddr := a2u(addr)
burnFoo(uAddr)
burnBar(uAddr)
burnBaz(uAddr)
burnQux(uAddr)
burnObl(uAddr)
burnUsdc(uAddr)
}
}

func burnFoo(addr pusers.AddressOrName) {
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
foo.Burn(addr, foo.BalanceOf(addr))
}

func burnBar(addr pusers.AddressOrName) {
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
bar.Burn(addr, bar.BalanceOf(addr))
}

func burnBaz(addr pusers.AddressOrName) {
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
baz.Burn(addr, baz.BalanceOf(addr))
}

func burnQux(addr pusers.AddressOrName) {
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
qux.Burn(addr, qux.BalanceOf(addr))
}

func burnObl(addr pusers.AddressOrName) {
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
obl.Burn(addr, obl.BalanceOf(addr))
}

func burnUsdc(addr pusers.AddressOrName) {
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
usdc.Burn(addr, usdc.BalanceOf(addr))
}

func TestBeforeResetObject(t *testing.T) {
// make some data
pools = make(poolMap)
pools["gno.land/r/gnoswap/v1/gns:gno.land/r/onbloc/usdc"] = &Pool{
token0Path: "gno.land/r/gnoswap/v1/gns",
token1Path: "gno.land/r/onbloc/usdc",
}

slot0FeeProtocol = 1
poolCreationFee = 100_000_000
withdrawalFeeBPS = 100

// transfer some tokens
std.TestSetRealm(std.NewUserRealm(users.Resolve(admin)))
foo.Transfer(a2u(addr01), 100_000_000)
bar.Transfer(a2u(addr01), 100_000_000)

uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(100_000_000))
uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(100_000_000))
}

func TestResetObject(t *testing.T) {
resetObject(t)
uassert.Equal(t, len(pools), 0)
uassert.Equal(t, slot0FeeProtocol, uint8(0))
uassert.Equal(t, poolCreationFee, uint64(100_000_000))
uassert.Equal(t, withdrawalFeeBPS, uint64(100))
}

func TestBurnTokens(t *testing.T) {
burnTokens(t)

uassert.Equal(t, foo.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0
uassert.Equal(t, bar.BalanceOf(a2u(addr01)), uint64(0)) // 100_000_000 -> 0
}
5 changes: 1 addition & 4 deletions pool/api_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestApiGetPools(t *testing.T) {
panic(err.Error())
}

uassert.Equal(t, response.Size(), 2) // should be same as the number of pools
uassert.Equal(t, 2, response.Size()) // should be same as the number of pools
}

func TestApiGetPool(t *testing.T) {
Expand Down Expand Up @@ -70,9 +70,6 @@ func TestMakeStatNode(t *testing.T) {

statHeight, _ := statNode.GetKey("height")
uassert.Equal(t, statHeight.String(), "124")

statTimestamp, _ := statNode.GetKey("timestamp")
uassert.Equal(t, statTimestamp.String(), "1234567895")
})
}

Expand Down
4 changes: 3 additions & 1 deletion pool/getter_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
u256 "gno.land/p/gnoswap/uint256"
)

func init() {
func TestInitData(t *testing.T) {
resetObject(t)

mockPool := &Pool{
token0Path: "token0",
token1Path: "token1",
Expand Down
118 changes: 0 additions & 118 deletions pool/helper_test.gno

This file was deleted.

Loading

0 comments on commit 23a3bed

Please sign in to comment.