Skip to content

Commit b0a6700

Browse files
committed
Add unit test
1 parent 39e69f5 commit b0a6700

File tree

7 files changed

+483
-1
lines changed

7 files changed

+483
-1
lines changed

client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func New(serviceDomain, apiKey string) *Client {
2929
return c
3030
}
3131

32-
func (c *Client) SetHTTPClient(client *http.Client) {
32+
func (c *Client) SetHTTPClient(client httpClient) {
3333
c.httpClient = client
3434
}
3535

client_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package microcms
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"net/url"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
type httpClientMock struct {
15+
t *testing.T
16+
method string
17+
url *url.URL
18+
reqHeaders map[string]string
19+
resHeaders map[string]string
20+
reqBody string
21+
resBody string
22+
}
23+
24+
var _ httpClient = &httpClientMock{}
25+
26+
func (m *httpClientMock) Do(req *http.Request) (*http.Response, error) {
27+
require.Equal(m.t, m.method, req.Method)
28+
require.Equal(m.t, m.url, req.URL)
29+
30+
for k, v := range m.reqHeaders {
31+
require.Equal(m.t, v, req.Header.Get(k))
32+
}
33+
34+
if len(m.reqBody) > 0 {
35+
var expectedReqBody interface{}
36+
require.NoError(m.t, json.NewDecoder(bytes.NewBufferString(m.reqBody)).Decode(&expectedReqBody))
37+
38+
var actualReqBody interface{}
39+
require.NoError(m.t, json.NewDecoder(req.Body).Decode(&actualReqBody))
40+
41+
require.EqualValues(m.t, expectedReqBody, actualReqBody)
42+
}
43+
44+
rec := httptest.NewRecorder()
45+
46+
for k, v := range m.resHeaders {
47+
rec.Header().Set(k, v)
48+
}
49+
if _, err := rec.Body.Write([]byte(m.resBody)); err != nil {
50+
return nil, err
51+
}
52+
53+
return rec.Result(), nil
54+
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/microcmsio/microcms-go-sdk/v0
22

33
go 1.17
4+
5+
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.0 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
11+
)

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

read_test.go

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
package microcms
2+
3+
import (
4+
"net/http"
5+
"net/url"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestClient_List(t *testing.T) {
12+
type Blog struct {
13+
ID string `json:"id"`
14+
Title string `json:"title"`
15+
}
16+
type BlogList struct {
17+
Contents []Blog
18+
TotalCount int
19+
Limit int
20+
Offset int
21+
}
22+
23+
client := New("serviceDomain", "apiKey")
24+
client.SetHTTPClient(&httpClientMock{
25+
t: t,
26+
method: http.MethodGet,
27+
url: &url.URL{
28+
Scheme: "https",
29+
Host: "serviceDomain.microcms.io",
30+
Path: "/api/v1/blog",
31+
},
32+
reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"},
33+
resHeaders: map[string]string{"Content-Type": "application/json"},
34+
resBody: `
35+
{
36+
"contents": [
37+
{
38+
"id": "foo",
39+
"title": "Hello, microCMS!",
40+
"createdAt": "2021-10-28T04:04:29.625Z",
41+
"updatedAt": "2021-10-28T04:04:29.625Z",
42+
"publishedAt": "2021-10-28T04:04:29.625Z",
43+
"revisedAt": "2021-10-28T04:04:29.625Z"
44+
}
45+
],
46+
"totalCount": 1,
47+
"limit": 10,
48+
"offset": 0
49+
}
50+
`,
51+
})
52+
53+
blog := BlogList{}
54+
err := client.List(
55+
ListParams{
56+
Endpoint: "blog",
57+
},
58+
&blog,
59+
)
60+
require.NoError(t, err)
61+
require.EqualValues(
62+
t,
63+
BlogList{
64+
Contents: []Blog{{ID: "foo", Title: "Hello, microCMS!"}},
65+
TotalCount: 1,
66+
Limit: 10,
67+
Offset: 0,
68+
},
69+
blog,
70+
)
71+
}
72+
73+
func TestClient_List_querystring(t *testing.T) {
74+
type Blog struct {
75+
ID string `json:"id"`
76+
Title string `json:"title"`
77+
}
78+
type BlogList struct {
79+
Contents []Blog
80+
TotalCount int
81+
Limit int
82+
Offset int
83+
}
84+
85+
client := New("serviceDomain", "apiKey")
86+
client.SetHTTPClient(&httpClientMock{
87+
t: t,
88+
method: http.MethodGet,
89+
url: &url.URL{
90+
Scheme: "https",
91+
Host: "serviceDomain.microcms.io",
92+
Path: "/api/v1/blog",
93+
RawQuery: "depth=1&draftKey=abcd&fields=id%2Ctitle&filters=publishedAt%5Bgreater_than%5D2021-01-01&ids=foo&limit=100&offset=1&orders=createdAt&q=Hello",
94+
},
95+
reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"},
96+
resHeaders: map[string]string{"Content-Type": "application/json"},
97+
resBody: `
98+
{
99+
"contents": [
100+
{
101+
"id": "foo",
102+
"title": "Hello, microCMS!",
103+
"createdAt": "2021-10-28T04:04:29.625Z",
104+
"updatedAt": "2021-10-28T04:04:29.625Z",
105+
"publishedAt": "2021-10-28T04:04:29.625Z",
106+
"revisedAt": "2021-10-28T04:04:29.625Z"
107+
}
108+
],
109+
"totalCount": 1,
110+
"limit": 10,
111+
"offset": 0
112+
}
113+
`,
114+
})
115+
116+
blog := BlogList{}
117+
err := client.List(
118+
ListParams{
119+
Endpoint: "blog",
120+
DraftKey: "abcd",
121+
Limit: 100,
122+
Offset: 1,
123+
Orders: []string{"createdAt"},
124+
Q: "Hello",
125+
Fields: []string{"id", "title"},
126+
IDs: []string{"foo"},
127+
Filters: "publishedAt[greater_than]2021-01-01",
128+
Depth: 1,
129+
},
130+
&blog,
131+
)
132+
require.NoError(t, err)
133+
require.EqualValues(
134+
t,
135+
BlogList{
136+
Contents: []Blog{{ID: "foo", Title: "Hello, microCMS!"}},
137+
TotalCount: 1,
138+
Limit: 10,
139+
Offset: 0,
140+
},
141+
blog,
142+
)
143+
}
144+
145+
func TestClient_Get(t *testing.T) {
146+
type Blog struct {
147+
ID string `json:"id"`
148+
Title string `json:"title"`
149+
}
150+
151+
client := New("serviceDomain", "apiKey")
152+
client.SetHTTPClient(&httpClientMock{
153+
t: t,
154+
method: http.MethodGet,
155+
url: &url.URL{
156+
Scheme: "https",
157+
Host: "serviceDomain.microcms.io",
158+
Path: "/api/v1/blog/foo",
159+
},
160+
reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"},
161+
resHeaders: map[string]string{"Content-Type": "application/json"},
162+
resBody: `
163+
{
164+
"id": "foo",
165+
"title": "Hello, microCMS!",
166+
"createdAt": "2021-10-28T04:04:29.625Z",
167+
"updatedAt": "2021-10-28T04:04:29.625Z",
168+
"publishedAt": "2021-10-28T04:04:29.625Z",
169+
"revisedAt": "2021-10-28T04:04:29.625Z"
170+
}
171+
`,
172+
})
173+
174+
blog := Blog{}
175+
err := client.Get(
176+
GetParams{
177+
Endpoint: "blog",
178+
ContentID: "foo",
179+
},
180+
&blog,
181+
)
182+
require.NoError(t, err)
183+
require.EqualValues(
184+
t, Blog{
185+
ID: "foo",
186+
Title: "Hello, microCMS!",
187+
},
188+
blog,
189+
)
190+
}
191+
192+
func TestClient_Get_querystring(t *testing.T) {
193+
type Blog struct {
194+
ID string `json:"id"`
195+
Title string `json:"title"`
196+
}
197+
198+
client := New("serviceDomain", "apiKey")
199+
client.SetHTTPClient(&httpClientMock{
200+
t: t,
201+
method: http.MethodGet,
202+
url: &url.URL{
203+
Scheme: "https",
204+
Host: "serviceDomain.microcms.io",
205+
Path: "/api/v1/blog/foo",
206+
RawQuery: "depth=1&draftKey=abcd&fields=id%2Ctitle",
207+
},
208+
reqHeaders: map[string]string{"X-MICROCMS-API-KEY": "apiKey"},
209+
resHeaders: map[string]string{"Content-Type": "application/json"},
210+
resBody: `
211+
{
212+
"id": "foo",
213+
"title": "Hello, microCMS!",
214+
"createdAt": "2021-10-28T04:04:29.625Z",
215+
"updatedAt": "2021-10-28T04:04:29.625Z",
216+
"publishedAt": "2021-10-28T04:04:29.625Z",
217+
"revisedAt": "2021-10-28T04:04:29.625Z"
218+
}
219+
`,
220+
})
221+
222+
blog := Blog{}
223+
err := client.Get(
224+
GetParams{
225+
Endpoint: "blog",
226+
ContentID: "foo",
227+
DraftKey: "abcd",
228+
Fields: []string{"id", "title"},
229+
Depth: 1,
230+
},
231+
&blog,
232+
)
233+
require.NoError(t, err)
234+
require.EqualValues(
235+
t, Blog{
236+
ID: "foo",
237+
Title: "Hello, microCMS!",
238+
},
239+
blog,
240+
)
241+
}

create.go write.go

File renamed without changes.

0 commit comments

Comments
 (0)