This repository has been archived on 2022-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
elon-eats-my-tweets/twitter/api_test.go

131 lines
3.6 KiB
Go

package twitter_test
import (
"errors"
"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
responseErr error
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"},
},
{
name: "network error",
responseErr: errors.New("boom"),
wantErr: "error fetching resource: boom",
},
{
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").
Return(&http.Response{StatusCode: tc.responseStatusCode, Body: io.NopCloser(tc.responseBody)}, tc.responseErr)
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) {
const (
userID = "1"
sinceID = "2"
)
testCases := []struct {
name string
responseStatusCode int
responseBody io.Reader
responseErr error
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"}},
},
{
name: "network error",
responseErr: errors.New("network error"),
wantTweets: nil,
wantErr: "error fetching resource: network error",
},
{
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.
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)
client := twitter.NewAPIClient(&getter)
tweets, err := client.GetTweets(userID, sinceID)
assert.Equal(t, tc.wantTweets, tweets)
if tc.wantErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.wantErr)
}
})
}
}