Use dto from code for getbook unit tests

This commit is contained in:
2026-02-03 18:40:55 +01:00
parent 02e4c579c6
commit 160d7e304f
2 changed files with 8 additions and 22 deletions

View File

@@ -7,29 +7,15 @@ import (
"net/http/httptest"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
type fetchedBook struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
ISBN string `json:"isbn"`
InventaireID string `json:"inventaireid"`
OpenLibraryId string `json:"openlibraryid"`
Summary string `json:"summary"`
Rating int `json:"rating"`
Read bool `json:"read"`
WantRead bool `json:"wantread"`
StartReadDate string `json:"startReadDate"`
EndReadDate string `json:"endReadDate"`
CoverPath string `json:"coverPath"`
}
func TestGetBook_Ok(t *testing.T) {
book := testGetBook(t, "3", http.StatusOK)
assert.Equal(t,
fetchedBook{
dto.BookGet{
Title: "D'un château l'autre",
Author: "Louis-Ferdinand Céline",
Rating: 10,
@@ -41,7 +27,7 @@ func TestGetBook_Ok(t *testing.T) {
func TestGetBook_NoUserBook(t *testing.T) {
book := testGetBook(t, "18", http.StatusOK)
assert.Equal(t,
fetchedBook{
dto.BookGet{
Title: "De sang-froid",
Author: "Truman Capote",
Read: false,
@@ -52,7 +38,7 @@ func TestGetBook_NoUserBook(t *testing.T) {
func TestGetBook_Description(t *testing.T) {
book := testGetBook(t, "22", http.StatusOK)
assert.Equal(t,
fetchedBook{
dto.BookGet{
Title: "Le complot contre l'Amérique",
Author: "Philip Roth",
ISBN: "9782070337903",
@@ -71,7 +57,7 @@ func TestGetBook_IdNotInt(t *testing.T) {
testGetBook(t, "wrong", http.StatusBadRequest)
}
func testGetBook(t *testing.T, id string, status int) fetchedBook {
func testGetBook(t *testing.T, id string, status int) dto.BookGet {
router := testutils.TestSetup()
token := testutils.ConnectDemoUser(router)
@@ -80,7 +66,7 @@ func testGetBook(t *testing.T, id string, status int) fetchedBook {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var book fetchedBook
var book dto.BookGet
err := json.Unmarshal(w.Body.Bytes(), &book)
if err != nil {
t.Error(err)