Skip to content

Commit

Permalink
feat: add a hanlder unit test for upstream and remove init
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiningRush committed Nov 20, 2020
1 parent f2ff23a commit 8a1da59
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type Config struct {
Authentication Authentication
}

func init() {
func InitConf() {
//go test
if workDir := os.Getenv("APISIX_API_WORKDIR"); workDir != "" {
WorkDir = workDir
Expand Down
35 changes: 35 additions & 0 deletions api/internal/core/store/store_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package store

import (
"context"
"github.com/stretchr/testify/mock"
)

type MockInterface struct {
mock.Mock
}

func (m *MockInterface) Get(key string) (interface{}, error) {
ret := m.Mock.Called(key)
return ret.Get(0), ret.Error(1)
}

func (m *MockInterface) List(input ListInput) (*ListOutput, error) {
ret := m.Mock.Called(input)
return ret.Get(0).(*ListOutput), ret.Error(1)
}

func (m *MockInterface) Create(ctx context.Context, obj interface{}) error {
ret := m.Mock.Called(ctx, obj)
return ret.Error(0)
}

func (m *MockInterface) Update(ctx context.Context, obj interface{}, createOnFail bool) error {
ret := m.Mock.Called(ctx, obj, createOnFail)
return ret.Error(0)
}

func (m *MockInterface) BatchDelete(ctx context.Context, keys []string) error {
ret := m.Mock.Called(ctx, keys)
return ret.Error(0)
}
51 changes: 51 additions & 0 deletions api/internal/handler/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ package consumer

import (
"encoding/json"
"fmt"
"github.com/shiningrush/droplet/data"
"github.com/stretchr/testify/mock"
"net/http"
"testing"
"time"

Expand All @@ -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)
}
}

func TestConsumer(t *testing.T) {
// init
err := storage.InitETCDClient([]string{"127.0.0.1:2379"})
Expand Down
2 changes: 1 addition & 1 deletion api/log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

var logger *zap.SugaredLogger

func init() {
func InitLog() {
writeSyncer := fileWriter()
encoder := getEncoder()
logLevel := getLogLevel()
Expand Down
3 changes: 2 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import (
)

func main() {

conf.InitConf()
log.InitLog()
if err := storage.InitETCDClient(conf.ETCDEndpoints); err != nil {
log.Error("init etcd client fail: %w", err)
panic(err)
Expand Down

0 comments on commit 8a1da59

Please sign in to comment.