Skip to content

Commit

Permalink
test: staking and upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jim380 committed Jun 9, 2024
1 parent 9e3a247 commit 2c9fff8
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 20 deletions.
13 changes: 0 additions & 13 deletions services/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,30 @@ import (
)

func TestNodeService_GetInfo(t *testing.T) {
// Load mock data from JSON file
mockData, err := os.ReadFile("../testutil/json/node.json")
require.NoError(t, err)

// Create a new HTTP server with a mock handler for node info
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(mockData)
}))
defer server.Close()

// Override the RESTAddr
originalRESTAddr := constants.RESTAddr
constants.RESTAddr = server.URL
defer func() { constants.RESTAddr = originalRESTAddr }()

// Initialize service
ns := &services.NodeService{}

// Prepare input and output
cfg := &config.Config{}
rd := &types.RESTData{}

// Call method
ns.GetInfo(cfg, rd)

// Parse the mock data to get the expected result
var expectedData types.NodeInfo
err = json.Unmarshal(mockData, &expectedData)
require.NoError(t, err)

// Debug statements
t.Logf("Mock Data: %s", string(mockData))
t.Logf("Expected Node Info: %+v", expectedData)
t.Logf("Actual Node Info: %+v", rd.NodeInfo)

// Assertions
require.NotNil(t, rd.NodeInfo)
require.Equal(t, expectedData.Application.Version, rd.NodeInfo.Application.Version)
require.Equal(t, expectedData.Application.SDKVersion, rd.NodeInfo.Application.SDKVersion)
Expand Down
10 changes: 3 additions & 7 deletions services/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func (stks *StakingService) Init(db *sql.DB) {
stks.DB = db
}

type totalSupply struct {
Amount types.Coin
}

func (ss *StakingService) GetInfo(cfg config.Config, denom string, rd *types.RESTData) {
var sp types.StakingPool

Expand All @@ -40,12 +36,12 @@ func (ss *StakingService) GetInfo(cfg config.Config, denom string, rd *types.RES
zap.L().Info("", zap.Bool("Success", true), zap.String("Bonded tokens", sp.Pool.Bonded_tokens))
}

sp.Pool.Total_supply = getTotalSupply(cfg, denom, zap.L())
sp.Pool.Total_supply = ss.getTotalSupply(cfg, denom, zap.L())
rd.StakingPool = sp
}

func getTotalSupply(cfg config.Config, denom string, log *zap.Logger) float64 {
var ts totalSupply
func (ss *StakingService) getTotalSupply(cfg config.Config, denom string, log *zap.Logger) float64 {
var ts types.Supply

route := rest.GetSupplyRoute(cfg)
res, err := utils.HttpQuery(constants.RESTAddr + route + denom)
Expand Down
76 changes: 76 additions & 0 deletions services/staking_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package services_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/jim380/Cendermint/config"
"github.com/jim380/Cendermint/constants"
"github.com/jim380/Cendermint/services"
"github.com/jim380/Cendermint/types"
"github.com/jim380/Cendermint/utils"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

type MockStakingService struct {
services.StakingService
MockGetTotalSupply func(cfg config.Config, denom string, log *zap.Logger) float64
}

func TestStakingService_GetInfo(t *testing.T) {
stakingPoolData, err := os.ReadFile("../testutil/json/staking_pool.json")
require.NoError(t, err)

coinSupplyData, err := os.ReadFile("../testutil/json/coin_supply.json")
require.NoError(t, err)

stakingPoolServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(stakingPoolData)
}))
defer stakingPoolServer.Close()

coinSupplyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(coinSupplyData)
}))
defer coinSupplyServer.Close()

originalRESTAddr := constants.RESTAddr
constants.RESTAddr = stakingPoolServer.URL
defer func() { constants.RESTAddr = originalRESTAddr }()

mss := &MockStakingService{
MockGetTotalSupply: func(cfg config.Config, denom string, log *zap.Logger) float64 {
res, err := utils.HttpQuery(coinSupplyServer.URL + "/supply/" + denom)
if err != nil {
log.Fatal("", zap.Bool("Success", false), zap.String("err", err.Error()))
}
var ts types.Supply
json.Unmarshal(res, &ts)
return utils.StringToFloat64(ts.Amount.Amount)
},
}

cfg := config.Config{}
rd := &types.RESTData{}

mss.GetInfo(cfg, "uatom", rd)

var expectedStakingPool types.StakingPool
err = json.Unmarshal(stakingPoolData, &expectedStakingPool)
require.NoError(t, err)

var expectedCoinSupply types.Supply
err = json.Unmarshal(coinSupplyData, &expectedCoinSupply)
require.NoError(t, err)

require.NotNil(t, rd.StakingPool)
require.Equal(t, expectedStakingPool.Pool.Bonded_tokens, rd.StakingPool.Pool.Bonded_tokens)
require.Equal(t, expectedStakingPool.Pool.Not_bonded_tokens, rd.StakingPool.Pool.Not_bonded_tokens)
require.Equal(t, utils.StringToFloat64(expectedCoinSupply.Amount.Amount), rd.StakingPool.Pool.Total_supply)
}
45 changes: 45 additions & 0 deletions services/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package services_test

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/jim380/Cendermint/config"
"github.com/jim380/Cendermint/constants"
"github.com/jim380/Cendermint/services"
"github.com/jim380/Cendermint/types"
"github.com/stretchr/testify/require"
)

func TestUpgradeService_GetInfo(t *testing.T) {
mockData, err := os.ReadFile("../testutil/json/upgrade.json")
require.NoError(t, err)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(mockData)
}))
defer server.Close()

originalRESTAddr := constants.RESTAddr
constants.RESTAddr = server.URL
defer func() { constants.RESTAddr = originalRESTAddr }()

us := &services.UpgradeService{}

cfg := config.Config{}
rd := &types.RESTData{}

us.GetInfo(cfg, rd)

var expectedData types.UpgradeInfo
err = json.Unmarshal(mockData, &expectedData)
require.NoError(t, err)

require.NotNil(t, rd.UpgradeInfo)
require.Equal(t, expectedData.Plan, rd.UpgradeInfo.Plan)
require.Equal(t, expectedData.Planned, rd.UpgradeInfo.Planned)
}
5 changes: 5 additions & 0 deletions testutil/json/coin_supply.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"supply": {
"amount": "401367772477254"
}
}
6 changes: 6 additions & 0 deletions testutil/json/staking_pool.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pool": {
"not_bonded_tokens": "8117333426566",
"bonded_tokens": "250944239739347"
}
}
3 changes: 3 additions & 0 deletions testutil/json/upgrade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plan": null
}
5 changes: 5 additions & 0 deletions types/coin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

type Supply struct {
Amount Coin
}

0 comments on commit 2c9fff8

Please sign in to comment.