github.com/fastwego/offiaccount@v1.0.1/apis/store/store_test.go (about)

     1  // Copyright 2020 FastWeGo
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package store
    16  
    17  import (
    18  	"net/http"
    19  	"os"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/fastwego/offiaccount"
    24  	"github.com/fastwego/offiaccount/test"
    25  )
    26  
    27  func TestMain(m *testing.M) {
    28  	test.Setup()
    29  	os.Exit(m.Run())
    30  }
    31  
    32  func TestUploadImg(t *testing.T) {
    33  	mockResp := map[string][]byte{
    34  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
    35  	}
    36  	var resp []byte
    37  	test.MockSvrHandler.HandleFunc(apiUploadImg, func(w http.ResponseWriter, r *http.Request) {
    38  		w.Write([]byte(resp))
    39  	})
    40  
    41  	type args struct {
    42  		ctx     *offiaccount.OffiAccount
    43  		payload []byte
    44  	}
    45  	tests := []struct {
    46  		name     string
    47  		args     args
    48  		wantResp []byte
    49  		wantErr  bool
    50  	}{
    51  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			resp = mockResp[tt.name]
    56  			gotResp, err := UploadImg(tt.args.ctx, tt.args.payload)
    57  			//fmt.Println(string(gotResp), err)
    58  			if (err != nil) != tt.wantErr {
    59  				t.Errorf("UploadImg() error = %v, wantErr %v", err, tt.wantErr)
    60  				return
    61  			}
    62  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
    63  				t.Errorf("UploadImg() gotResp = %v, want %v", gotResp, tt.wantResp)
    64  			}
    65  		})
    66  	}
    67  }