Refactor: make only one API route to update userbooks
This commit is contained in:
@@ -1,87 +0,0 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPutRatingUserBooksHandler_UpdateRating(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 5
|
||||
}`
|
||||
bookId := "17"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, 5, book.Rating)
|
||||
assert.Equal(t, true, book.Read)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RateNewBookMakeItRead(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 7
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, 7, book.Rating)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RateWantedBook(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 6
|
||||
}`
|
||||
bookId := "2"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, 6, book.Rating)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RatingTypeWrong(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": "bad"
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RatingMin(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": -3
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RatingMax(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 15
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_BadBookId(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 15
|
||||
}`
|
||||
bookId := "18574"
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func testPutRateUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId+"/rate")
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPutReadUserBooks_NewReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true
|
||||
}`
|
||||
bookId := "21"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_NewReadDateOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true,
|
||||
"endDate": "2025-10-20"
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, "2025-10-20", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_UnsetEndDate(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true,
|
||||
"endDate": "null"
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, "", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_UnsetReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": false
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, false, book.Read)
|
||||
assert.Equal(t, "", book.EndReadDate)
|
||||
}
|
||||
|
||||
func testPutReadUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId+"/read")
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPutStartReadUserBooks_NoDate(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"date": "2025-11-19"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutStartReadUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutStartReadUserBooks_WrongDateFormat(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"startDate": "19/11/2025"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutStartReadUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestPutStartReadUserBooks_NewReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"startDate": "2025-11-19"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutStartReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, "2025-11-19", book.StartReadDate)
|
||||
}
|
||||
|
||||
func TestPutStartReadUserBooks_Unset(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"startDate": "null"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutStartReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, "", book.StartReadDate)
|
||||
}
|
||||
|
||||
func testPutStartReadUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId+"/startread")
|
||||
}
|
||||
190
internal/apitest/put_userbook_test.go
Normal file
190
internal/apitest/put_userbook_test.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPutRatingUserBooksHandler_UpdateRating(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 5
|
||||
}`
|
||||
bookId := "17"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, 5, book.Rating)
|
||||
assert.Equal(t, true, book.Read)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RateNewBookMakeItRead(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 7
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, 7, book.Rating)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RateWantedBook(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 6
|
||||
}`
|
||||
bookId := "2"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, 6, book.Rating)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RatingTypeWrong(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": "bad"
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RatingMin(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": -3
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_RatingMax(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 15
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutRatingUserBooksHandler_BadBookId(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 15
|
||||
}`
|
||||
bookId := "18574"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_NewReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true
|
||||
}`
|
||||
bookId := "21"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_NewReadDateOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true,
|
||||
"endDate": "2025-10-20"
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, "2025-10-20", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_UnsetEndDate(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true,
|
||||
"endDate": "null"
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.Read)
|
||||
assert.Equal(t, "", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutReadUserBooks_UnsetReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": false
|
||||
}`
|
||||
bookId := "9"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, false, book.Read)
|
||||
assert.Equal(t, "", book.EndReadDate)
|
||||
}
|
||||
|
||||
func TestPutStartReadUserBooks_WrongDateFormat(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"startDate": "19/11/2025"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestPutStartReadUserBooks_NewReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"startDate": "2025-11-19"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, "2025-11-19", book.StartReadDate)
|
||||
}
|
||||
|
||||
func TestPutStartReadUserBooks_Unset(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"startDate": "null"
|
||||
}`
|
||||
bookId := "6"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, "", book.StartReadDate)
|
||||
}
|
||||
|
||||
func TestPutWantRead_SetTrue(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"wantread": true
|
||||
}`
|
||||
bookId := "17"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutWantRead_SetFalse(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"wantread": false
|
||||
}`
|
||||
bookId := "2"
|
||||
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func testPutUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId)
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPutWantRead_SetTrue(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"wantread": true
|
||||
}`
|
||||
bookId := "17"
|
||||
testPutWantReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, true, book.WantRead)
|
||||
}
|
||||
|
||||
func TestPutWantRead_SetFalse(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"wantread": false
|
||||
}`
|
||||
bookId := "2"
|
||||
testPutWantReadUserBooks(t, payload, bookId, http.StatusOK)
|
||||
book := testGetBook(t, bookId, http.StatusOK)
|
||||
assert.Equal(t, false, book.WantRead)
|
||||
}
|
||||
|
||||
func testPutWantReadUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId+"/wantread")
|
||||
}
|
||||
Reference in New Issue
Block a user