From ba3c302dc46554577efe654a6e27cee1b803a2a6 Mon Sep 17 00:00:00 2001 From: Jon Lundy Date: Tue, 6 Sep 2022 15:31:48 -0600 Subject: [PATCH] tests: add testing around set and es aggregate --- Makefile | 4 +- local.mk.secret | Bin 396 -> 0 bytes pkg/es/driver/mem-store/mem-store.go | 32 ++++- pkg/es/driver/projecter/projecter.go | 5 +- pkg/es/es.go | 7 +- pkg/es/es_test.go | 187 ++++++++++++++++++--------- pkg/set/set_test.go | 25 ++++ 7 files changed, 188 insertions(+), 72 deletions(-) delete mode 100644 local.mk.secret create mode 100644 pkg/set/set_test.go diff --git a/Makefile b/Makefile index f0d6d54..4f7cbc8 100644 --- a/Makefile +++ b/Makefile @@ -33,8 +33,10 @@ ifeq (, $(shell which gqlgen)) endif gqlgen + +EV_HOST?=localhost:8080 load: - watch -n .1 "http POST localhost:8080/inbox/asdf/test a=b one=1 two:='{\"v\":2}' | jq" + watch -n .1 "http POST $(EV_HOST)/inbox/asdf/test a=b one=1 two:='{\"v\":2}' | jq" bi: go build . diff --git a/local.mk.secret b/local.mk.secret deleted file mode 100644 index 14a036c5f418071be2f04e3c81f315b28d9b3dd2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 396 zcmV;70dxL^0Sp7iK5b$115hLZ2mqBq;Pfg$AzOkdT(^^Om zAUuEl2wASLU3RBF$>TEX$_-41Z(QhPGW1^;0>Q#xdHh5Yz-_-55n1i^r^Q)QwaRbUKz$(zoFNQ@-C>Dg73c_DPH{z!j>u8x1x21eueEo;snbB`Ywc2S#X9aHO&wvK^86PW&3=K^ q@y=QpttFuN@#xNE0gpJrl>wa 0 { @@ -140,6 +161,7 @@ func (m *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Even break } } + event.SetStreamID(m.streamID, events...) return nil }) @@ -147,8 +169,6 @@ func (m *eventLog) Read(ctx context.Context, pos int64, count int64) (event.Even return nil, err } - event.SetStreamID(m.streamID, events...) - return events, nil } diff --git a/pkg/es/driver/projecter/projecter.go b/pkg/es/driver/projecter/projecter.go index 28a5309..0d619c0 100644 --- a/pkg/es/driver/projecter/projecter.go +++ b/pkg/es/driver/projecter/projecter.go @@ -71,8 +71,9 @@ func (w *wrapper) Append(ctx context.Context, events event.Events, version uint6 for i := range events { e := events[i] eventType := event.TypeOf(e) - streamID := e.EventMeta().StreamID - streamPos := e.EventMeta().Position + m := e.EventMeta() + streamID := m.StreamID + streamPos := m.Position e1 := event.NewPtr(streamID, streamPos) event.SetStreamID("$all", e1) diff --git a/pkg/es/es.go b/pkg/es/es.go index e5604eb..beb9817 100644 --- a/pkg/es/es.go +++ b/pkg/es/es.go @@ -76,8 +76,6 @@ func Open(ctx context.Context, dsn string, options ...Option) (*EventStore, erro ctx, span := lg.Span(ctx) defer span.End() - Mes_open.Add(ctx, 1) - name, _, ok := strings.Cut(dsn, ":") if !ok { return nil, fmt.Errorf("%w: no scheme", ErrNoDriver) @@ -101,6 +99,8 @@ func Open(ctx context.Context, dsn string, options ...Option) (*EventStore, erro o.Apply(es) } + Mes_open.Add(ctx, 1) + return es, err } @@ -197,6 +197,9 @@ func (es *EventStore) LastIndex(ctx context.Context, streamID string) (uint64, e return l.LastIndex(ctx) } func (es *EventStore) EventStream() driver.EventStream { + if es == nil { + return nil + } d := es.Driver for d != nil { if d, ok := d.(driver.EventStream); ok { diff --git a/pkg/es/es_test.go b/pkg/es/es_test.go index 2572260..7300415 100644 --- a/pkg/es/es_test.go +++ b/pkg/es/es_test.go @@ -12,63 +12,15 @@ import ( "github.com/sour-is/ev/pkg/es" memstore "github.com/sour-is/ev/pkg/es/driver/mem-store" + "github.com/sour-is/ev/pkg/es/driver/projecter" + "github.com/sour-is/ev/pkg/es/driver/streamer" "github.com/sour-is/ev/pkg/es/event" ) -func TestES(t *testing.T) { - is := is.New(t) - - ctx := context.Background() - - err := event.Register(ctx, &ValueSet{}) - is.NoErr(err) - - memstore.Init(ctx) - - _, err = es.Open(ctx, "mem") - is.True(errors.Is(err, es.ErrNoDriver)) - - _, err = es.Open(ctx, "bogo:") - is.True(errors.Is(err, es.ErrNoDriver)) - - es, err := es.Open(ctx, "mem:") - is.NoErr(err) - - thing := &Thing{Name: "time"} - err = es.Load(ctx, thing) - is.NoErr(err) - - t.Log(thing.StreamVersion(), thing.Name, thing.Value) - - err = thing.OnSetValue(time.Now().String()) - is.NoErr(err) - - i, err := es.Save(ctx, thing) - is.NoErr(err) - - t.Log(thing.StreamVersion(), thing.Name, thing.Value) - t.Log("Wrote: ", i) - - i, err = es.Append(ctx, "thing-time", event.NewEvents(&ValueSet{Value: "xxx"})) - is.NoErr(err) - is.Equal(i, uint64(1)) - - events, err := es.Read(ctx, "thing-time", -1, -11) - is.NoErr(err) - - for i, e := range events { - t.Logf("event %d %d - %v\n", i, e.EventMeta().Position, e) - } - - 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)) - -} +var ( + _ event.Event = (*ValueSet)(nil) + _ event.Aggregate = (*Thing)(nil) +) type Thing struct { Name string @@ -77,9 +29,9 @@ type Thing struct { event.AggregateRoot } -func (a *Thing) StreamID() string { - return fmt.Sprintf("thing-%s", a.Name) -} +// 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) { @@ -118,7 +70,120 @@ func (e *ValueSet) UnmarshalBinary(b []byte) error { return json.Unmarshal(b, e) } -var ( - _ event.Event = (*ValueSet)(nil) - _ event.Aggregate = (*Thing)(nil) -) +func TestES(t *testing.T) { + is := is.New(t) + + ctx := context.Background() + + err := event.Register(ctx, &ValueSet{}) + is.NoErr(err) + + es.Init(ctx) + memstore.Init(ctx) + + { + store, err := es.Open(ctx, "mem") + is.True(errors.Is(err, es.ErrNoDriver)) + is.True(store.EventStream() == nil) + } + + { + _, err := es.Open(ctx, "bogo:") + is.True(errors.Is(err, es.ErrNoDriver)) + } + + store, err := es.Open(ctx, "mem:", streamer.New(ctx), projecter.New(ctx)) + is.NoErr(err) + + thing := &Thing{Name: "time"} + err = store.Load(ctx, thing) + is.NoErr(err) + + t.Log(thing.StreamVersion(), thing.Name, thing.Value) + + err = thing.OnSetValue(time.Now().String()) + is.NoErr(err) + + thing.SetStreamID("thing-time") + i, err := store.Save(ctx, thing) + is.NoErr(err) + + t.Log(thing.StreamVersion(), thing.Name, thing.Value) + t.Log("Wrote: ", i) + + i, err = store.Append(ctx, "thing-time", event.NewEvents(&ValueSet{Value: "xxx"})) + is.NoErr(err) + is.Equal(i, uint64(1)) + + events, err := store.Read(ctx, "thing-time", -1, -11) + is.NoErr(err) + + for i, e := range events { + t.Logf("event %d %d - %v\n", i, e.EventMeta().Position, e) + } + + first, err := store.FirstIndex(ctx, "thing-time") + is.NoErr(err) + is.Equal(first, uint64(1)) + + last, err := store.LastIndex(ctx, "thing-time") + is.NoErr(err) + is.Equal(last, uint64(2)) + + stream := store.EventStream() + is.True(stream != nil) +} + +func TestESOperations(t *testing.T) { + is := is.New(t) + ctx := context.Background() + + es.Init(ctx) + memstore.Init(ctx) + + store, err := es.Open(ctx, "mem:", streamer.New(ctx), projecter.New(ctx)) + is.NoErr(err) + + thing, err := es.Create(ctx, store, "thing-1", func(ctx context.Context, agg *Thing) error { + return agg.OnSetValue("foo") + }) + + is.NoErr(err) + is.Equal(thing.Version(), uint64(1)) + is.Equal(thing.Value, "foo") + + thing, err = es.Update(ctx, store, "thing-1", func(ctx context.Context, agg *Thing) error { + return agg.OnSetValue("bar") + }) + + is.NoErr(err) + is.Equal(thing.Version(), uint64(2)) + is.Equal(thing.Value, "bar") + + thing, err = es.Upsert(ctx, store, "thing-2", func(ctx context.Context, agg *Thing) error { + return agg.OnSetValue("bin") + }) + + is.NoErr(err) + is.Equal(thing.Version(), uint64(1)) + is.Equal(thing.Value, "bin") + + thing, err = es.Upsert(ctx, store, "thing-2", func(ctx context.Context, agg *Thing) error { + return agg.OnSetValue("baz") + }) + + is.NoErr(err) + is.Equal(thing.Version(), uint64(2)) + is.Equal(thing.Value, "baz") + +} + +func TestUnwrap(t *testing.T) { + is := is.New(t) + + err := errors.New("foo") + werr := fmt.Errorf("wrap: %w", err) + + is.Equal(es.Unwrap(werr), err) + is.Equal(es.Unwrap("test"), "") +} diff --git a/pkg/set/set_test.go b/pkg/set/set_test.go new file mode 100644 index 0000000..8c8ce4b --- /dev/null +++ b/pkg/set/set_test.go @@ -0,0 +1,25 @@ +package set_test + +import ( + "strings" + "testing" + + "github.com/matryer/is" + "github.com/sour-is/ev/pkg/set" +) + +func TestStringSet(t *testing.T) { + is := is.New(t) + + s := set.New(strings.Fields("one two three")...) + + is.True(s.Has("one")) + is.True(s.Has("two")) + is.True(s.Has("three")) + is.True(!s.Has("four")) + + is.Equal(set.New("one").String(), "set(one)") + + var n set.Set[string] + is.Equal(n.String(), "set()") +}