Add tests for CollectionService
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
62b5b21e1c
commit
260df95025
3 changed files with 290 additions and 0 deletions
235
collection_test.go
Normal file
235
collection_test.go
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
package reddit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
var expectedCollection = &Collection{
|
||||||
|
ID: "37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
Created: &Timestamp{time.Date(2020, 8, 6, 23, 25, 3, 0, time.UTC)},
|
||||||
|
Updated: &Timestamp{time.Date(2020, 8, 7, 1, 59, 32, 0, time.UTC)},
|
||||||
|
|
||||||
|
Title: "Test Title",
|
||||||
|
Permalink: "https://www.reddit.com/r/helloworldtestt/collection/37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
Layout: "TIMELINE",
|
||||||
|
|
||||||
|
SubredditID: "t5_2uquw1",
|
||||||
|
Author: "v_95",
|
||||||
|
AuthorID: "t2_164ab8",
|
||||||
|
PrimaryPostID: "t3_hs0cyh",
|
||||||
|
PostIDs: []string{"t3_hs0cyh", "t3_hqrg8s", "t3_hs03f3"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedCollections = []*Collection{
|
||||||
|
{
|
||||||
|
ID: "37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
Created: &Timestamp{time.Date(2020, 8, 6, 23, 25, 3, 0, time.UTC)},
|
||||||
|
Updated: &Timestamp{time.Date(2020, 8, 7, 1, 59, 32, 0, time.UTC)},
|
||||||
|
|
||||||
|
Title: "Test Title",
|
||||||
|
Permalink: "https://www.reddit.com/r/helloworldtestt/collection/37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
Layout: "TIMELINE",
|
||||||
|
|
||||||
|
SubredditID: "t5_2uquw1",
|
||||||
|
Author: "v_95",
|
||||||
|
AuthorID: "t2_164ab8",
|
||||||
|
PostIDs: []string{"t3_hs0cyh", "t3_hqrg8s", "t3_hs03f3"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "8e94db00-6605-46c6-b0d2-44653d6f538c",
|
||||||
|
Created: &Timestamp{time.Date(2020, 8, 7, 0, 56, 29, 0, time.UTC)},
|
||||||
|
Updated: &Timestamp{time.Date(2020, 8, 7, 1, 59, 27, 0, time.UTC)},
|
||||||
|
|
||||||
|
Title: "Test Title 2",
|
||||||
|
Description: "Test Description",
|
||||||
|
Permalink: "https://www.reddit.com/r/helloworldtestt/collection/8e94db00-6605-46c6-b0d2-44653d6f538c",
|
||||||
|
|
||||||
|
SubredditID: "t5_2uquw1",
|
||||||
|
Author: "v_95",
|
||||||
|
AuthorID: "t2_164ab8",
|
||||||
|
PostIDs: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "a1b3e088-f6b8-4d98-9e93-adaacef113cd",
|
||||||
|
Created: &Timestamp{time.Date(2020, 8, 7, 0, 55, 24, 0, time.UTC)},
|
||||||
|
Updated: &Timestamp{time.Date(2020, 8, 7, 0, 55, 24, 0, time.UTC)},
|
||||||
|
|
||||||
|
Title: "Test Title 3",
|
||||||
|
Permalink: "https://www.reddit.com/r/helloworldtestt/collection/a1b3e088-f6b8-4d98-9e93-adaacef113cd",
|
||||||
|
|
||||||
|
SubredditID: "t5_2uquw1",
|
||||||
|
Author: "v_95",
|
||||||
|
AuthorID: "t2_164ab8",
|
||||||
|
PostIDs: []string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_Get(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("./testdata/collection/collection.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/collection", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
form.Set("include_links", "false")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
collection, _, err := client.Collection.Get(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedCollection, collection)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_FromSubreddit(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("./testdata/collection/collections.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/subreddit_collections", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodGet, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("sr_fullname", "t5_2uquw1")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
collections, _, err := client.Collection.FromSubreddit(ctx, "t5_2uquw1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedCollections, collections)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_Create(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
blob, err := readFileContents("./testdata/collection/collection.json")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/create_collection", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("title", "Test Title")
|
||||||
|
form.Set("sr_fullname", "t5_2uquw1")
|
||||||
|
form.Set("display_layout", "TIMELINE")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
|
||||||
|
fmt.Fprint(w, blob)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, _, err = client.Collection.Create(ctx, nil)
|
||||||
|
assert.EqualError(t, err, "createRequest: cannot be nil")
|
||||||
|
|
||||||
|
collection, _, err := client.Collection.Create(ctx, &CollectionCreateRequest{
|
||||||
|
Title: "Test Title",
|
||||||
|
SubredditID: "t5_2uquw1",
|
||||||
|
Layout: "TIMELINE",
|
||||||
|
})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedCollection, collection)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_Delete(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/delete_collection", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := client.Collection.Delete(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_AddPost(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/add_post_to_collection", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("link_fullname", "t3_hs03f3")
|
||||||
|
form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := client.Collection.AddPost(ctx, "t3_hs03f3", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_RemovePost(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/remove_post_in_collection", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("link_fullname", "t3_hs03f3")
|
||||||
|
form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := client.Collection.RemovePost(ctx, "t3_hs03f3", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCollectionService_ReorderPosts(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/v1/collections/reorder_collection", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, http.MethodPost, r.Method)
|
||||||
|
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("collection_id", "37f1e52d-7ec9-466b-b4cc-59e86e071ed7")
|
||||||
|
form.Set("link_ids", "t3_hs0cyh,t3_hqrg8s,t3_hs03f3")
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, form, r.Form)
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := client.Collection.ReorderPosts(ctx, "37f1e52d-7ec9-466b-b4cc-59e86e071ed7", "t3_hs0cyh", "t3_hqrg8s", "t3_hs03f3")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
14
testdata/collection/collection.json
vendored
Normal file
14
testdata/collection/collection.json
vendored
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"subreddit_id": "t5_2uquw1",
|
||||||
|
"description": "",
|
||||||
|
"primary_link_id": "t3_hs0cyh",
|
||||||
|
"author_name": "v_95",
|
||||||
|
"collection_id": "37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
"display_layout": "TIMELINE",
|
||||||
|
"permalink": "https://www.reddit.com/r/helloworldtestt/collection/37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
"link_ids": ["t3_hs0cyh", "t3_hqrg8s", "t3_hs03f3"],
|
||||||
|
"title": "Test Title",
|
||||||
|
"created_at_utc": 1596756303.999,
|
||||||
|
"author_id": "t2_164ab8",
|
||||||
|
"last_update_utc": 1596765572.741
|
||||||
|
}
|
41
testdata/collection/collections.json
vendored
Normal file
41
testdata/collection/collections.json
vendored
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"subreddit_id": "t5_2uquw1",
|
||||||
|
"description": "",
|
||||||
|
"author_name": "v_95",
|
||||||
|
"collection_id": "37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
"display_layout": "TIMELINE",
|
||||||
|
"permalink": "https://www.reddit.com/r/helloworldtestt/collection/37f1e52d-7ec9-466b-b4cc-59e86e071ed7",
|
||||||
|
"link_ids": ["t3_hs0cyh", "t3_hqrg8s", "t3_hs03f3"],
|
||||||
|
"title": "Test Title",
|
||||||
|
"created_at_utc": 1596756303.999,
|
||||||
|
"author_id": "t2_164ab8",
|
||||||
|
"last_update_utc": 1596765572.741
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"subreddit_id": "t5_2uquw1",
|
||||||
|
"description": "Test Description",
|
||||||
|
"author_name": "v_95",
|
||||||
|
"collection_id": "8e94db00-6605-46c6-b0d2-44653d6f538c",
|
||||||
|
"display_layout": null,
|
||||||
|
"permalink": "https://www.reddit.com/r/helloworldtestt/collection/8e94db00-6605-46c6-b0d2-44653d6f538c",
|
||||||
|
"link_ids": [],
|
||||||
|
"title": "Test Title 2",
|
||||||
|
"created_at_utc": 1596761789.879,
|
||||||
|
"author_id": "t2_164ab8",
|
||||||
|
"last_update_utc": 1596765567.702
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"subreddit_id": "t5_2uquw1",
|
||||||
|
"description": "",
|
||||||
|
"author_name": "v_95",
|
||||||
|
"collection_id": "a1b3e088-f6b8-4d98-9e93-adaacef113cd",
|
||||||
|
"display_layout": null,
|
||||||
|
"permalink": "https://www.reddit.com/r/helloworldtestt/collection/a1b3e088-f6b8-4d98-9e93-adaacef113cd",
|
||||||
|
"link_ids": [],
|
||||||
|
"title": "Test Title 3",
|
||||||
|
"created_at_utc": 1596761724.19,
|
||||||
|
"author_id": "t2_164ab8",
|
||||||
|
"last_update_utc": 1596761724.19
|
||||||
|
}
|
||||||
|
]
|
Loading…
Reference in a new issue