Skip to content

Commit

Permalink
API-24: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovov committed Aug 29, 2023
1 parent 843942a commit 47e46bf
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 99 deletions.
51 changes: 51 additions & 0 deletions models/position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package models

import (
"math/big"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestGetPositionFromContract(t *testing.T) {
timeNow := time.Now()

testCases := []struct {
name string
obj positionContract
blockN uint64
time uint64
want *Position
}{
{
name: "blank object",
want: &Position{},
},
{
name: "full object",
obj: positionContract{
TotalPnl: big.NewInt(1),
AccruedFunding: big.NewInt(2),
PositionSize: big.NewInt(3),
},
time: uint64(timeNow.Unix()),
blockN: uint64(4),
want: &Position{
TotalPnl: big.NewInt(1),
AccruedFunding: big.NewInt(2),
PositionSize: big.NewInt(3),
BlockTimestamp: uint64(timeNow.Unix()),
BlockNumber: uint64(4),
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
res := GetPositionFromContract(tt.obj, tt.blockN, tt.time)

require.Equal(t, tt.want, res)
})
}
}
42 changes: 0 additions & 42 deletions models/model_test.go → models/trade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,45 +94,3 @@ func TestGetTradeFromEvent(t *testing.T) {
})
}
}

func TestGetPositionFromContract(t *testing.T) {
timeNow := time.Now()

testCases := []struct {
name string
obj positionContract
blockN uint64
time uint64
want *Position
}{
{
name: "blank object",
want: &Position{},
},
{
name: "full object",
obj: positionContract{
TotalPnl: big.NewInt(1),
AccruedFunding: big.NewInt(2),
PositionSize: big.NewInt(3),
},
time: uint64(timeNow.Unix()),
blockN: uint64(4),
want: &Position{
TotalPnl: big.NewInt(1),
AccruedFunding: big.NewInt(2),
PositionSize: big.NewInt(3),
BlockTimestamp: uint64(timeNow.Unix()),
BlockNumber: uint64(4),
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
res := GetPositionFromContract(tt.obj, tt.blockN, tt.time)

require.Equal(t, tt.want, res)
})
}
}
72 changes: 72 additions & 0 deletions services/positions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package services

import (
"fmt"
"log"
"math/big"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gateway-fm/perpsv3-Go/contracts/coreGoerli"
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli"
"github.com/gateway-fm/perpsv3-Go/contracts/spotMarketGoerli"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/stretchr/testify/require"
)

func TestService_GetPosition_OnChain(t *testing.T) {
rpc := os.Getenv("TEST_RPC")
if rpc == "" {
log.Fatal("no rpc in env vars")
}

rpcClient, _ := ethclient.Dial(rpc)

coreC, _ := coreGoerli.NewCoreGoerli(common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"), rpcClient)
spot, _ := spotMarketGoerli.NewSpotMarketGoerli(common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"), rpcClient)
perps, _ := perpsMarketGoerli.NewPerpsMarketGoerli(common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"), rpcClient)

testCases := []struct {
name string
accountID *big.Int
marketID *big.Int
wantErr error
}{
{
name: "good data",
accountID: big.NewInt(28006),
marketID: big.NewInt(100),
},
{
name: "0 account ID no errors",
accountID: big.NewInt(0),
marketID: big.NewInt(100),
},
{
name: "bad market id",
accountID: big.NewInt(28006),
marketID: big.NewInt(0),
wantErr: errors.GetReadContractErr(fmt.Errorf("execution reverted"), "PerpsMarket", "getOpenPosition"),
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
s := NewService(rpcClient, coreC, 11664658, spot, 10875051, perps, 0)

res, err := s.GetPosition(tt.accountID, tt.marketID)

if tt.wantErr == nil {
require.NotNil(t, res)
require.NotNil(t, res.PositionSize)
require.NotNil(t, res.AccruedFunding)
require.NotNil(t, res.TotalPnl)
require.NotEqual(t, res.BlockTimestamp, 0)
require.NotEqual(t, res.BlockNumber, 0)
} else {
require.EqualError(t, tt.wantErr, err.Error())
}
})
}
}
57 changes: 0 additions & 57 deletions services/service_test.go → services/trades_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package services

import (
"fmt"
"log"
"math/big"
"os"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/gateway-fm/perpsv3-Go/contracts/coreGoerli"
"github.com/gateway-fm/perpsv3-Go/contracts/perpsMarketGoerli"
"github.com/gateway-fm/perpsv3-Go/contracts/spotMarketGoerli"
"github.com/gateway-fm/perpsv3-Go/errors"
"github.com/gateway-fm/perpsv3-Go/models"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -120,58 +118,3 @@ func TestService_RetrieveTrades_OnChain(t *testing.T) {
})
}
}

func TestService_GetPosition_OnChain(t *testing.T) {
rpc := os.Getenv("TEST_RPC")
if rpc == "" {
log.Fatal("no rpc in env vars")
}

rpcClient, _ := ethclient.Dial(rpc)

coreC, _ := coreGoerli.NewCoreGoerli(common.HexToAddress("0x76490713314fCEC173f44e99346F54c6e92a8E42"), rpcClient)
spot, _ := spotMarketGoerli.NewSpotMarketGoerli(common.HexToAddress("0x5FF4b3aacdeC86782d8c757FAa638d8790799E83"), rpcClient)
perps, _ := perpsMarketGoerli.NewPerpsMarketGoerli(common.HexToAddress("0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b"), rpcClient)

testCases := []struct {
name string
accountID *big.Int
marketID *big.Int
wantErr error
}{
{
name: "good data",
accountID: big.NewInt(28006),
marketID: big.NewInt(100),
},
{
name: "0 account ID no errors",
accountID: big.NewInt(0),
marketID: big.NewInt(100),
},
{
name: "bad market id",
accountID: big.NewInt(28006),
marketID: big.NewInt(0),
wantErr: errors.GetReadContractErr(fmt.Errorf("execution reverted"), "PerpsMarket", "getOpenPosition"),
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
s := NewService(rpcClient, coreC, 11664658, spot, 10875051, perps, 0)

res, err := s.GetPosition(tt.accountID, tt.marketID)

if tt.wantErr == nil {
require.NotNil(t, res)
require.NotNil(t, res.PositionSize)
require.NotNil(t, res.AccruedFunding)
require.NotNil(t, res.TotalPnl)
require.NotEqual(t, res.BlockTimestamp, 0)
require.NotEqual(t, res.BlockNumber, 0)
} else {
require.EqualError(t, tt.wantErr, err.Error())
}
})
}
}

0 comments on commit 47e46bf

Please sign in to comment.