feat(bookstack-api-42g): implement ShelvesService List, Get, ListAll
Implement ShelvesService with List, Get, and ListAll iterator. Add mock server tests.
This commit is contained in:
parent
ac29420b5f
commit
13a952355e
4 changed files with 89 additions and 33 deletions
62
shelves_test.go
Normal file
62
shelves_test.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package bookstack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShelvesService_List(t *testing.T) {
|
||||
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/shelves" {
|
||||
t.Errorf("path = %s, want /api/shelves", r.URL.Path)
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"data": []map[string]any{{"id": 1, "name": "Shelf One"}},
|
||||
"total": 1,
|
||||
})
|
||||
})
|
||||
|
||||
shelves, err := c.Shelves.List(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(shelves) != 1 {
|
||||
t.Fatalf("got %d shelves, want 1", len(shelves))
|
||||
}
|
||||
}
|
||||
|
||||
func TestShelvesService_Get(t *testing.T) {
|
||||
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/shelves/7" {
|
||||
t.Errorf("path = %s, want /api/shelves/7", r.URL.Path)
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"id": 7, "name": "My Shelf",
|
||||
})
|
||||
})
|
||||
|
||||
shelf, err := c.Shelves.Get(context.Background(), 7)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if shelf.Name != "My Shelf" {
|
||||
t.Errorf("Name = %q, want %q", shelf.Name, "My Shelf")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShelvesService_Get_NotFound(t *testing.T) {
|
||||
c := testClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(map[string]any{
|
||||
"error": map[string]string{"message": "Shelf not found"},
|
||||
})
|
||||
})
|
||||
|
||||
_, err := c.Shelves.Get(context.Background(), 999)
|
||||
if !errors.Is(err, ErrNotFound) {
|
||||
t.Errorf("expected ErrNotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue