public async Task CanSaveAndRetrieveAggregateRoots() { var aggregateId = Guid.NewGuid(); var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(aggregateId); aggregateRoot.ChangeValue(0); aggregateRoot.ChangeValue(1); aggregateRoot.ChangeValue(2); aggregateRoot.ChangeValue(3); aggregateRoot.ChangeValue(4); aggregateRoot.ChangeValue(5); aggregateRoot.ChangeValue(6); aggregateRoot.ChangeValue(7); aggregateRoot.ChangeValue(8); aggregateRoot.ChangeValue(9); aggregateRoot.ChangeValue(10); await this.repository.SaveAsync(aggregateRoot).ConfigureAwait(false); // Adds eleven events and creates a snapshot aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(12); await this.repository.SaveAsync(aggregateRoot).ConfigureAwait(false); // Adds two more events var aggregateRootFromEventStore = await this.repository .GetByIdAsync <MyDynamicEventSourcedAggregateRoot>(aggregateId) .ConfigureAwait(false); aggregateRootFromEventStore.Value.Should().Be(12); aggregateRootFromEventStore.Version.Should().Be(12); }
public async Task GlobalSnapshotStrategyDoesNotApply_WhenAggregateVersionIsZero() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId).WithVersion(0); await this.testee.SaveAsync(aggregateRoot).ConfigureAwait(false); A.CallTo(() => this.eventStream.SaveSnapshotAsync(A <ISnapshot> .Ignored)).MustNotHaveHappened(); }
public async Task GlobalSnapshotStrategyDoesNotApplyAnymore_WhenThereIsATypedSnapshotStrategy() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId).WithVersion(100); this.testee.WithSnapshotStrategyFor <MyDynamicEventSourcedAggregateRoot>(30); await this.testee.SaveAsync(aggregateRoot).ConfigureAwait(false); A.CallTo(() => this.eventStream.SaveSnapshotAsync(A <MySnapshot> .Ignored)).MustNotHaveHappened(); }
public async Task BuiltInGlobalSnapshotStrategyHasAThresholdOf100() { const int Version = 100; var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId).WithVersion(Version); await this.testee.SaveAsync(aggregateRoot).ConfigureAwait(false); A.CallTo(() => this.eventStream.SaveSnapshotAsync(A <MySnapshot> .That.Matches(s => s.Version == Version))).MustHaveHappened(); }
public async Task CanSetAndUseTypedSnapshotStrategy() { const int Version = 20; var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId).WithVersion(Version); this.testee.WithSnapshotStrategyFor <MyDynamicEventSourcedAggregateRoot>(10); await this.testee.SaveAsync(aggregateRoot).ConfigureAwait(false); A.CallTo(() => this.eventStream.SaveSnapshotAsync(A <MySnapshot> .That.Matches(s => s.Version == Version))).MustHaveHappened(); }
public async Task CanSaveEventsWithoutHeaders() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId); aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(22); await this.testee.SaveAsync(aggregateRoot).ConfigureAwait(false); A.CallTo(() => this.eventStream.SaveAsync(A <IEnumerable <VersionableEvent> > .Ignored, 1, A <IDictionary <string, object> > .Ignored)).MustHaveHappened(); }
public async Task CommitsUncommittedEventsOnAggregateRootAfterSavingUncommittedEvents() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId); aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(22); await this.testee.SaveAsync(aggregateRoot).ConfigureAwait(false); aggregateRoot.UncommittedEvents.Should().BeEmpty(); }
public async Task CanSaveEventsWithHeaders() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId); aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(22); var headers = new Dictionary <string, object> { { "UserName", "Patrick" }, { "MagicNumber", 42 } }; await this.testee.SaveAsync(aggregateRoot, headers).ConfigureAwait(false); A.CallTo(() => this.eventStream.SaveAsync(A <IEnumerable <VersionableEvent> > .Ignored, 1, headers)).MustHaveHappened(); }
public async Task CanSaveAndReplayEventsSinceLatestSnapshot() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId); aggregateRoot.ChangeValue(0); aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(22); await this.SaveEventsAsync(aggregateRoot).ConfigureAwait(false); var firstSnapshot = aggregateRoot.CreateSnapshot(); await this.SaveSnapshotAsync(firstSnapshot).ConfigureAwait(false); aggregateRoot.ChangeValue(33); aggregateRoot.ChangeValue(44); await this.SaveEventsAsync(aggregateRoot).ConfigureAwait(false); var secondSnapshot = aggregateRoot.CreateSnapshot(); await this.SaveSnapshotAsync(secondSnapshot).ConfigureAwait(false); aggregateRoot.ChangeValue(55); aggregateRoot.ChangeValue(66); aggregateRoot.ChangeValue(77); await this.SaveEventsAsync(aggregateRoot).ConfigureAwait(false); using (var eventStream = this.testee.OpenStream <MyDynamicEventSourcedAggregateRoot>(this.aggregateId)) { var hasSnapshot = await eventStream.HasSnapshotAsync().ConfigureAwait(false); var snapshotFromEventStore = await eventStream.GetLatestSnapshotAsync().ConfigureAwait(false); hasSnapshot.Should().BeTrue(); snapshotFromEventStore.Version.Should().Be(secondSnapshot.Version); var eventHistorySinceLatestSnapshot = await eventStream .ReplayAsyncFromSnapshot(snapshotFromEventStore) .ConfigureAwait(false); eventHistorySinceLatestSnapshot.Should().HaveCount(3); eventHistorySinceLatestSnapshot.Should().Contain(e => (e as ValueEvent).Value == 55); eventHistorySinceLatestSnapshot.Should().Contain(e => (e as ValueEvent).Value == 66); eventHistorySinceLatestSnapshot.Should().Contain(e => (e as ValueEvent).Value == 77); } }
public async Task DispatchesEventsAfterSavingThem() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId); aggregateRoot.ChangeValue(0); aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(22); using (var eventStream = this.testee.OpenStream <MyDynamicEventSourcedAggregateRoot>(this.aggregateId)) { await eventStream.SaveAsync( aggregateRoot.UncommittedEvents.OfType <VersionableEvent>(), aggregateRoot.Version, new Dictionary <string, object>()).ConfigureAwait(false); } A.CallTo(() => this.bus.PublishAsync <IEvent>(A <ValueEvent> .That.Matches(e => e.Value == 0))).MustHaveHappened(); A.CallTo(() => this.bus.PublishAsync <IEvent>(A <ValueEvent> .That.Matches(e => e.Value == 11))).MustHaveHappened(); A.CallTo(() => this.bus.PublishAsync <IEvent>(A <ValueEvent> .That.Matches(e => e.Value == 22))).MustHaveHappened(); }
public async Task CanSaveAndReplayAllEvents() { var aggregateRoot = new MyDynamicEventSourcedAggregateRoot(this.aggregateId); aggregateRoot.ChangeValue(0); aggregateRoot.ChangeValue(11); aggregateRoot.ChangeValue(22); using (var eventStream = this.testee.OpenStream <MyDynamicEventSourcedAggregateRoot>(this.aggregateId)) { await eventStream.SaveAsync( aggregateRoot.UncommittedEvents.OfType <VersionableEvent>(), aggregateRoot.Version, new Dictionary <string, object>()).ConfigureAwait(false); var eventHistory = await eventStream.ReplayAsync().ConfigureAwait(false); eventHistory.Should().HaveCount(3); eventHistory.Should().Contain(e => (e as ValueEvent).Value == 0); eventHistory.Should().Contain(e => (e as ValueEvent).Value == 11); eventHistory.Should().Contain(e => (e as ValueEvent).Value == 22); } }
public DynamicEventSourcedAggregateRootTest() { this.testee = new MyDynamicEventSourcedAggregateRoot(); }