1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-20 14:31:09 +02:00
pocketbase/tools/hook/hook_test.go

162 lines
3.8 KiB
Go
Raw Normal View History

2022-07-07 00:19:05 +03:00
package hook
import (
"errors"
"testing"
)
2024-09-29 19:23:19 +03:00
func TestHookAddHandlerAndAdd(t *testing.T) {
calls := ""
h := Hook[*Event]{}
h.BindFunc(func(e *Event) error { calls += "1"; return e.Next() })
h.BindFunc(func(e *Event) error { calls += "2"; return e.Next() })
h3Id := h.BindFunc(func(e *Event) error { calls += "3"; return e.Next() })
h.Bind(&Handler[*Event]{
Id: h3Id, // should replace 3
Func: func(e *Event) error { calls += "3'"; return e.Next() },
})
h.Bind(&Handler[*Event]{
Func: func(e *Event) error { calls += "4"; return e.Next() },
Priority: -2,
})
h.Bind(&Handler[*Event]{
Func: func(e *Event) error { calls += "5"; return e.Next() },
Priority: -1,
})
h.Bind(&Handler[*Event]{
Func: func(e *Event) error { calls += "6"; return e.Next() },
})
h.Bind(&Handler[*Event]{
Func: func(e *Event) error { calls += "7"; e.Next(); return errors.New("test") }, // error shouldn't stop the chain
})
h.Trigger(
&Event{},
func(e *Event) error { calls += "8"; return e.Next() },
func(e *Event) error { calls += "9"; return nil }, // skip next
func(e *Event) error { calls += "10"; return e.Next() },
)
if total := len(h.handlers); total != 7 {
t.Fatalf("Expected %d handlers, found %d", 7, total)
}
expectedCalls := "45123'6789"
if calls != expectedCalls {
t.Fatalf("Expected calls sequence %q, got %q", expectedCalls, calls)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
}
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
func TestHookLength(t *testing.T) {
h := Hook[*Event]{}
2024-09-29 19:23:19 +03:00
if l := h.Length(); l != 0 {
t.Fatalf("Expected 0 hook handlers, got %d", l)
}
2024-09-29 19:23:19 +03:00
h.BindFunc(func(e *Event) error { return e.Next() })
h.BindFunc(func(e *Event) error { return e.Next() })
2024-09-29 19:23:19 +03:00
if l := h.Length(); l != 2 {
t.Fatalf("Expected 2 hook handlers, got %d", l)
2022-07-07 00:19:05 +03:00
}
}
2024-09-29 19:23:19 +03:00
func TestHookUnbind(t *testing.T) {
h := Hook[*Event]{}
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
calls := ""
2024-09-29 19:23:19 +03:00
id1 := h.BindFunc(func(e *Event) error { calls += "1"; return e.Next() })
h.BindFunc(func(e *Event) error { calls += "2"; return e.Next() })
h.Bind(&Handler[*Event]{
Func: func(e *Event) error { calls += "3"; return e.Next() },
})
2024-09-29 19:23:19 +03:00
h.Unbind("missing") // should do nothing and not panic
2024-09-29 19:23:19 +03:00
if total := len(h.handlers); total != 3 {
t.Fatalf("Expected %d handlers, got %d", 3, total)
}
2024-09-29 19:23:19 +03:00
h.Unbind(id1)
2024-09-29 19:23:19 +03:00
if total := len(h.handlers); total != 2 {
t.Fatalf("Expected %d handlers, got %d", 2, total)
}
2024-09-29 19:23:19 +03:00
err := h.Trigger(&Event{}, func(e *Event) error { calls += "4"; return e.Next() })
if err != nil {
t.Fatal(err)
}
2024-09-29 19:23:19 +03:00
expectedCalls := "234"
2024-09-29 19:23:19 +03:00
if calls != expectedCalls {
t.Fatalf("Expected calls sequence %q, got %q", expectedCalls, calls)
}
}
2024-09-29 19:23:19 +03:00
func TestHookUnbindAll(t *testing.T) {
h := Hook[*Event]{}
2024-09-29 19:23:19 +03:00
h.UnbindAll() // should do nothing and not panic
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
h.BindFunc(func(e *Event) error { return nil })
h.BindFunc(func(e *Event) error { return nil })
2022-07-07 00:19:05 +03:00
if total := len(h.handlers); total != 2 {
2024-09-29 19:23:19 +03:00
t.Fatalf("Expected %d handlers before UnbindAll, found %d", 2, total)
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
h.UnbindAll()
2022-07-07 00:19:05 +03:00
if total := len(h.handlers); total != 0 {
2024-09-29 19:23:19 +03:00
t.Fatalf("Expected no handlers after UnbindAll, found %d", total)
2022-07-07 00:19:05 +03:00
}
}
2024-09-29 19:23:19 +03:00
func TestHookTriggerErrorPropagation(t *testing.T) {
err := errors.New("test")
2022-07-07 00:19:05 +03:00
scenarios := []struct {
2024-09-29 19:23:19 +03:00
name string
handlers []func(*Event) error
2022-07-07 00:19:05 +03:00
expectedError error
}{
{
2024-09-29 19:23:19 +03:00
"without error",
[]func(*Event) error{
2024-09-29 19:23:19 +03:00
func(e *Event) error { return e.Next() },
func(e *Event) error { return e.Next() },
2022-07-07 00:19:05 +03:00
},
nil,
},
{
2024-09-29 19:23:19 +03:00
"with error",
[]func(*Event) error{
2024-09-29 19:23:19 +03:00
func(e *Event) error { return e.Next() },
func(e *Event) error { e.Next(); return err },
func(e *Event) error { return e.Next() },
2022-07-07 00:19:05 +03:00
},
2024-09-29 19:23:19 +03:00
err,
2022-07-07 00:19:05 +03:00
},
}
2024-09-29 19:23:19 +03:00
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
h := Hook[*Event]{}
for _, handler := range s.handlers {
h.BindFunc(handler)
}
result := h.Trigger(&Event{})
if result != s.expectedError {
t.Fatalf("Expected %v, got %v", s.expectedError, result)
}
})
2022-07-07 00:19:05 +03:00
}
}