77 lines
1.4 KiB
Go
77 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/config"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func testSetup() *gin.Engine {
|
|
c := config.LoadConfig("config_test/test.toml")
|
|
return setup(&c)
|
|
}
|
|
|
|
func TestGetBooksHandler(t *testing.T) {
|
|
router := testSetup()
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/books", nil)
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
}
|
|
|
|
func TestPostBookHandler_Ok(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"title": "Le château",
|
|
"author": "Kafka",
|
|
"rating": 9
|
|
}`
|
|
testPostBookHandler(t, bookJson, 200)
|
|
}
|
|
|
|
func TestPostBookHandler_OkOnlyTitle(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"title": "Le château"
|
|
}`
|
|
testPostBookHandler(t, bookJson, 200)
|
|
}
|
|
|
|
func TestPostBookHandler_noTitle(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"author": "Kafka",
|
|
"rating": 9
|
|
}`
|
|
testPostBookHandler(t, bookJson, 400)
|
|
}
|
|
|
|
func TestPostBookHandler_WrongRating(t *testing.T) {
|
|
bookJson :=
|
|
`{
|
|
"title": "Le château",
|
|
"author": "Kafka",
|
|
"rating": 15
|
|
}`
|
|
testPostBookHandler(t, bookJson, 400)
|
|
}
|
|
|
|
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) {
|
|
router := testSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
req, _ := http.NewRequest("POST", "/book",
|
|
strings.NewReader(string(bookJson)))
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, expectedCode, w.Code)
|
|
|
|
}
|