Skip to content

Commit

Permalink
Make testing suite to ensure queries never alter state (osmosis-labs#…
Browse files Browse the repository at this point in the history
…3001)

* create func to check alter state

* add tests

* format

* Update app/apptesting/test_suite.go

Co-authored-by: Roman <roman@osmosis.team>

* remove fmt

* epochs: try to use Invoke

* gamm: use Invoke

* incentives: use Invoke

* lockup: use Invoke

* mint: use Invoke

* pool-incentives: use Invoke

* superfluid: use Invoke

* tokenfactory

* txfees

* format

* fix

Co-authored-by: Roman <roman@osmosis.team>
  • Loading branch information
2 people authored and Ruslan Akhtariev committed Nov 1, 2022
1 parent f431464 commit 91f5b18
Show file tree
Hide file tree
Showing 11 changed files with 918 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ func (s *KeeperTestHelper) BuildTx(
return txBuilder.GetTx()
}

// StateNotAltered validates that app state is not altered. Fails if it is.
func (s *KeeperTestHelper) StateNotAltered() {
oldState := s.App.ExportState(s.Ctx)
s.Commit()
newState := s.App.ExportState(s.Ctx)
s.Require().Equal(oldState, newState)
}

// CreateRandomAccounts is a function return a list of randomly generated AccAddresses
func CreateRandomAccounts(numAccts int) []sdk.AccAddress {
testAddrs := make([]sdk.AccAddress, numAccts)
Expand Down
4 changes: 4 additions & 0 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,7 @@ func (app *OsmosisApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddr
},
)
}

func (app *OsmosisApp) ExportState(ctx sdk.Context) map[string]json.RawMessage {
return app.mm.ExportGenesis(ctx, app.AppCodec())
}
73 changes: 73 additions & 0 deletions x/epochs/client/cli/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cli_test

import (
gocontext "context"
"testing"
"time"

"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v12/app/apptesting"
"github.com/osmosis-labs/osmosis/v12/x/epochs/types"
)

type QueryTestSuite struct {
apptesting.KeeperTestHelper
queryClient types.QueryClient
}

func (s *QueryTestSuite) SetupSuite() {
s.Setup()
s.queryClient = types.NewQueryClient(s.QueryHelper)

// add new epoch
epoch := types.EpochInfo{
Identifier: "weekly",
StartTime: time.Time{},
Duration: time.Hour,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
}
s.App.EpochsKeeper.AddEpochInfo(s.Ctx, epoch)

s.Commit()
}

func (s *QueryTestSuite) TestQueriesNeverAlterState() {
testCases := []struct {
name string
query string
input interface{}
output interface{}
}{
{
"Query current epoch",
"/osmosis.epochs.v1beta1.Query/CurrentEpoch",
&types.QueryCurrentEpochRequest{Identifier: "weekly"},
&types.QueryCurrentEpochResponse{},
},
{
"Query epochs info",
"/osmosis.epochs.v1beta1.Query/EpochInfos",
&types.QueryEpochsInfoRequest{},
&types.QueryEpochsInfoResponse{},
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
s.SetupSuite()
err := s.QueryHelper.Invoke(gocontext.Background(), tc.query, tc.input, tc.output)
s.Require().NoError(err)
s.StateNotAltered()
})
}
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
102 changes: 102 additions & 0 deletions x/gamm/client/cli/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cli_test

import (
gocontext "context"
"testing"

"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v12/app/apptesting"
"github.com/osmosis-labs/osmosis/v12/x/gamm/types"
)

type QueryTestSuite struct {
apptesting.KeeperTestHelper
queryClient types.QueryClient
}

func (s *QueryTestSuite) SetupSuite() {
s.Setup()
s.queryClient = types.NewQueryClient(s.QueryHelper)
// create a new pool
s.PrepareBalancerPool()
s.Commit()
}

func (s *QueryTestSuite) TestQueriesNeverAlterState() {
testCases := []struct {
name string
query string
input interface{}
output interface{}
}{
{
"Query pools",
"/osmosis.gamm.v1beta1.Query/Pools",
&types.QueryPoolsRequest{},
&types.QueryPoolsResponse{},
},
{
"Query single pool",
"/osmosis.gamm.v1beta1.Query/Pool",
&types.QueryPoolRequest{PoolId: 1},
&types.QueryPoolsResponse{},
},
{
"Query num pools",
"/osmosis.gamm.v1beta1.Query/NumPools",
&types.QueryNumPoolsRequest{},
&types.QueryNumPoolsResponse{},
},
{
"Query pool params",
"/osmosis.gamm.v1beta1.Query/PoolParams",
&types.QueryPoolParamsRequest{PoolId: 1},
&types.QueryPoolParamsResponse{},
},
{
"Query pool type",
"/osmosis.gamm.v1beta1.Query/PoolType",
&types.QueryPoolTypeRequest{PoolId: 1},
&types.QueryPoolTypeResponse{},
},
{
"Query spot price",
"/osmosis.gamm.v1beta1.Query/SpotPrice",
&types.QuerySpotPriceRequest{PoolId: 1, BaseAssetDenom: "foo", QuoteAssetDenom: "bar"},
&types.QuerySpotPriceResponse{},
},
{
"Query total liquidity",
"/osmosis.gamm.v1beta1.Query/TotalLiquidity",
&types.QueryTotalLiquidityRequest{},
&types.QueryTotalLiquidityResponse{},
},
{
"Query pool total liquidity",
"/osmosis.gamm.v1beta1.Query/TotalPoolLiquidity",
&types.QueryTotalPoolLiquidityRequest{PoolId: 1},
&types.QueryTotalPoolLiquidityResponse{},
},
{
"Query total shares",
"/osmosis.gamm.v1beta1.Query/TotalShares",
&types.QueryTotalSharesRequest{PoolId: 1},
&types.QueryTotalSharesResponse{},
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
s.SetupSuite()
err := s.QueryHelper.Invoke(gocontext.Background(), tc.query, tc.input, tc.output)
s.Require().NoError(err)
s.StateNotAltered()
})
}
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
110 changes: 110 additions & 0 deletions x/incentives/client/cli/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cli_test

import (
gocontext "context"
"testing"
"time"

"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v12/app/apptesting"
"github.com/osmosis-labs/osmosis/v12/x/incentives/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type QueryTestSuite struct {
apptesting.KeeperTestHelper
queryClient types.QueryClient
}

func (s *QueryTestSuite) SetupSuite() {
s.Setup()
s.queryClient = types.NewQueryClient(s.QueryHelper)

// create a pool
s.PrepareBalancerPool()
// set up lock with id = 1
s.LockTokens(s.TestAccs[0], sdk.Coins{sdk.NewCoin("gamm/pool/1", sdk.NewInt(1000000))}, time.Hour*24)

s.Commit()
}

func (s *QueryTestSuite) TestQueriesNeverAlterState() {
testCases := []struct {
name string
query string
input interface{}
output interface{}
}{
{
"Query active gauges",
"/osmosis.incentives.Query/ActiveGauges",
&types.ActiveGaugesRequest{},
&types.ActiveGaugesResponse{},
},
{
"Query active gauges per denom",
"/osmosis.incentives.Query/ActiveGaugesPerDenom",
&types.ActiveGaugesPerDenomRequest{Denom: "stake"},
&types.ActiveGaugesPerDenomResponse{},
},
{
"Query gauge by id",
"/osmosis.incentives.Query/GaugeByID",
&types.GaugeByIDRequest{Id: 1},
&types.GaugeByIDResponse{},
},
{
"Query all gauges",
"/osmosis.incentives.Query/Gauges",
&types.GaugesRequest{},
&types.GaugesResponse{},
},
{
"Query lockable durations",
"/osmosis.incentives.Query/LockableDurations",
&types.QueryLockableDurationsRequest{},
&types.QueryLockableDurationsResponse{},
},
{
"Query module to distibute coins",
"/osmosis.incentives.Query/ModuleToDistributeCoins",
&types.ModuleToDistributeCoinsRequest{},
&types.ModuleToDistributeCoinsResponse{},
},
{
"Query reward estimate",
"/osmosis.incentives.Query/RewardsEst",
&types.RewardsEstRequest{Owner: s.TestAccs[0].String()},
&types.RewardsEstResponse{},
},
{
"Query upcoming gauges",
"/osmosis.incentives.Query/UpcomingGauges",
&types.UpcomingGaugesRequest{},
&types.UpcomingGaugesResponse{},
},
{
"Query upcoming gauges",
"/osmosis.incentives.Query/UpcomingGaugesPerDenom",
&types.UpcomingGaugesPerDenomRequest{Denom: "stake"},
&types.UpcomingGaugesPerDenomResponse{},
},
}

for _, tc := range testCases {
tc := tc

s.Run(tc.name, func() {
s.SetupSuite()
err := s.QueryHelper.Invoke(gocontext.Background(), tc.query, tc.input, tc.output)
s.Require().NoError(err)
s.StateNotAltered()
})
}
}

func TestQueryTestSuite(t *testing.T) {
suite.Run(t, new(QueryTestSuite))
}
Loading

0 comments on commit 91f5b18

Please sign in to comment.