WIP: more subreddit service tests
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
bbf7de995d
commit
33a14122bf
4 changed files with 2138 additions and 35 deletions
64
subreddit.go
64
subreddit.go
|
@ -21,10 +21,9 @@ type SubredditService interface {
|
||||||
GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
||||||
GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
||||||
|
|
||||||
GetMineWhereSubscriber(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
GetSubscribed(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
||||||
GetMineWhereContributor(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
GetApproved(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
||||||
GetMineWhereModerator(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
GetModerated(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
||||||
GetMineWhereStreams(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error)
|
|
||||||
|
|
||||||
GetSticky1(ctx context.Context, subreddit string) (*LinkAndComments, *Response, error)
|
GetSticky1(ctx context.Context, subreddit string) (*LinkAndComments, *Response, error)
|
||||||
GetSticky2(ctx context.Context, subreddit string) (*LinkAndComments, *Response, error)
|
GetSticky2(ctx context.Context, subreddit string) (*LinkAndComments, *Response, error)
|
||||||
|
@ -91,58 +90,53 @@ func (s *SubredditServiceOp) GetByName(ctx context.Context, subreddit string) (*
|
||||||
return root.Data, resp, nil
|
return root.Data, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPopular returns popular subreddits
|
// GetPopular returns popular subreddits.
|
||||||
func (s *SubredditServiceOp) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetPopular(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/popular", opts)
|
return s.getSubreddits(ctx, "subreddits/popular", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNew returns new subreddits
|
// GetNew returns new subreddits.
|
||||||
func (s *SubredditServiceOp) GetNew(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetNew(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/new", opts)
|
return s.getSubreddits(ctx, "subreddits/new", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGold returns gold subreddits
|
// GetGold returns gold subreddits.
|
||||||
func (s *SubredditServiceOp) GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetGold(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/gold", opts)
|
return s.getSubreddits(ctx, "subreddits/gold", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDefault returns default subreddits
|
// GetDefault returns default subreddits.
|
||||||
func (s *SubredditServiceOp) GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetDefault(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/default", opts)
|
return s.getSubreddits(ctx, "subreddits/default", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMineWhereSubscriber returns the list of subreddits the client is subscribed to
|
// GetSubscribed returns the list of subreddits the client is subscribed to.
|
||||||
func (s *SubredditServiceOp) GetMineWhereSubscriber(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetSubscribed(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/mine/subscriber", opts)
|
return s.getSubreddits(ctx, "subreddits/mine/subscriber", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMineWhereContributor returns the list of subreddits the client is a contributor to
|
// GetApproved returns the list of subreddits the client is an approved user in.
|
||||||
func (s *SubredditServiceOp) GetMineWhereContributor(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetApproved(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMineWhereModerator returns the list of subreddits the client is a moderator in
|
// GetModerated returns the list of subreddits the client is a moderator of.
|
||||||
func (s *SubredditServiceOp) GetMineWhereModerator(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditServiceOp) GetModerated(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
return s.getSubreddits(ctx, "subreddits/mine/moderator", opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMineWhereStreams returns the list of subreddits the client is subscribed to and has hosted videos in
|
// GetSticky1 returns the first stickied post on a subreddit (if it exists).
|
||||||
func (s *SubredditServiceOp) GetMineWhereStreams(ctx context.Context, opts *ListOptions) (*Subreddits, *Response, error) {
|
|
||||||
return s.getSubreddits(ctx, "subreddits/mine/contributor", opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSticky1 returns the first stickied post on a subreddit (if it exists)
|
|
||||||
func (s *SubredditServiceOp) GetSticky1(ctx context.Context, name string) (*LinkAndComments, *Response, error) {
|
func (s *SubredditServiceOp) GetSticky1(ctx context.Context, name string) (*LinkAndComments, *Response, error) {
|
||||||
return s.getSticky(ctx, name, sticky1)
|
return s.getSticky(ctx, name, sticky1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSticky2 returns the second stickied post on a subreddit (if it exists)
|
// GetSticky2 returns the second stickied post on a subreddit (if it exists).
|
||||||
func (s *SubredditServiceOp) GetSticky2(ctx context.Context, name string) (*LinkAndComments, *Response, error) {
|
func (s *SubredditServiceOp) GetSticky2(ctx context.Context, name string) (*LinkAndComments, *Response, error) {
|
||||||
return s.getSticky(ctx, name, sticky2)
|
return s.getSticky(ctx, name, sticky2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe subscribes to subreddits based on their names
|
// Subscribe subscribes to subreddits based on their names.
|
||||||
// Returns {} on success
|
// Returns {} on success.
|
||||||
func (s *SubredditServiceOp) Subscribe(ctx context.Context, subreddits ...string) (*Response, error) {
|
func (s *SubredditServiceOp) Subscribe(ctx context.Context, subreddits ...string) (*Response, error) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("action", "sub")
|
form.Set("action", "sub")
|
||||||
|
@ -150,8 +144,8 @@ func (s *SubredditServiceOp) Subscribe(ctx context.Context, subreddits ...string
|
||||||
return s.handleSubscription(ctx, form)
|
return s.handleSubscription(ctx, form)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeByID subscribes to subreddits based on their id
|
// SubscribeByID subscribes to subreddits based on their id.
|
||||||
// Returns {} on success
|
// Returns {} on success.
|
||||||
func (s *SubredditServiceOp) SubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
|
func (s *SubredditServiceOp) SubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("action", "sub")
|
form.Set("action", "sub")
|
||||||
|
@ -159,8 +153,8 @@ func (s *SubredditServiceOp) SubscribeByID(ctx context.Context, ids ...string) (
|
||||||
return s.handleSubscription(ctx, form)
|
return s.handleSubscription(ctx, form)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unsubscribe unsubscribes from subreddits based on their names
|
// Unsubscribe unsubscribes from subreddits based on their names.
|
||||||
// Returns {} on success
|
// Returns {} on success.
|
||||||
func (s *SubredditServiceOp) Unsubscribe(ctx context.Context, subreddits ...string) (*Response, error) {
|
func (s *SubredditServiceOp) Unsubscribe(ctx context.Context, subreddits ...string) (*Response, error) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("action", "unsub")
|
form.Set("action", "unsub")
|
||||||
|
@ -168,8 +162,8 @@ func (s *SubredditServiceOp) Unsubscribe(ctx context.Context, subreddits ...stri
|
||||||
return s.handleSubscription(ctx, form)
|
return s.handleSubscription(ctx, form)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnsubscribeByID unsubscribes from subreddits based on their id
|
// UnsubscribeByID unsubscribes from subreddits based on their id.
|
||||||
// Returns {} on success
|
// Returns {} on success.
|
||||||
func (s *SubredditServiceOp) UnsubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
|
func (s *SubredditServiceOp) UnsubscribeByID(ctx context.Context, ids ...string) (*Response, error) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Set("action", "unsub")
|
form.Set("action", "unsub")
|
||||||
|
@ -177,7 +171,7 @@ func (s *SubredditServiceOp) UnsubscribeByID(ctx context.Context, ids ...string)
|
||||||
return s.handleSubscription(ctx, form)
|
return s.handleSubscription(ctx, form)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchSubredditNames searches for subreddits with names beginning with the query provided
|
// SearchSubredditNames searches for subreddits with names beginning with the query provided.
|
||||||
func (s *SubredditServiceOp) SearchSubredditNames(ctx context.Context, query string) ([]string, *Response, error) {
|
func (s *SubredditServiceOp) SearchSubredditNames(ctx context.Context, query string) ([]string, *Response, error) {
|
||||||
path := fmt.Sprintf("api/search_reddit_names?query=%s", query)
|
path := fmt.Sprintf("api/search_reddit_names?query=%s", query)
|
||||||
|
|
||||||
|
@ -257,10 +251,10 @@ func (s *SubredditServiceOp) getSubreddits(ctx context.Context, path string, opt
|
||||||
return l, resp, nil
|
return l, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSticky returns one of the 2 stickied posts of the subreddit (if they exist)
|
// getSticky returns one of the 2 stickied posts of the subreddit (if they exist).
|
||||||
// Num should be equal to 1 or 2, depending on which one you want
|
// Num should be equal to 1 or 2, depending on which one you want.
|
||||||
// If it's <= 1, it's 1
|
// If it's <= 1, it's 1.
|
||||||
// If it's >= 2, it's 2
|
// If it's >= 2, it's 2.
|
||||||
func (s *SubredditServiceOp) getSticky(ctx context.Context, subreddit string, num sticky) (*LinkAndComments, *Response, error) {
|
func (s *SubredditServiceOp) getSticky(ctx context.Context, subreddit string, num sticky) (*LinkAndComments, *Response, error) {
|
||||||
type query struct {
|
type query struct {
|
||||||
Num sticky `url:"num"`
|
Num sticky `url:"num"`
|
||||||
|
|
|
@ -81,6 +81,35 @@ var expectedSubreddits = &Subreddits{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var expectedStickyPost = &LinkAndComments{
|
||||||
|
Link: Link{
|
||||||
|
ID: "hcl9gq",
|
||||||
|
FullID: "t3_hcl9gq",
|
||||||
|
Created: &Timestamp{time.Date(2020, 6, 20, 12, 8, 57, 0, time.UTC)},
|
||||||
|
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
|
||||||
|
|
||||||
|
Permalink: "/r/nba/comments/hcl9gq/daily_discussion_thread_freetalk_and_other/",
|
||||||
|
URL: "https://www.reddit.com/r/nba/comments/hcl9gq/daily_discussion_thread_freetalk_and_other/",
|
||||||
|
|
||||||
|
Title: "Daily Discussion Thread | Free-Talk and Other Updates - June 20, 2020",
|
||||||
|
Body: "Talk about whatever is on your mind, basketball related or not.\n\n# Useful Links \u0026amp; Other Resources\n\n[List of All #NBATogether Live Classic Games Streamed to Date](https://www.youtube.com/results?search_query=%23NBATogetherLive)\n\n[r/nba Discord Server](https://www.discord.gg/nba)\n\n[r/nba Twitter](https://twitter.com/nba_reddit)\n\n[Read Our Community's Rules and Guidelines](https://www.reddit.com/r/nba/wiki/rules)",
|
||||||
|
|
||||||
|
Score: 16,
|
||||||
|
UpvoteRatio: 0.82,
|
||||||
|
NumberOfComments: 25,
|
||||||
|
|
||||||
|
SubredditID: "t5_2qo4s",
|
||||||
|
SubredditName: "nba",
|
||||||
|
SubredditNamePrefixed: "r/nba",
|
||||||
|
|
||||||
|
AuthorID: "t2_6l4z3",
|
||||||
|
AuthorName: "AutoModerator",
|
||||||
|
|
||||||
|
IsSelfPost: true,
|
||||||
|
Stickied: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubredditServiceOp_GetByName(t *testing.T) {
|
func TestSubredditServiceOp_GetByName(t *testing.T) {
|
||||||
setup()
|
setup()
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
@ -160,3 +189,75 @@ func TestSubredditServiceOp_GetDefault(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expectedSubreddits, subreddits)
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetSubscribed(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/mine/subscriber", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetSubscribed(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetApproved(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/mine/contributor", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetApproved(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetModerated(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/mine/moderator", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetModerated(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo: WIP
|
||||||
|
func TestSubredditServiceOp_GetSticky1(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/sticky.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/r/nba/about/sticky", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "1", r.Form.Get("num"))
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
post, _, err := client.Subreddit.GetSticky1(ctx, "nba")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedStickyPost.Link, post.Link)
|
||||||
|
// b, _ := json.MarshalIndent(post.Comments, "", " ")
|
||||||
|
// fmt.Println(string(b))
|
||||||
|
}
|
||||||
|
|
2007
testdata/subreddit/sticky.json
vendored
Normal file
2007
testdata/subreddit/sticky.json
vendored
Normal file
File diff suppressed because it is too large
Load diff
|
@ -82,6 +82,7 @@ func (t Timespan) String() string {
|
||||||
return timespans[t]
|
return timespans[t]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: remove this, it is not needed
|
||||||
type sticky int
|
type sticky int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
Loading…
Reference in a new issue