forked from s-norris/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft_order_test.go
409 lines (334 loc) · 11.4 KB
/
draft_order_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package goshopify
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/shopspring/decimal"
)
func draftOrderTests(t *testing.T, draftOrder DraftOrder) {
// Check that dates are parsed
d := time.Date(2019, time.April, 9, 10, 02, 43, 0, time.UTC)
if !d.Equal(*draftOrder.CreatedAt) {
t.Errorf("Order.CreatedAt returned %+v, expected %+v", draftOrder.CreatedAt, d)
}
// Check null dates
if draftOrder.UpdatedAt == nil {
t.Errorf("DraftOrder.UpdatedAt returned %+v, expected %+v", draftOrder.UpdatedAt, nil)
}
// Check prices
p := "206.25"
if !(p == draftOrder.TotalPrice) {
t.Errorf("draftOrder.TotalPrice returned %+v, expected %+v", draftOrder.TotalPrice, p)
}
// Check null prices, notice that prices are usually not empty.
if draftOrder.TotalTax != "0.00" {
t.Errorf("draftOrder.TotalTax returned %+v, expected %+v", draftOrder.TotalTax, nil)
}
//
// Check customer
if draftOrder.Customer == nil {
t.Error("Expected Customer to not be nil")
}
if draftOrder.Customer.Email != "[email protected]" {
t.Errorf("Customer.Email, expected %v, actual %v", "[email protected]", draftOrder.Customer.Email)
}
ptp := decimal.NewFromFloat(199)
lineItem := draftOrder.LineItems[0]
if !ptp.Equals(*lineItem.Price) {
t.Errorf("DraftOrder.LineItems[0].Price, expected %v, actual %v", "199.00", lineItem.Price)
}
}
func TestDraftOrderGet(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/994118539.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("draft_order.json")))
draftOrder, err := client.DraftOrder.Get(994118539, nil)
if err != nil {
t.Errorf("DraftOrder.Get returned error: %v", err)
}
draftOrderTests(t, *draftOrder)
}
func TestDraftOrderCreate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders.json", client.pathPrefix),
httpmock.NewStringResponder(201, `{"draft_order":{"id": 1}}`))
draftOrder := DraftOrder{
LineItems: []LineItem{
LineItem{
VariantID: 1,
Quantity: 1,
},
},
}
d, err := client.DraftOrder.Create(draftOrder)
if err != nil {
t.Errorf("DraftOrder.Create returned error: %v", err)
}
expected := DraftOrder{ID: 1}
if d.ID != expected.ID {
t.Errorf("DraftOrder.Create returned id %d, expected %d", d.ID, expected.ID)
}
}
func TestDraftOrderUpdate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"draft_order":{"id": 1}}`))
draftOrder := DraftOrder{
ID: 1,
Note: "slow order",
TaxesIncluded: true,
}
d, err := client.DraftOrder.Update(draftOrder)
if err != nil {
t.Errorf("DraftOrder.Create returned an error %v", err)
}
expected := DraftOrder{ID: 1}
if d.ID != expected.ID {
t.Errorf("DraftOrder.Update returned id %d, expected %d", d.ID, expected.ID)
}
}
func TestDraftOrderCount(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/count.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"count": 7}`))
params := map[string]string{"status": "open"}
httpmock.RegisterResponderWithQuery(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/count.json", client.pathPrefix),
params,
httpmock.NewStringResponder(200, `{"count": 2}`))
cnt, err := client.DraftOrder.Count(nil)
if err != nil {
t.Errorf("DraftOrder.Count returned an error: %v", err)
}
expected := 7
if cnt != expected {
t.Errorf("DraftOrder.Count returned %d, expected %d", cnt, expected)
}
status := "open"
cnt, err = client.DraftOrder.Count(DraftOrderCountOptions{Status: status})
if err != nil {
t.Errorf("DraftOrder.Count returned an error: %v", err)
}
expected = 2
if cnt != expected {
t.Errorf("DraftOrder.Count returned %d, expected %d", cnt, expected)
}
}
func TestDraftOrderList(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("draft_orders.json")))
draftOrders, err := client.DraftOrder.List(nil)
if err != nil {
t.Errorf("DraftOrder.List returned error: %v", err)
}
if len(draftOrders) != 1 {
t.Errorf("DraftOrder.List got %d orders, expected: 1", len(draftOrders))
}
draftOrder := draftOrders[0]
draftOrderTests(t, draftOrder)
}
func TestDraftOrderListOptions(t *testing.T) {
setup()
defer teardown()
params := map[string]string{
"fields": "id,name",
"limit": "250",
"status": "any",
}
httpmock.RegisterResponderWithQuery(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders.json", client.pathPrefix),
params,
httpmock.NewBytesResponder(200, loadFixture("draft_orders.json")))
options := DraftOrderListOptions{
Limit: 250,
Status: "any",
Fields: "id,name",
}
draftOrders, err := client.DraftOrder.List(options)
if err != nil {
t.Errorf("DraftOrder.List returned error: %v", err)
}
if len(draftOrders) != 1 {
t.Errorf("DraftOrder.List got %d orders, expected: 1", len(draftOrders))
}
draftOrder := draftOrders[0]
draftOrderTests(t, draftOrder)
}
func TestDraftOrderInvoice(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/send_invoice.json", client.pathPrefix),
httpmock.NewBytesResponder(201, loadFixture("invoice.json")))
invoice := DraftOrderInvoice{
To: "[email protected]",
From: "[email protected]",
Bcc: []string{
},
Subject: "Apple Computer Invoice",
CustomMessage: "Thank you for ordering!",
}
draftInvoice, err := client.DraftOrder.Invoice(1, invoice)
if err != nil {
t.Errorf("DraftOrder.Invoice returned an error: %v", err)
}
if !reflect.DeepEqual(*draftInvoice, invoice) {
t.Errorf("DraftOrder.Invoice returned %+v, expected %+v,", draftInvoice, invoice)
}
}
func TestDraftOrderDelete(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"DELETE",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1.json", client.pathPrefix),
httpmock.NewBytesResponder(200, nil))
err := client.DraftOrder.Delete(1)
if err != nil {
t.Errorf("DraftOrder.Delete returned an error %v", err)
}
}
func TestDraftOrderComplete(t *testing.T) {
setup()
defer teardown()
params := map[string]string{"payment_pending": "false"}
httpmock.RegisterResponderWithQuery(
"PUT",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/complete.json", client.pathPrefix),
params,
httpmock.NewBytesResponder(200, loadFixture("draft_order.json")))
draftOrder, err := client.DraftOrder.Complete(1, false)
if err != nil {
t.Errorf("DraftOrder.Complete returned an error %v", err)
}
draftOrderTests(t, *draftOrder)
}
func TestDraftOrderCompletePending(t *testing.T) {
setup()
defer teardown()
params := map[string]string{"payment_pending": "true"}
httpmock.RegisterResponderWithQuery(
"PUT",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/complete.json", client.pathPrefix),
params,
httpmock.NewBytesResponder(200, loadFixture("draft_order.json")))
draftOrder, err := client.DraftOrder.Complete(1, true)
if err != nil {
t.Errorf("DraftOrder.Complete returned an error %v", err)
}
draftOrderTests(t, *draftOrder)
}
func TestDraftOrderListMetafields(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"metafields": [{"id":1},{"id":2}]}`))
metafields, err := client.DraftOrder.ListMetafields(1, nil)
if err != nil {
t.Errorf("DraftOrder.ListMetafields() returned error: %v", err)
}
expected := []Metafield{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(metafields, expected) {
t.Errorf("Order.ListMetafields() returned %+v, expected %+v", metafields, expected)
}
}
func TestDraftOrderCountMetafields(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields/count.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"count": 3}`))
params := map[string]string{"created_at_min": "2016-01-01T00:00:00Z"}
httpmock.RegisterResponderWithQuery(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields/count.json", client.pathPrefix),
params,
httpmock.NewStringResponder(200, `{"count": 2}`))
cnt, err := client.DraftOrder.CountMetafields(1, nil)
if err != nil {
t.Errorf("Order.CountMetafields() returned error: %v", err)
}
expected := 3
if cnt != expected {
t.Errorf("Order.CountMetafields() returned %d, expected %d", cnt, expected)
}
date := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)
cnt, err = client.DraftOrder.CountMetafields(1, CountOptions{CreatedAtMin: date})
if err != nil {
t.Errorf("Order.CountMetafields() returned error: %v", err)
}
expected = 2
if cnt != expected {
t.Errorf("Order.CountMetafields() returned %d, expected %d", cnt, expected)
}
}
func TestDraftOrderGetMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields/2.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"metafield": {"id":2}}`))
metafield, err := client.DraftOrder.GetMetafield(1, 2, nil)
if err != nil {
t.Errorf("Order.GetMetafield() returned error: %v", err)
}
expected := &Metafield{ID: 2}
if !reflect.DeepEqual(metafield, expected) {
t.Errorf("Order.GetMetafield() returned %+v, expected %+v", metafield, expected)
}
}
func TestDraftOrderCreateMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("metafield.json")))
metafield := Metafield{
Key: "app_key",
Value: "app_value",
ValueType: "string",
Namespace: "affiliates",
}
returnedMetafield, err := client.DraftOrder.CreateMetafield(1, metafield)
if err != nil {
t.Errorf("Order.CreateMetafield() returned error: %v", err)
}
MetafieldTests(t, *returnedMetafield)
}
func TestDraftOrderUpdateMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields/2.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("metafield.json")))
metafield := Metafield{
ID: 2,
Key: "app_key",
Value: "app_value",
ValueType: "string",
Namespace: "affiliates",
}
returnedMetafield, err := client.DraftOrder.UpdateMetafield(1, metafield)
if err != nil {
t.Errorf("Order.UpdateMetafield() returned error: %v", err)
}
MetafieldTests(t, *returnedMetafield)
}
func TestDraftOrderDeleteMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("DELETE", fmt.Sprintf("https://fooshop.myshopify.com/%s/draft_orders/1/metafields/2.json", client.pathPrefix),
httpmock.NewStringResponder(200, "{}"))
err := client.DraftOrder.DeleteMetafield(1, 2)
if err != nil {
t.Errorf("Order.DeleteMetafield() returned error: %v", err)
}
}