Use dto from code for unit tests

This commit is contained in:
2026-02-03 18:24:42 +01:00
parent fe75973acd
commit 02e4c579c6
4 changed files with 17 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/url"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@@ -59,23 +60,24 @@ func TestGetReadBooksHandler_CheckOneBook(t *testing.T) {
token := testutils.ConnectDemo2User(router)
result := testGetReadBooksHandler(t, router, token, 200, "100", "")
var book bookUserGetBook
var book dto.BookUserGetBook
for _, b := range result.Books {
if b.Title == "De sang-froid" {
book = b
}
}
assert.Equal(t,
bookUserGetBook{
BookId: 18,
dto.BookUserGetBook{
ID: 18,
Title: "De sang-froid",
Author: "Truman Capote",
Rating: 6,
Read: true,
CoverPath: "/bookcover/desangfroid.jpg",
}, book)
}
func testGetReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) bookUserGet {
func testGetReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) dto.BookUserGet {
u, err := url.Parse("/mybooks/read")
if err != nil {
t.Error(err)

View File

@@ -4,6 +4,7 @@ import (
"net/url"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@@ -27,7 +28,7 @@ func TestGetReadingBooksHandler_Demo2(t *testing.T) {
assert.Equal(t, int64(0), result.Count)
}
func testGetReadingBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) bookUserGet {
func testGetReadingBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) dto.BookUserGet {
u, err := url.Parse("/mybooks/reading")
if err != nil {
t.Error(err)

View File

@@ -7,33 +7,20 @@ import (
"net/http/httptest"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
type bookUserGet struct {
Count int64 `json:"count"`
Books []bookUserGetBook `json:"books"`
}
type bookUserGetBook 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"`
WantRead bool `json:"wantread"`
}
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, url string) bookUserGet {
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, url string) dto.BookUserGet {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var parsedResponse bookUserGet
var parsedResponse dto.BookUserGet
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
if err != nil {
t.Error(err)

View File

@@ -3,6 +3,7 @@ package apitest
import (
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/dto"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
@@ -26,6 +27,6 @@ func TestGetWantReadBooksHandler_Demo2(t *testing.T) {
assert.Equal(t, int64(0), result.Count)
}
func testGetWantReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) bookUserGet {
func testGetWantReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) dto.BookUserGet {
return testGetbooksHandler(t, router, userToken, expectedCode, "/mybooks/wantread")
}