First implementation of fetching collections of book managed by user

This commit is contained in:
2026-04-02 16:23:22 +02:00
parent acdc3972bd
commit a280647575
18 changed files with 377 additions and 41 deletions

View File

@@ -1,11 +1,7 @@
package apitest
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"git.artlef.fr/bibliomane/internal/dto"
@@ -14,45 +10,12 @@ import (
)
func TestFetchAllBooks(t *testing.T) {
result := testFetchBooks(t, "15", "0")
status, result := testFetchBooks(t, "15", "0")
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, int64(31), result.Count)
assert.Equal(t, 15, len(result.Books))
}
func testFetchBooks(t *testing.T, limit string, offset string) dto.BookItemsGet {
router := testutils.TestSetup()
u, err := url.Parse("/ws/books")
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()
}
q := u.Query()
q.Set("lang", "fr")
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 result dto.BookItemsGet
s := w.Body.String()
err = json.Unmarshal([]byte(s), &result)
if err != nil {
t.Error(err)
}
assert.Equal(t, 200, w.Code)
return result
func testFetchBooks(t *testing.T, limit string, offset string) (int, dto.BookItemsGet) {
return testutils.TestFetchModel[dto.BookItemsGet](t, "/ws/books", limit, offset)
}