ev/pkg/es/es_test.go

125 lines
2.2 KiB
Go
Raw Normal View History

2022-08-04 14:37:51 -06:00
package es_test
import (
"context"
2022-08-14 10:56:00 -06:00
"encoding/json"
2022-08-12 15:53:16 -06:00
"errors"
2022-08-04 14:37:51 -06:00
"fmt"
"testing"
"time"
2022-08-06 09:52:36 -06:00
"github.com/matryer/is"
2022-08-04 14:37:51 -06:00
"github.com/sour-is/ev/pkg/es"
2022-08-04 21:07:10 -06:00
memstore "github.com/sour-is/ev/pkg/es/driver/mem-store"
2022-08-04 14:37:51 -06:00
"github.com/sour-is/ev/pkg/es/event"
)
func TestES(t *testing.T) {
2022-08-06 09:52:36 -06:00
is := is.New(t)
2022-08-12 15:53:16 -06:00
2022-08-04 14:37:51 -06:00
ctx := context.Background()
2022-08-06 09:52:36 -06:00
err := event.Register(ctx, &ValueSet{})
is.NoErr(err)
2022-08-04 14:37:51 -06:00
2022-08-04 21:07:10 -06:00
memstore.Init(ctx)
2022-08-04 14:37:51 -06:00
2022-08-12 15:53:16 -06:00
_, err = es.Open(ctx, "mem")
is.True(errors.Is(err, es.ErrNoDriver))
_, err = es.Open(ctx, "bogo:")
is.True(errors.Is(err, es.ErrNoDriver))
2022-08-04 21:07:10 -06:00
es, err := es.Open(ctx, "mem:")
2022-08-06 09:52:36 -06:00
is.NoErr(err)
2022-08-04 14:37:51 -06:00
thing := &Thing{Name: "time"}
err = es.Load(ctx, thing)
2022-08-06 09:52:36 -06:00
is.NoErr(err)
2022-08-04 14:37:51 -06:00
t.Log(thing.StreamVersion(), thing.Name, thing.Value)
err = thing.OnSetValue(time.Now().String())
2022-08-06 09:52:36 -06:00
is.NoErr(err)
2022-08-04 14:37:51 -06:00
i, err := es.Save(ctx, thing)
2022-08-06 09:52:36 -06:00
is.NoErr(err)
2022-08-04 14:37:51 -06:00
t.Log(thing.StreamVersion(), thing.Name, thing.Value)
t.Log("Wrote: ", i)
2022-08-12 15:53:16 -06:00
i, err = es.Append(ctx, "thing-time", event.NewEvents(&ValueSet{Value: "xxx"}))
is.NoErr(err)
is.Equal(i, uint64(1))
2022-08-04 14:37:51 -06:00
events, err := es.Read(ctx, "thing-time", -1, -11)
2022-08-06 09:52:36 -06:00
is.NoErr(err)
2022-08-04 14:37:51 -06:00
for i, e := range events {
t.Logf("event %d %d - %v\n", i, e.EventMeta().Position, e)
}
2022-08-12 15:53:16 -06:00
first, err := es.FirstIndex(ctx, "thing-time")
is.NoErr(err)
is.Equal(first, uint64(1))
last, err := es.LastIndex(ctx, "thing-time")
is.NoErr(err)
is.Equal(last, uint64(2))
2022-08-04 14:37:51 -06:00
}
type Thing struct {
Name string
Value string
event.AggregateRoot
}
func (a *Thing) StreamID() string {
return fmt.Sprintf("thing-%s", a.Name)
}
func (a *Thing) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case *ValueSet:
a.Value = e.Value
}
}
}
func (a *Thing) OnSetValue(value string) error {
event.Raise(a, &ValueSet{Value: value})
return nil
}
type ValueSet struct {
Value string
eventMeta event.Meta
}
func (e *ValueSet) EventMeta() event.Meta {
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
func (e *ValueSet) SetEventMeta(eventMeta event.Meta) {
if e == nil {
return
}
e.eventMeta = eventMeta
}
2022-08-14 10:56:00 -06:00
func (e *ValueSet) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *ValueSet) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}
2022-08-04 14:37:51 -06:00
var (
_ event.Event = (*ValueSet)(nil)
_ event.Aggregate = (*Thing)(nil)
)