Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting aggregate version after snapshot was loaded #416

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading