Use dto from code for getbook unit tests
This commit is contained in:
@@ -7,29 +7,15 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestGetBook_Ok(t *testing.T) {
|
||||||
book := testGetBook(t, "3", http.StatusOK)
|
book := testGetBook(t, "3", http.StatusOK)
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
fetchedBook{
|
dto.BookGet{
|
||||||
Title: "D'un château l'autre",
|
Title: "D'un château l'autre",
|
||||||
Author: "Louis-Ferdinand Céline",
|
Author: "Louis-Ferdinand Céline",
|
||||||
Rating: 10,
|
Rating: 10,
|
||||||
@@ -41,7 +27,7 @@ func TestGetBook_Ok(t *testing.T) {
|
|||||||
func TestGetBook_NoUserBook(t *testing.T) {
|
func TestGetBook_NoUserBook(t *testing.T) {
|
||||||
book := testGetBook(t, "18", http.StatusOK)
|
book := testGetBook(t, "18", http.StatusOK)
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
fetchedBook{
|
dto.BookGet{
|
||||||
Title: "De sang-froid",
|
Title: "De sang-froid",
|
||||||
Author: "Truman Capote",
|
Author: "Truman Capote",
|
||||||
Read: false,
|
Read: false,
|
||||||
@@ -52,7 +38,7 @@ func TestGetBook_NoUserBook(t *testing.T) {
|
|||||||
func TestGetBook_Description(t *testing.T) {
|
func TestGetBook_Description(t *testing.T) {
|
||||||
book := testGetBook(t, "22", http.StatusOK)
|
book := testGetBook(t, "22", http.StatusOK)
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
fetchedBook{
|
dto.BookGet{
|
||||||
Title: "Le complot contre l'Amérique",
|
Title: "Le complot contre l'Amérique",
|
||||||
Author: "Philip Roth",
|
Author: "Philip Roth",
|
||||||
ISBN: "9782070337903",
|
ISBN: "9782070337903",
|
||||||
@@ -71,7 +57,7 @@ func TestGetBook_IdNotInt(t *testing.T) {
|
|||||||
testGetBook(t, "wrong", http.StatusBadRequest)
|
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()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
@@ -80,7 +66,7 @@ func testGetBook(t *testing.T, id string, status int) fetchedBook {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
var book fetchedBook
|
var book dto.BookGet
|
||||||
err := json.Unmarshal(w.Body.Bytes(), &book)
|
err := json.Unmarshal(w.Body.Bytes(), &book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func TestPostImportBookHandler_Ok(t *testing.T) {
|
|||||||
book := testGetBook(t, strconv.FormatUint(uint64(id), 10), 200)
|
book := testGetBook(t, strconv.FormatUint(uint64(id), 10), 200)
|
||||||
assert.Equal(t, "les Hauts de Hurle-Vent", book.Title)
|
assert.Equal(t, "les Hauts de Hurle-Vent", book.Title)
|
||||||
assert.Equal(t, "Emily Brontë", book.Author)
|
assert.Equal(t, "Emily Brontë", book.Author)
|
||||||
assert.Equal(t, "isbn:9782253004752", book.InventaireID)
|
assert.Equal(t, "isbn:9782253004752", book.InventaireId)
|
||||||
assert.Equal(t, "/bookcover/44abbcbdc1092212c2bae66f5165019dac1e2a7b.webp", book.CoverPath)
|
assert.Equal(t, "/bookcover/44abbcbdc1092212c2bae66f5165019dac1e2a7b.webp", book.CoverPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ func TestPostImportBookHandler_OkAuthorKey(t *testing.T) {
|
|||||||
book := testGetBook(t, strconv.FormatUint(uint64(id), 10), 200)
|
book := testGetBook(t, strconv.FormatUint(uint64(id), 10), 200)
|
||||||
assert.Equal(t, "Dr Bloodmoney", book.Title)
|
assert.Equal(t, "Dr Bloodmoney", book.Title)
|
||||||
assert.Equal(t, "Philip K. Dick", book.Author)
|
assert.Equal(t, "Philip K. Dick", book.Author)
|
||||||
assert.Equal(t, "isbn:9782290033630", book.InventaireID)
|
assert.Equal(t, "isbn:9782290033630", book.InventaireId)
|
||||||
assert.Equal(t, "/bookcover/1d1493159d031224a42b37c4417fcbb8c76b00bd.webp", book.CoverPath)
|
assert.Equal(t, "/bookcover/1d1493159d031224a42b37c4417fcbb8c76b00bd.webp", book.CoverPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user