nic-chen commented on a change in pull request #859:
URL: https://github.com/apache/apisix-dashboard/pull/859#discussion_r530143277
##########
File path: api/internal/handler/consumer/consumer_test.go
##########
@@ -30,6 +34,53 @@ import (
"github.com/apisix/manager-api/internal/core/store"
)
+func TestHandler_Get(t *testing.T) {
+ tests := []struct {
+ caseDesc string
+ giveInput *GetInput
+ giveRet interface{}
+ giveErr error
+ wantErr error
+ wantGetKey string
+ wantRet interface{}
+ }{
+ {
+ caseDesc: "normal",
+ giveInput: &GetInput{Username: "test"},
+ wantGetKey: "test",
+ giveRet: "hello",
+ wantRet: "hello",
+ },
+ {
+ caseDesc: "failed",
+ giveInput: &GetInput{Username: "failed key"},
+ wantGetKey: "failed key",
+ giveErr: fmt.Errorf("get failed"),
+ wantErr: fmt.Errorf("get failed"),
+ wantRet: &data.SpecCodeResponse{
+ StatusCode: http.StatusInternalServerError,
+ },
+ },
+ }
+
+ for _, tc := range tests {
+ getCalled := true
+ mStore := &store.MockInterface{}
+ mStore.On("Get", mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ assert.Equal(t, tc.wantGetKey, args.Get(0), tc.caseDesc)
+ }).Return(tc.giveRet, tc.giveErr)
+
+ h := Handler{consumerStore: mStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(tc.giveInput)
+ ret, err := h.Get(ctx)
+ assert.True(t, getCalled, tc.caseDesc)
+ assert.Equal(t, tc.wantRet, ret, tc.caseDesc)
+ assert.Equal(t, tc.wantErr, err, tc.caseDesc)
+ }
+}
+
Review comment:
Could you please add test cases for other api, too ? This can be used as
a template for other handlers. Thank you very much.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]