-
Notifications
You must be signed in to change notification settings - Fork 7
/
parcel_test.go
75 lines (69 loc) · 1.53 KB
/
parcel_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package sendcloud_test
import (
"encoding/json"
"testing"
"github.com/afosto/sendcloud-go"
"github.com/stretchr/testify/assert"
)
func TestGetPayload(t *testing.T) {
tests := []struct {
Name string
Params sendcloud.ParcelParams
}{
{
Name: "Should ignore empty weight",
Params: sendcloud.ParcelParams{},
},
{
Name: "Should include weight in request",
Params: sendcloud.ParcelParams{
Weight: "0.040",
},
},
}
for _, test := range tests {
payload := test.Params.GetPayload()
b, _ := json.Marshal(payload)
var obj map[string]map[string]interface{}
err := json.Unmarshal(b, &obj)
assert.NoError(t, err)
if test.Params.Weight == "" {
_, ok := obj["parcel"]["weight"]
assert.False(t, ok)
}
}
}
func TestGetResponse(t *testing.T) {
tests := []struct {
Name string
Response sendcloud.ParcelResponseContainer
Out sendcloud.Parcel
}{
{
Name: "Should ignore nil weight",
Response: sendcloud.ParcelResponseContainer{
Parcel: sendcloud.ParcelResponse{},
},
Out: sendcloud.Parcel{},
},
{
Name: "Should include weight",
Response: sendcloud.ParcelResponseContainer{
Parcel: sendcloud.ParcelResponse{
Weight: "0.05",
},
},
Out: sendcloud.Parcel{
Weight: "0.05",
},
},
}
for _, test := range tests {
res := test.Response.GetResponse()
if test.Response.Parcel.Weight != "" {
assert.Equal(t, test.Out.Weight, res.(*sendcloud.Parcel).Weight, test.Name)
} else {
assert.Equal(t, res.(*sendcloud.Parcel).Weight, "", test.Name)
}
}
}