Delete search service, move its methods to other services
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -98,7 +98,6 @@ type Client struct {
|
|||||||
Moderation *ModerationService
|
Moderation *ModerationService
|
||||||
Multi *MultiService
|
Multi *MultiService
|
||||||
Post *PostService
|
Post *PostService
|
||||||
Search *SearchService
|
|
||||||
Subreddit *SubredditService
|
Subreddit *SubredditService
|
||||||
User *UserService
|
User *UserService
|
||||||
|
|
||||||
@@ -144,7 +143,6 @@ func newClient(httpClient *http.Client) *Client {
|
|||||||
c.Listings = &ListingsService{client: c}
|
c.Listings = &ListingsService{client: c}
|
||||||
c.Moderation = &ModerationService{client: c}
|
c.Moderation = &ModerationService{client: c}
|
||||||
c.Multi = &MultiService{client: c}
|
c.Multi = &MultiService{client: c}
|
||||||
c.Search = &SearchService{client: c}
|
|
||||||
c.Subreddit = &SubredditService{client: c}
|
c.Subreddit = &SubredditService{client: c}
|
||||||
c.User = &UserService{client: c}
|
c.User = &UserService{client: c}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ func testClientServices(t *testing.T, c *Client) {
|
|||||||
"Moderation",
|
"Moderation",
|
||||||
"Multi",
|
"Multi",
|
||||||
"Post",
|
"Post",
|
||||||
"Search",
|
|
||||||
"Subreddit",
|
"Subreddit",
|
||||||
"User",
|
"User",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,19 @@
|
|||||||
package reddit
|
package reddit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SearchService handles communication with the search
|
/*
|
||||||
// related methods of the Reddit API.
|
|
||||||
//
|
For searches to include NSFW results, the user must
|
||||||
// For searches to include NSFW results, the user must
|
enable the following setting in their preferences:
|
||||||
// enable the following setting in their preferences:
|
"include not safe for work (NSFW) search results in searches"
|
||||||
// "include not safe for work (NSFW) search results in searches"
|
Note: The "limit" parameter in searches is prone to inconsistent
|
||||||
// Note: The "limit" parameter in searches is prone to inconsistent
|
behaviour, e.g. sometimes limit=1 returns nothing when it should.
|
||||||
// behaviour, e.g. sometimes limit=1 returns nothing when it should.
|
|
||||||
//
|
*/
|
||||||
// Reddit API docs: https://www.reddit.com/dev/api/#section_search
|
|
||||||
type SearchService struct {
|
|
||||||
client *Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func newSearchOptions(opts ...SearchOptionSetter) url.Values {
|
func newSearchOptions(opts ...SearchOptionSetter) url.Values {
|
||||||
searchOptions := make(url.Values)
|
searchOptions := make(url.Values)
|
||||||
@@ -134,7 +127,6 @@ func FromAllTime(opts url.Values) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setType sets the type option.
|
// setType sets the type option.
|
||||||
// It could be user, link, sr (subreddit).
|
|
||||||
// For mod actions, it's for the type of action (e.g. "banuser", "spamcomment").
|
// For mod actions, it's for the type of action (e.g. "banuser", "spamcomment").
|
||||||
func setType(v string) SearchOptionSetter {
|
func setType(v string) SearchOptionSetter {
|
||||||
return func(opts url.Values) {
|
return func(opts url.Values) {
|
||||||
@@ -153,77 +145,3 @@ func setQuery(v string) SearchOptionSetter {
|
|||||||
func setRestrict(opts url.Values) {
|
func setRestrict(opts url.Values) {
|
||||||
opts.Set("restrict_sr", "true")
|
opts.Set("restrict_sr", "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Posts searches for posts.
|
|
||||||
// If the list of subreddits provided is empty, the search is run against r/all.
|
|
||||||
func (s *SearchService) Posts(ctx context.Context, query string, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
|
||||||
opts = append(opts, setType("link"), setQuery(query))
|
|
||||||
|
|
||||||
path := "search"
|
|
||||||
if len(subreddits) > 0 {
|
|
||||||
path = fmt.Sprintf("r/%s/search", strings.Join(subreddits, "+"))
|
|
||||||
opts = append(opts, setRestrict)
|
|
||||||
}
|
|
||||||
|
|
||||||
form := newSearchOptions(opts...)
|
|
||||||
path = addQuery(path, form)
|
|
||||||
|
|
||||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
root := new(rootListing)
|
|
||||||
resp, err := s.client.Do(ctx, req, root)
|
|
||||||
if err != nil {
|
|
||||||
return nil, resp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return root.getPosts(), resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subreddits searches for subreddits.
|
|
||||||
// The sort and timespan options don't affect the results for this search.
|
|
||||||
func (s *SearchService) Subreddits(ctx context.Context, query string, opts ...SearchOptionSetter) (*Subreddits, *Response, error) {
|
|
||||||
opts = append(opts, setType("sr"), setQuery(query))
|
|
||||||
form := newSearchOptions(opts...)
|
|
||||||
|
|
||||||
path := "search"
|
|
||||||
path = addQuery(path, form)
|
|
||||||
|
|
||||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
root := new(rootListing)
|
|
||||||
resp, err := s.client.Do(ctx, req, root)
|
|
||||||
if err != nil {
|
|
||||||
return nil, resp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return root.getSubreddits(), resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Users searches for users.
|
|
||||||
// The sort and timespan options don't affect the results for this search.
|
|
||||||
func (s *SearchService) Users(ctx context.Context, query string, opts ...SearchOptionSetter) (*Users, *Response, error) {
|
|
||||||
opts = append(opts, setType("user"), setQuery(query))
|
|
||||||
form := newSearchOptions(opts...)
|
|
||||||
|
|
||||||
path := "search"
|
|
||||||
path = addQuery(path, form)
|
|
||||||
|
|
||||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
root := new(rootListing)
|
|
||||||
resp, err := s.client.Do(ctx, req, root)
|
|
||||||
if err != nil {
|
|
||||||
return nil, resp, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return root.getUsers(), resp, nil
|
|
||||||
}
|
|
||||||
|
|||||||
-260
@@ -1,260 +0,0 @@
|
|||||||
package reddit
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
var expectedSearchPosts = &Posts{
|
|
||||||
Posts: []*Post{
|
|
||||||
{
|
|
||||||
ID: "hybow9",
|
|
||||||
FullID: "t3_hybow9",
|
|
||||||
Created: &Timestamp{time.Date(2020, 7, 26, 18, 14, 24, 0, time.UTC)},
|
|
||||||
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
|
|
||||||
|
|
||||||
Permalink: "https://www.reddit.com/r/WatchPeopleDieInside/comments/hybow9/pregnancy_test/",
|
|
||||||
URL: "https://v.redd.it/ra4qnt8bt8d51",
|
|
||||||
|
|
||||||
Title: "Pregnancy test",
|
|
||||||
|
|
||||||
Score: 103829,
|
|
||||||
UpvoteRatio: 0.88,
|
|
||||||
NumberOfComments: 3748,
|
|
||||||
|
|
||||||
SubredditID: "t5_3h4zq",
|
|
||||||
SubredditName: "WatchPeopleDieInside",
|
|
||||||
SubredditNamePrefixed: "r/WatchPeopleDieInside",
|
|
||||||
|
|
||||||
AuthorID: "t2_3p32m02",
|
|
||||||
AuthorName: "chocolat_ice_cream",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: "hmwhd7",
|
|
||||||
FullID: "t3_hmwhd7",
|
|
||||||
Created: &Timestamp{time.Date(2020, 7, 7, 15, 19, 42, 0, time.UTC)},
|
|
||||||
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
|
|
||||||
|
|
||||||
Permalink: "https://www.reddit.com/r/worldnews/comments/hmwhd7/brazilian_president_jair_bolsonaro_tests_positive/",
|
|
||||||
URL: "https://www.theguardian.com/world/2020/jul/07/jair-bolsonaro-coronavirus-positive-test-brazil-president",
|
|
||||||
|
|
||||||
Title: "Brazilian president Jair Bolsonaro tests positive for coronavirus",
|
|
||||||
|
|
||||||
Score: 149238,
|
|
||||||
UpvoteRatio: 0.94,
|
|
||||||
NumberOfComments: 7415,
|
|
||||||
|
|
||||||
SubredditID: "t5_2qh13",
|
|
||||||
SubredditName: "worldnews",
|
|
||||||
SubredditNamePrefixed: "r/worldnews",
|
|
||||||
|
|
||||||
AuthorID: "t2_wgrkg",
|
|
||||||
AuthorName: "Jeremy_Martin",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
After: "t3_hmwhd7",
|
|
||||||
}
|
|
||||||
|
|
||||||
var expectedSearchUsers = &Users{
|
|
||||||
Users: []*User{
|
|
||||||
{
|
|
||||||
ID: "abc",
|
|
||||||
Name: "user1",
|
|
||||||
Created: &Timestamp{time.Date(2019, 8, 14, 23, 38, 42, 0, time.UTC)},
|
|
||||||
|
|
||||||
PostKarma: 5730,
|
|
||||||
CommentKarma: 11740,
|
|
||||||
|
|
||||||
HasVerifiedEmail: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: "def",
|
|
||||||
Name: "user2",
|
|
||||||
Created: &Timestamp{time.Date(2020, 5, 7, 3, 16, 46, 0, time.UTC)},
|
|
||||||
|
|
||||||
PostKarma: 2485,
|
|
||||||
CommentKarma: 127,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var expectedSearchSubreddits = &Subreddits{
|
|
||||||
Subreddits: []*Subreddit{
|
|
||||||
{
|
|
||||||
ID: "2qh23",
|
|
||||||
FullID: "t5_2qh23",
|
|
||||||
Created: &Timestamp{time.Date(2008, 1, 25, 5, 11, 28, 0, time.UTC)},
|
|
||||||
|
|
||||||
URL: "/r/test/",
|
|
||||||
Name: "test",
|
|
||||||
NamePrefixed: "r/test",
|
|
||||||
Title: "Testing",
|
|
||||||
Type: "public",
|
|
||||||
|
|
||||||
Subscribers: 8174,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: "333yu",
|
|
||||||
FullID: "t5_333yu",
|
|
||||||
Created: &Timestamp{time.Date(2014, 8, 18, 23, 29, 47, 0, time.UTC)},
|
|
||||||
|
|
||||||
URL: "/r/trollingforababy/",
|
|
||||||
Name: "trollingforababy",
|
|
||||||
NamePrefixed: "r/trollingforababy",
|
|
||||||
Title: "Crushing it with reddit karma",
|
|
||||||
Description: "This is a group for laughing at and mocking the awkward, ridiculous, and sometimes painful things we endure while trying for a baby. Trollingforababy is for people who are trying to conceive, and are not currently pregnant. \n\nPlease look at our complete list of rules before participating.",
|
|
||||||
Type: "public",
|
|
||||||
|
|
||||||
Subscribers: 10244,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
After: "t5_333yu",
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchService_Posts(t *testing.T) {
|
|
||||||
setup()
|
|
||||||
defer teardown()
|
|
||||||
|
|
||||||
blob, err := readFileContents("testdata/search/posts.json")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("type", "link")
|
|
||||||
form.Set("q", "test")
|
|
||||||
form.Set("after", "t3_testpost")
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, form, r.Form)
|
|
||||||
|
|
||||||
fmt.Fprint(w, blob)
|
|
||||||
})
|
|
||||||
|
|
||||||
posts, _, err := client.Search.Posts(ctx, "test", nil, SetAfter("t3_testpost"))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expectedSearchPosts, posts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchService_Posts_InSubreddit(t *testing.T) {
|
|
||||||
setup()
|
|
||||||
defer teardown()
|
|
||||||
|
|
||||||
blob, err := readFileContents("testdata/search/posts.json")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
mux.HandleFunc("/r/test/search", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("type", "link")
|
|
||||||
form.Set("q", "test")
|
|
||||||
form.Set("restrict_sr", "true")
|
|
||||||
form.Set("after", "t3_testpost")
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, form, r.Form)
|
|
||||||
|
|
||||||
fmt.Fprint(w, blob)
|
|
||||||
})
|
|
||||||
|
|
||||||
posts, _, err := client.Search.Posts(ctx, "test", []string{"test"}, SetAfter("t3_testpost"))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expectedSearchPosts, posts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchService_Posts_InSubreddits(t *testing.T) {
|
|
||||||
setup()
|
|
||||||
defer teardown()
|
|
||||||
|
|
||||||
blob, err := readFileContents("testdata/search/posts.json")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
mux.HandleFunc("/r/test+golang+nba/search", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("type", "link")
|
|
||||||
form.Set("q", "test")
|
|
||||||
form.Set("restrict_sr", "true")
|
|
||||||
form.Set("after", "t3_testpost")
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, form, r.Form)
|
|
||||||
|
|
||||||
fmt.Fprint(w, blob)
|
|
||||||
})
|
|
||||||
|
|
||||||
posts, _, err := client.Search.Posts(ctx, "test", []string{"test", "golang", "nba"}, SetAfter("t3_testpost"))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expectedSearchPosts, posts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchService_Subreddits(t *testing.T) {
|
|
||||||
setup()
|
|
||||||
defer teardown()
|
|
||||||
|
|
||||||
blob, err := readFileContents("testdata/search/subreddits.json")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("type", "sr")
|
|
||||||
form.Set("q", "test")
|
|
||||||
form.Set("before", "t5_testsr")
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, form, r.Form)
|
|
||||||
|
|
||||||
fmt.Fprint(w, blob)
|
|
||||||
})
|
|
||||||
|
|
||||||
subreddits, _, err := client.Search.Subreddits(ctx, "test", SetBefore("t5_testsr"))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expectedSearchSubreddits, subreddits)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchService_Users(t *testing.T) {
|
|
||||||
setup()
|
|
||||||
defer teardown()
|
|
||||||
|
|
||||||
blob, err := readFileContents("testdata/search/users.json")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
assert.Equal(t, http.MethodGet, r.Method)
|
|
||||||
|
|
||||||
form := url.Values{}
|
|
||||||
form.Set("type", "user")
|
|
||||||
form.Set("q", "test")
|
|
||||||
form.Set("limit", "2")
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, form, r.Form)
|
|
||||||
|
|
||||||
fmt.Fprint(w, blob)
|
|
||||||
})
|
|
||||||
|
|
||||||
users, _, err := client.Search.Users(ctx, "test", SetLimit(2))
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, expectedSearchUsers, users)
|
|
||||||
}
|
|
||||||
|
|
||||||
func printJSON(v interface{}) {
|
|
||||||
b, _ := json.MarshalIndent(v, "", " ")
|
|
||||||
fmt.Println(string(b))
|
|
||||||
}
|
|
||||||
@@ -243,6 +243,35 @@ func (s *SubredditService) SearchNames(ctx context.Context, query string) ([]str
|
|||||||
return root.Names, resp, nil
|
return root.Names, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SearchPosts searches for posts.
|
||||||
|
// If the list of subreddits provided is empty, the search is run against r/all.
|
||||||
|
func (s *SubredditService) SearchPosts(ctx context.Context, query string, subreddits []string, opts ...SearchOptionSetter) (*Posts, *Response, error) {
|
||||||
|
if len(subreddits) > 0 {
|
||||||
|
opts = append(opts, setRestrict)
|
||||||
|
} else {
|
||||||
|
subreddits = append(subreddits, "all")
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = append(opts, setQuery(query))
|
||||||
|
form := newSearchOptions(opts...)
|
||||||
|
|
||||||
|
path := fmt.Sprintf("r/%s/search", strings.Join(subreddits, "+"))
|
||||||
|
path = addQuery(path, form)
|
||||||
|
|
||||||
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
root := new(rootListing)
|
||||||
|
resp, err := s.client.Do(ctx, req, root)
|
||||||
|
if err != nil {
|
||||||
|
return nil, resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return root.getPosts(), resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SubredditService) getSubreddits(ctx context.Context, path string, opts *ListOptions) (*Subreddits, *Response, error) {
|
func (s *SubredditService) getSubreddits(ctx context.Context, path string, opts *ListOptions) (*Subreddits, *Response, error) {
|
||||||
path, err := addOptions(path, opts)
|
path, err := addOptions(path, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -150,6 +150,56 @@ var expectedSubredditNames = []string{
|
|||||||
"golang_jobs",
|
"golang_jobs",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var expectedSearchPosts = &Posts{
|
||||||
|
Posts: []*Post{
|
||||||
|
{
|
||||||
|
ID: "hybow9",
|
||||||
|
FullID: "t3_hybow9",
|
||||||
|
Created: &Timestamp{time.Date(2020, 7, 26, 18, 14, 24, 0, time.UTC)},
|
||||||
|
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
|
||||||
|
|
||||||
|
Permalink: "https://www.reddit.com/r/WatchPeopleDieInside/comments/hybow9/pregnancy_test/",
|
||||||
|
URL: "https://v.redd.it/ra4qnt8bt8d51",
|
||||||
|
|
||||||
|
Title: "Pregnancy test",
|
||||||
|
|
||||||
|
Score: 103829,
|
||||||
|
UpvoteRatio: 0.88,
|
||||||
|
NumberOfComments: 3748,
|
||||||
|
|
||||||
|
SubredditID: "t5_3h4zq",
|
||||||
|
SubredditName: "WatchPeopleDieInside",
|
||||||
|
SubredditNamePrefixed: "r/WatchPeopleDieInside",
|
||||||
|
|
||||||
|
AuthorID: "t2_3p32m02",
|
||||||
|
AuthorName: "chocolat_ice_cream",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "hmwhd7",
|
||||||
|
FullID: "t3_hmwhd7",
|
||||||
|
Created: &Timestamp{time.Date(2020, 7, 7, 15, 19, 42, 0, time.UTC)},
|
||||||
|
Edited: &Timestamp{time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
|
||||||
|
|
||||||
|
Permalink: "https://www.reddit.com/r/worldnews/comments/hmwhd7/brazilian_president_jair_bolsonaro_tests_positive/",
|
||||||
|
URL: "https://www.theguardian.com/world/2020/jul/07/jair-bolsonaro-coronavirus-positive-test-brazil-president",
|
||||||
|
|
||||||
|
Title: "Brazilian president Jair Bolsonaro tests positive for coronavirus",
|
||||||
|
|
||||||
|
Score: 149238,
|
||||||
|
UpvoteRatio: 0.94,
|
||||||
|
NumberOfComments: 7415,
|
||||||
|
|
||||||
|
SubredditID: "t5_2qh13",
|
||||||
|
SubredditName: "worldnews",
|
||||||
|
SubredditNamePrefixed: "r/worldnews",
|
||||||
|
|
||||||
|
AuthorID: "t2_wgrkg",
|
||||||
|
AuthorName: "Jeremy_Martin",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
After: "t3_hmwhd7",
|
||||||
|
}
|
||||||
|
|
||||||
var expectedModerators = []Moderator{
|
var expectedModerators = []Moderator{
|
||||||
{ID: "t2_test1", Name: "testuser1", Permissions: []string{"all"}},
|
{ID: "t2_test1", Name: "testuser1", Permissions: []string{"all"}},
|
||||||
{ID: "t2_test2", Name: "testuser2", Permissions: []string{"all"}},
|
{ID: "t2_test2", Name: "testuser2", Permissions: []string{"all"}},
|
||||||
@@ -567,6 +617,86 @@ func TestSubredditService_SearchNames(t *testing.T) {
|
|||||||
assert.Equal(t, expectedSubredditNames, names)
|
assert.Equal(t, expectedSubredditNames, names)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSubredditService_SearchPosts(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("testdata/subreddit/search-posts.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/r/all/search", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("q", "test")
|
||||||
|
form.Set("after", "t3_testpost")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
posts, _, err := client.Subreddit.SearchPosts(ctx, "test", nil, SetAfter("t3_testpost"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSearchPosts, posts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditService_SearchPosts_InSubreddit(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("testdata/subreddit/search-posts.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/r/test/search", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("q", "test")
|
||||||
|
form.Set("restrict_sr", "true")
|
||||||
|
form.Set("after", "t3_testpost")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
posts, _, err := client.Subreddit.SearchPosts(ctx, "test", []string{"test"}, SetAfter("t3_testpost"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSearchPosts, posts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSubredditService_SearchPosts_InSubreddits(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("testdata/subreddit/search-posts.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/r/test+golang+nba/search", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("q", "test")
|
||||||
|
form.Set("restrict_sr", "true")
|
||||||
|
form.Set("after", "t3_testpost")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
posts, _, err := client.Subreddit.SearchPosts(ctx, "test", []string{"test", "golang", "nba"}, SetAfter("t3_testpost"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedSearchPosts, posts)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubredditService_Moderators(t *testing.T) {
|
func TestSubredditService_Moderators(t *testing.T) {
|
||||||
setup()
|
setup()
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|||||||
Vendored
-208
File diff suppressed because one or more lines are too long
Vendored
-138
@@ -1,138 +0,0 @@
|
|||||||
{
|
|
||||||
"kind": "Listing",
|
|
||||||
"data": {
|
|
||||||
"after": "",
|
|
||||||
"dist": 2,
|
|
||||||
"facets": {},
|
|
||||||
"modhash": null,
|
|
||||||
"children": [
|
|
||||||
{
|
|
||||||
"kind": "t2",
|
|
||||||
"data": {
|
|
||||||
"is_employee": false,
|
|
||||||
"icon_img": "https://www.redditstatic.com/avatars/avatar_default_12_FF4500.png",
|
|
||||||
"pref_show_snoovatar": false,
|
|
||||||
"name": "user1",
|
|
||||||
"is_friend": false,
|
|
||||||
"created": 1565854722,
|
|
||||||
"has_subscribed": true,
|
|
||||||
"hide_from_robots": false,
|
|
||||||
"verified": true,
|
|
||||||
"created_utc": 1565825922,
|
|
||||||
"subreddit": {
|
|
||||||
"default_set": true,
|
|
||||||
"banner_img": "",
|
|
||||||
"restrict_posting": true,
|
|
||||||
"user_is_banned": false,
|
|
||||||
"free_form_reports": true,
|
|
||||||
"community_icon": null,
|
|
||||||
"show_media": true,
|
|
||||||
"description": "",
|
|
||||||
"user_is_muted": false,
|
|
||||||
"display_name": "u_user1",
|
|
||||||
"header_img": null,
|
|
||||||
"title": "",
|
|
||||||
"previous_names": [],
|
|
||||||
"user_is_moderator": false,
|
|
||||||
"over_18": false,
|
|
||||||
"icon_size": [256, 256],
|
|
||||||
"primary_color": "",
|
|
||||||
"icon_img": "https://www.redditstatic.com/avatars/avatar_default_12_FF4500.png",
|
|
||||||
"icon_color": "#FF4500",
|
|
||||||
"submit_link_label": "",
|
|
||||||
"header_size": null,
|
|
||||||
"restrict_commenting": false,
|
|
||||||
"subscribers": 0,
|
|
||||||
"submit_text_label": "",
|
|
||||||
"is_default_icon": true,
|
|
||||||
"link_flair_position": "",
|
|
||||||
"display_name_prefixed": "u/user1",
|
|
||||||
"key_color": "",
|
|
||||||
"name": "t5_sr1",
|
|
||||||
"is_default_banner": true,
|
|
||||||
"url": "/user/user1/",
|
|
||||||
"banner_size": null,
|
|
||||||
"user_is_contributor": false,
|
|
||||||
"public_description": "",
|
|
||||||
"link_flair_enabled": false,
|
|
||||||
"disable_contributor_requests": false,
|
|
||||||
"subreddit_type": "user",
|
|
||||||
"user_is_subscriber": false
|
|
||||||
},
|
|
||||||
"comment_karma": 11740,
|
|
||||||
"is_gold": false,
|
|
||||||
"is_mod": true,
|
|
||||||
"accept_chats": false,
|
|
||||||
"link_karma": 5730,
|
|
||||||
"has_verified_email": true,
|
|
||||||
"id": "abc",
|
|
||||||
"accept_pms": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"kind": "t2",
|
|
||||||
"data": {
|
|
||||||
"is_employee": false,
|
|
||||||
"icon_img": "https://www.redditstatic.com/avatars/avatar_default_16_FF585B.png",
|
|
||||||
"pref_show_snoovatar": false,
|
|
||||||
"name": "user2",
|
|
||||||
"is_friend": false,
|
|
||||||
"created": 1588850206,
|
|
||||||
"has_subscribed": true,
|
|
||||||
"hide_from_robots": true,
|
|
||||||
"verified": true,
|
|
||||||
"created_utc": 1588821406,
|
|
||||||
"subreddit": {
|
|
||||||
"default_set": true,
|
|
||||||
"banner_img": "",
|
|
||||||
"restrict_posting": true,
|
|
||||||
"user_is_banned": false,
|
|
||||||
"free_form_reports": true,
|
|
||||||
"community_icon": null,
|
|
||||||
"show_media": true,
|
|
||||||
"description": "",
|
|
||||||
"user_is_muted": false,
|
|
||||||
"display_name": "u_user2",
|
|
||||||
"header_img": null,
|
|
||||||
"title": "",
|
|
||||||
"previous_names": [],
|
|
||||||
"user_is_moderator": false,
|
|
||||||
"over_18": true,
|
|
||||||
"icon_size": [256, 256],
|
|
||||||
"primary_color": "",
|
|
||||||
"icon_img": "https://www.redditstatic.com/avatars/avatar_default_16_FF585B.png",
|
|
||||||
"icon_color": "#FF585B",
|
|
||||||
"submit_link_label": "",
|
|
||||||
"header_size": null,
|
|
||||||
"restrict_commenting": false,
|
|
||||||
"subscribers": 0,
|
|
||||||
"submit_text_label": "",
|
|
||||||
"is_default_icon": true,
|
|
||||||
"link_flair_position": "",
|
|
||||||
"display_name_prefixed": "u/user2",
|
|
||||||
"key_color": "",
|
|
||||||
"name": "t5_sr2",
|
|
||||||
"is_default_banner": true,
|
|
||||||
"url": "/user/user2/",
|
|
||||||
"banner_size": null,
|
|
||||||
"user_is_contributor": false,
|
|
||||||
"public_description": "",
|
|
||||||
"link_flair_enabled": false,
|
|
||||||
"disable_contributor_requests": false,
|
|
||||||
"subreddit_type": "user",
|
|
||||||
"user_is_subscriber": false
|
|
||||||
},
|
|
||||||
"comment_karma": 127,
|
|
||||||
"is_gold": false,
|
|
||||||
"is_mod": false,
|
|
||||||
"accept_chats": false,
|
|
||||||
"link_karma": 2485,
|
|
||||||
"has_verified_email": false,
|
|
||||||
"id": "def",
|
|
||||||
"accept_pms": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"before": null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-2
@@ -160,7 +160,7 @@ var expectedUserSubreddits = &Subreddits{
|
|||||||
After: "t5_3knn1",
|
After: "t5_3knn1",
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedSearchUsers2 = &Users{
|
var expectedSearchUsers = &Users{
|
||||||
Users: []*User{
|
Users: []*User{
|
||||||
{
|
{
|
||||||
ID: "179965",
|
ID: "179965",
|
||||||
@@ -943,5 +943,5 @@ func TestUserService_Search(t *testing.T) {
|
|||||||
|
|
||||||
users, _, err := client.User.Search(ctx, "test")
|
users, _, err := client.User.Search(ctx, "test")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, expectedSearchUsers2, users)
|
assert.Equal(t, expectedSearchUsers, users)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user