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

feat: add a unit test for consumer and remove implicit init #859

Merged
merged 16 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 4 additions & 0 deletions api/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ type Config struct {
Authentication Authentication
}

// TODO: it is just for integration tests, we should call "InitLog" explicitly when remove all handler's integration tests
func init() {
InitConf()
}
func InitConf() {
//go test
if workDir := os.Getenv("APISIX_API_WORKDIR"); workDir != "" {
WorkDir = workDir
Expand Down
51 changes: 51 additions & 0 deletions api/internal/core/store/store_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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,
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
},
},
}

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)
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add test cases for other api, too ? This can be used as a template for other handlers. Thank you very much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will do it this week

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

var logger *zap.SugaredLogger

// TODO: it is just for integration tests, we should call "InitLog" explicitly when remove all handler's integration tests
func init() {
InitLog()
}
func InitLog() {
writeSyncer := fileWriter()
encoder := getEncoder()
logLevel := getLogLevel()
Expand Down
1 change: 0 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
)

func main() {

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