-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlist_tweets_lookup_test.go
202 lines (198 loc) · 5.01 KB
/
list_tweets_lookup_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
package gotwtr_test
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/sivchari/gotwtr"
)
func Test_lookUpListTweets(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
client *http.Client
id string
opt []*gotwtr.ListTweetsOption
}
tests := []struct {
name string
args args
want *gotwtr.ListTweetsResponse
wantErr bool
}{
{
name: "200 ok no option",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
body := `{
"data": [
{
"id": "1067094924124872705",
"text": "Just getting started with Twitter APIs? Find out what you need in order to build an app. Watch this video! https://t.co/Hg8nkfoizN"
}
],
"meta": {
"result_count": 1
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
}
}),
id: "2244994945",
opt: []*gotwtr.ListTweetsOption{},
},
want: &gotwtr.ListTweetsResponse{
Tweets: []*gotwtr.Tweet{
{
ID: "1067094924124872705",
Text: "Just getting started with Twitter APIs? Find out what you need in order to build an app. Watch this video! https://t.co/Hg8nkfoizN",
},
},
Meta: &gotwtr.ListMeta{
ResultCount: 1,
},
Includes: nil,
Errors: nil,
},
wantErr: false,
},
{
name: "200 ok option",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
body := `{
"data": [
{
"author_id": "2244994945",
"created_at": "2018-11-26T16:37:10.000Z",
"text": "Just getting started with Twitter APIs? Find out what you need in order to build an app. Watch this video! https://t.co/Hg8nkfoizN",
"id": "1067094924124872705"
}
],
"includes": {
"users": [
{
"verified": true,
"username": "TwitterDev",
"id": "2244994945",
"name": "Twitter Dev"
}
]
},
"meta": {
"result_count": 1
}
}`
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
}
}),
id: "84839422",
opt: []*gotwtr.ListTweetsOption{
{
UserFields: []gotwtr.UserField{
gotwtr.UserFieldVerified,
gotwtr.UserFieldUserName,
gotwtr.UserFieldID,
gotwtr.UserFieldName,
},
},
},
},
want: &gotwtr.ListTweetsResponse{
Tweets: []*gotwtr.Tweet{
{
AuthorID: "2244994945",
CreatedAt: "2018-11-26T16:37:10.000Z",
Text: "Just getting started with Twitter APIs? Find out what you need in order to build an app. Watch this video! https://t.co/Hg8nkfoizN",
ID: "1067094924124872705",
},
},
Includes: &gotwtr.ListIncludes{
Users: []*gotwtr.User{
{
Verified: true,
UserName: "TwitterDev",
ID: "2244994945",
Name: "Twitter Dev",
},
},
},
Meta: &gotwtr.ListMeta{
ResultCount: 1,
},
Errors: nil,
},
wantErr: false,
},
{
name: "404 not found",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
body := `{
"errors":[
{
"parameters":{
"id":[
"111111111111111111111111"
]
},
"message":"The id query parameter value [111111111111111111111111] is not valid"
}
],
"title":"Invalid Request",
"detail":"One or more parameters to your request was invalid.",
"type":"https://api.twitter.com/2/problems/invalid-request"
}`
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(strings.NewReader(body)),
}
}),
id: "111111111111111111111111",
opt: []*gotwtr.ListTweetsOption{},
},
want: &gotwtr.ListTweetsResponse{
Tweets: nil,
Errors: []*gotwtr.APIResponseError{
{
Parameters: gotwtr.Parameter{
ID: []string{"111111111111111111111111"},
},
Message: "The id query parameter value [111111111111111111111111] is not valid",
},
},
Meta: nil,
Title: "Invalid Request",
Detail: "One or more parameters to your request was invalid.",
Type: "https://api.twitter.com/2/problems/invalid-request",
},
wantErr: true,
},
}
for i, tt := range tests {
i := i
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := gotwtr.New("key", gotwtr.WithHTTPClient(tt.args.client))
got, err := c.LookUpListTweets(tt.args.ctx, tt.args.id, tt.args.opt...)
if (err != nil) != tt.wantErr {
t.Errorf("client.LookUpListsTweetsByID() index = %v error = %v, wantErr %v", i, err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("client.LookUpListsTweetsByID() index = %v mismatch (-want +got):\n%s", i, diff)
return
}
})
}
}