github.com/fastwego/offiaccount@v1.0.1/apis/store/stock/stock_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 stock 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 TestReduce(t *testing.T) { 69 mockResp := map[string][]byte{ 70 "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), 71 } 72 var resp []byte 73 test.MockSvrHandler.HandleFunc(apiReduce, 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 := Reduce(tt.args.ctx, tt.args.payload) 93 //fmt.Println(string(gotResp), err) 94 if (err != nil) != tt.wantErr { 95 t.Errorf("Reduce() error = %v, wantErr %v", err, tt.wantErr) 96 return 97 } 98 if !reflect.DeepEqual(gotResp, tt.wantResp) { 99 t.Errorf("Reduce() gotResp = %v, want %v", gotResp, tt.wantResp) 100 } 101 }) 102 } 103 }