ev/pkg/es/event/events_test.go

70 lines
1.2 KiB
Go
Raw Normal View History

2022-08-04 14:37:51 -06:00
package event_test
import (
"bytes"
2022-08-06 09:52:36 -06:00
"context"
2022-08-14 10:56:00 -06:00
"encoding/json"
2022-08-04 14:37:51 -06:00
"testing"
"github.com/matryer/is"
"github.com/sour-is/ev/pkg/es/event"
)
type DummyEvent struct {
Value string
eventMeta event.Meta
}
func (e *DummyEvent) EventMeta() event.Meta {
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
func (e *DummyEvent) SetEventMeta(eventMeta event.Meta) {
if e == nil {
return
}
e.eventMeta = eventMeta
}
2022-08-14 10:56:00 -06:00
func (e *DummyEvent) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *DummyEvent) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}
2022-08-04 14:37:51 -06:00
func TestEventEncode(t *testing.T) {
is := is.New(t)
2022-08-06 09:52:36 -06:00
ctx := context.Background()
2022-08-04 14:37:51 -06:00
2022-08-06 09:52:36 -06:00
err := event.Register(ctx, &DummyEvent{})
is.NoErr(err)
2022-08-04 14:37:51 -06:00
var lis event.Events = event.NewEvents(
&DummyEvent{Value: "testA"},
&DummyEvent{Value: "testB"},
&DummyEvent{Value: "testC"},
)
lis.SetStreamID("test")
blis, err := event.EncodeEvents(lis...)
is.NoErr(err)
for _, b := range blis {
sp := bytes.SplitN(b, []byte{'\t'}, 4)
is.Equal(len(sp), 4)
is.Equal(string(sp[1]), "test")
is.Equal(string(sp[2]), "event_test.DummyEvent")
}
2022-08-06 09:52:36 -06:00
chk, err := event.DecodeEvents(ctx, blis...)
2022-08-04 14:37:51 -06:00
is.NoErr(err)
for i := range chk {
is.Equal(lis[i], chk[i])
}
}