Add existing book fields in "create book" form

This commit is contained in:
2026-03-31 17:35:32 +02:00
parent c1b6b61678
commit 32d39cabcd
7 changed files with 162 additions and 19 deletions

View File

@@ -1,12 +1,15 @@
package apitest
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"git.artlef.fr/bibliomane/internal/dto"
"git.artlef.fr/bibliomane/internal/testutils"
"github.com/stretchr/testify/assert"
)
@@ -45,6 +48,36 @@ func TestPostBookHandler_noTitle(t *testing.T) {
testPostBookHandler(t, bookJson, 400)
}
func TestPostBookHandler_AllFields(t *testing.T) {
bookJson :=
`{
"author": "Kafka",
"title": "Amerika",
"isbn": "978-2-07-036803-7",
"inventaireid": "isbn:9782070368037",
"openlibraryid": "OL8838048M",
"shortdescription": "Roman de Franz Kafka",
"summary": "L'Amérique (Amerika en version originale allemande) ou Le Disparu (Der Verschollene, titre voulu par l'auteur et rendu au livre dans ses plus récentes éditions) est le premier roman de Franz Kafka (1883-1924)."
}`
id := testPostBookHandler(t, bookJson, 200)
createdBook := testGetBook(t, strconv.FormatUint(uint64(id), 10), http.StatusOK)
assert.Equal(t,
dto.FullBookGet{
Title: "Amerika",
Author: "Kafka",
AuthorID: 27,
ISBN: "978-2-07-036803-7",
InventaireId: "isbn:9782070368037",
OpenLibraryId: "OL8838048M",
Summary: "L'Amérique (Amerika en version originale allemande) ou Le Disparu (Der Verschollene, titre voulu par l'auteur et rendu au livre dans ses plus récentes éditions) est le premier roman de Franz Kafka (1883-1924).",
Rating: 0,
Read: false,
CoverPath: "",
Review: "",
}, createdBook)
}
func TestPostBookHandler_TitleTooLong(t *testing.T) {
bookJson :=
`{
@@ -63,7 +96,7 @@ func TestPostBookHandler_AuthorTooLong(t *testing.T) {
testPostBookHandler(t, bookJson, 400)
}
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) {
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) uint {
router := testutils.TestSetup()
w := httptest.NewRecorder()
@@ -75,4 +108,16 @@ func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) {
assert.Equal(t, expectedCode, w.Code)
if w.Code != http.StatusOK {
return 0
}
var parsed struct {
ID uint
}
err := json.Unmarshal(w.Body.Bytes(), &parsed)
if err != nil {
t.Error(err)
}
return parsed.ID
}