Make "want read" button work
This commit is contained in:
@@ -12,11 +12,12 @@ import (
|
||||
)
|
||||
|
||||
type fetchedBook struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Summary string `json:"summary"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Summary string `json:"summary"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
}
|
||||
|
||||
func TestGetBook_Ok(t *testing.T) {
|
||||
|
||||
@@ -14,11 +14,12 @@ import (
|
||||
)
|
||||
|
||||
type bookUserGet struct {
|
||||
BookId uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
Read bool `json:"read"`
|
||||
BookId uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
}
|
||||
|
||||
func TestGetBooksHandler_Demo(t *testing.T) {
|
||||
|
||||
25
internal/apitest/put_userbook_read_test.go
Normal file
25
internal/apitest/put_userbook_read_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/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(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/book/"+bookId+"/read")
|
||||
}
|
||||
@@ -1,90 +1,74 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPutBookUpdate_NewReadOk(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"read": true
|
||||
}`
|
||||
testPutUserBooksHandler(t, payload, "21", http.StatusOK)
|
||||
}
|
||||
|
||||
func TestPutUserBooksHandler_UpdateRating(t *testing.T) {
|
||||
func TestPutRatingUserBooksHandler_UpdateRating(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 5
|
||||
}`
|
||||
bookId := "17"
|
||||
testPutUserBooksHandler(t, payload, bookId, http.StatusOK)
|
||||
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 TestPutUserBooksHandler_RateNewBookMakeItRead(t *testing.T) {
|
||||
func TestPutRatingUserBooksHandler_RateNewBookMakeItRead(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 7
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooksHandler(t, payload, bookId, http.StatusOK)
|
||||
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 TestPutUserBooksHandler_RatingTypeWrong(t *testing.T) {
|
||||
func TestPutRatingUserBooksHandler_RatingTypeWrong(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": "bad"
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooksHandler(t, payload, bookId, http.StatusInternalServerError)
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func TestPutUserBooksHandler_RatingMin(t *testing.T) {
|
||||
func TestPutRatingUserBooksHandler_RatingMin(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": -3
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooksHandler(t, payload, bookId, http.StatusBadRequest)
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutUserBooksHandler_RatingMax(t *testing.T) {
|
||||
func TestPutRatingUserBooksHandler_RatingMax(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 15
|
||||
}`
|
||||
bookId := "18"
|
||||
testPutUserBooksHandler(t, payload, bookId, http.StatusBadRequest)
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func TestPutUserBooksHandler_BadBookId(t *testing.T) {
|
||||
func TestPutRatingUserBooksHandler_BadBookId(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"rating": 15
|
||||
}`
|
||||
bookId := "18574"
|
||||
testPutUserBooksHandler(t, payload, bookId, http.StatusNotFound)
|
||||
testPutRateUserBooks(t, payload, bookId, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func testPutUserBooksHandler(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
router := testutils.TestSetup()
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("PUT", "/book/"+bookId, strings.NewReader(payload))
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, expectedCode, w.Code)
|
||||
func testPutRateUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
||||
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/book/"+bookId+"/rate")
|
||||
}
|
||||
|
||||
35
internal/apitest/put_userbook_wantread_test.go
Normal file
35
internal/apitest/put_userbook_wantread_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/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, "/book/"+bookId+"/wantread")
|
||||
}
|
||||
Reference in New Issue
Block a user