-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsonplaceholder_test.go
74 lines (57 loc) · 1.16 KB
/
jsonplaceholder_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
package jsonplaceholder_test
import (
"context"
"testing"
"github.com/postfinance/httpclient/example/jsonplaceholder"
)
func getClient(t *testing.T) *jsonplaceholder.Client {
c, err := jsonplaceholder.NewClient(
"https://jsonplaceholder.typicode.com",
)
if err != nil {
t.Fatal(err)
}
return c
}
func TestCreate(t *testing.T) {
c := getClient(t)
p := jsonplaceholder.Post{
UserID: 101,
Title: "Blog post title",
Body: "Blog post body",
}
post, _, err := c.Post.Create(context.Background(), &p)
if err != nil {
t.Error(err)
}
if post.ID == 0 {
t.Error("post ID cannot be zero")
}
t.Logf("%#v\n", post)
}
func TestGet(t *testing.T) {
c := getClient(t)
post, _, err := c.Post.Get(context.Background(), 1)
if err != nil {
t.Error(err)
}
t.Logf("%#v\n", post)
}
func TestList(t *testing.T) {
c := getClient(t)
posts, _, err := c.Post.List(context.Background())
if err != nil {
t.Error(err)
}
if len(posts) == 0 {
t.Error("no posts found")
}
t.Logf("number of posts: %d\n", len(posts))
}
func TestDelete(t *testing.T) {
c := getClient(t)
_, err := c.Post.Delete(context.Background(), 1)
if err != nil {
t.Error(err)
}
}