52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type bookSearchGet struct {
|
|
Title string `json:"title" binding:"required,max=300"`
|
|
Author string `json:"author" binding:"max=100"`
|
|
Id uint `json:"id"`
|
|
}
|
|
|
|
func TestSearchBook_MultipleBooks(t *testing.T) {
|
|
books := testSearchBook(t, "san")
|
|
|
|
assert.Equal(t, 2, len(books))
|
|
}
|
|
|
|
func TestSearchBook_AllFields(t *testing.T) {
|
|
books := testSearchBook(t, "de san")
|
|
assert.Equal(t, 1, len(books))
|
|
assert.Equal(t,
|
|
[]bookSearchGet{{Title: "De sang-froid", Author: "Truman Capote", Id: 18}},
|
|
books)
|
|
}
|
|
|
|
func testSearchBook(t *testing.T, searchterm string) []bookSearchGet {
|
|
router := testutils.TestSetup()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("GET", "/search/"+searchterm, nil)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
var books []bookSearchGet
|
|
err := json.Unmarshal(w.Body.Bytes(), &books)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
assert.Equal(t, 200, w.Code)
|
|
return books
|
|
}
|