package twitterapi_test //go:generate mockery --recursive --name Getter --output ../generated/mocks import ( "errors" "io" "net/http" "strings" "testing" "time" "git.netflux.io/rob/elon-eats-my-tweets/generated/mocks" "git.netflux.io/rob/elon-eats-my-tweets/twitterapi" "github.com/stretchr/testify/assert" ) func TestGetMe(t *testing.T) { testCases := []struct { name string responseStatusCode int responseBody io.Reader responseErr error wantUser *twitterapi.User wantErr string }{ { name: "successful request", responseStatusCode: 200, responseBody: strings.NewReader(`{"Data": {"id": "1", "name": "foo", "username": "foo bar"}}`), wantUser: &twitterapi.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(""), 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 := twitterapi.NewClient(&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" utcLoc, _ := time.LoadLocation("UTC") testCases := []struct { name string sinceID string wantAPIURL string responseStatusCode int responseBody io.Reader responseErr error wantTweets []*twitterapi.Tweet wantErr string }{ { 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", 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: "network error", wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100", responseErr: errors.New("network error"), wantTweets: nil, wantErr: "error fetching resource: network error", }, { name: "500 response", wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100", responseStatusCode: 500, responseBody: strings.NewReader("whale"), wantTweets: nil, wantErr: "error fetching resource: status code 500", }, { name: "decoder error", wantAPIURL: "https://api.twitter.com/2/users/" + userID + "/tweets?tweet.fields=created_at&max_results=100", responseStatusCode: 200, responseBody: strings.NewReader(""), 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", tc.wantAPIURL). Return(&http.Response{StatusCode: tc.responseStatusCode, Body: io.NopCloser(tc.responseBody)}, tc.responseErr) client := twitterapi.NewClient(&getter) tweets, err := client.GetTweets(userID, tc.sinceID) assert.Equal(t, tc.wantTweets, tweets) if tc.wantErr == "" { assert.NoError(t, err) } else { assert.EqualError(t, err, tc.wantErr) } }) } }