github.com/fastwego/offiaccount@v1.0.1/apis/store/order/order_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 order 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 TestGetById(t *testing.T) { 33 mockResp := map[string][]byte{ 34 "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), 35 } 36 var resp []byte 37 test.MockSvrHandler.HandleFunc(apiGetById, 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 := GetById(tt.args.ctx, tt.args.payload) 57 //fmt.Println(string(gotResp), err) 58 if (err != nil) != tt.wantErr { 59 t.Errorf("GetById() error = %v, wantErr %v", err, tt.wantErr) 60 return 61 } 62 if !reflect.DeepEqual(gotResp, tt.wantResp) { 63 t.Errorf("GetById() gotResp = %v, want %v", gotResp, tt.wantResp) 64 } 65 }) 66 } 67 } 68 func TestGetByFilter(t *testing.T) { 69 mockResp := map[string][]byte{ 70 "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), 71 } 72 var resp []byte 73 test.MockSvrHandler.HandleFunc(apiGetByFilter, 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 := GetByFilter(tt.args.ctx, tt.args.payload) 93 //fmt.Println(string(gotResp), err) 94 if (err != nil) != tt.wantErr { 95 t.Errorf("GetByFilter() error = %v, wantErr %v", err, tt.wantErr) 96 return 97 } 98 if !reflect.DeepEqual(gotResp, tt.wantResp) { 99 t.Errorf("GetByFilter() gotResp = %v, want %v", gotResp, tt.wantResp) 100 } 101 }) 102 } 103 } 104 func TestSetDelivery(t *testing.T) { 105 mockResp := map[string][]byte{ 106 "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), 107 } 108 var resp []byte 109 test.MockSvrHandler.HandleFunc(apiSetDelivery, 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 := SetDelivery(tt.args.ctx, tt.args.payload) 129 //fmt.Println(string(gotResp), err) 130 if (err != nil) != tt.wantErr { 131 t.Errorf("SetDelivery() error = %v, wantErr %v", err, tt.wantErr) 132 return 133 } 134 if !reflect.DeepEqual(gotResp, tt.wantResp) { 135 t.Errorf("SetDelivery() gotResp = %v, want %v", gotResp, tt.wantResp) 136 } 137 }) 138 } 139 } 140 func TestClose(t *testing.T) { 141 mockResp := map[string][]byte{ 142 "case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"), 143 } 144 var resp []byte 145 test.MockSvrHandler.HandleFunc(apiClose, func(w http.ResponseWriter, r *http.Request) { 146 w.Write([]byte(resp)) 147 }) 148 149 type args struct { 150 ctx *offiaccount.OffiAccount 151 payload []byte 152 } 153 tests := []struct { 154 name string 155 args args 156 wantResp []byte 157 wantErr bool 158 }{ 159 {name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false}, 160 } 161 for _, tt := range tests { 162 t.Run(tt.name, func(t *testing.T) { 163 resp = mockResp[tt.name] 164 gotResp, err := Close(tt.args.ctx, tt.args.payload) 165 //fmt.Println(string(gotResp), err) 166 if (err != nil) != tt.wantErr { 167 t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr) 168 return 169 } 170 if !reflect.DeepEqual(gotResp, tt.wantResp) { 171 t.Errorf("Close() gotResp = %v, want %v", gotResp, tt.wantResp) 172 } 173 }) 174 } 175 }