Files
bibliomane/internal/apitest/search_book_test.go

159 lines
4.2 KiB
Go

package apitest
import (
"encoding/json"
"fmt"
"strconv"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"git.artlef.fr/bibliomane/internal/dto"
"git.artlef.fr/bibliomane/internal/testutils"
"github.com/stretchr/testify/assert"
)
func TestSearchBook_MultipleBooks(t *testing.T) {
result := testSearchBook(t, "san", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(2), result.Count)
assert.Equal(t, 2, len(result.Books))
}
func TestSearchBook_OneBookNotUserBook(t *testing.T) {
result := testSearchBook(t, "iliade", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
Title: "Iliade",
Author: "Homère",
ID: 29,
Rating: 0,
Read: false,
WantRead: false,
CoverPath: "/static/bookcover/iliade.jpeg",
}},
result.Books)
}
func TestSearchBook_OneBookRead(t *testing.T) {
result := testSearchBook(t, "dieux", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
Title: "Les dieux ont soif",
Author: "Anatole France",
ID: 4,
Rating: 7,
Read: true,
StartReadDate: "2026-01-30",
WantRead: false,
CoverPath: "/static/bookcover/lesdieuxontsoif.jpg",
}},
result.Books)
}
func TestSearchBook_OneBookStartRead(t *testing.T) {
result := testSearchBook(t, "Recherches", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
Title: "Recherches philosophiques",
Author: "Ludwig Wittgenstein",
ID: 30,
Rating: 0,
Read: false,
StartReadDate: "2025-11-22",
WantRead: false,
CoverPath: "/static/bookcover/Recherches-philosophiques.jpg",
}},
result.Books)
}
func TestSearchBook_ISBN(t *testing.T) {
result := testSearchBook(t, "9782070337903", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
Title: "Le complot contre l'Amérique",
Author: "Philip Roth",
ID: 22,
Rating: 6,
Read: true,
WantRead: false,
CoverPath: "/static/bookcover/lecomplotcontrelamerique.jpg",
}},
result.Books)
}
func TestSearchBook_ISBNInventaire(t *testing.T) {
result := testSearchBook(t, "9782253158400", "", "", dto.InventaireIfNothingFound)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
ID: 0,
Title: "Les premières enquêtes de Maigret",
Author: "Georges Simenon",
Description: "roman de Georges Simenon",
InventaireID: "inv:cd8af0fc32d2f721d2853d02682dfd44",
IsInventaireEdition: true,
Rating: 0,
Read: false,
WantRead: false,
CoverPath: "https://inventaire.io/img/entities/17663a503d984204078c910d5bae68d52942b47d",
}},
result.Books)
}
func TestSearchBook_Limit(t *testing.T) {
result := testSearchBook(t, "a", "10", "", dto.NoInventaireSearch)
assert.Equal(t, 10, len(result.Books))
}
func TestSearchBook_Offset(t *testing.T) {
result := testSearchBook(t, "sa", "", "2", dto.NoInventaireSearch)
assert.Equal(t, int64(5), result.Count)
assert.Equal(t, 3, len(result.Books))
}
func testSearchBook(t *testing.T, searchterm string, limit string, offset string, inventaireSearchType dto.InventaireSearchType) dto.BookItemsGet {
router := testutils.TestSetup()
u, err := url.Parse("/ws/search/" + searchterm)
if err != nil {
t.Error(err)
}
if limit != "" {
q := u.Query()
q.Set("limit", limit)
u.RawQuery = q.Encode()
}
if offset != "" {
q := u.Query()
q.Set("offset", offset)
u.RawQuery = q.Encode()
}
q := u.Query()
q.Set("lang", "fr")
q.Set("inventaire", strconv.Itoa(int(inventaireSearchType)))
u.RawQuery = q.Encode()
token := testutils.ConnectDemoUser(router)
req, _ := http.NewRequest("GET", u.String(), nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var result dto.BookItemsGet
s := w.Body.String()
err = json.Unmarshal([]byte(s), &result)
if err != nil {
t.Error(err)
}
assert.Equal(t, 200, w.Code)
return result
}