Exemplo n.º 1
0
        public async Task Access_metadata_from_all_emitted_events()
        {
            // Arrange

            var snapshotStrategy = CreateSnapshotStrategy();

            var eventsMetadataService = new EventsMetadataService();

            var stores  = new InMemoryStores();
            var session = _sessionFactory(stores, stores.EventStore, stores.SnapshotStore, _eventPublisherMock.Object, snapshotStrategy, eventsMetadataService);

            var stubAggregate = StubSnapshotAggregate.Create("Snap");

            stubAggregate.AddEntity("Child 1");
            stubAggregate.AddEntity("Child 2");

            await session.AddAsync(stubAggregate).ConfigureAwait(false);

            // Act

            await session.SaveChangesAsync().ConfigureAwait(false);

            // Assert

            var eventsWithMetadata = eventsMetadataService.GetEvents().ToList();

            eventsWithMetadata.Count().Should().Be(3);

            eventsWithMetadata[0].Metadata.GetValue(MetadataKeys.EventName).Should().Be("StubCreated");
            eventsWithMetadata[1].Metadata.GetValue(MetadataKeys.EventName).Should().Be(nameof(ChildCreatedEvent));
            eventsWithMetadata[2].Metadata.GetValue(MetadataKeys.EventName).Should().Be(nameof(ChildCreatedEvent));
        }
Exemplo n.º 2
0
        public async Task When_call_SaveChanges_Should_store_the_snapshot()
        {
            // Arrange

            var snapshotStrategy = CreateSnapshotStrategy();

            var stores  = new InMemoryStores();
            var session = _sessionFactory(stores, stores.EventStore, stores.SnapshotStore, _eventPublisherMock.Object, snapshotStrategy, null);

            var stubAggregate = StubSnapshotAggregate.Create("Snap");

            stubAggregate.AddEntity("Child 1");
            stubAggregate.AddEntity("Child 2");

            await session.AddAsync(stubAggregate).ConfigureAwait(false);

            // Act

            await session.SaveChangesAsync().ConfigureAwait(false);

            // Assert

            var committedSnapshot = stores.Snapshots.First(e => e.AggregateId == stubAggregate.Id);

            committedSnapshot.Should().NotBeNull();

            var snapshotClrType = committedSnapshot.Metadata.GetValue(MetadataKeys.SnapshotClrType, value => value.ToString());

            Type.GetType(snapshotClrType).Name.Should().Be(typeof(StubSnapshotAggregateSnapshot).Name);

            var snapshot = (StubSnapshotAggregateSnapshot)committedSnapshot.Data;

            snapshot.Name.Should().Be(stubAggregate.Name);
            snapshot.SimpleEntities.Count.Should().Be(stubAggregate.Entities.Count);
        }
Exemplo n.º 3
0
        public async Task Getting_snapshot_and_forward_events()
        {
            var snapshotStrategy = CreateSnapshotStrategy();

            var stores = new InMemoryStores();

            var session = _sessionFactory(stores, stores.EventStore, stores.SnapshotStore, _eventPublisherMock.Object, snapshotStrategy, null);

            var stubAggregate = StubSnapshotAggregate.Create("Snap");

            await session.AddAsync(stubAggregate).ConfigureAwait(false);

            await session.SaveChangesAsync().ConfigureAwait(false); // Version 1

            stubAggregate.ChangeName("Renamed");
            stubAggregate.ChangeName("Renamed again");

            // dont make snapshot
            snapshotStrategy = CreateSnapshotStrategy(false);

            session = _sessionFactory(stores, stores.EventStore, stores.SnapshotStore, _eventPublisherMock.Object, snapshotStrategy, null);

            await session.AddAsync(stubAggregate).ConfigureAwait(false);

            await session.SaveChangesAsync().ConfigureAwait(false); // Version 3

            var stubAggregateFromSnapshot = await session.GetByIdAsync <StubSnapshotAggregate>(stubAggregate.Id).ConfigureAwait(false);

            stubAggregateFromSnapshot.Version.Should().Be(3);
        }
Exemplo n.º 4
0
        public async Task Should_restore_aggregate_using_snapshot()
        {
            var snapshotStrategy = CreateSnapshotStrategy();

            var eventStore = new StubEventStore();

            var session = _sessionFactory(eventStore, _eventPublisherMock.Object, snapshotStrategy);

            var stubAggregate = StubSnapshotAggregate.Create("Snap");

            stubAggregate.AddEntity("Child 1");
            stubAggregate.AddEntity("Child 2");

            await session.AddAsync(stubAggregate).ConfigureAwait(false);

            await session.SaveChangesAsync().ConfigureAwait(false);

            session = _sessionFactory(eventStore, _eventPublisherMock.Object, snapshotStrategy);

            var aggregate = await session.GetByIdAsync <StubSnapshotAggregate>(stubAggregate.Id).ConfigureAwait(false);

            eventStore.GetSnapshotMethodCalled.Should().BeTrue();

            aggregate.Version.Should().Be(3);
            aggregate.Id.Should().Be(stubAggregate.Id);
        }
Exemplo n.º 5
0
        public When_create_snapshot()
        {
            var stubSnapshotAggregate = StubSnapshotAggregate.Create("Superman");

            stubSnapshotAggregate.ChangeName("Batman");
            stubSnapshotAggregate.AddEntity("entity 1");
            var entityId = stubSnapshotAggregate.AddEntity("entity 2");

            stubSnapshotAggregate.DisableEntity(entityId);

            _snapshot = ((ISnapshotAggregate)stubSnapshotAggregate).CreateSnapshot();
        }
        public When_restore_snapshot()
        {
            var aggregateId = Guid.NewGuid();
            var version     = 1;

            _snapshot = new StubSnapshotAggregateSnapshot
            {
                Name = "Coringa",
            };

            _stubAggregate = new StubSnapshotAggregate();
            ((ISnapshotAggregate)_stubAggregate).Restore(new SnapshotRestore(aggregateId, version, _snapshot, MetadataCollection.Empty));
        }
Exemplo n.º 7
0
        public async Task When_call_SaveChanges_Should_store_the_snapshot()
        {
            // Arrange

            var snapshotStrategy = CreateSnapshotStrategy();

            var eventStore = new StubEventStore();
            var session    = _sessionFactory(eventStore, _eventPublisherMock.Object, snapshotStrategy);

            var stubAggregate = StubSnapshotAggregate.Create("Snap");

            stubAggregate.AddEntity("Child 1");
            stubAggregate.AddEntity("Child 2");

            await session.AddAsync(stubAggregate).ConfigureAwait(false);


            // Act

            await session.SaveChangesAsync().ConfigureAwait(false);

            // Assert

            eventStore.SaveSnapshotMethodCalled.Should().BeTrue();

            var commitedSnapshot = StubEventStore.Snapshots.First(e => e.AggregateId == stubAggregate.Id);

            commitedSnapshot.Should().NotBeNull();

            var metadata = (IMetadata)_textSerializer.Deserialize <EventSource.Metadata>(commitedSnapshot.SerializedMetadata);

            var snapshotClrType = metadata.GetValue(MetadataKeys.SnapshotClrType);

            Type.GetType(snapshotClrType).Name.Should().Be(typeof(StubSnapshotAggregateSnapshot).Name);

            var snapshot = _textSerializer.Deserialize <StubSnapshotAggregateSnapshot>(commitedSnapshot.SerializedData);

            snapshot.Name.Should().Be(stubAggregate.Name);
            snapshot.SimpleEntities.Count.Should().Be(stubAggregate.Entities.Count);
        }
Exemplo n.º 8
0
        public async Task When_not_exists_snapshot_yet_Then_aggregate_should_be_constructed_using_your_events()
        {
            var snapshotStrategy = CreateSnapshotStrategy(false);

            var stores = new InMemoryStores();

            var session = _sessionFactory(stores, stores.EventStore, stores.SnapshotStore, _eventPublisherMock.Object, snapshotStrategy, null);

            var stubAggregate = StubSnapshotAggregate.Create("Snap");

            stubAggregate.AddEntity("Child 1");
            stubAggregate.AddEntity("Child 2");

            await session.AddAsync(stubAggregate).ConfigureAwait(false);

            await session.SaveChangesAsync().ConfigureAwait(false);

            session = _sessionFactory(stores, stores.EventStore, stores.SnapshotStore, _eventPublisherMock.Object, snapshotStrategy, null);

            var aggregate = await session.GetByIdAsync <StubSnapshotAggregate>(stubAggregate.Id).ConfigureAwait(false);

            aggregate.Name.Should().Be(stubAggregate.Name);
            aggregate.Entities.Count.Should().Be(stubAggregate.Entities.Count);
        }