Skip to content

Commit

Permalink
Fix setting aggregate version after snapshot was loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
totemcaf committed Dec 14, 2023
1 parent ac3a972 commit 6a31b3c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions aggregatestore/events/aggregatestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (r *AggregateStore) Load(ctx context.Context, aggregateType eh.AggregateTyp
if snapshot != nil {
sa.ApplySnapshot(snapshot)
fromVersion = snapshot.Version + 1
a.SetAggregateVersion(snapshot.Version)
}
}

Expand Down
44 changes: 44 additions & 0 deletions aggregatestore/events/aggregatestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,50 @@ func TestAggregateStore_TakeSnapshot(t *testing.T) {
assert.Equal(t, 1, a.appliedEvents)
}

func TestAggregateStore_TakeSnapshot_no_additional_events_after_snapshot_was_saved(t *testing.T) {
eventStore := &mocks.EventStore{
Events: make([]eh.Event, 0),
}

// The snapshot should have all the events applied to it, so no additional events should be applied
const eventCount = 3

store, err := NewAggregateStore(eventStore, WithSnapshotStrategy(NewEveryNumberEventSnapshotStrategy(eventCount)))
if err != nil {
t.Fatal("there should be no error:", err)
}

ctx := context.Background()

id := uuid.New()
agg := NewTestAggregateOther(id)

timestamp := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

for i := 0; i < eventCount; i++ {
agg.AppendEvent(mocks.EventType, &mocks.EventData{Content: fmt.Sprintf("event%d", i)}, timestamp)

if err := store.Save(ctx, agg); err != nil {
t.Error("should not be an error")
}
}

assert.NotNil(t, eventStore.Snapshot, "snapshot should be taken")

agg2, err := store.Load(ctx, agg.AggregateType(), agg.EntityID())
if err != nil {
t.Error("should not be an error")
}

a, ok := agg2.(*TestAggregateOther)
if !ok {
t.Error("wrong aggregate type")
}

assert.Equal(t, eventCount, a.AggregateVersion(), "aggregate version should be set correctly")
assert.Equal(t, 0, a.appliedEvents, "no additional events after snapshot was taken")
}

func TestAggregateStore_AggregateNotRegistered(t *testing.T) {
store, _ := createStore(t)

Expand Down

0 comments on commit 6a31b3c

Please sign in to comment.