-
Notifications
You must be signed in to change notification settings - Fork 13
/
manage_tweet_test.go
144 lines (138 loc) · 3.24 KB
/
manage_tweet_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
package gotwtr_test
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/sivchari/gotwtr"
)
func Test_postTweet(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
client *http.Client
body *gotwtr.PostTweetOption
}
tests := []struct {
name string
args args
want *gotwtr.PostTweetResponse
wantErr bool
}{
{
name: "201 ok default payload",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(request *http.Request) *http.Response {
body := `
{
"data": {
"id": "1445880548472328192",
"text": "Hello world!"
}
}
`
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(strings.NewReader(body)),
}
}),
body: &gotwtr.PostTweetOption{
Text: "Hello world!",
},
},
want: &gotwtr.PostTweetResponse{
PostTweetData: gotwtr.PostTweetData{
ID: "1445880548472328192",
Text: "Hello world!",
},
},
wantErr: false,
},
}
for i, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := gotwtr.New("test-key", gotwtr.WithHTTPClient(tt.args.client))
got, err := c.PostTweet(tt.args.ctx, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("PostTweet() index = %v error = %v, wantErr %v", i, err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("PostTweet() index = %v mismatch (-want +got):\n%s", i, diff)
return
}
})
}
}
func Test_deleteTweet(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
client *http.Client
tweetID string
}
tests := []struct {
name string
args args
want *gotwtr.DeleteTweetResponse
wantErr bool
}{
{
name: "200 ok default payload",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(request *http.Request) *http.Response {
body := `
{
"data": {
"deleted": true
}
}
`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
}
}),
tweetID: "1445880548472328192",
},
want: &gotwtr.DeleteTweetResponse{
Data: gotwtr.DeleteTweetData{
Deleted: true,
},
},
wantErr: false,
},
{
name: "empty tweet id",
args: args{
ctx: context.Background(),
tweetID: "",
},
want: nil,
wantErr: true,
},
}
for i, tt := range tests {
i := i
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := gotwtr.New("test-key", gotwtr.WithHTTPClient(tt.args.client))
got, err := c.DeleteTweet(tt.args.ctx, tt.args.tweetID)
if (err != nil) != tt.wantErr {
t.Errorf("DeleteTweet() index = %v error = %v, wantErr %v", i, err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("DeleteTweet() index = %v mismatch (-want +got):\n%s", i, diff)
return
}
})
}
}