forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 256
/
articles_test.go
206 lines (173 loc) · 4.79 KB
/
articles_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
package goshopify
import (
"context"
"fmt"
"reflect"
"testing"
"github.com/jarcoal/httpmock"
)
func TestArticleList(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"articles": [{"id":1},{"id":2}]}`,
),
)
articles, err := client.Article.List(context.Background(), 241253187, nil)
if err != nil {
t.Errorf("Article.List returned error: %v", err)
}
expected := []Article{
{
Id: 1,
},
{
Id: 2,
},
}
if !reflect.DeepEqual(articles, expected) {
t.Errorf("Articles.List returned %+v, expected %+v", articles, expected)
}
}
func TestArticleCreate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles.json", client.pathPrefix),
httpmock.NewStringResponder(
201,
`{"article": {"id": 1}}`,
),
)
article := Article{Title: "Test Article"}
createdArticle, err := client.Article.Create(context.Background(), 241253187, article)
if err != nil {
t.Errorf("Article.Create returned error: %v", err)
}
expected := &Article{Id: 1}
if !reflect.DeepEqual(createdArticle, expected) {
t.Errorf("Article.Create returned %+v, expected %+v", createdArticle, expected)
}
}
func TestArticleGet(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles/1.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"article": {"id": 1, "title": "Test Article"}}`,
),
)
article, err := client.Article.Get(context.Background(), 241253187, 1)
if err != nil {
t.Errorf("Article.Get returned error: %v", err)
}
expected := &Article{Id: 1, Title: "Test Article"}
if !reflect.DeepEqual(article, expected) {
t.Errorf("Article.Get returned %+v, expected %+v", article, expected)
}
}
func TestArticleUpdate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"PUT",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles/1.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"article": {"id": 1, "title": "Updated Article"}}`,
),
)
article := Article{Title: "Updated Article"}
updatedArticle, err := client.Article.Update(context.Background(), 241253187, 1, article)
if err != nil {
t.Errorf("Article.Update returned error: %v", err)
}
expected := &Article{Id: 1, Title: "Updated Article"}
if !reflect.DeepEqual(updatedArticle, expected) {
t.Errorf("Article.Update returned %+v, expected %+v", updatedArticle, expected)
}
}
func TestArticleDelete(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"DELETE",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles/1.json", client.pathPrefix),
httpmock.NewStringResponder(
204, // No content response
``,
),
)
err := client.Article.Delete(context.Background(), 241253187, 1)
if err != nil {
t.Errorf("Article.Delete returned error: %v", err)
}
}
func TestArticleListTags(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/articles/tags.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"tags": ["tag1", "tag2"]}`,
),
)
tags, err := client.Article.ListTags(context.Background(), nil)
if err != nil {
t.Errorf("Article.ListTags returned error: %v", err)
}
expected := []string{"tag1", "tag2"}
if !reflect.DeepEqual(tags, expected) {
t.Errorf("Article.ListTags returned %+v, expected %+v", tags, expected)
}
}
func TestArticleCount(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles/count.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"count": 2}`,
),
)
count, err := client.Article.Count(context.Background(), 241253187, nil)
if err != nil {
t.Errorf("Article.Count returned error: %v", err)
}
expected := 2
if count != expected {
t.Errorf("Article.Count returned %d, expected %d", count, expected)
}
}
func TestArticleListBlogTags(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/blogs/241253187/articles/tags.json", client.pathPrefix),
httpmock.NewStringResponder(
200,
`{"tags": ["blogTag1", "blogTag2"]}`,
),
)
tags, err := client.Article.ListBlogTags(context.Background(), 241253187, nil)
if err != nil {
t.Errorf("Article.ListBlogTags returned error: %v", err)
}
expected := []string{"blogTag1", "blogTag2"}
if !reflect.DeepEqual(tags, expected) {
t.Errorf("Article.ListBlogTags returned %+v, expected %+v", tags, expected)
}
}