Files
multiarch-docker-images/archivist/internal/pbapp/setup_test.go
T
thomas 9d7a1add12 archivist: fix setup.go to keep the original collection shape
The previous commit replaced setup.go with an incomplete rewrite. This
restores the original file (fixed columns with created/updated, pages,
sha256 unique index, config-removal cleanup, event relation,
superuser upsert, errAppendOnly) and keeps only the intended changes:
- enum fields become select fields created with the dummy placeholder
  (or the configured template names for the template field)
- the startup cleanup of the enum value lists runs at the end of Setup
- setup_test.go asserts the select columns and the cleanup
2026-11-12 09:43:52 +01:00

116 lines
3.4 KiB
Go

package pbapp
import (
"reflect"
"testing"
"ganigeorgiev.com/pocketbase/pocketbase"
"ganigeorgiev.com/pocketbase/pocketbase/core"
"github.com/napnap75/archivist/internal/config"
)
// TestEnsureDocumentsCreatesSelectEnums replaces the former text-storage
// workaround: declared enums are now official select fields (US-03, US-37),
// created with the single dummy placeholder.
func TestEnsureDocumentsCreatesSelectEnums(t *testing.T) {
app := pocketbase.NewWithConfig(pocketbase.Config{DefaultDataDir: t.TempDir()})
if err := app.Bootstrap(); err != nil {
t.Fatalf("bootstrap PocketBase: %v", err)
}
defer app.ClearBootstrap()
fields := []config.Field{
{Name: "documentType", Type: "enum"},
{Name: "bankAccount", Type: "enum"},
}
cfg := &config.Config{
Fields: map[string]config.Field{
"documentType": fields[0],
"bankAccount": fields[1],
},
FieldList: fields,
}
if err := Setup(app, "admin@example.net", "password-123456", cfg); err != nil {
t.Fatalf("setup: %v", err)
}
coll, err := app.FindCollectionByNameOrId("documents")
if err != nil {
t.Fatalf("find documents collection: %v", err)
}
for _, name := range []string{"documentType", "bankAccount"} {
field, ok := coll.Fields.GetByName(name).(*core.SelectField)
if !ok || field == nil {
t.Fatalf("field %q is not a select field", name)
}
if !reflect.DeepEqual(field.Values, []string{DummyEnumValue}) {
t.Errorf("field %q values = %v, want [%s]", name, field.Values, DummyEnumValue)
}
}
}
// TestCleanupEnumValues covers the startup cleanup (US-37): a value kept
// because a document uses it, an unused value dropped, the dummy
// placeholder kept only while alone, and the template field forced to the
// configured template names.
func TestCleanupEnumValues(t *testing.T) {
app := pocketbase.NewWithConfig(pocketbase.Config{DefaultDataDir: t.TempDir()})
if err := app.Bootstrap(); err != nil {
t.Fatalf("bootstrap PocketBase: %v", err)
}
defer app.ClearBootstrap()
fields := []config.Field{
{Name: "documentType", Type: "enum"},
{Name: "template", Type: "enum"},
}
cfg := &config.Config{
Fields: map[string]config.Field{
"documentType": fields[0],
"template": fields[1],
},
FieldList: fields,
Templates: map[string]*config.CompiledTemplate{
"default": {},
},
}
if err := Setup(app, "admin@example.net", "password-123456", cfg); err != nil {
t.Fatalf("setup: %v", err)
}
if err := AddEnumValue(app, "documentType", "Facture"); err != nil {
t.Fatalf("add enum value: %v", err)
}
if err := AddEnumValue(app, "documentType", "obsolete"); err != nil {
t.Fatalf("add enum value: %v", err)
}
coll, err := app.FindCollectionByNameOrId("documents")
if err != nil {
t.Fatalf("find documents collection: %v", err)
}
rec := core.NewRecord(coll)
rec.Set("file_key", "pending/x.pdf")
rec.Set("status", "pending")
rec.Set("documentType", "Facture")
if err := app.Save(rec); err != nil {
t.Fatalf("save record: %v", err)
}
CleanupEnumValues(app, cfg)
values, err := enumFieldValues(app, "documentType")
if err != nil {
t.Fatalf("enum field values: %v", err)
}
if !reflect.DeepEqual(values, []string{"Facture"}) {
t.Errorf("documentType values = %v, want [Facture]", values)
}
tplValues, err := enumFieldValues(app, "template")
if err != nil {
t.Fatalf("enum field values: %v", err)
}
if !reflect.DeepEqual(tplValues, []string{"default"}) {
t.Errorf("template values = %v, want [default]", tplValues)
}
}