47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestFakeRecordThenRecentNewestFirstByKind(t *testing.T) {
|
|
f := NewFake()
|
|
ctx := context.Background()
|
|
_ = f.Record(ctx, Event{Kind: "proposal_made", At: time.Unix(1, 0), Data: map[string]any{"n": "a"}})
|
|
_ = f.Record(ctx, Event{Kind: "action_taken", At: time.Unix(2, 0)})
|
|
_ = f.Record(ctx, Event{Kind: "proposal_made", At: time.Unix(3, 0), Data: map[string]any{"n": "b"}})
|
|
|
|
got, err := f.Recent(ctx, "proposal_made", 5)
|
|
if err != nil {
|
|
t.Fatalf("Recent: %v", err)
|
|
}
|
|
if len(got) != 2 || got[0].Data["n"] != "b" {
|
|
t.Fatalf("want newest-first [b,a], got %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestFakeRecentRespectsLimit(t *testing.T) {
|
|
f := NewFake()
|
|
ctx := context.Background()
|
|
for i := 0; i < 4; i++ {
|
|
_ = f.Record(ctx, Event{Kind: "proposal_made", At: time.Unix(int64(i), 0)})
|
|
}
|
|
got, _ := f.Recent(ctx, "proposal_made", 2)
|
|
if len(got) != 2 {
|
|
t.Fatalf("limit not honored: %d", len(got))
|
|
}
|
|
}
|
|
|
|
func TestNopStoreIsInert(t *testing.T) {
|
|
s := NewNop()
|
|
if err := s.Record(context.Background(), Event{Kind: "x"}); err != nil {
|
|
t.Fatalf("nop Record: %v", err)
|
|
}
|
|
got, err := s.Recent(context.Background(), "x", 5)
|
|
if err != nil || len(got) != 0 {
|
|
t.Fatalf("nop Recent = %+v, %v", got, err)
|
|
}
|
|
}
|