2022-05-22 20:11:33 +00:00
|
|
|
package twitter_test
|
|
|
|
|
|
|
|
import (
|
2022-05-23 03:11:17 +00:00
|
|
|
"errors"
|
2022-05-22 20:11:33 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.netflux.io/rob/elon-eats-my-tweets/generated/mocks"
|
|
|
|
"git.netflux.io/rob/elon-eats-my-tweets/twitter"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetMe(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
responseStatusCode int
|
|
|
|
responseBody io.Reader
|
2022-05-23 03:11:17 +00:00
|
|
|
responseErr error
|
2022-05-22 20:11:33 +00:00
|
|
|
wantUser *twitter.User
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "successful request",
|
|
|
|
responseStatusCode: 200,
|
|
|
|
responseBody: strings.NewReader(`{"Data": {"id": "1", "name": "foo", "username": "foo bar"}}`),
|
|
|
|
wantUser: &twitter.User{ID: "1", Name: "foo", Username: "foo bar"},
|
|
|
|
},
|
2022-05-23 03:11:17 +00:00
|
|
|
{
|
|
|
|
name: "network error",
|
|
|
|
responseErr: errors.New("boom"),
|
|
|
|
wantErr: "error fetching resource: boom",
|
|
|
|
},
|
2022-05-22 20:11:33 +00:00
|
|
|
{
|
|
|
|
name: "500 response",
|
|
|
|
responseStatusCode: 500,
|
|
|
|
responseBody: strings.NewReader("whale"),
|
|
|
|
wantUser: nil,
|
|
|
|
wantErr: "error fetching resource: status code 500",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "decoder error",
|
|
|
|
responseStatusCode: 200,
|
|
|
|
responseBody: strings.NewReader("<html></html>"),
|
|
|
|
wantUser: nil,
|
|
|
|
wantErr: "error decoding resource: invalid character '<' looking for beginning of value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var getter mocks.Getter
|
|
|
|
getter.
|
|
|
|
On("Get", "https://api.twitter.com/2/users/me").
|
2022-05-23 03:11:17 +00:00
|
|
|
Return(&http.Response{StatusCode: tc.responseStatusCode, Body: io.NopCloser(tc.responseBody)}, tc.responseErr)
|
2022-05-22 20:11:33 +00:00
|
|
|
client := twitter.NewAPIClient(&getter)
|
|
|
|
|
|
|
|
user, err := client.GetMe()
|
|
|
|
assert.Equal(t, tc.wantUser, user)
|
|
|
|
|
|
|
|
if tc.wantErr == "" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTweets(t *testing.T) {
|
2022-05-23 03:11:17 +00:00
|
|
|
const (
|
|
|
|
userID = "1"
|
|
|
|
sinceID = "2"
|
|
|
|
)
|
2022-05-22 20:11:33 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
responseStatusCode int
|
|
|
|
responseBody io.Reader
|
2022-05-23 03:11:17 +00:00
|
|
|
responseErr error
|
2022-05-22 20:11:33 +00:00
|
|
|
wantTweets []*twitter.Tweet
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "successful request",
|
|
|
|
responseStatusCode: 200,
|
|
|
|
responseBody: strings.NewReader(`{"Data": [{"id": "101", "text": "foo"}, {"id": "102", "text": "bar"}]}`),
|
|
|
|
wantTweets: []*twitter.Tweet{{ID: "101", Text: "foo"}, {ID: "102", Text: "bar"}},
|
|
|
|
},
|
2022-05-23 03:11:17 +00:00
|
|
|
{
|
|
|
|
name: "network error",
|
|
|
|
responseErr: errors.New("network error"),
|
|
|
|
wantTweets: nil,
|
|
|
|
wantErr: "error fetching resource: network error",
|
|
|
|
},
|
2022-05-22 20:11:33 +00:00
|
|
|
{
|
|
|
|
name: "500 response",
|
|
|
|
responseStatusCode: 500,
|
|
|
|
responseBody: strings.NewReader("whale"),
|
|
|
|
wantTweets: nil,
|
|
|
|
wantErr: "error fetching resource: status code 500",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "decoder error",
|
|
|
|
responseStatusCode: 200,
|
|
|
|
responseBody: strings.NewReader("<html></html>"),
|
|
|
|
wantTweets: nil,
|
|
|
|
wantErr: "error decoding resource: invalid character '<' looking for beginning of value",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var getter mocks.Getter
|
|
|
|
getter.
|
2022-05-23 03:11:17 +00:00
|
|
|
On("Get", "https://api.twitter.com/2/users/"+userID+"/tweets?since_id="+sinceID).
|
|
|
|
Return(&http.Response{StatusCode: tc.responseStatusCode, Body: io.NopCloser(tc.responseBody)}, tc.responseErr)
|
2022-05-22 20:11:33 +00:00
|
|
|
client := twitter.NewAPIClient(&getter)
|
|
|
|
|
2022-05-23 03:11:17 +00:00
|
|
|
tweets, err := client.GetTweets(userID, sinceID)
|
2022-05-22 20:11:33 +00:00
|
|
|
assert.Equal(t, tc.wantTweets, tweets)
|
|
|
|
|
|
|
|
if tc.wantErr == "" {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, err, tc.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|