Fix tests

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian 2020-07-30 12:22:39 -04:00
parent 112f7f0370
commit d65358b6cc
5 changed files with 95 additions and 131 deletions

14
post.go
View file

@ -497,7 +497,7 @@ func addCommentToTree(pc *PostAndComments, comment *Comment) {
}
}
func (s *PostService) random(ctx context.Context, subreddits ...string) (*Post, []*Comment, *Response, error) {
func (s *PostService) random(ctx context.Context, subreddits ...string) (*PostAndComments, *Response, error) {
path := "random"
if len(subreddits) > 0 {
path = fmt.Sprintf("r/%s/random", strings.Join(subreddits, "+"))
@ -505,30 +505,30 @@ func (s *PostService) random(ctx context.Context, subreddits ...string) (*Post,
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}
root := new(PostAndComments)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, resp, err
return nil, resp, err
}
return root.Post, root.Comments, resp, nil
return root, resp, nil
}
// RandomFromSubreddits returns a random post and its comments from the subreddits.
// If no subreddits are provided, Reddit runs the query against your subscriptions.
func (s *PostService) RandomFromSubreddits(ctx context.Context, subreddits ...string) (*Post, []*Comment, *Response, error) {
func (s *PostService) RandomFromSubreddits(ctx context.Context, subreddits ...string) (*PostAndComments, *Response, error) {
return s.random(ctx, subreddits...)
}
// Random returns a random post and its comments from all of Reddit.
func (s *PostService) Random(ctx context.Context) (*Post, []*Comment, *Response, error) {
func (s *PostService) Random(ctx context.Context) (*PostAndComments, *Response, error) {
return s.random(ctx, "all")
}
// RandomFromSubscriptions returns a random post and its comments from your subscriptions.
func (s *PostService) RandomFromSubscriptions(ctx context.Context) (*Post, []*Comment, *Response, error) {
func (s *PostService) RandomFromSubscriptions(ctx context.Context) (*PostAndComments, *Response, error) {
return s.random(ctx)
}

View file

@ -10,84 +10,85 @@ import (
"github.com/stretchr/testify/assert"
)
var expectedPost2 = &Post{
ID: "testpost",
FullID: "t3_testpost",
Created: &Timestamp{time.Date(2020, 7, 18, 10, 26, 7, 0, time.UTC)},
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/"),
URL: "https://www.reddit.com/r/test/comments/testpost/test/",
Title: "Test",
Body: "Hello",
Score: 1,
UpvoteRatio: 1,
NumberOfComments: 2,
SubredditID: "t5_2qh23",
SubredditName: "test",
SubredditNamePrefixed: "r/test",
AuthorID: "t2_testuser",
AuthorName: "testuser",
IsSelfPost: true,
}
var expectedComments = []*Comment{
{
ID: "testc1",
FullID: "t1_testc1",
Created: &Timestamp{time.Date(2020, 7, 18, 10, 31, 59, 0, time.UTC)},
var expectedPostAndComments = &PostAndComments{
Post: &Post{
ID: "testpost",
FullID: "t3_testpost",
Created: &Timestamp{time.Date(2020, 7, 18, 10, 26, 7, 0, time.UTC)},
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
ParentID: "t3_testpost",
Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/testc1/"),
Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/"),
URL: "https://www.reddit.com/r/test/comments/testpost/test/",
Body: "Hi",
Author: "testuser",
AuthorID: "t2_testuser",
Subreddit: "test",
SubredditNamePrefixed: "r/test",
SubredditID: "t5_2qh23",
Title: "Test",
Body: "Hello",
Score: 1,
Controversiality: 0,
UpvoteRatio: 1,
NumberOfComments: 2,
PostID: "t3_testpost",
SubredditID: "t5_2qh23",
SubredditName: "test",
SubredditNamePrefixed: "r/test",
IsSubmitter: true,
CanGild: true,
AuthorID: "t2_testuser",
AuthorName: "testuser",
Replies: Replies{
Comments: []*Comment{
{
ID: "testc2",
FullID: "t1_testc2",
Created: &Timestamp{time.Date(2020, 7, 18, 10, 32, 28, 0, time.UTC)},
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
IsSelfPost: true,
},
Comments: []*Comment{
{
ID: "testc1",
FullID: "t1_testc1",
Created: &Timestamp{time.Date(2020, 7, 18, 10, 31, 59, 0, time.UTC)},
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
ParentID: "t1_testc1",
Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/testc2/"),
ParentID: "t3_testpost",
Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/testc1/"),
Body: "Hello",
Author: "testuser",
AuthorID: "t2_testuser",
Body: "Hi",
Author: "testuser",
AuthorID: "t2_testuser",
Subreddit: "test",
SubredditNamePrefixed: "r/test",
SubredditID: "t5_2qh23",
Subreddit: "test",
SubredditNamePrefixed: "r/test",
SubredditID: "t5_2qh23",
Score: 1,
Controversiality: 0,
Score: 1,
Controversiality: 0,
PostID: "t3_testpost",
PostID: "t3_testpost",
IsSubmitter: true,
CanGild: true,
IsSubmitter: true,
CanGild: true,
Replies: Replies{
Comments: []*Comment{
{
ID: "testc2",
FullID: "t1_testc2",
Created: &Timestamp{time.Date(2020, 7, 18, 10, 32, 28, 0, time.UTC)},
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
ParentID: "t1_testc1",
Permalink: Permalink("https://www.reddit.com/r/test/comments/testpost/test/testc2/"),
Body: "Hello",
Author: "testuser",
AuthorID: "t2_testuser",
Subreddit: "test",
SubredditNamePrefixed: "r/test",
SubredditID: "t5_2qh23",
Score: 1,
Controversiality: 0,
PostID: "t3_testpost",
IsSubmitter: true,
CanGild: true,
},
},
},
},
@ -141,10 +142,9 @@ func TestPostService_Get(t *testing.T) {
fmt.Fprint(w, blob)
})
post, comments, _, err := client.Post.Get(ctx, "test")
postAndComments, _, err := client.Post.Get(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments)
assert.Equal(t, expectedPostAndComments, postAndComments)
}
func TestPostService_SubmitText(t *testing.T) {
@ -741,7 +741,7 @@ func TestPostService_More(t *testing.T) {
fmt.Fprint(w, blob)
})
_, err = client.Post.More(ctx, parentComment)
_, err = client.Comment.LoadMoreReplies(ctx, parentComment)
assert.NoError(t, err)
assert.Nil(t, parentComment.Replies.MoreComments)
assert.Len(t, parentComment.Replies.Comments, 1)
@ -752,7 +752,7 @@ func TestPostService_MoreNil(t *testing.T) {
setup()
defer teardown()
_, err := client.Post.More(ctx, nil)
_, err := client.Comment.LoadMoreReplies(ctx, nil)
assert.EqualError(t, err, "comment: must not be nil")
parentComment := &Comment{
@ -762,7 +762,7 @@ func TestPostService_MoreNil(t *testing.T) {
}
// should return nil, nil since comment does not have More struct
resp, err := client.Post.More(ctx, parentComment)
resp, err := client.Comment.LoadMoreReplies(ctx, parentComment)
assert.NoError(t, err)
assert.Nil(t, resp)
@ -771,7 +771,7 @@ func TestPostService_MoreNil(t *testing.T) {
}
// should return nil, nil since comment's More struct has 0 children
resp, err = client.Post.More(ctx, parentComment)
resp, err = client.Comment.LoadMoreReplies(ctx, parentComment)
assert.NoError(t, err)
assert.Nil(t, resp)
}
@ -788,10 +788,9 @@ func TestPostService_RandomFromSubreddits(t *testing.T) {
fmt.Fprint(w, blob)
})
post, comments, _, err := client.Post.RandomFromSubreddits(ctx, "test")
postAndComments, _, err := client.Post.RandomFromSubreddits(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments)
assert.Equal(t, expectedPostAndComments, postAndComments)
}
func TestPostService_Random(t *testing.T) {
@ -806,10 +805,9 @@ func TestPostService_Random(t *testing.T) {
fmt.Fprint(w, blob)
})
post, comments, _, err := client.Post.Random(ctx)
postAndComments, _, err := client.Post.Random(ctx)
assert.NoError(t, err)
assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments)
assert.Equal(t, expectedPostAndComments, postAndComments)
}
func TestPostService_RandomFromSubscriptions(t *testing.T) {
@ -824,10 +822,9 @@ func TestPostService_RandomFromSubscriptions(t *testing.T) {
fmt.Fprint(w, blob)
})
post, comments, _, err := client.Post.RandomFromSubscriptions(ctx)
postAndComments, _, err := client.Post.RandomFromSubscriptions(ctx)
assert.NoError(t, err)
assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments)
assert.Equal(t, expectedPostAndComments, postAndComments)
}
func TestPostService_Delete(t *testing.T) {

View file

@ -164,12 +164,12 @@ func (s *SubredditService) GetModerated(ctx context.Context, opts *ListOptions)
}
// GetSticky1 returns the first stickied post on a subreddit (if it exists).
func (s *SubredditService) GetSticky1(ctx context.Context, name string) (*Post, []*Comment, *Response, error) {
func (s *SubredditService) GetSticky1(ctx context.Context, name string) (*PostAndComments, *Response, error) {
return s.getSticky(ctx, name, 1)
}
// GetSticky2 returns the second stickied post on a subreddit (if it exists).
func (s *SubredditService) GetSticky2(ctx context.Context, name string) (*Post, []*Comment, *Response, error) {
func (s *SubredditService) GetSticky2(ctx context.Context, name string) (*PostAndComments, *Response, error) {
return s.getSticky(ctx, name, 2)
}
@ -278,7 +278,7 @@ func (s *SubredditService) getSubreddits(ctx context.Context, path string, opts
// 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.
func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num int) (*Post, []*Comment, *Response, error) {
func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num int) (*PostAndComments, *Response, error) {
type query struct {
Num int `url:"num"`
}
@ -286,21 +286,21 @@ func (s *SubredditService) getSticky(ctx context.Context, subreddit string, num
path := fmt.Sprintf("r/%s/about/sticky", subreddit)
path, err := addOptions(path, query{num})
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}
root := new(PostAndComments)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, nil, resp, err
return nil, resp, err
}
return root.Post, root.Comments, resp, nil
return root, resp, nil
}
// Moderators returns the moderators of a subreddit.

View file

@ -140,35 +140,6 @@ var expectedSubreddits = &Subreddits{
},
}
var expectedSticky = &postAndComments{
Post: &Post{
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: "https://www.reddit.com/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,
},
}
var expectSubredditInfos = []*SubredditInfo{
{Name: "golang", Subscribers: 119_722, ActiveUsers: 531},
{Name: "golang_infosec", Subscribers: 1_776, ActiveUsers: 0},
@ -449,10 +420,9 @@ func TestSubredditService_GetSticky1(t *testing.T) {
fmt.Fprint(w, blob)
})
post, comments, _, err := client.Subreddit.GetSticky1(ctx, "test")
postAndComments, _, err := client.Subreddit.GetSticky1(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments)
assert.Equal(t, expectedPostAndComments, postAndComments)
}
func TestSubredditService_GetSticky2(t *testing.T) {
@ -472,10 +442,9 @@ func TestSubredditService_GetSticky2(t *testing.T) {
fmt.Fprint(w, blob)
})
post, comments, _, err := client.Subreddit.GetSticky2(ctx, "test")
postAndComments, _, err := client.Subreddit.GetSticky2(ctx, "test")
assert.NoError(t, err)
assert.Equal(t, expectedPost2, post)
assert.Equal(t, expectedComments, comments)
assert.Equal(t, expectedPostAndComments, postAndComments)
}
func TestSubredditService_Subscribe(t *testing.T) {

View file

@ -447,5 +447,3 @@ func (pc *PostAndComments) UnmarshalJSON(data []byte) error {
func (pc *PostAndComments) hasMore() bool {
return pc.moreComments != nil && len(pc.moreComments.Children) > 0
}
func (pc *PostAndComments) M() *More { return pc.moreComments }