public async Task Find_loads_events()
        {
            var user = fixture.Create <FakeUser>();

            user.ChangeUsername(fixture.Create("username"));
            Mock.Get(eventStore)
            .Setup(
                x =>
                x.LoadEvents <FakeUser>(user.Id, 0, CancellationToken.None))
            .ReturnsAsync(user.PendingEvents)
            .Verifiable();

            await sut.Find(user.Id, CancellationToken.None);

            Mock.Get(eventStore).Verify();
        }
        public async Task Find_loads_events()
        {
            // Arrange
            var user = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());

            user.ChangeUsername(username: Guid.NewGuid().ToString());

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(user.Id, 0, default))
            .ReturnsAsync(user.FlushPendingEvents())
            .Verifiable();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                FakeUser.Factory);

            // Act
            await sut.Find(user.Id, default);

            // Assert
            Mock.Get(eventStore).Verify();
        }
        public async Task Find_restores_aggregate_using_memento_if_found()
        {
            // Arrange
            var      user    = new FakeUser(id: Guid.NewGuid(), username: Guid.NewGuid().ToString());
            IMemento memento = user.SaveToMemento();

            user.ChangeUsername(username: Guid.NewGuid().ToString());

            IMementoStore mementoStore = Mock.Of <IMementoStore>();

            Mock.Get(mementoStore)
            .Setup(x => x.Find <FakeUser>(user.Id, default))
            .ReturnsAsync(memento);

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(user.Id, 1, default))
            .ReturnsAsync(user.FlushPendingEvents().Skip(1))
            .Verifiable();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                mementoStore,
                FakeUser.Factory,
                FakeUser.Factory);

            // Act
            FakeUser actual = await sut.Find(user.Id, default);

            // Assert
            Mock.Get(eventStore).Verify();
            actual.ShouldBeEquivalentTo(user);
        }
        public async Task Find_restores_aggregate_from_events()
        {
            // Arrange
            FakeUser user = _fixture.Create <FakeUser>();

            user.ChangeUsername(_fixture.Create("username"));

            Mock.Get(_eventStore)
            .Setup(
                x =>
                x.LoadEvents <FakeUser>(user.Id, 0, CancellationToken.None))
            .ReturnsAsync(user.FlushPendingEvents())
            .Verifiable();

            var sut = new SqlEventSourcedRepository <FakeUser>(
                _eventStore,
                _eventPublisher,
                FakeUser.Factory);

            // Act
            FakeUser actual = await sut.Find(user.Id, CancellationToken.None);

            // Assert
            Mock.Get(_eventStore).Verify();
            actual.ShouldBeEquivalentTo(user);
        }
        public async Task Find_returns_null_if_no_event()
        {
            // Arrange
            var userId = Guid.NewGuid();

            ISqlEventStore eventStore = Mock.Of <ISqlEventStore>();

            Mock.Get(eventStore)
            .Setup(x => x.LoadEvents <FakeUser>(userId, 0, default))
            .ReturnsAsync(new IDomainEvent[0]);

            var sut = new SqlEventSourcedRepository <FakeUser>(
                eventStore,
                Mock.Of <ISqlEventPublisher>(),
                FakeUser.Factory);

            // Act
            FakeUser actual = await sut.Find(userId, default);

            // Assert
            actual.Should().BeNull();
        }