Add author books list
This commit is contained in:
71
internal/apitest/get_book_per_author.go
Normal file
71
internal/apitest/get_book_per_author.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type bookAuthorGet struct {
|
||||
Id uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
CoverPath string `json:"coverPath"`
|
||||
}
|
||||
|
||||
func TestSearchBookPerAuthor_Ok(t *testing.T) {
|
||||
books := testFetchBookAuthor(t, 1, "", "")
|
||||
assert.Equal(t, 3, len(books))
|
||||
}
|
||||
|
||||
func TestSearchBookPerAuthor_Limit(t *testing.T) {
|
||||
books := testFetchBookAuthor(t, 1, "2", "")
|
||||
assert.Equal(t, 2, len(books))
|
||||
}
|
||||
|
||||
func TestSearchBookPerAuthor_Offset(t *testing.T) {
|
||||
books := testFetchBookAuthor(t, 1, "10", "2")
|
||||
assert.Equal(t, 1, len(books))
|
||||
}
|
||||
|
||||
func testFetchBookAuthor(t *testing.T, authorId uint, limit string, offset string) []bookAuthorGet {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
u, err := url.Parse(fmt.Sprintf("/author/%d/books", authorId))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if limit != "" {
|
||||
q := u.Query()
|
||||
q.Set("limit", limit)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
if offset != "" {
|
||||
q := u.Query()
|
||||
q.Set("offset", offset)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var books []bookAuthorGet
|
||||
s := w.Body.String()
|
||||
err = json.Unmarshal([]byte(s), &books)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 200, w.Code)
|
||||
return books
|
||||
}
|
||||
@@ -71,3 +71,22 @@ func TestGetReadingBooksCountHandler_Demo(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, 2, c.Count)
|
||||
}
|
||||
|
||||
func TestGetAuthorsBooksCountHandler_Demo(t *testing.T) {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/author/1/books/count", nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var c countResponse
|
||||
err := json.Unmarshal(w.Body.Bytes(), &c)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 3, c.Count)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user