-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the logic of Challenge query
- Loading branch information
Showing
16 changed files
with
459 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/Finschia/finschia-sdk/types" | ||
"github.com/Finschia/finschia-sdk/x/or/settlement/types" | ||
) | ||
|
||
func (k Keeper) SetChallenge(ctx sdk.Context, challengeID int64, challenge types.Challenge) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := challengeKey(challengeID) | ||
|
||
bz, err := challenge.Marshal() | ||
if err != nil { | ||
panic(err) | ||
} | ||
store.Set(key, bz) | ||
} | ||
|
||
func (k Keeper) GetChallenge(ctx sdk.Context, challengeID int64) (*types.Challenge, error) { | ||
store := ctx.KVStore(k.storeKey) | ||
key := challengeKey(challengeID) | ||
bz := store.Get(key) | ||
if bz == nil { | ||
return nil, types.ErrChallengeNotExist.Wrapf("no challenge for %d", challengeID) | ||
} | ||
|
||
var challenge types.Challenge | ||
k.cdc.MustUnmarshal(bz, &challenge) | ||
|
||
return &challenge, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"github.com/Finschia/finschia-sdk/x/or/settlement/types" | ||
) | ||
|
||
func (s *KeeperTestSuite) TestGetChallenge() { | ||
testCases := map[string]struct { | ||
challengeID int64 | ||
expected *types.Challenge | ||
err error | ||
}{ | ||
"valid request": { | ||
challengeID: 1, | ||
expected: &types.Challenge{}, | ||
}, | ||
"not found (not existing challenge id)": { | ||
challengeID: 2, | ||
err: types.ErrChallengeNotExist, | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
s.Run(name, func() { | ||
if tc.expected != nil { | ||
s.app.SettlementKeeper.SetChallenge(s.ctx, tc.challengeID, *tc.expected) | ||
} | ||
challenge, err := s.app.SettlementKeeper.GetChallenge(s.ctx, tc.challengeID) | ||
s.Require().ErrorIs(err, tc.err) | ||
if tc.err != nil { | ||
return | ||
} | ||
s.Require().Equal(tc.expected, challenge) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
sdk "github.com/Finschia/finschia-sdk/types" | ||
"github.com/Finschia/finschia-sdk/x/or/settlement/types" | ||
) | ||
|
||
func (s *KeeperTestSuite) TestChallenge() { | ||
ctx := sdk.WrapSDKContext(s.ctx) | ||
|
||
testCases := map[string]struct { | ||
req *types.QueryChallengeRequest | ||
expectedRes *types.QueryChallengeResponse | ||
err error | ||
}{ | ||
"valid request": { | ||
req: &types.QueryChallengeRequest{ChallengeId: 1}, | ||
expectedRes: &types.QueryChallengeResponse{Challenge: &types.Challenge{}}, | ||
}, | ||
"nil request": { | ||
err: status.Error(codes.InvalidArgument, "empty request"), | ||
}, | ||
"non-existent challenge id": { | ||
req: &types.QueryChallengeRequest{ChallengeId: 2}, | ||
err: status.Error(codes.NotFound, types.ErrChallengeNotExist.Wrapf("no challenge for %d", 2).Error()), | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
s.Run(name, func() { | ||
if tc.expectedRes != nil { | ||
s.app.SettlementKeeper.SetChallenge(s.ctx, tc.req.ChallengeId, *tc.expectedRes.Challenge) | ||
} | ||
res, err := s.app.SettlementKeeper.Challenge(ctx, tc.req) | ||
if tc.err != nil { | ||
s.Require().EqualError(tc.err, err.Error()) | ||
return | ||
} | ||
s.Require().NoError(err) | ||
s.Require().Equal(tc.expectedRes, res) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
"github.com/Finschia/finschia-sdk/baseapp" | ||
"github.com/Finschia/finschia-sdk/simapp" | ||
sdk "github.com/Finschia/finschia-sdk/types" | ||
"github.com/Finschia/finschia-sdk/x/or/settlement/types" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
suite.Suite | ||
|
||
app *simapp.SimApp | ||
ctx sdk.Context | ||
|
||
queryClient types.QueryClient | ||
} | ||
|
||
func (s *KeeperTestSuite) SetupTest() { | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, tmproto.Header{}) | ||
app.SettlementKeeper.SetParams(ctx, types.DefaultParams()) | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, app.InterfaceRegistry()) | ||
types.RegisterQueryServer(queryHelper, app.SettlementKeeper) | ||
queryClient := types.NewQueryClient(queryHelper) | ||
|
||
s.app = app | ||
s.ctx = ctx | ||
s.queryClient = queryClient | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} |
Oops, something went wrong.