Files
sm-journal-plugin/controllers/api/categories_test.go
Jakub Zych 740dcd086d feat(api): serve the category content field on GET /_journal/api/v1/categories
- add the nullable TEXT column golem15_journal_categories.content (migration 202610090001)
- map it as models.Category.Content and emit category content on every categories row
- document the categories row shape and the stored-as-is HTML semantics
2026-10-09 16:24:20 +02:00

63 lines
2.0 KiB
Go

package api
import (
"reflect"
"sort"
"testing"
"git.golem15.com/golem15/sm-journal-plugin/models"
)
func sortedCategoryKeys(row map[string]any) []string {
keys := make([]string, 0, len(row))
for k := range row {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func TestSerializeCategoryNodeContent(t *testing.T) {
html := "<h2>Acme</h2>\n<p>Pillar copy</p>\n"
var parentID uint = 1
root := models.Category{ID: 1, Name: "News", Slug: "news", Content: &html}
child := models.Category{ID: 2, Name: "Quiet", Slug: "quiet", ParentID: &parentID}
byParent := map[uint][]models.Category{1: {child}}
counts := map[uint]int64{1: 2}
row := serializeCategoryNode(&root, byParent, counts, true)
if row == nil {
t.Fatal("root row is nil")
}
if got, ok := row["content"].(string); !ok || got != html {
t.Fatalf("root content = %#v, want %q", row["content"], html)
}
wantRoot := []string{"children", "content", "description", "id", "name", "nest_depth", "parent_id", "post_count", "slug"}
if got := sortedCategoryKeys(row); !reflect.DeepEqual(got, wantRoot) {
t.Fatalf("root keys = %v, want %v", got, wantRoot)
}
kids, ok := row["children"].([]map[string]any)
if !ok || len(kids) != 1 {
t.Fatalf("children = %#v, want one row", row["children"])
}
v, present := kids[0]["content"]
if !present || v != nil {
t.Fatalf("child content = %#v (present %v), want nil", v, present)
}
wantChild := []string{"content", "description", "id", "name", "nest_depth", "parent_id", "post_count", "slug"}
if got := sortedCategoryKeys(kids[0]); !reflect.DeepEqual(got, wantChild) {
t.Fatalf("child keys = %v, want %v", got, wantChild)
}
listed := serializeCategoryNode(&root, byParent, counts, false)
if listed == nil {
t.Fatal("root omitted without include_empty")
}
if _, has := listed["children"]; has {
t.Fatalf("empty child listed without include_empty: %#v", listed["children"])
}
if got, ok := listed["content"].(string); !ok || got != html {
t.Fatalf("root content without include_empty = %#v, want %q", listed["content"], html)
}
}