Add subreddit service tests
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -0,0 +1,162 @@
|
|||||||
|
package geddit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
var expectedSubreddit = &Subreddit{
|
||||||
|
ID: "2rc7j",
|
||||||
|
FullID: "t5_2rc7j",
|
||||||
|
Created: &Timestamp{time.Date(2009, 11, 11, 0, 54, 28, 0, time.UTC)},
|
||||||
|
|
||||||
|
URL: "/r/golang/",
|
||||||
|
Name: "golang",
|
||||||
|
NamePrefixed: "r/golang",
|
||||||
|
Title: "The Go Programming Language",
|
||||||
|
Description: "Ask questions and post articles about the Go programming language and related tools, events etc.",
|
||||||
|
Type: "public",
|
||||||
|
|
||||||
|
Subscribers: 116532,
|
||||||
|
ActiveUserCount: Int(386),
|
||||||
|
NSFW: false,
|
||||||
|
UserIsMod: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedSubreddits = &Subreddits{
|
||||||
|
After: "t5_2qh0u",
|
||||||
|
Before: "",
|
||||||
|
Subreddits: []Subreddit{
|
||||||
|
{
|
||||||
|
ID: "2qs0k",
|
||||||
|
FullID: "t5_2qs0k",
|
||||||
|
Created: &Timestamp{time.Date(2009, 1, 25, 2, 25, 57, 0, time.UTC)},
|
||||||
|
|
||||||
|
URL: "/r/Home/",
|
||||||
|
Name: "Home",
|
||||||
|
NamePrefixed: "r/Home",
|
||||||
|
Title: "Home",
|
||||||
|
Type: "public",
|
||||||
|
|
||||||
|
Subscribers: 15336,
|
||||||
|
NSFW: false,
|
||||||
|
UserIsMod: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "2qh1i",
|
||||||
|
FullID: "t5_2qh1i",
|
||||||
|
Created: &Timestamp{time.Date(2008, 1, 25, 3, 52, 15, 0, time.UTC)},
|
||||||
|
|
||||||
|
URL: "/r/AskReddit/",
|
||||||
|
Name: "AskReddit",
|
||||||
|
NamePrefixed: "r/AskReddit",
|
||||||
|
Title: "Ask Reddit...",
|
||||||
|
Description: "r/AskReddit is the place to ask and answer thought-provoking questions.",
|
||||||
|
Type: "public",
|
||||||
|
|
||||||
|
Subscribers: 28449174,
|
||||||
|
NSFW: false,
|
||||||
|
UserIsMod: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "2qh0u",
|
||||||
|
FullID: "t5_2qh0u",
|
||||||
|
Created: &Timestamp{time.Date(2008, 1, 25, 0, 31, 9, 0, time.UTC)},
|
||||||
|
|
||||||
|
URL: "/r/pics/",
|
||||||
|
Name: "pics",
|
||||||
|
NamePrefixed: "r/pics",
|
||||||
|
Title: "Reddit Pics",
|
||||||
|
Description: "A place for pictures and photographs.",
|
||||||
|
Type: "public",
|
||||||
|
|
||||||
|
Subscribers: 24987753,
|
||||||
|
NSFW: false,
|
||||||
|
UserIsMod: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetByName(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/about.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/r/golang/about", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddit, _, err := client.Subreddit.GetByName(ctx, "golang")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddit, subreddit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetPopular(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/popular", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetPopular(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetNew(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/new", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetNew(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetGold(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/gold", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetGold(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditServiceOp_GetDefault(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob := readFileContents(t, "testdata/subreddit/list.json")
|
||||||
|
|
||||||
|
mux.HandleFunc("/subreddits/default", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
subreddits, _, err := client.Subreddit.GetDefault(ctx, nil)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSubreddits, subreddits)
|
||||||
|
}
|
||||||
Vendored
+96
@@ -0,0 +1,96 @@
|
|||||||
|
{
|
||||||
|
"kind": "t5",
|
||||||
|
"data": {
|
||||||
|
"user_flair_background_color": null,
|
||||||
|
"submit_text_html": null,
|
||||||
|
"restrict_posting": true,
|
||||||
|
"user_is_banned": false,
|
||||||
|
"free_form_reports": true,
|
||||||
|
"wiki_enabled": null,
|
||||||
|
"user_is_muted": false,
|
||||||
|
"user_can_flair_in_sr": null,
|
||||||
|
"display_name": "golang",
|
||||||
|
"header_img": "https://b.thumbs.redditmedia.com/7BDtSXbohQaPFuaa6oCA5HtE53Flgld6rj3G7-TavDs.png",
|
||||||
|
"title": "The Go Programming Language",
|
||||||
|
"allow_galleries": true,
|
||||||
|
"icon_size": null,
|
||||||
|
"primary_color": "",
|
||||||
|
"active_user_count": 386,
|
||||||
|
"icon_img": "",
|
||||||
|
"display_name_prefixed": "r/golang",
|
||||||
|
"accounts_active": 386,
|
||||||
|
"public_traffic": false,
|
||||||
|
"subscribers": 116532,
|
||||||
|
"user_flair_richtext": [],
|
||||||
|
"videostream_links_count": 0,
|
||||||
|
"name": "t5_2rc7j",
|
||||||
|
"quarantine": false,
|
||||||
|
"hide_ads": false,
|
||||||
|
"emojis_enabled": false,
|
||||||
|
"advertiser_category": "",
|
||||||
|
"public_description": "Ask questions and post articles about the Go programming language and related tools, events etc.",
|
||||||
|
"comment_score_hide_mins": 0,
|
||||||
|
"user_has_favorited": false,
|
||||||
|
"user_flair_template_id": null,
|
||||||
|
"community_icon": "https://styles.redditmedia.com/t5_2rc7j/styles/communityIcon_wy4riduoe9k11.png?width=256&s=0d681daaa8d4b6271e6be788d0f9379f0661e04a",
|
||||||
|
"banner_background_image": "https://styles.redditmedia.com/t5_2rc7j/styles/bannerBackgroundImage_k15p9ugyd9k11.png?width=4000&s=dc19f23446f14c3dee0ab59c538fd5dfb243eeb9",
|
||||||
|
"original_content_tag_enabled": false,
|
||||||
|
"submit_text": "",
|
||||||
|
"description_html": "<!-- SC_OFF --><div class=\"md\"><p>Please follow the <a href=\"https://golang.org/conduct\">Go Community Code of Conduct</a> while posting here. In short:</p>\n\n<ul>\n<li>Treat everyone with respect and kindness.</li>\n<li>Be thoughtful in how you communicate.</li>\n<li>Don’t be destructive or inflammatory.</li>\n<li>If you encounter an issue, please contact the moderators.</li>\n</ul>\n\n<p><strong>Documentation</strong></p>\n\n<ul>\n<li><a href=\"http://golang.org/doc/\">Official Go Documentation</a></li>\n<li><a href=\"http://golang.org/pkg/\">Standard Library Docs</a></li>\n<li><a href=\"http://godoc.org/\">Other Package Docs</a></li>\n</ul>\n\n<p><strong>Community</strong></p>\n\n<ul>\n<li><a href=\"http://groups.google.com/group/golang-nuts\">Go Nuts Mailing List</a></li>\n<li><a href=\"http://stackoverflow.com/questions/tagged/go\">Go questions in Stackoverflow</a></li>\n<li>#go-nuts in irc.freenode.org</li>\n<li><a href=\"http://dave.cheney.net/resources-for-new-go-programmers\">Resources for new Go programmers</a></li>\n</ul>\n\n<p><strong>Other Resources</strong></p>\n\n<ul>\n<li><a href=\"https://developers.google.com/appengine/docs/go/\">Go for App Engine</a>!</li>\n</ul>\n</div><!-- SC_ON -->",
|
||||||
|
"spoilers_enabled": true,
|
||||||
|
"header_title": null,
|
||||||
|
"header_size": [153, 55],
|
||||||
|
"user_flair_position": "left",
|
||||||
|
"all_original_content": false,
|
||||||
|
"has_menu_widget": false,
|
||||||
|
"is_enrolled_in_new_modmail": null,
|
||||||
|
"key_color": "",
|
||||||
|
"can_assign_user_flair": false,
|
||||||
|
"created": 1257929668.0,
|
||||||
|
"wls": 6,
|
||||||
|
"show_media_preview": true,
|
||||||
|
"submission_type": "any",
|
||||||
|
"user_is_subscriber": true,
|
||||||
|
"disable_contributor_requests": false,
|
||||||
|
"allow_videogifs": true,
|
||||||
|
"user_flair_type": "text",
|
||||||
|
"allow_polls": true,
|
||||||
|
"collapse_deleted_comments": true,
|
||||||
|
"emojis_custom_size": null,
|
||||||
|
"public_description_html": "<!-- SC_OFF --><div class=\"md\"><p>Ask questions and post articles about the Go programming language and related tools, events etc.</p>\n</div><!-- SC_ON -->",
|
||||||
|
"allow_videos": true,
|
||||||
|
"is_crosspostable_subreddit": true,
|
||||||
|
"notification_level": "low",
|
||||||
|
"can_assign_link_flair": false,
|
||||||
|
"accounts_active_is_fuzzed": false,
|
||||||
|
"submit_text_label": null,
|
||||||
|
"link_flair_position": "left",
|
||||||
|
"user_sr_flair_enabled": true,
|
||||||
|
"user_flair_enabled_in_sr": false,
|
||||||
|
"allow_discovery": true,
|
||||||
|
"user_sr_theme_enabled": true,
|
||||||
|
"link_flair_enabled": true,
|
||||||
|
"subreddit_type": "public",
|
||||||
|
"suggested_comment_sort": null,
|
||||||
|
"banner_img": "",
|
||||||
|
"user_flair_text": null,
|
||||||
|
"banner_background_color": "",
|
||||||
|
"show_media": false,
|
||||||
|
"id": "2rc7j",
|
||||||
|
"user_is_moderator": false,
|
||||||
|
"over18": false,
|
||||||
|
"description": "Please follow the [Go Community Code of Conduct](https://golang.org/conduct) while posting here. In short:\n\n* Treat everyone with respect and kindness.\n* Be thoughtful in how you communicate.\n* Don’t be destructive or inflammatory.\n* If you encounter an issue, please contact the moderators.\n\n**Documentation**\n\n* [Official Go Documentation](http://golang.org/doc/)\n* [Standard Library Docs](http://golang.org/pkg/)\n* [Other Package Docs](http://godoc.org/)\n\n**Community**\n\n* [Go Nuts Mailing List](http://groups.google.com/group/golang-nuts)\n* [Go questions in Stackoverflow](http://stackoverflow.com/questions/tagged/go)\n* #go-nuts in irc.freenode.org\n* [Resources for new Go programmers](http://dave.cheney.net/resources-for-new-go-programmers)\n\n**Other Resources**\n\n* [Go for App Engine](https://developers.google.com/appengine/docs/go/)!",
|
||||||
|
"submit_link_label": null,
|
||||||
|
"user_flair_text_color": null,
|
||||||
|
"restrict_commenting": false,
|
||||||
|
"user_flair_css_class": null,
|
||||||
|
"allow_images": true,
|
||||||
|
"lang": "en",
|
||||||
|
"whitelist_status": "all_ads",
|
||||||
|
"url": "/r/golang/",
|
||||||
|
"created_utc": 1257900868.0,
|
||||||
|
"banner_size": null,
|
||||||
|
"mobile_banner_image": "",
|
||||||
|
"user_is_contributor": false
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+300
File diff suppressed because one or more lines are too long
@@ -287,7 +287,7 @@ type Subreddit struct {
|
|||||||
Name string `json:"display_name,omitempty"`
|
Name string `json:"display_name,omitempty"`
|
||||||
NamePrefixed string `json:"display_name_prefixed,omitempty"`
|
NamePrefixed string `json:"display_name_prefixed,omitempty"`
|
||||||
Title string `json:"title,omitempty"`
|
Title string `json:"title,omitempty"`
|
||||||
PublicDescription string `json:"public_description,omitempty"`
|
Description string `json:"public_description,omitempty"`
|
||||||
Type string `json:"subreddit_type,omitempty"`
|
Type string `json:"subreddit_type,omitempty"`
|
||||||
SuggestedCommentSort string `json:"suggested_comment_sort,omitempty"`
|
SuggestedCommentSort string `json:"suggested_comment_sort,omitempty"`
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user