Create PostAndCommentService, holds common methods
Some functionality such as saving, voting, deleting, etc. is common to both posts and comments, so this service will hold such methods. Tweaked the separate post/comment services accordingly. Fixed tests. Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
@@ -87,81 +87,7 @@ func TestCommentService_Edit(t *testing.T) {
|
||||
fmt.Fprint(w, blob)
|
||||
})
|
||||
|
||||
_, _, err := client.Comment.Edit(ctx, "t3_test", "test comment")
|
||||
assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`)
|
||||
|
||||
comment, _, err := client.Comment.Edit(ctx, "t1_test", "test comment")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedCommentSubmitOrEdit, comment)
|
||||
}
|
||||
|
||||
func TestCommentService_Delete(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/api/del", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
|
||||
form := url.Values{}
|
||||
form.Set("id", "t1_test")
|
||||
|
||||
err := r.ParseForm()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, form, r.PostForm)
|
||||
|
||||
fmt.Fprint(w, `{}`)
|
||||
})
|
||||
|
||||
_, err := client.Comment.Delete(ctx, "t3_test")
|
||||
assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`)
|
||||
|
||||
res, err := client.Comment.Delete(ctx, "t1_test")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
func TestCommentService_Save(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/api/save", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
|
||||
form := url.Values{}
|
||||
form.Set("id", "t1_test")
|
||||
|
||||
err := r.ParseForm()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, form, r.PostForm)
|
||||
})
|
||||
|
||||
_, err := client.Comment.Save(ctx, "t3_test")
|
||||
assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`)
|
||||
|
||||
res, err := client.Comment.Save(ctx, "t1_test")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
func TestCommentService_Unsave(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/api/unsave", func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
|
||||
form := url.Values{}
|
||||
form.Set("id", "t1_test")
|
||||
|
||||
err := r.ParseForm()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, form, r.PostForm)
|
||||
})
|
||||
|
||||
_, err := client.Comment.Unsave(ctx, "t3_test")
|
||||
assert.EqualError(t, err, `must provide comment id (starting with t1_); id provided: "t3_test"`)
|
||||
|
||||
res, err := client.Comment.Unsave(ctx, "t1_test")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user