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/twitterapi/client_test.go

153 lines
5.0 KiB
Go
Raw Normal View History

2022-05-24 19:20:28 +00:00
package twitterapi_test
//go:generate mockery --recursive --name Getter --output ../generated/mocks
2022-05-22 20:11:33 +00:00
import (
2022-05-23 03:11:17 +00:00
"errors"
2022-05-22 20:11:33 +00:00
"io"
"net/http"
"strings"
"testing"
2022-05-24 19:20:28 +00:00
"time"
2022-05-22 20:11:33 +00:00
"git.netflux.io/rob/elon-eats-my-tweets/generated/mocks"
2022-05-24 19:20:28 +00:00
"git.netflux.io/rob/elon-eats-my-tweets/twitterapi"
2022-05-22 20:11:33 +00:00
"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-24 19:20:28 +00:00
wantUser *twitterapi.User
2022-05-22 20:11:33 +00:00
wantErr string
}{
{
name: "successful request",
responseStatusCode: 200,
responseBody: strings.NewReader(`{"Data": {"id": "1", "name": "foo", "username": "foo bar"}}`),
2022-05-24 19:20:28 +00:00
wantUser: &twitterapi.User{ID: "1", Name: "foo", Username: "foo bar"},
2022-05-22 20:11:33 +00:00
},
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-24 19:20:28 +00:00
client := twitterapi.NewClient(&getter)
2022-05-22 20:11:33 +00:00
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-24 19:20:28 +00:00
const userID = "1"
utcLoc, _ := time.LoadLocation("UTC")
2022-05-22 20:11:33 +00:00
testCases := []struct {
name string
2022-05-24 19:20:28 +00:00
sinceID string
wantAPIURL string
2022-05-22 20:11:33 +00:00
responseStatusCode int
responseBody io.Reader
2022-05-23 03:11:17 +00:00
responseErr error
2022-05-24 19:20:28 +00:00
wantTweets []*twitterapi.Tweet
2022-05-22 20:11:33 +00:00
wantErr string
}{
{
2022-05-24 19:20:28 +00:00
name: "successful request, empty since_id",
wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100",
responseStatusCode: 200,
responseBody: strings.NewReader(`{"Data": [{"id": "101", "text": "foo", "created_at": "2019-06-04T23:12:08.000Z"}, {"id": "102", "text": "bar", "created_at": "2019-06-04T23:12:08.000Z"}]}`),
wantTweets: []*twitterapi.Tweet{
{ID: "101", Text: "foo", CreatedAt: time.Date(2019, 6, 4, 23, 12, 8, 0, utcLoc)},
{ID: "102", Text: "bar", CreatedAt: time.Date(2019, 6, 4, 23, 12, 8, 0, utcLoc)},
},
},
{
name: "successful request, non-empty since_id",
sinceID: "2",
wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100&since_id=2",
2022-05-22 20:11:33 +00:00
responseStatusCode: 200,
2022-05-24 19:20:28 +00:00
responseBody: strings.NewReader(`{"Data": [{"id": "101", "text": "foo", "created_at": "2019-06-04T23:12:08.000Z"}, {"id": "102", "text": "bar", "created_at": "2019-06-04T23:12:08.000Z"}]}`),
wantTweets: []*twitterapi.Tweet{
{ID: "101", Text: "foo", CreatedAt: time.Date(2019, 6, 4, 23, 12, 8, 0, utcLoc)},
{ID: "102", Text: "bar", CreatedAt: time.Date(2019, 6, 4, 23, 12, 8, 0, utcLoc)},
},
2022-05-22 20:11:33 +00:00
},
2022-05-23 03:11:17 +00:00
{
name: "network error",
2022-05-24 19:20:28 +00:00
wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100",
2022-05-23 03:11:17 +00:00
responseErr: errors.New("network error"),
wantTweets: nil,
wantErr: "error fetching resource: network error",
},
2022-05-22 20:11:33 +00:00
{
name: "500 response",
2022-05-24 19:20:28 +00:00
wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100",
2022-05-22 20:11:33 +00:00
responseStatusCode: 500,
responseBody: strings.NewReader("whale"),
wantTweets: nil,
wantErr: "error fetching resource: status code 500",
},
{
name: "decoder error",
2022-05-24 19:20:28 +00:00
wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100",
2022-05-22 20:11:33 +00:00
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-24 19:20:28 +00:00
On("Get", tc.wantAPIURL).
2022-05-23 03:11:17 +00:00
Return(&http.Response{StatusCode: tc.responseStatusCode, Body: io.NopCloser(tc.responseBody)}, tc.responseErr)
2022-05-24 19:20:28 +00:00
client := twitterapi.NewClient(&getter)
2022-05-22 20:11:33 +00:00
2022-05-24 19:20:28 +00:00
tweets, err := client.GetTweets(userID, tc.sinceID)
2022-05-22 20:11:33 +00:00
2022-05-24 19:20:28 +00:00
assert.Equal(t, tc.wantTweets, tweets)
2022-05-22 20:11:33 +00:00
if tc.wantErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.wantErr)
}
})
}
}