Add a view to see all books in a collection
This commit is contained in:
@@ -12,8 +12,8 @@ import (
|
||||
func TestFetchAllCollections_OK(t *testing.T) {
|
||||
status, res := testFetchCollections(t, "10", "0")
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
assert.Equal(t, int64(3), res.Count)
|
||||
assert.Equal(t, 3, len(res.Collections))
|
||||
assert.Equal(t, int64(2), res.Count)
|
||||
assert.Equal(t, 2, len(res.Collections))
|
||||
}
|
||||
|
||||
func testFetchCollections(t *testing.T, limit string, offset string) (int, dto.CollectionItemsGet) {
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
@@ -61,20 +58,8 @@ func TestGetBook_IdNotInt(t *testing.T) {
|
||||
testGetBook(t, "wrong", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func testGetBook(t *testing.T, id string, status int) dto.FullBookGet {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("GET", "/ws/book/"+id, nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var book dto.FullBookGet
|
||||
err := json.Unmarshal(w.Body.Bytes(), &book)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, status, w.Code)
|
||||
func testGetBook(t *testing.T, id string, expectedStatus int) dto.FullBookGet {
|
||||
status, book := testutils.TestFetchOneModel[dto.FullBookGet](t, "/ws/book", id)
|
||||
assert.Equal(t, expectedStatus, status)
|
||||
return book
|
||||
}
|
||||
|
||||
34
internal/apitest/get_collection_test.go
Normal file
34
internal/apitest/get_collection_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetCollection_Ok(t *testing.T) {
|
||||
status, collection := testGetCollection(t, "1", "10", "0")
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
assert.Equal(t, "Littérature française", collection.Name)
|
||||
assert.Equal(t, 6, len(collection.Books))
|
||||
}
|
||||
|
||||
func TestGetCollection_Limit(t *testing.T) {
|
||||
status, collection := testGetCollection(t, "2", "3", "0")
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
assert.Equal(t, "Nouvelles", collection.Name)
|
||||
assert.Equal(t, 3, len(collection.Books))
|
||||
assert.Equal(t, int64(4), collection.Count)
|
||||
}
|
||||
|
||||
func TestGetCollection_Unauthorized(t *testing.T) {
|
||||
status, _ := testGetCollection(t, "4", "10", "0")
|
||||
assert.Equal(t, http.StatusUnauthorized, status)
|
||||
}
|
||||
|
||||
func testGetCollection(t *testing.T, id string, limit string, offset string) (int, dto.CollectionGet) {
|
||||
return testutils.TestFetchModel[dto.CollectionGet](t, "/ws/collection/"+id, limit, offset)
|
||||
}
|
||||
Reference in New Issue
Block a user