-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboxes_test.go
160 lines (143 loc) · 2.91 KB
/
boxes_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package postmaster
import (
"testing"
)
func TestBoxNew(t *testing.T) {
pm := New("apikey")
b := pm.Box()
if b.Id != -1 {
t.Error("new box should have ID = -1")
}
if b.p != pm {
t.Error("new box should have Postmaster instance initialized")
}
}
func TestBoxCreate(t *testing.T) {
// Mock
c := make(chan *restMockObj, 1)
post = restMock(c, nil, 100, nil)
pm := New("apikey")
b := pm.Box()
b.Create()
ret := <-c
if ret.endpoint != "packages" {
t.Error("wrong endpoint")
}
if ret.version != "v1" {
t.Error("wrong version")
}
b.Id = 1
_, err := b.Create()
if err == nil {
t.Error("it shouldn't be possible to create an existing box")
}
}
func TestBoxGet(t *testing.T) {
// Mock
c := make(chan *restMockObj, 1)
get = restMockGet(c, nil, 100, nil)
pm := New("apikey")
b := pm.Box()
_, err := b.Get()
if err == nil {
t.Error("it shouldn't be possible to get a non-existing box")
}
b.Id = 1234
_, err = b.Get()
if err != nil {
t.Error("err should be nil")
}
ret := <-c
if ret.endpoint != "packages/1234" {
t.Error("wrong endpoint")
}
if ret.version != "v1" {
t.Error("wrong version")
}
}
func TestBoxDelete(t *testing.T) {
// Mock
c := make(chan *restMockObj, 1)
del = restMock(c, nil, 100, nil)
pm := New("apikey")
b := pm.Box()
_, err := b.Delete()
if err == nil {
t.Error("it shouldn't be possible to delete a non-existing box")
}
b.Id = 1234
_, err = b.Delete()
if err != nil {
t.Error("err should be nil")
}
ret := <-c
if ret.endpoint != "packages/1234" {
t.Error("wrong endpoint")
}
if ret.version != "v1" {
t.Error("wrong version")
}
}
func TestBoxUpdate(t *testing.T) {
// Mock
c := make(chan *restMockObj, 1)
put = restMock(c, nil, 100, nil)
pm := New("apikey")
b := pm.Box()
_, err := b.Update()
if err == nil {
t.Error("it shouldn't be possible to update a non-existing box")
}
b.Id = 1234
_, err = b.Update()
if err != nil {
t.Error("err should be nil")
}
ret := <-c
if ret.endpoint != "packages/1234" {
t.Error("wrong endpoint")
}
if ret.version != "v1" {
t.Error("wrong version")
}
}
func TestBoxList(t *testing.T) {
// Mock
c := make(chan *restMockObj, 1)
get = restMockGet(c, nil, 100, nil)
pm := New("apikey")
pm.ListBoxes(10, "cursor")
ret := <-c
if ret.endpoint != "packages" {
t.Error("wrong endpoint")
}
if ret.version != "v1" {
t.Error("wrong version")
}
}
func TestFit(t *testing.T) {
// Mock
c := make(chan *restMockObj, 1)
post = restMock(c, nil, 100, nil)
pm := New("apikey")
boxes := []Box{Box{}, Box{}}
items := []Item{Item{}}
pm.Fit(boxes, items, 10)
ret := <-c
params := ret.params.(FitMessage)
if len(params.Boxes) != 2 {
t.Error("wrong boxes count")
}
if len(params.Items) != 1 {
t.Error("wrong items count")
}
if params.PackageLimit != 10 {
t.Error("wrong package limit")
}
if ret.endpoint != "packages/fit" {
t.Error("wrong endpoint")
}
if ret.version != "v1" {
t.Error("wrong version")
}
}