Files
sm-faq-plugin/models/faq_test.go
Jakub Zych 919cf06eca test(17-06): FAQ plugin unit tests
- TestFaqContentDecode, TestFaqModel and TestFaqSerialize (Docker-free):
  content as list, JSON text or row-keyed object, required export
  fields, the model contract, output sanitizing, null description,
  question text and the slug shape
- TestFaqAPI and TestFaqImport (Postgres): active duplicate behind an
  inactive lower id, default locale per is_default, a disabled default,
  malformed slugs, keyed content order, malformed rows aborting before
  any write
2026-10-12 19:42:42 +02:00

36 lines
1.2 KiB
Go

package models_test
import (
"slices"
"testing"
"git.golem15.com/golem15/sm-faq-plugin/models"
)
// TestFaqModel pins the FAQ model contract: the PHP class string and table,
// the fillable list (no id or timestamps), the required name and slug, and
// the stored content read back as rows from the JSON column.
func TestFaqModel(t *testing.T) {
var f models.FAQ
if f.TableName() != "golem15_faq_faqs" || f.MorphName() != `Golem15\FAQ\Models\FAQ` {
t.Fatal("table or morph name changed")
}
if fill := f.Fillable(); !slices.Equal(fill, []string{"name", "slug", "locale_id", "is_active", "description", "content"}) {
t.Fatalf("Fillable = %v", fill)
}
if rules := f.Rules(); len(rules) != 2 || rules["name"] != "required" || rules["slug"] != "required" {
t.Fatalf("Rules = %v", rules)
}
if err := f.Content.Scan([]byte(`[{"question":"Q","answer":"<p>A</p>"},{"question":"Q2","answer":3}]`)); err != nil {
t.Fatal(err)
}
items := f.Items()
if len(items) != 2 || items[0]["question"] != "Q" || items[1]["answer"] != float64(3) {
t.Fatalf("items = %v", items)
}
var empty models.FAQ
if err := empty.Content.Scan(nil); err != nil || len(empty.Items()) != 0 {
t.Fatalf("null content = %v %v", empty.Items(), err)
}
}