Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(profile): normalize test cases format #866

Merged
merged 7 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x/profile/client/cli/query_coordinator_by_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func (suite *QueryTestSuite) TestShowCoordinatorByAddress() {
obj types.CoordinatorByAddress
}{
{
desc: "found",
desc: "should show an existing coordinator from address",
id: objs[0].Address,
args: common,
obj: objs[0],
},
{
desc: "not found",
desc: "should send error for a non existing coordinator",
id: "not_found",
args: common,
err: status.Error(codes.NotFound, "not found"),
Expand Down
10 changes: 5 additions & 5 deletions x/profile/client/cli/query_coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func (suite *QueryTestSuite) TestShowCoordinator() {
obj types.Coordinator
}{
{
desc: "found",
desc: "should show an existing coordinator",
id: fmt.Sprintf("%d", objs[0].CoordinatorID),
args: common,
obj: objs[0],
},
{
desc: "not found",
desc: "should send error for a non existing coordinator",
id: "not_found",
args: common,
err: status.Error(codes.NotFound, "not found"),
Expand Down Expand Up @@ -80,7 +80,7 @@ func (suite *QueryTestSuite) TestListCoordinator() {
}
return args
}
suite.T().Run("ByOffset", func(t *testing.T) {
suite.T().Run("should allow listing coordinators by offset", func(t *testing.T) {
step := 2
for i := 0; i < len(objs); i += step {
args := request(nil, uint64(i), uint64(step), false)
Expand All @@ -92,7 +92,7 @@ func (suite *QueryTestSuite) TestListCoordinator() {
require.Subset(t, objs, resp.Coordinator)
}
})
suite.T().Run("ByKey", func(t *testing.T) {
suite.T().Run("should allow listing coordinators by key", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(objs); i += step {
Expand All @@ -106,7 +106,7 @@ func (suite *QueryTestSuite) TestListCoordinator() {
next = resp.Pagination.NextKey
}
})
suite.T().Run("Total", func(t *testing.T) {
suite.T().Run("should allow listing all coordinators", func(t *testing.T) {
args := request(nil, 0, uint64(len(objs)), true)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListCoordinator(), args)
require.NoError(t, err)
Expand Down
10 changes: 5 additions & 5 deletions x/profile/client/cli/query_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func (suite *QueryTestSuite) TestShowValidator() {
obj types.Validator
}{
{
desc: "found",
desc: "should show an existing validator",
idAddress: objs[0].Address,

args: common,
obj: objs[0],
},
{
desc: "not found",
desc: "should send error for a non existing validator",
idAddress: strconv.Itoa(100000),

args: common,
Expand Down Expand Up @@ -88,7 +88,7 @@ func (suite *QueryTestSuite) TestListValidator() {
}
return args
}
suite.T().Run("ByOffset", func(t *testing.T) {
suite.T().Run("should allow listing validators by offset", func(t *testing.T) {
step := 2
for i := 0; i < len(objs); i += step {
args := request(nil, uint64(i), uint64(step), false)
Expand All @@ -100,7 +100,7 @@ func (suite *QueryTestSuite) TestListValidator() {
require.Subset(t, nullify.Fill(objs), nullify.Fill(resp.Validator))
}
})
suite.T().Run("ByKey", func(t *testing.T) {
suite.T().Run("should allow listing validators by key", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(objs); i += step {
Expand All @@ -114,7 +114,7 @@ func (suite *QueryTestSuite) TestListValidator() {
next = resp.Pagination.NextKey
}
})
suite.T().Run("Total", func(t *testing.T) {
suite.T().Run("should allow listing all validators", func(t *testing.T) {
args := request(nil, 0, uint64(len(objs)), true)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListValidator(), args)
require.NoError(t, err)
Expand Down
22 changes: 12 additions & 10 deletions x/profile/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ func TestGenesis(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
r := sample.Rand()

genesisState := sample.ProfileGenesisState(r)
profile.InitGenesis(ctx, *tk.ProfileKeeper, genesisState)
got := profile.ExportGenesis(ctx, *tk.ProfileKeeper)

// Compare lists
require.ElementsMatch(t, genesisState.ValidatorList, got.ValidatorList)
require.ElementsMatch(t, genesisState.ValidatorByOperatorAddressList, got.ValidatorByOperatorAddressList)
require.ElementsMatch(t, genesisState.CoordinatorList, got.CoordinatorList)
require.ElementsMatch(t, genesisState.CoordinatorByAddressList, got.CoordinatorByAddressList)
require.Equal(t, genesisState.CoordinatorCounter, got.CoordinatorCounter)
t.Run("should allow import and export the genesis state", func(t *testing.T) {
genesisState := sample.ProfileGenesisState(r)
profile.InitGenesis(ctx, *tk.ProfileKeeper, genesisState)
got := profile.ExportGenesis(ctx, *tk.ProfileKeeper)

// Compare lists
require.ElementsMatch(t, genesisState.ValidatorList, got.ValidatorList)
require.ElementsMatch(t, genesisState.ValidatorByOperatorAddressList, got.ValidatorByOperatorAddressList)
require.ElementsMatch(t, genesisState.CoordinatorList, got.CoordinatorList)
require.ElementsMatch(t, genesisState.CoordinatorByAddressList, got.CoordinatorByAddressList)
require.Equal(t, genesisState.CoordinatorCounter, got.CoordinatorCounter)
})

// this line is used by starport scaffolding # genesis/test/assert
}
82 changes: 52 additions & 30 deletions x/profile/keeper/coordinator_by_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,49 @@ func createNCoordinatorBoth(keeper *keeper.Keeper, ctx sdk.Context, n int) ([]ty
func TestCoordinatorByAddressGet(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items, _ := createNCoordinatorBoth(tk.ProfileKeeper, ctx, 10)
for _, item := range items {
rst, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, item.Address)
require.NoError(t, err)
require.Equal(t, item, rst)
}

t.Run("should allow getting coordinator by address", func(t *testing.T) {
for _, item := range items {
rst, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, item.Address)
require.NoError(t, err)
require.Equal(t, item, rst)
}
})
}

func TestCoordinatorByAddressInvalid(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items := createNCoordinatorByAddress(tk.ProfileKeeper, ctx, 10)
for _, item := range items {
_, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, item.Address)
require.ErrorIs(t, err, spnerrors.ErrCritical)
}

t.Run("should prevent finding coordinator by address if no associated coordinator exist", func(t *testing.T) {
for _, item := range items {
_, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, item.Address)
require.ErrorIs(t, err, spnerrors.ErrCritical)
}
})
}

func TestCoordinatorByAddressRemove(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items := createNCoordinatorByAddress(tk.ProfileKeeper, ctx, 10)
for _, item := range items {
tk.ProfileKeeper.RemoveCoordinatorByAddress(ctx, item.Address)
_, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, item.Address)
require.ErrorIs(t, err, types.ErrCoordAddressNotFound)
}

t.Run("should allow removing coordinator by address", func(t *testing.T) {
for _, item := range items {
tk.ProfileKeeper.RemoveCoordinatorByAddress(ctx, item.Address)
_, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, item.Address)
require.ErrorIs(t, err, types.ErrCoordAddressNotFound)
}
})
}

func TestCoordinatorByAddressGetAll(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items := createNCoordinatorByAddress(tk.ProfileKeeper, ctx, 10)
require.ElementsMatch(t, items, tk.ProfileKeeper.GetAllCoordinatorByAddress(ctx))

t.Run("should allow getting all coordinator by address", func(t *testing.T) {
require.ElementsMatch(t, items, tk.ProfileKeeper.GetAllCoordinatorByAddress(ctx))

})
}

func TestCoordinatorIDFromAddress(t *testing.T) {
Expand All @@ -85,12 +98,16 @@ func TestCoordinatorIDFromAddress(t *testing.T) {
Active: true,
})

id, err := tk.ProfileKeeper.CoordinatorIDFromAddress(ctx, address)
require.NoError(t, err)
require.Equal(t, uint64(10), id)
t.Run("should allow retrieving coordinator ID from address", func(t *testing.T) {
id, err := tk.ProfileKeeper.CoordinatorIDFromAddress(ctx, address)
require.NoError(t, err)
require.Equal(t, uint64(10), id)
})

_, err = tk.ProfileKeeper.CoordinatorIDFromAddress(ctx, sample.Address(r))
require.ErrorIs(t, err, types.ErrCoordAddressNotFound)
t.Run("should prevent retrieving coordinator ID if address doesn't exist", func(t *testing.T) {
_, err := tk.ProfileKeeper.CoordinatorIDFromAddress(ctx, sample.Address(r))
require.ErrorIs(t, err, types.ErrCoordAddressNotFound)
})
}

func TestActiveCoordinatorByAddressGet(t *testing.T) {
Expand All @@ -108,10 +125,12 @@ func TestActiveCoordinatorByAddressGet(t *testing.T) {
Active: true,
})

rst, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, address)
require.NoError(t, err)
require.Equal(t, uint64(10), rst.CoordinatorID)
require.Equal(t, address, rst.Address)
t.Run("should allow getting active validator by address", func(t *testing.T) {
rst, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, address)
require.NoError(t, err)
require.Equal(t, uint64(10), rst.CoordinatorID)
require.Equal(t, address, rst.Address)
})

// set invalid critical error state
tk.ProfileKeeper.SetCoordinator(ctx, types.Coordinator{
Expand All @@ -120,11 +139,14 @@ func TestActiveCoordinatorByAddressGet(t *testing.T) {
Active: false,
})

rst, err = tk.ProfileKeeper.GetCoordinatorByAddress(ctx, address)
require.ErrorIs(t, err, spnerrors.ErrCritical)
t.Run("should prevent getting inactive validator by address", func(t *testing.T) {
_, err := tk.ProfileKeeper.GetCoordinatorByAddress(ctx, address)
require.ErrorIs(t, err, spnerrors.ErrCritical)

// set valid state where coordinator is disabled
tk.ProfileKeeper.RemoveCoordinatorByAddress(ctx, address)
_, err = tk.ProfileKeeper.GetCoordinatorByAddress(ctx, address)
require.ErrorIs(t, err, types.ErrCoordAddressNotFound)
})

// set valid state where coordinator is disabled
tk.ProfileKeeper.RemoveCoordinatorByAddress(ctx, address)
rst, err = tk.ProfileKeeper.GetCoordinatorByAddress(ctx, address)
require.ErrorIs(t, err, types.ErrCoordAddressNotFound)
}
23 changes: 16 additions & 7 deletions x/profile/keeper/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,31 @@ func createNCoordinator(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.C
func TestCoordinatorGet(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items := createNCoordinator(tk.ProfileKeeper, ctx, 10)
for _, item := range items {
coord, found := tk.ProfileKeeper.GetCoordinator(ctx, item.CoordinatorID)
require.True(t, found)
require.Equal(t, item, coord)
}

t.Run("should allow getting coordinator", func(t *testing.T) {
for _, item := range items {
coord, found := tk.ProfileKeeper.GetCoordinator(ctx, item.CoordinatorID)
require.True(t, found)
require.Equal(t, item, coord)
}
})
}

func TestCoordinatorGetAll(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items := createNCoordinator(tk.ProfileKeeper, ctx, 10)
require.ElementsMatch(t, items, tk.ProfileKeeper.GetAllCoordinator(ctx))

t.Run("should allow getting all coordinators", func(t *testing.T) {
require.ElementsMatch(t, items, tk.ProfileKeeper.GetAllCoordinator(ctx))
})
}

func TestCoordinatorCounter(t *testing.T) {
ctx, tk, _ := testkeeper.NewTestSetup(t)
items := createNCoordinator(tk.ProfileKeeper, ctx, 10)
counter := uint64(len(items))
require.Equal(t, counter, tk.ProfileKeeper.GetCoordinatorCounter(ctx))

t.Run("should allow getting coordinator counter", func(t *testing.T) {
require.Equal(t, counter, tk.ProfileKeeper.GetCoordinatorCounter(ctx))
})
}
8 changes: 4 additions & 4 deletions x/profile/keeper/grpc_coordinator_by_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ func TestCoordinatorByAddressQuerySingle(t *testing.T) {
err error
}{
{
desc: "First",
desc: "should allow querying first coordinator by address",
request: &types.QueryGetCoordinatorByAddressRequest{Address: msgs[0].Address},
response: &types.QueryGetCoordinatorByAddressResponse{CoordinatorByAddress: msgs[0]},
},
{
desc: "Second",
desc: "should allow querying second coordinator by address",
request: &types.QueryGetCoordinatorByAddressRequest{Address: msgs[1].Address},
response: &types.QueryGetCoordinatorByAddressResponse{CoordinatorByAddress: msgs[1]},
},
{
desc: "KeyNotFound",
desc: "should prevent querying non existing coordinator by address",
request: &types.QueryGetCoordinatorByAddressRequest{Address: "missing"},
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "InvalidRequest",
desc: "should prevent querying with invalid request",
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
Expand Down
16 changes: 8 additions & 8 deletions x/profile/keeper/grpc_coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ func TestCoordinatorQuerySingle(t *testing.T) {
err error
}{
{
desc: "First",
desc: "should allow querying first coordinator",
request: &types.QueryGetCoordinatorRequest{CoordinatorID: msgs[0].CoordinatorID},
response: &types.QueryGetCoordinatorResponse{Coordinator: msgs[0]},
},
{
desc: "Second",
desc: "should allow querying second coordinator",
request: &types.QueryGetCoordinatorRequest{CoordinatorID: msgs[1].CoordinatorID},
response: &types.QueryGetCoordinatorResponse{Coordinator: msgs[1]},
},
{
desc: "coordinator not found",
desc: "should prevent querying non existing coordinator",
request: &types.QueryGetCoordinatorRequest{CoordinatorID: uint64(len(msgs))},
err: status.Error(codes.NotFound, "not found"),
},
{
desc: "Invalid request",
desc: "should prevent querying with invalid request",
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestCoordinatorQueryPaginated(t *testing.T) {
},
}
}
t.Run("ByOffset", func(t *testing.T) {
t.Run("should allow querying coordinators by offsets", func(t *testing.T) {
step := 2
for i := 0; i < len(msgs); i += step {
resp, err := tk.ProfileKeeper.CoordinatorAll(wctx, request(nil, uint64(i), uint64(step), false))
Expand All @@ -78,7 +78,7 @@ func TestCoordinatorQueryPaginated(t *testing.T) {
require.Subset(t, msgs, resp.Coordinator)
}
})
t.Run("ByKey", func(t *testing.T) {
t.Run("should allow querying coordinators by key", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(msgs); i += step {
Expand All @@ -89,13 +89,13 @@ func TestCoordinatorQueryPaginated(t *testing.T) {
next = resp.Pagination.NextKey
}
})
t.Run("Total", func(t *testing.T) {
t.Run("should allow querying all coordinators", func(t *testing.T) {
resp, err := tk.ProfileKeeper.CoordinatorAll(wctx, request(nil, 0, 0, true))
require.NoError(t, err)
require.Equal(t, len(msgs), int(resp.Pagination.Total))
require.ElementsMatch(t, msgs, resp.Coordinator)
})
t.Run("InvalidRequest", func(t *testing.T) {
t.Run("should prevent querying with invalid request", func(t *testing.T) {
_, err := tk.ProfileKeeper.CoordinatorAll(wctx, nil)
require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request"))
})
Expand Down
Loading