diff --git a/subreddit_test.go b/subreddit_test.go index 7fa11e1..cc5e86d 100644 --- a/subreddit_test.go +++ b/subreddit_test.go @@ -10,6 +10,61 @@ import ( "github.com/stretchr/testify/assert" ) +var expectedPosts = &Posts{ + Posts: []*Post{ + { + ID: "agi5zf", + FullID: "t3_agi5zf", + Created: &Timestamp{time.Date(2019, 1, 16, 5, 57, 51, 0, time.UTC)}, + Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, + + Permalink: "https://www.reddit.com/r/test/comments/agi5zf/test/", + URL: "https://www.reddit.com/r/test/comments/agi5zf/test/", + + Title: "test", + Body: "test", + + Score: 253, + UpvoteRatio: 0.99, + NumberOfComments: 1634, + + SubredditID: "t5_2qh23", + SubredditName: "test", + SubredditNamePrefixed: "r/test", + + AuthorID: "t2_30a5ktgt", + AuthorName: "kmiller0112", + + IsSelfPost: true, + Stickied: true, + }, + { + ID: "hyhquk", + FullID: "t3_hyhquk", + Created: &Timestamp{time.Date(2020, 7, 27, 0, 5, 10, 0, time.UTC)}, + Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)}, + + Permalink: "https://www.reddit.com/r/test/comments/hyhquk/veggies/", + URL: "https://i.imgur.com/LrN2mPw.jpg", + + Title: "Veggies", + + Score: 4, + UpvoteRatio: 1, + NumberOfComments: 0, + + SubredditID: "t5_2qh23", + SubredditName: "test", + SubredditNamePrefixed: "r/test", + + AuthorID: "t2_6fqntbwq", + AuthorName: "MuckleMcDuckle", + }, + }, + After: "t3_hyhquk", + Before: "", +} + var expectedSubreddit = &Subreddit{ ID: "2rc7j", FullID: "t5_2rc7j", @@ -153,6 +208,91 @@ var expectedRandomSubreddit = &Subreddit{ Subscribers: 52357, } +func TestSubredditService_Hot(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/subreddit/posts.json") + assert.NoError(t, err) + + mux.HandleFunc("/r/test/hot", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + posts, _, err := client.Subreddit.Hot(ctx, []string{"test"}) + assert.NoError(t, err) + assert.Equal(t, expectedPosts, posts) +} + +func TestSubredditService_New(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/subreddit/posts.json") + assert.NoError(t, err) + + mux.HandleFunc("/r/test/new", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + posts, _, err := client.Subreddit.New(ctx, []string{"test"}) + assert.NoError(t, err) + assert.Equal(t, expectedPosts, posts) +} + +func TestSubredditService_Rising(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/subreddit/posts.json") + assert.NoError(t, err) + + mux.HandleFunc("/r/test/rising", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + posts, _, err := client.Subreddit.Rising(ctx, []string{"test"}) + assert.NoError(t, err) + assert.Equal(t, expectedPosts, posts) +} + +func TestSubredditService_Controversial(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/subreddit/posts.json") + assert.NoError(t, err) + + mux.HandleFunc("/r/test/controversial", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + posts, _, err := client.Subreddit.Controversial(ctx, []string{"test"}) + assert.NoError(t, err) + assert.Equal(t, expectedPosts, posts) +} + +func TestSubredditService_Top(t *testing.T) { + setup() + defer teardown() + + blob, err := readFileContents("testdata/subreddit/posts.json") + assert.NoError(t, err) + + mux.HandleFunc("/r/test/top", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + fmt.Fprint(w, blob) + }) + + posts, _, err := client.Subreddit.Top(ctx, []string{"test"}) + assert.NoError(t, err) + assert.Equal(t, expectedPosts, posts) +} + func TestSubredditService_Get(t *testing.T) { setup() defer teardown() diff --git a/testdata/subreddit/posts.json b/testdata/subreddit/posts.json new file mode 100644 index 0000000..0ee123b --- /dev/null +++ b/testdata/subreddit/posts.json @@ -0,0 +1,267 @@ +{ + "kind": "Listing", + "data": { + "modhash": null, + "dist": 2, + "children": [ + { + "kind": "t3", + "data": { + "approved_at_utc": null, + "subreddit": "test", + "selftext": "test", + "author_fullname": "t2_30a5ktgt", + "saved": false, + "mod_reason_title": null, + "gilded": 0, + "clicked": false, + "title": "test", + "link_flair_richtext": [], + "subreddit_name_prefixed": "r/test", + "hidden": false, + "pwls": 6, + "link_flair_css_class": null, + "downs": 0, + "thumbnail_height": null, + "top_awarded_type": null, + "hide_score": false, + "name": "t3_agi5zf", + "quarantine": false, + "link_flair_text_color": "dark", + "upvote_ratio": 0.99, + "author_flair_background_color": null, + "subreddit_type": "public", + "ups": 253, + "total_awards_received": 0, + "media_embed": {}, + "thumbnail_width": null, + "author_flair_template_id": null, + "is_original_content": false, + "user_reports": [], + "secure_media": null, + "is_reddit_media_domain": false, + "is_meta": false, + "category": null, + "secure_media_embed": {}, + "link_flair_text": null, + "can_mod_post": false, + "score": 253, + "approved_by": null, + "author_premium": false, + "thumbnail": "self", + "edited": false, + "author_flair_css_class": null, + "author_flair_richtext": [], + "gildings": {}, + "content_categories": null, + "is_self": true, + "mod_note": null, + "created": 1547647071, + "link_flair_type": "text", + "wls": 6, + "removed_by_category": null, + "banned_by": null, + "author_flair_type": "text", + "domain": "self.test", + "allow_live_comments": true, + "selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>test</p>\n</div><!-- SC_ON -->", + "likes": null, + "suggested_sort": null, + "banned_at_utc": null, + "view_count": null, + "archived": true, + "no_follow": true, + "is_crosspostable": true, + "pinned": false, + "over_18": false, + "all_awardings": [], + "awarders": [], + "media_only": false, + "can_gild": true, + "spoiler": false, + "locked": false, + "author_flair_text": null, + "treatment_tags": [], + "visited": false, + "removed_by": null, + "num_reports": null, + "distinguished": null, + "subreddit_id": "t5_2qh23", + "mod_reason_by": null, + "removal_reason": null, + "link_flair_background_color": "", + "id": "agi5zf", + "is_robot_indexable": true, + "report_reasons": null, + "author": "kmiller0112", + "discussion_type": null, + "num_comments": 1634, + "send_replies": true, + "whitelist_status": "all_ads", + "contest_mode": false, + "mod_reports": [], + "author_patreon_flair": false, + "author_flair_text_color": null, + "permalink": "/r/test/comments/agi5zf/test/", + "parent_whitelist_status": "all_ads", + "stickied": true, + "url": "https://www.reddit.com/r/test/comments/agi5zf/test/", + "subreddit_subscribers": 8154, + "created_utc": 1547618271, + "num_crossposts": 7, + "media": null, + "is_video": false + } + }, + { + "kind": "t3", + "data": { + "approved_at_utc": null, + "subreddit": "test", + "selftext": "", + "author_fullname": "t2_6fqntbwq", + "saved": false, + "mod_reason_title": null, + "gilded": 0, + "clicked": false, + "title": "Veggies", + "link_flair_richtext": [], + "subreddit_name_prefixed": "r/test", + "hidden": false, + "pwls": 6, + "link_flair_css_class": null, + "downs": 0, + "thumbnail_height": 140, + "top_awarded_type": null, + "hide_score": false, + "name": "t3_hyhquk", + "quarantine": false, + "link_flair_text_color": "dark", + "upvote_ratio": 1, + "author_flair_background_color": null, + "subreddit_type": "public", + "ups": 4, + "total_awards_received": 0, + "media_embed": {}, + "thumbnail_width": 140, + "author_flair_template_id": null, + "is_original_content": false, + "user_reports": [], + "secure_media": null, + "is_reddit_media_domain": false, + "is_meta": false, + "category": null, + "secure_media_embed": {}, + "link_flair_text": null, + "can_mod_post": false, + "score": 4, + "approved_by": null, + "author_premium": false, + "thumbnail": "https://b.thumbs.redditmedia.com/rg4Aa--ZrHz2PNrmZbBk1cxajQrkRv2cvx2uhp7SSFo.jpg", + "edited": false, + "author_flair_css_class": null, + "author_flair_richtext": [], + "gildings": {}, + "post_hint": "image", + "content_categories": null, + "is_self": false, + "mod_note": null, + "created": 1595837110, + "link_flair_type": "text", + "wls": 6, + "removed_by_category": null, + "banned_by": null, + "author_flair_type": "text", + "domain": "i.imgur.com", + "allow_live_comments": false, + "selftext_html": null, + "likes": null, + "suggested_sort": null, + "banned_at_utc": null, + "url_overridden_by_dest": "https://i.imgur.com/LrN2mPw.jpg", + "view_count": null, + "archived": false, + "no_follow": false, + "is_crosspostable": true, + "pinned": false, + "over_18": false, + "preview": { + "images": [ + { + "source": { + "url": "https://external-preview.redd.it/ljFZZBn60orDIFTvDbPCXM-Thg9XsXAVm5kmH62gxKw.png?auto=webp&s=f5103946eee4586cba8a1ba410e3098e9a14bb58", + "width": 720, + "height": 859 + }, + "resolutions": [ + { + "url": "https://external-preview.redd.it/ljFZZBn60orDIFTvDbPCXM-Thg9XsXAVm5kmH62gxKw.png?width=108&crop=smart&auto=webp&s=a6904af790568dcea8fd3566e5d469df88a3891d", + "width": 108, + "height": 128 + }, + { + "url": "https://external-preview.redd.it/ljFZZBn60orDIFTvDbPCXM-Thg9XsXAVm5kmH62gxKw.png?width=216&crop=smart&auto=webp&s=09720b85b3b469b37030db3e3a5ab7fa231480f9", + "width": 216, + "height": 257 + }, + { + "url": "https://external-preview.redd.it/ljFZZBn60orDIFTvDbPCXM-Thg9XsXAVm5kmH62gxKw.png?width=320&crop=smart&auto=webp&s=78ace2e1c15e0e82dcfc95574d3ea3756812fd98", + "width": 320, + "height": 381 + }, + { + "url": "https://external-preview.redd.it/ljFZZBn60orDIFTvDbPCXM-Thg9XsXAVm5kmH62gxKw.png?width=640&crop=smart&auto=webp&s=d5d5305e3d97553176170ead8462cc0d155a7793", + "width": 640, + "height": 763 + } + ], + "variants": {}, + "id": "bxde3rpzP-mqawZJwpBIzEiH1y9nOLW3n1ghq9FPAR8" + } + ], + "enabled": true + }, + "all_awardings": [], + "awarders": [], + "media_only": false, + "can_gild": true, + "spoiler": false, + "locked": false, + "author_flair_text": null, + "treatment_tags": [], + "visited": false, + "removed_by": null, + "num_reports": null, + "distinguished": null, + "subreddit_id": "t5_2qh23", + "mod_reason_by": null, + "removal_reason": null, + "link_flair_background_color": "", + "id": "hyhquk", + "is_robot_indexable": true, + "report_reasons": null, + "author": "MuckleMcDuckle", + "discussion_type": null, + "num_comments": 0, + "send_replies": true, + "whitelist_status": "all_ads", + "contest_mode": false, + "mod_reports": [], + "author_patreon_flair": false, + "author_flair_text_color": null, + "permalink": "/r/test/comments/hyhquk/veggies/", + "parent_whitelist_status": "all_ads", + "stickied": false, + "url": "https://i.imgur.com/LrN2mPw.jpg", + "subreddit_subscribers": 8154, + "created_utc": 1595808310, + "num_crossposts": 0, + "media": null, + "is_video": false + } + } + ], + "after": "t3_hyhquk", + "before": null + } +}