You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/01 08:56:09 UTC

[GitHub] [apisix-dashboard] ShiningRush commented on a change in pull request #859: feat: add a unit test for consumer and remove implicit init

ShiningRush commented on a change in pull request #859:
URL: https://github.com/apache/apisix-dashboard/pull/859#discussion_r533173590



##########
File path: api/internal/handler/consumer/consumer_test.go
##########
@@ -18,230 +18,381 @@
 package consumer
 
 import (
-	"encoding/json"
-	"testing"
-	"time"
-
-	"github.com/shiningrush/droplet"
-	"github.com/stretchr/testify/assert"
-
+	"context"
+	"fmt"
 	"github.com/apisix/manager-api/internal/core/entity"
-	"github.com/apisix/manager-api/internal/core/storage"
 	"github.com/apisix/manager-api/internal/core/store"
+	"github.com/shiningrush/droplet"
+	"github.com/shiningrush/droplet/data"
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/mock"
+	"net/http"
+	"testing"
 )
 
-func TestConsumer(t *testing.T) {
-	// init
-	err := storage.InitETCDClient([]string{"127.0.0.1:2379"})
-	assert.Nil(t, err)
-	err = store.InitStores()
-	assert.Nil(t, err)
+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,

Review comment:
       Return values is correct according
   ```go
   func SpecCodeResponse(err error) *data.SpecCodeResponse {
   	errMsg := err.Error()
   	if strings.Contains(errMsg, "required") ||
   		strings.Contains(errMsg, "conflicted") ||
   		strings.Contains(errMsg, "invalid") ||
   		strings.Contains(errMsg, "missing") ||
   		strings.Contains(errMsg, "schema validate failed") {
   		return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}
   	}
   
   	if strings.Contains(errMsg, "not found") {
   		return &data.SpecCodeResponse{StatusCode: http.StatusNotFound}
   	}
   
   	return &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError}
   }
   ```
   
   This is unit test, we can not break old logic.




----------------------------------------------------------------
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:
users@infra.apache.org