-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
116 lines (106 loc) · 2.98 KB
/
client_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package trakpak
import (
"io/ioutil"
"os"
"testing"
"time"
)
var shipper = Shipper{
ShipperCompany: "BOOK PEOPLE",
ShipperAddress1: "PARC MENAI",
ShipperCity: "BANGOR",
ShipperCounty: "GWYNEDD",
ShipperCountryCode: "GB",
ShipperPostcode: "LL57 4FB",
}
func newClient() *Client {
licenseNo := os.Getenv("TRAKPAK_LICENSE")
userID := os.Getenv("TRAKPAK_USER")
password := os.Getenv("TRAKPAK_PASSWORD")
if licenseNo == "" {
panic("TRAKPAK_LICENSE not set")
}
if userID == "" {
panic("TRAKPAK_USER not set")
}
if licenseNo == "" {
panic("TRAKPAK_PASSWORD not set")
}
c := NewClient(licenseNo, userID, password)
c.TestMode = true
return c
}
func TestLabels(t *testing.T) {
c := newClient()
tests := []struct {
code string
price, weight float64
depth, width, height int
destination Destination
}{
{
"RSLW", 11.99, 1.606, 32, 24, 19,
Destination{"", "c/o Sue Steed", "Carrer de la Valleta, 6.", "Ginestar", "Tarragona", "Catalunya", "ES", "43748", "Ken Best", "", "", ""},
},
{
"LPSV", 4.99, 1.115, 32, 24, 19,
Destination{"", "c/o Dan Badge Manufacturing sdn.bhd", "26-S, Jalan Bidara 2/5,", "Taman Bidara", "Selayang, Batu Caves", "Selangor", "MY", "68100", "Mr Foo Bar", "", "", ""},
},
{
"LPSV", 4.99, 1.115, 32, 24, 19,
Destination{"", "5485 Byscane Lane", "", "", "Minnetonka", "Minnesota", "US", "55345-5603", "Mrs Alice Smith", "", "", ""},
},
{
"RSLW", 11.99, 1.606, 32, 24, 19,
Destination{"", "Ruiterslaan 6", "", "", "Wijnegem", "", "BE", "2110", "Frederic Verhulst", "", "", ""},
},
{
"LPSV", 4.99, 1.115, 32, 24, 19,
Destination{"", "11 Pisani Court", "Golden Grove", "", "Adelaide", "South Australia", "AU", "5125", "Mrs A Smith", "", "", ""},
},
}
for _, test := range tests {
s := &Shipment{
ShipmentDate: Time(time.Now()),
Shipper: &shipper,
Destination: &test.destination,
ShipmentInformation: &ShipmentInformation{
Service: "TPWW",
TotalPieces: 1,
TotalWeight: test.weight,
WeightID: "K",
Width: test.width,
Height: test.height,
Length: test.depth,
DescriptionOfGoods: "Books",
Value: 4.99,
ValueCurrency: "GBP",
Terms: "DDU", // FIXME: What is this???
LabelImageFormat: "PDF",
ItemInformation: []ItemInformation{
{"Book", "", 1, test.price, test.code, "GB"},
},
},
}
filename := "/tmp/" + test.destination.DestinationCountryCode + ".pdf"
defer os.Remove(filename)
if err := ship(c, s, filename); err != nil {
t.Errorf("Failed on %s: %v", test.destination.DestinationCountryCode, err)
}
}
}
func ship(c *Client, s *Shipment, filename string) error {
resp, err := c.BookShipment(s)
if err != nil {
return err
}
label, err := resp.LabelImage.Decode()
if err != nil {
return err
}
err = ioutil.WriteFile(filename, label, 0666)
if err != nil {
return err
}
return nil
}