ev/pkg/es/event/events_test.go

58 lines
1021 B
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"
2023-02-26 22:33:01 -07:00
"go.sour.is/ev/pkg/es/event"
2022-08-04 14:37:51 -06:00
)
type DummyEvent struct {
Value string
2023-04-02 16:45:17 -06:00
event.IsEvent
2022-08-04 14:37:51 -06:00
}
2022-08-15 08:05:04 -06:00
func (e *DummyEvent) MarshalBinary() ([]byte, error) {
2022-08-14 10:56:00 -06:00
return json.Marshal(e)
}
2022-08-15 08:05:04 -06:00
func (e *DummyEvent) UnmarshalBinary(b []byte) error {
2022-08-14 10:56:00 -06:00
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])
}
}