forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant_test.go
459 lines (365 loc) · 13.5 KB
/
variant_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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package goshopify
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/shopspring/decimal"
)
func variantTests(t *testing.T, variant Variant) {
// Check that the ID is assigned to the returned variant
expectedInt := int64(1)
if variant.ID != expectedInt {
t.Errorf("Variant.ID returned %+v, expected %+v", variant.ID, expectedInt)
}
// Check that the Title is assigned to the returned variant
expectedTitle := "Yellow"
if variant.Title != expectedTitle {
t.Errorf("Variant.Title returned %+v, expected %+v", variant.Title, expectedTitle)
}
expectedInventoryItemId := int64(1)
if variant.InventoryItemId != expectedInventoryItemId {
t.Errorf("Variant.InventoryItemId returned %+v, expected %+v", variant.InventoryItemId, expectedInventoryItemId)
}
expectedMetafieldCount := 0
if len(variant.Metafields) != expectedMetafieldCount {
t.Errorf("Variant.Metafield returned %+v, expected %+v", variant.Metafields, expectedMetafieldCount)
}
}
func variantWithMetafieldsTests(t *testing.T, variant Variant) {
// Check that the ID is assigned to the returned variant
expectedInt := int64(2)
if variant.ID != expectedInt {
t.Errorf("Variant.ID returned %+v, expected %+v", variant.ID, expectedInt)
}
// Check that the Title is assigned to the returned variant
expectedTitle := "Blue"
if variant.Title != expectedTitle {
t.Errorf("Variant.Title returned %+v, expected %+v", variant.Title, expectedTitle)
}
expectedInventoryItemId := int64(1)
if variant.InventoryItemId != expectedInventoryItemId {
t.Errorf("Variant.InventoryItemId returned %+v, expected %+v", variant.InventoryItemId, expectedInventoryItemId)
}
expectedMetafieldCount := 1
if len(variant.Metafields) != expectedMetafieldCount {
t.Errorf("Variant.Metafield returned %+v, expected %+v", variant.Metafields, expectedMetafieldCount)
}
expectedMetafieldDescription := "description"
if variant.Metafields[0].Description != expectedMetafieldDescription {
t.Errorf("Variant.Metafield.Description returned %+v, expected %+v", variant.Metafields[0].Description, expectedMetafieldDescription)
}
}
func TestVariantList(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"variants": [{"id":1},{"id":2}]}`))
variants, err := client.Variant.List(1, nil)
if err != nil {
t.Errorf("Variant.List returned error: %v", err)
}
expected := []Variant{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(variants, expected) {
t.Errorf("Variant.List returned %+v, expected %+v", variants, expected)
}
}
func TestVariantCount(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants/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/products/1/variants/count.json", client.pathPrefix),
params,
httpmock.NewStringResponder(200, `{"count": 2}`))
cnt, err := client.Variant.Count(1, nil)
if err != nil {
t.Errorf("Variant.Count returned error: %v", err)
}
expected := 3
if cnt != expected {
t.Errorf("Variant.Count returned %d, expected %d", cnt, expected)
}
date := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)
cnt, err = client.Variant.Count(1, CountOptions{CreatedAtMin: date})
if err != nil {
t.Errorf("Variant.Count returned %d, expected %d", cnt, expected)
}
expected = 2
if cnt != expected {
t.Errorf("Variant.Count returned %d, expected %d", cnt, expected)
}
}
func TestVariantGet(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"variant": {"id":1}}`))
variant, err := client.Variant.Get(1, nil)
if err != nil {
t.Errorf("Variant.Get returned error: %v", err)
}
expected := &Variant{ID: 1}
if !reflect.DeepEqual(variant, expected) {
t.Errorf("Variant.Get returned %+v, expected %+v", variant, expected)
}
}
func TestVariantCreate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant.json")))
price := decimal.NewFromFloat(1)
variant := Variant{
Option1: "Yellow",
Price: &price,
}
result, err := client.Variant.Create(1, variant)
if err != nil {
t.Errorf("Variant.Create returned error: %v", err)
}
variantTests(t, *result)
}
func TestVariantCreateWithMetafields(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant_with_metafields.json")))
price := decimal.NewFromFloat(2)
variant := Variant{
Option1: "Blue",
Price: &price,
}
result, err := client.Variant.Create(1, variant)
if err != nil {
t.Errorf("Variant.Create returned error: %v", err)
}
variantWithMetafieldsTests(t, *result)
}
func TestVariantUpdate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant.json")))
variant := Variant{
ID: 1,
Option1: "Green",
}
variant.Option1 = "Yellow"
returnedVariant, err := client.Variant.Update(variant)
if err != nil {
t.Errorf("Variant.Update returned error: %v", err)
}
variantTests(t, *returnedVariant)
}
func TestVariantWithMetafieldsUpdate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/2.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant_with_metafields.json")))
variant := Variant{
ID: 2,
Option1: "Green",
Metafields: []Metafield{
{
ID: 123,
Description: "Original",
},
},
}
variant.Option1 = "Blue"
variant.Metafields[0].Description = "description"
returnedVariant, err := client.Variant.Update(variant)
if err != nil {
t.Errorf("Variant.Update returned error: %v", err)
}
variantWithMetafieldsTests(t, *returnedVariant)
}
func TestVariantDelete(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("DELETE", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, "{}"))
err := client.Variant.Delete(1, 1)
if err != nil {
t.Errorf("Variant.Delete returned error: %v", err)
}
}
func TestVariantListMetafields(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1/metafields.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"metafields": [{"id":1},{"id":2}]}`))
metafields, err := client.Variant.ListMetafields(1, nil)
if err != nil {
t.Errorf("Variant.ListMetafields() returned error: %v", err)
}
expected := []Metafield{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(metafields, expected) {
t.Errorf("Variant.ListMetafields() returned %+v, expected %+v", metafields, expected)
}
}
func TestVariantCountMetafields(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/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/variants/1/metafields/count.json", client.pathPrefix),
params,
httpmock.NewStringResponder(200, `{"count": 2}`))
cnt, err := client.Variant.CountMetafields(1, nil)
if err != nil {
t.Errorf("Variant.CountMetafields() returned error: %v", err)
}
expected := 3
if cnt != expected {
t.Errorf("Variant.CountMetafields() returned %d, expected %d", cnt, expected)
}
date := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)
cnt, err = client.Variant.CountMetafields(1, CountOptions{CreatedAtMin: date})
if err != nil {
t.Errorf("Variant.CountMetafields() returned error: %v", err)
}
expected = 2
if cnt != expected {
t.Errorf("Variant.CountMetafields() returned %d, expected %d", cnt, expected)
}
}
func TestVariantGetMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1/metafields/2.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"metafield": {"id":2}}`))
metafield, err := client.Variant.GetMetafield(1, 2, nil)
if err != nil {
t.Errorf("Variant.GetMetafield() returned error: %v", err)
}
expected := &Metafield{ID: 2}
if !reflect.DeepEqual(metafield, expected) {
t.Errorf("Variant.GetMetafield() returned %+v, expected %+v", metafield, expected)
}
}
func TestVariantCreateMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1/metafields.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("metafield.json")))
metafield := Metafield{
Key: "app_key",
Value: "app_value",
ValueType: "string",
Type: "single_line_text_field",
Namespace: "affiliates",
}
returnedMetafield, err := client.Variant.CreateMetafield(1, metafield)
if err != nil {
t.Errorf("Variant.CreateMetafield() returned error: %v", err)
}
MetafieldTests(t, *returnedMetafield)
}
func TestVariantUpdateMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1/metafields/2.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("metafield.json")))
metafield := Metafield{
ID: 2,
Key: "app_key",
Value: "app_value",
ValueType: "string",
Type: "single_line_text_field",
Namespace: "affiliates",
}
returnedMetafield, err := client.Variant.UpdateMetafield(1, metafield)
if err != nil {
t.Errorf("Variant.UpdateMetafield() returned error: %v", err)
}
MetafieldTests(t, *returnedMetafield)
}
func TestVariantDeleteMetafield(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("DELETE", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1/metafields/2.json", client.pathPrefix),
httpmock.NewStringResponder(200, "{}"))
err := client.Variant.DeleteMetafield(1, 2)
if err != nil {
t.Errorf("Variant.DeleteMetafield() returned error: %v", err)
}
}
func TestVariantListWithTaxCode(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"variants": [{"id":1, "tax_code":"P0000000"},{"id":2, "tax_code":"P0000000"}]}`))
variants, err := client.Variant.List(1, nil)
if err != nil {
t.Errorf("Variant.List returned error: %v", err)
}
expected := []Variant{{ID: 1, TaxCode: "P0000000"}, {ID: 2, TaxCode: "P0000000"}}
if !reflect.DeepEqual(variants, expected) {
t.Errorf("Variant.List returned %+v, expected %+v", variants, expected)
}
}
func TestVariantGetWithTaxCode(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"variant": {"id":1, "tax_code":"P0000000"}}`))
variant, err := client.Variant.Get(1, nil)
if err != nil {
t.Errorf("Variant.Get returned error: %v", err)
}
expected := &Variant{ID: 1, TaxCode: "P0000000"}
if !reflect.DeepEqual(variant, expected) {
t.Errorf("Variant.Get returned %+v, expected %+v", variant, expected)
}
}
func TestVariantCreateWithTaxCode(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant_with_taxcode.json")))
price := decimal.NewFromFloat(1)
variant := Variant{
Option1: "Yellow",
Price: &price,
TaxCode: "P0000000",
}
result, err := client.Variant.Create(1, variant)
if err != nil {
t.Errorf("Variant.Create returned error: %v", err)
}
variantTestsWithTaxCode(t, *result)
}
func variantTestsWithTaxCode(t *testing.T, variant Variant) {
// Check that the ID is assigned to the returned variant
expectedInt := int64(1)
if variant.ID != expectedInt {
t.Errorf("Variant.ID returned %+v, expected %+v", variant.ID, expectedInt)
}
// Check that the Title is assigned to the returned variant
expectedTitle := "Green"
if variant.Title != expectedTitle {
t.Errorf("Variant.Title returned %+v, expected %+v", variant.Title, expectedTitle)
}
expectedInventoryItemId := int64(1)
if variant.InventoryItemId != expectedInventoryItemId {
t.Errorf("Variant.InventoryItemId returned %+v, expected %+v", variant.InventoryItemId, expectedInventoryItemId)
}
expectedMetafieldCount := 0
if len(variant.Metafields) != expectedMetafieldCount {
t.Errorf("Variant.Metafield returned %+v, expected %+v", variant.Metafields, expectedMetafieldCount)
}
// Check that the Tax_code is assigned to the returned variant
expectedTacCode := "P0000000"
if variant.TaxCode != expectedTacCode {
t.Errorf("Variant.TaxCode returned %+v, expected %+v", variant.TaxCode, expectedTacCode)
}
}