forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 256
/
scripttag_test.go
122 lines (96 loc) · 3.3 KB
/
scripttag_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
package goshopify
import (
"context"
"fmt"
"reflect"
"testing"
"github.com/jarcoal/httpmock"
)
func TestScriptTagList(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/script_tags.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"script_tags": [{"id": 1},{"id": 2}]}`))
scriptTags, err := client.ScriptTag.List(context.Background(), nil)
if err != nil {
t.Errorf("ScriptTag.List returned error: %v", err)
}
expected := []ScriptTag{{Id: 1}, {Id: 2}}
if !reflect.DeepEqual(scriptTags, expected) {
t.Errorf("ScriptTag.List returned %+v, expected %+v", scriptTags, expected)
}
}
func TestScriptTagCount(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/script_tags/count.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"count": 3}`))
cnt, err := client.ScriptTag.Count(context.Background(), nil)
if err != nil {
t.Errorf("ScriptTag.Count returned error: %v", err)
}
expected := 3
if cnt != expected {
t.Errorf("ScriptTag.Count returned %d, expected %d", cnt, expected)
}
}
func TestScriptTagGet(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("GET", fmt.Sprintf("https://fooshop.myshopify.com/%s/script_tags/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, `{"script_tag": {"id": 1}}`))
scriptTag, err := client.ScriptTag.Get(context.Background(), 1, nil)
if err != nil {
t.Errorf("ScriptTag.Get returned error: %v", err)
}
expected := &ScriptTag{Id: 1}
if !reflect.DeepEqual(scriptTag, expected) {
t.Errorf("ScriptTag.Get returned %+v, expected %+v", scriptTag, expected)
}
}
func scriptTagTests(t *testing.T, tag ScriptTag) {
expected := uint64(870402688)
if tag.Id != expected {
t.Errorf("tag.Id is %+v, expected %+v", tag.Id, expected)
}
}
func TestScriptTagCreate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/script_tags.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("script_tags.json")))
tag0 := ScriptTag{
Src: "https://djavaskripped.org/fancy.js",
Event: "onload",
DisplayScope: "all",
}
returnedTag, err := client.ScriptTag.Create(context.Background(), tag0)
if err != nil {
t.Errorf("ScriptTag.Create returned error: %v", err)
}
scriptTagTests(t, *returnedTag)
}
func TestScriptTagUpdate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/script_tags/1.json", client.pathPrefix),
httpmock.NewBytesResponder(200, loadFixture("script_tags.json")))
tag := ScriptTag{
Id: 1,
Src: "https://djavaskripped.org/fancy.js",
}
returnedTag, err := client.ScriptTag.Update(context.Background(), tag)
if err != nil {
t.Errorf("ScriptTag.Update returned error: %v", err)
}
scriptTagTests(t, *returnedTag)
}
func TestScriptTagDelete(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder("DELETE", fmt.Sprintf("https://fooshop.myshopify.com/%s/script_tags/1.json", client.pathPrefix),
httpmock.NewStringResponder(200, "{}"))
if err := client.ScriptTag.Delete(context.Background(), 1); err != nil {
t.Errorf("ScriptTag.Delete returned error: %v", err)
}
}