github.com/fastwego/offiaccount@v1.0.1/apis/store/shelf/shelf_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 shelf
    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 TestAdd(t *testing.T) {
    33  	mockResp := map[string][]byte{
    34  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
    35  	}
    36  	var resp []byte
    37  	test.MockSvrHandler.HandleFunc(apiAdd, 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 := Add(tt.args.ctx, tt.args.payload)
    57  			//fmt.Println(string(gotResp), err)
    58  			if (err != nil) != tt.wantErr {
    59  				t.Errorf("Add() error = %v, wantErr %v", err, tt.wantErr)
    60  				return
    61  			}
    62  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
    63  				t.Errorf("Add() gotResp = %v, want %v", gotResp, tt.wantResp)
    64  			}
    65  		})
    66  	}
    67  }
    68  func TestDel(t *testing.T) {
    69  	mockResp := map[string][]byte{
    70  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
    71  	}
    72  	var resp []byte
    73  	test.MockSvrHandler.HandleFunc(apiDel, func(w http.ResponseWriter, r *http.Request) {
    74  		w.Write([]byte(resp))
    75  	})
    76  
    77  	type args struct {
    78  		ctx     *offiaccount.OffiAccount
    79  		payload []byte
    80  	}
    81  	tests := []struct {
    82  		name     string
    83  		args     args
    84  		wantResp []byte
    85  		wantErr  bool
    86  	}{
    87  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			resp = mockResp[tt.name]
    92  			gotResp, err := Del(tt.args.ctx, tt.args.payload)
    93  			//fmt.Println(string(gotResp), err)
    94  			if (err != nil) != tt.wantErr {
    95  				t.Errorf("Del() error = %v, wantErr %v", err, tt.wantErr)
    96  				return
    97  			}
    98  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
    99  				t.Errorf("Del() gotResp = %v, want %v", gotResp, tt.wantResp)
   100  			}
   101  		})
   102  	}
   103  }
   104  func TestMod(t *testing.T) {
   105  	mockResp := map[string][]byte{
   106  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
   107  	}
   108  	var resp []byte
   109  	test.MockSvrHandler.HandleFunc(apiMod, func(w http.ResponseWriter, r *http.Request) {
   110  		w.Write([]byte(resp))
   111  	})
   112  
   113  	type args struct {
   114  		ctx     *offiaccount.OffiAccount
   115  		payload []byte
   116  	}
   117  	tests := []struct {
   118  		name     string
   119  		args     args
   120  		wantResp []byte
   121  		wantErr  bool
   122  	}{
   123  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
   124  	}
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127  			resp = mockResp[tt.name]
   128  			gotResp, err := Mod(tt.args.ctx, tt.args.payload)
   129  			//fmt.Println(string(gotResp), err)
   130  			if (err != nil) != tt.wantErr {
   131  				t.Errorf("Mod() error = %v, wantErr %v", err, tt.wantErr)
   132  				return
   133  			}
   134  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
   135  				t.Errorf("Mod() gotResp = %v, want %v", gotResp, tt.wantResp)
   136  			}
   137  		})
   138  	}
   139  }
   140  func TestGetAll(t *testing.T) {
   141  	mockResp := map[string][]byte{
   142  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
   143  	}
   144  	var resp []byte
   145  	test.MockSvrHandler.HandleFunc(apiGetAll, func(w http.ResponseWriter, r *http.Request) {
   146  		w.Write([]byte(resp))
   147  	})
   148  
   149  	type args struct {
   150  		ctx *offiaccount.OffiAccount
   151  	}
   152  	tests := []struct {
   153  		name     string
   154  		args     args
   155  		wantResp []byte
   156  		wantErr  bool
   157  	}{
   158  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
   159  	}
   160  	for _, tt := range tests {
   161  		t.Run(tt.name, func(t *testing.T) {
   162  			resp = mockResp[tt.name]
   163  			gotResp, err := GetAll(tt.args.ctx)
   164  			//fmt.Println(string(gotResp), err)
   165  			if (err != nil) != tt.wantErr {
   166  				t.Errorf("GetAll() error = %v, wantErr %v", err, tt.wantErr)
   167  				return
   168  			}
   169  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
   170  				t.Errorf("GetAll() gotResp = %v, want %v", gotResp, tt.wantResp)
   171  			}
   172  		})
   173  	}
   174  }
   175  func TestGetById(t *testing.T) {
   176  	mockResp := map[string][]byte{
   177  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
   178  	}
   179  	var resp []byte
   180  	test.MockSvrHandler.HandleFunc(apiGetById, func(w http.ResponseWriter, r *http.Request) {
   181  		w.Write([]byte(resp))
   182  	})
   183  
   184  	type args struct {
   185  		ctx     *offiaccount.OffiAccount
   186  		payload []byte
   187  	}
   188  	tests := []struct {
   189  		name     string
   190  		args     args
   191  		wantResp []byte
   192  		wantErr  bool
   193  	}{
   194  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
   195  	}
   196  	for _, tt := range tests {
   197  		t.Run(tt.name, func(t *testing.T) {
   198  			resp = mockResp[tt.name]
   199  			gotResp, err := GetById(tt.args.ctx, tt.args.payload)
   200  			//fmt.Println(string(gotResp), err)
   201  			if (err != nil) != tt.wantErr {
   202  				t.Errorf("GetById() error = %v, wantErr %v", err, tt.wantErr)
   203  				return
   204  			}
   205  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
   206  				t.Errorf("GetById() gotResp = %v, want %v", gotResp, tt.wantResp)
   207  			}
   208  		})
   209  	}
   210  }