refactoring and fixing bug.

This commit is contained in:
didier amyot committed 2014-12-07 20:07:39 -08:00
1 parent a617cf50ee
commit 40130e3c13
6 files changed
+205 -202

No files matched your search

+3 -183
View File
@@ -2,183 +2,10 @@ package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestGet(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Error("bad request verb")
}
if auth := r.Header.Get("Authorization"); auth != "Bearer access_token" {
t.Error("bad authorization in header :", auth)
}
if contentType := r.Header.Get("Content-Type"); contentType != "application/json; charset=utf-8" {
t.Error("bad content-type in header ", contentType)
}
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
if res, err := tw.Get("/"); err != nil {
t.Error(err)
} else {
ioutil.ReadAll(res.Body)
}
}
func TestPost(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Error("bad request verb")
}
if auth := r.Header.Get("Authorization"); auth != "Bearer access_token" {
t.Error("bad authorization in header :", auth)
}
if contentType := r.Header.Get("Content-Type"); contentType != "application/json; charset=utf-8" {
t.Error("bad content-type in header ", contentType)
}
if body, err := ioutil.ReadAll(r.Body); err != nil {
t.Error(err)
} else if string(body) != `{"data": "is_hugue"}` {
t.Error("bad body", string(body))
}
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
if res, err := tw.Post("/", `{"data": "is_hugue"}`); err != nil {
t.Error(err)
} else {
ioutil.ReadAll(res.Body)
}
}
func TestGetAndDeserializeError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
fmt.Fprint(w, `{"error": "bar"}`)
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
m := make(map[string]string)
if err := tw.GetAndDeserialize("/foo", map[string]string{}, &m); err == nil {
t.Error("shoud have an error message here")
} else if err.Error() != `{"error": "bar"}` {
t.Error("error message should propagate here")
} else if terr, ok := err.(*TwitterErr); !ok {
t.Error("the error should be a twitter error")
} else if terr.Status != 404 {
t.Error("bad status")
}
}
func TestGetAndDeserialize(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("foo") != "bar" {
t.Error("bad params")
}
fmt.Fprint(w, `{"foo": "bar"}`)
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
m := make(map[string]string)
if err := tw.GetAndDeserialize("/foo", map[string]string{"foo": "bar"}, &m); err != nil {
t.Error(err)
}
if m["foo"] != "bar" {
t.Fail()
}
}
func TestPostAndDeserialize(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if body, err := ioutil.ReadAll(r.Body); err != nil {
t.Error(err)
} else if string(body) != `val=7` {
t.Error("bad body")
}
fmt.Fprint(w, `{"foo": "bar"}`)
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
m := make(map[string]string)
if err := tw.PostAndDeserialize("/foo", map[string]string{"val": "7"}, &m); err != nil {
t.Error(err)
}
if m["foo"] != "bar" {
t.Fail()
}
}
func TestGetTwitterIdByScreenName(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if url_ := r.URL.String(); url_ != "/users/lookup.json?include_entities=id&screen_name=bobLeChef" {
t.Error("bad url :", url_)
}
fmt.Fprint(w, `[{"id": 1492}]`)
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
if id, err := tw.GetTwitterIdByScreenName("bobLeChef"); err != nil {
t.Error(err)
} else if id != 1492 {
t.Error("found bad id")
}
}
func TestGetFollowerByCursor(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if url_ := r.URL.String(); url_ != "/followers/list.json?count=200&cursor=89&screen_name=bobLeChef&skip_status=true" {
t.Error("bad url :", url_)
}
fmt.Fprint(w, `{"users": [{"id": 1492, "screen_name": "bob_le_chef"}], "next_cursor_str": "1793"}`)
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
followersListC := tw.GetFollowerByCursor("bobLeChef", "89")
followers := <-followersListC
if followers.NextCursor != "1793" {
t.Error("bad cursor")
} else if len(followers.Followers) != 1 {
t.Error("bad number of followers")
} else if user := followers.Followers[0]; !reflect.DeepEqual(user, &User{"bob_le_chef", 1492}) {
t.Error("bad user", user)
}
}
func TestGetFollowerIdsByCursor(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if url_ := r.URL.String(); url_ != "/followers/ids.json?count=5000&cursor=89&screen_name=bobLeChef" {
t.Error("bad url :", url_)
}
fmt.Fprint(w, `{"ids": [1492], "next_cursor_str": "1793"}`)
}))
defer ts.Close()
tw := NewTwitterApi(ts.URL, "access_token")
followersListC := tw.GetGetFollowerIdsByCursor("bobLeChef", "89")
followers := <-followersListC
if followers.NextCursor != "1793" {
t.Error("bad cursor")
} else if len(followers.Followers) != 1 {
t.Error("bad number of followers", len(followers.Followers))
} else if id := followers.Followers[0]; id != 1492 {
t.Error("bad id")
}
}
type MockFollowerGetter struct {
T *testing.T
}
@@ -203,7 +30,7 @@ func (m *MockFollowerGetter) GetFollowerByCursor(screenName, cursor string) <-ch
return followerListC
}
func (m *MockFollowerGetter) GetGetFollowerIdsByCursor(screenName, cursor string) <-chan *FollowerIDList {
func (m *MockFollowerGetter) GetFollowerIdsByCursor(screenName, cursor string) <-chan *FollowerIDList {
followerListC := make(chan *FollowerIDList)
go func() {
switch cursor {
@@ -265,13 +92,6 @@ func TestGetFollowerIds(t *testing.T) {
}
}
func TestAsCommaSeparatedString(t *testing.T) {
tw := new(TwitterApi)
if s := tw.asCommaSeparatedString([]uint64{10765432100123456789, 78}); s != "10765432100123456789,78" {
t.Error(s)
}
}
func uint64Range(n int) []uint64 {
ret := make([]uint64, n)
for i := 0; i < n; i++ {
@@ -280,11 +100,11 @@ func uint64Range(n int) []uint64 {
return ret
}
func TestGetTwitterScreenNameByIds(t *testing.T) {
func TestGetScreenNameByIds(t *testing.T) {
fg := &MockFollowerGetter{t}
ids := uint64Range(233)
ret := make(map[string]bool)
for name := range GetTwitterScreenNameByIds(fg, makeUInt64Channel(ids...)) {
for name := range GetScreenNameByIds(fg, makeUInt64Channel(ids...)) {
ret[name] = true
}
if len(ret) != 233 {