Collections: allow to drag and drop to change book position
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(4), res.Count)
|
||||
assert.Equal(t, 4, len(res.Collections))
|
||||
}
|
||||
|
||||
func testFetchCollections(t *testing.T, limit string, offset string) (int, dto.CollectionItemsGet) {
|
||||
|
||||
113
internal/apitest/post_collection_changeposition_test.go
Normal file
113
internal/apitest/post_collection_changeposition_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPostCollectionChangePositionHandler_PositionOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 14,
|
||||
"position": 2
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
_, collection := testGetCollection(t, collectionId, "10", "0")
|
||||
assert.Equal(t, uint(1), collection.Items[0].Position)
|
||||
assert.Equal(t, uint(2), collection.Items[1].Position)
|
||||
assert.Equal(t, uint(3), collection.Items[2].Position)
|
||||
assert.Equal(t, uint(4), collection.Items[3].Position)
|
||||
}
|
||||
|
||||
func TestPostCollectionChangePositionHandler_ChangeOtherElement(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 17,
|
||||
"position": 3
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
_, collection := testGetCollection(t, collectionId, "10", "0")
|
||||
assert.Equal(t, "Duo", collection.Items[3].Book.Title)
|
||||
}
|
||||
|
||||
func TestPostCollectionChangePositionHandler_LastPosition(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 19,
|
||||
"position": 546
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
_, collection := testGetCollection(t, collectionId, "10", "0")
|
||||
assert.Equal(t, "Recherches philosophiques", collection.Items[7].Book.Title)
|
||||
assert.Equal(t, "Le château", collection.Items[6].Book.Title)
|
||||
}
|
||||
|
||||
func TestPostCollectionChangePositionHandler_FirstPosition(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 16,
|
||||
"position": 1
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
_, collection := testGetCollection(t, collectionId, "10", "0")
|
||||
assert.Equal(t, "Duo", collection.Items[0].Book.Title)
|
||||
}
|
||||
|
||||
func TestPostCollectionChangePositionHandler_WrongPosition(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 9,
|
||||
"position": 0
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusBadRequest, status)
|
||||
}
|
||||
|
||||
func TestPostCollectionChangePositionHandler_MissingPosition(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 9
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusBadRequest, status)
|
||||
}
|
||||
|
||||
func TestPostCollectionChangePositionWrongCollection(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"itemId": 1,
|
||||
"position": 9
|
||||
}`
|
||||
collectionId := "5"
|
||||
status := testPostCollectionChangePositionHandler(collectionId, payload)
|
||||
assert.Equal(t, http.StatusInternalServerError, status)
|
||||
}
|
||||
|
||||
func testPostCollectionChangePositionHandler(collectionId string, payload string) int {
|
||||
router := testutils.TestSetup()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("POST", "/ws/collection/"+collectionId+"/changeposition",
|
||||
strings.NewReader(payload))
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
return w.Code
|
||||
}
|
||||
Reference in New Issue
Block a user