public void Find_should_return_hydrated_entity_when_it_has_snapsot_and_no_later_events()
        {
            var entityId = Guid.NewGuid();

            var snapshot = new MementoOriginator.Memento
            {
                SourceId = entityId,
                Version  = 3,
                Value    = 30
            };

            var eventStoreMock = new Mock <IEventStore>();

            eventStoreMock.Setup(eventStore => eventStore.Load(entityId, snapshot.Version + 1)).Returns(Enumerable.Empty <Commit>);

            var snapshotStoreMock = new Mock <ISnapshotStore>();

            snapshotStoreMock.Setup(snapshotStore => snapshotStore.Get(entityId, int.MaxValue)).Returns(() => snapshot);

            var repository = new EventSourcedRepository <MementoOriginator>(eventStoreMock.Object, snapshotStoreMock.Object);

            var entity = repository.Find(entityId);

            entity.Id.Should().Be(entityId);
            entity.Version.Should().Be(snapshot.Version);
            entity.Value.Should().Be(snapshot.Value);
        }
        public void Find_should_return_null_when_there_are_no_evetns_with_specified_source_id()
        {
            var eventStoreMock = new Mock <IEventStore>();

            eventStoreMock.Setup(eventStore => eventStore.Load(It.IsAny <Guid>(), 0)).Returns(Enumerable.Empty <Commit>);

            var repository = new EventSourcedRepository <CorrectEventSourced>(eventStoreMock.Object);

            repository.Find(Guid.NewGuid()).Should().BeNull();
        }
        public void Find_should_return_hydrated_entity_when_it_has_snapsot_and_events()
        {
            var entityId = Guid.NewGuid();

            var snapshot = new MementoOriginator.Memento
            {
                SourceId = entityId,
                Version  = 3,
                Value    = 30
            };

            var events = new IEvent[]
            {
                new CorrectEventSourced.Updated {
                    SourceId = entityId, SourceVersion = snapshot.Version + 1, Value = 40
                },
                new CorrectEventSourced.Updated {
                    SourceId = entityId, SourceVersion = snapshot.Version + 2, Value = 50
                }
            };

            var commit = new Commit
            {
                Id       = Guid.NewGuid(),
                SourceId = entityId,
                Changes  = events
            };

            var snapshotStoreMock = new Mock <ISnapshotStore>();

            snapshotStoreMock.Setup(snapshotStore => snapshotStore.Get(entityId, int.MaxValue)).Returns(() => snapshot);

            var eventStoreMock = new Mock <IEventStore>();

            eventStoreMock.Setup(eventStore => eventStore.Load(entityId, snapshot.Version + 1)).Returns(new[] { commit });

            var repository = new EventSourcedRepository <MementoOriginator>(eventStoreMock.Object, snapshotStoreMock.Object);

            var entity = repository.Find(entityId);

            entity.Id.Should().Be(entityId);
            entity.Version.Should().Be(events.Last().SourceVersion);
            entity.Value.Should().Be(events.OfType <CorrectEventSourced.Updated>().Last().Value);
        }
        public void Find_should_return_hydrated_entity_when_it_has_events()
        {
            var entityId = Guid.NewGuid();

            var events = new IEvent[]
            {
                new CorrectEventSourced.Created {
                    SourceId = entityId, SourceVersion = 1, Value = 1
                },
                new CorrectEventSourced.Updated {
                    SourceId = entityId, SourceVersion = 2, Value = 20
                },
                new CorrectEventSourced.Updated {
                    SourceId = entityId, SourceVersion = 3, Value = 30
                }
            };

            var commit = new Commit
            {
                Id       = Guid.NewGuid(),
                SourceId = entityId,
                Changes  = events
            };

            var eventStoreMock = new Mock <IEventStore>();

            eventStoreMock.Setup(eventStore => eventStore.Load(entityId, 0)).Returns(new [] { commit });

            var repository = new EventSourcedRepository <CorrectEventSourced>(eventStoreMock.Object);

            var entity = repository.Find(entityId);

            entity.Id.Should().Be(entityId);
            entity.Version.Should().Be(events.Last().SourceVersion);
            entity.Value.Should().Be(events.Cast <dynamic>().Last().Value);
        }