Skip to content

Commit

Permalink
api/admin: Cleanup SuccessResponseTests (#2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Jan 22, 2024
1 parent e65fac7 commit 1753393
Showing 1 changed file with 53 additions and 68 deletions.
121 changes: 53 additions & 68 deletions api/admin/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,23 @@ import (
"github.com/ava-labs/avalanchego/utils/rpc"
)

var errTest = errors.New("non-nil error")
var (
errTest = errors.New("non-nil error")

// SuccessResponseTest defines the expected result of an API call that returns SuccessResponse
type SuccessResponseTest struct {
Err error
}

// GetSuccessResponseTests returns a list of possible SuccessResponseTests
func GetSuccessResponseTests() []SuccessResponseTest {
return []SuccessResponseTest{
SuccessResponseTests = []struct {
name string
expectedErr error
}{
{
Err: nil,
name: "no error",
expectedErr: nil,
},
{
Err: errTest,
name: "error",
expectedErr: errTest,
},
}
}
)

type mockClient struct {
response interface{}
Expand Down Expand Up @@ -76,74 +75,62 @@ func (mc *mockClient) SendRequest(_ context.Context, _ string, _ interface{}, re
}

func TestStartCPUProfiler(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.StartCPUProfiler(context.Background())
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.StartCPUProfiler(context.Background())
require.ErrorIs(t, err, test.expectedErr)
})
}
}

func TestStopCPUProfiler(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.StopCPUProfiler(context.Background())
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.StopCPUProfiler(context.Background())
require.ErrorIs(t, err, test.expectedErr)
})
}
}

func TestMemoryProfile(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.MemoryProfile(context.Background())
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.MemoryProfile(context.Background())
require.ErrorIs(t, err, test.expectedErr)
})
}
}

func TestLockProfile(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.LockProfile(context.Background())
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.LockProfile(context.Background())
require.ErrorIs(t, err, test.expectedErr)
})
}
}

func TestAlias(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.Alias(context.Background(), "alias", "alias2")
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.Alias(context.Background(), "alias", "alias2")
require.ErrorIs(t, err, test.expectedErr)
})
}
}

func TestAliasChain(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.AliasChain(context.Background(), "chain", "chain-alias")
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.AliasChain(context.Background(), "chain", "chain-alias")
require.ErrorIs(t, err, test.expectedErr)
})
}
}

Expand All @@ -169,14 +156,12 @@ func TestGetChainAliases(t *testing.T) {
}

func TestStacktrace(t *testing.T) {
require := require.New(t)

tests := GetSuccessResponseTests()

for _, test := range tests {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.Err)}
err := mockClient.Stacktrace(context.Background())
require.ErrorIs(err, test.Err)
for _, test := range SuccessResponseTests {
t.Run(test.name, func(t *testing.T) {
mockClient := client{requester: NewMockClient(&api.EmptyReply{}, test.expectedErr)}
err := mockClient.Stacktrace(context.Background())
require.ErrorIs(t, err, test.expectedErr)
})
}
}

Expand Down

0 comments on commit 1753393

Please sign in to comment.