Files
bibliomane/internal/apitest/post_book_test.go

91 lines
3.3 KiB
Go

package apitest
import (
"net/http"
"strconv"
"testing"
"git.artlef.fr/bibliomane/internal/testutils"
"github.com/stretchr/testify/assert"
)
func TestPostBookHandler_OkAuthorExist(t *testing.T) {
bookJson :=
`{
"title": "Le Procès",
"author": "Franz Kafka"
}`
testPostBookHandler(t, bookJson, 200)
}
func TestPostBookHandler_OkNewAuthor(t *testing.T) {
bookJson :=
`{
"title": "Le joueur d'échecs",
"author": "Stefan Zweig"
}`
testPostBookHandler(t, bookJson, 200)
}
func TestPostBookHandler_OkOnlyTitle(t *testing.T) {
bookJson :=
`{
"title": "Le Procès"
}`
testPostBookHandler(t, bookJson, 200)
}
func TestPostBookHandler_noTitle(t *testing.T) {
bookJson :=
`{
"author": "Kafka"
}`
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, "Amerika", createdBook.Title)
assert.Equal(t, "Kafka", createdBook.Author)
assert.Equal(t, "978-2-07-036803-7", createdBook.ISBN)
assert.Equal(t, "isbn:9782070368037", createdBook.InventaireId)
assert.Equal(t, "OL8838048M", createdBook.OpenLibraryId)
assert.Equal(t, "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).", createdBook.Summary)
}
func TestPostBookHandler_TitleTooLong(t *testing.T) {
bookJson :=
`{
"title": "Noisy outlaws, unfriendly blobs, and some other things that aren't as scary, maybe, depending on how you feel about lost lands, stray cellphones, creatures from the sky, parents who disappear in Peru, a man named Lars Farf, and one other story we couldn't quite finish, so maybe you could help us out.",
"author": "Eli Horowitz"
}`
testPostBookHandler(t, bookJson, 400)
}
func TestPostBookHandler_AuthorTooLong(t *testing.T) {
bookJson :=
`{
"title": "something",
"author": "Wolfeschlegelsteinhausenbergerdorffwelchevoralternwarengewissenhaftschaferswessenschafewarenwohlgepflegeundsorgfaltigkeitbeschutzenvonangreifendurchihrraubgierigfeindewelchevoralternzwolftausendjahresvorandieerscheinenvanderersteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenachdiesternwelchegehabtbewohnbarplanetenkreisedrehensichundwohinderneurassevonverstandigmenschlichkeitkonntefortpflanzenundsicherfreuenanlebenslanglichfreudeundruhemitnichteinfurchtvorangreifenvonandererintelligentgeschopfsvonhinzwischensternartigraum"
}`
testPostBookHandler(t, bookJson, 400)
}
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) uint {
status, id := testutils.TestPostCall(t, "/ws/book", bookJson)
assert.Equal(t, expectedCode, status)
return id
}