public void Aggregate_ShouldBeRebuildedFromSnapshot_WhenSnapshotDataAvailable() { var aggStore = new AggregateStore(new EventStore(new DefaultEventSerializer(), new EventDataStorage()), new DefaultAggregateSerializer(), new SnapshotDataStorage()); var car = new Car(); car.ChangeTyre(new Tyre() { Price = 10 }, TyrePlacement.LeftBack); car.ChangeTyre(new Tyre() { Price = 11 }, TyrePlacement.RightBack); car.ChangeTyre(new Tyre() { Price = 12 }, TyrePlacement.RightBack); car.ChangeTyre(new Tyre() { Price = 13 }, TyrePlacement.RightBack); car.ChangeTyre(new Tyre() { Price = 14 }, TyrePlacement.RightBack); car.ChangeTyre(new Tyre() { Price = 15 }, TyrePlacement.RightFront); aggStore.Save(car); var carFromSnapshot = aggStore.Get <Car>(car.Id); carFromSnapshot.ChangeTyre(new Tyre() { Price = 17 }, TyrePlacement.RightBack); carFromSnapshot.ChangeTyre(new Tyre() { Price = 16 }, TyrePlacement.RightFront); aggStore.Save(carFromSnapshot); var carFromSnapshot2 = aggStore.Get <Car>(car.Id); Assert.Equal(8, carFromSnapshot2.Version); Assert.Equal(16, carFromSnapshot2.RightFrontTyre.Price); }
public void CreateNewAggregateIfNoExistingEventStream() { var id = GuidStrategy.NewGuid(); var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object); var aggregate = aggregateStore.Get(typeof(FakeAggregate), id); Assert.Equal(0, aggregate.Version); }
public void RebuildAggregateIfExistingEventStream() { var id = GuidStrategy.NewGuid(); var events = new Event[] { new FakeEvent(), new FakeEvent() }; var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object); eventStore.Setup(mock => mock.GetStream(id, 1)).Returns(new[] { new Commit(1L, DateTime.UtcNow, Guid.NewGuid(), id, 1, HeaderCollection.Empty, new EventCollection(events)) }); var aggregate = aggregateStore.Get(typeof(FakeAggregate), id); Assert.Equal(1, aggregate.Version); }
public void ThrowInvalidOperationExceptionIfAggregateStreamCorrupt() { var id = GuidStrategy.NewGuid(); var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings()); snapshotStore.Setup(mock => mock.GetSnapshot(typeof(FakeAggregate), id, Int32.MaxValue)).Returns(new Snapshot(id, 10, new FakeAggregate(id, 10))); eventStore.Setup(mock => mock.GetStream(id, 11)).Returns(new[] { new Commit(1L, DateTime.UtcNow, Guid.NewGuid(), id, 12, HeaderCollection.Empty, EventCollection.Empty) }); var ex = Assert.Throws <InvalidOperationException>(() => aggregateStore.Get(typeof(FakeAggregate), id)); Assert.Equal(Exceptions.MissingAggregateCommits.FormatWith(11, 12), ex.Message); }
public void UseSnapshotIfAvailable() { var id = GuidStrategy.NewGuid(); var snapshot = new FakeAggregate(id, 10); var events = new Event[] { new FakeEvent(), new FakeEvent() }; var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object); snapshotStore.Setup(mock => mock.GetSnapshot(typeof(FakeAggregate), id, Int32.MaxValue)).Returns(new Snapshot(id, 10, snapshot)); eventStore.Setup(mock => mock.GetStream(id, 11)).Returns(new[] { new Commit(1L, DateTime.UtcNow, Guid.NewGuid(), id, 11, HeaderCollection.Empty, new EventCollection(events)) }); var aggregate = aggregateStore.Get(typeof(FakeAggregate), id); snapshotStore.Verify(mock => mock.GetSnapshot(typeof(FakeAggregate), id, Int32.MaxValue), Times.Once()); Assert.Equal(11, aggregate.Version); }
public void SaveSnapshotIfLoadedCommitsGreaterThanSnapshotInterval() { var id = GuidStrategy.NewGuid(); var commits = new List <Commit>(); var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings()); for (var i = 0; i < 10; i++) { commits.Add(new Commit(1L, DateTime.UtcNow, Guid.NewGuid(), id, 11 + i, HeaderCollection.Empty, EventCollection.Empty)); } snapshotStore.Setup(mock => mock.GetSnapshot(typeof(FakeAggregate), id, Int32.MaxValue)).Returns(new Snapshot(id, 10, new FakeAggregate(id, 10))); eventStore.Setup(mock => mock.GetStream(id, 11)).Returns(commits); var aggregate = aggregateStore.Get(typeof(FakeAggregate), id); snapshotStore.Verify(mock => mock.Save(It.Is <Snapshot>(s => s.Version == 20)), Times.Once()); Assert.Equal(20, aggregate.Version); }
public void SaveSnapshotIfLoadedCommitsGreaterThanSnapshotInterval() { var id = GuidStrategy.NewGuid(); var commits = new List<Commit>(); var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings()); for (var i = 0; i < 10; i++) commits.Add(new Commit(1L, DateTime.UtcNow, Guid.NewGuid(), id, 11 + i, HeaderCollection.Empty, EventCollection.Empty)); snapshotStore.Setup(mock => mock.GetSnapshot(typeof(FakeAggregate), id, Int32.MaxValue)).Returns(new Snapshot(id, 10, new FakeAggregate(id, 10))); eventStore.Setup(mock => mock.GetStream(id, 11)).Returns(commits); var aggregate = aggregateStore.Get(typeof(FakeAggregate), id); snapshotStore.Verify(mock => mock.Save(It.Is<Snapshot>(s => s.Version == 20)), Times.Once()); Assert.Equal(20, aggregate.Version); }
public void ThrowInvalidOperationExceptionIfAggregateStreamCorrupt() { var id = GuidStrategy.NewGuid(); var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings()); snapshotStore.Setup(mock => mock.GetSnapshot(typeof(FakeAggregate), id, Int32.MaxValue)).Returns(new Snapshot(id, 10, new FakeAggregate(id, 10))); eventStore.Setup(mock => mock.GetStream(id, 11)).Returns(new[] { new Commit(1L, DateTime.UtcNow, Guid.NewGuid(), id, 12, HeaderCollection.Empty, EventCollection.Empty) }); var ex = Assert.Throws<InvalidOperationException>(() => aggregateStore.Get(typeof(FakeAggregate), id)); Assert.Equal(Exceptions.MissingAggregateCommits.FormatWith(11, 12), ex.Message); }