Exemplo n.º 1
0
        public async Task Can_get_by_id()
        {
            // Arrange
            const int fakeAggregateRootId = 1;

            Preconditions.EnsureRandomFake();
            FakeAggregateRoot expected = await DbContext
                                         .FakeAggregateRoots
                                         .FindAsync(fakeAggregateRootId);

            // Act
            FakeAggregateRoot actual;

            using (RepositoryScope scope = CreateScope())
            {
                actual = await scope
                         .Repository
                         .GetByIdAsync(
                    fakeAggregateRootId,
                    _cancellationToken);
            }

            // Assert
            actual.ShouldNotBeNull();
            _fakeAggregateRootEqualityComparer.Equals(actual, expected).ShouldBeTrue();
        }
Exemplo n.º 2
0
        public void Equals_SameClassesDifferentId_ReturnsFalse()
        {
            var fake1 = new FakeAggregateRoot(new FakeDomainId(Guid.NewGuid()));
            var fake2 = new FakeAggregateRoot(new FakeDomainId(Guid.NewGuid()));

            Assert.NotEqual(fake1, fake2);
        }
Exemplo n.º 3
0
        public async Task Can_update()
        {
            // Arrange
            const string updatedName = "updatedName";

            Preconditions.EnsureRandomFake();
            FakeAggregateRoot existed = await DbContext
                                        .FakeAggregateRoots
                                        .FirstOrDefaultAsync();

            // Act
            using (RepositoryScope scope = CreateScope())
            {
                existed.ChangeName(updatedName);
                await scope
                .Repository
                .UpdateAsync(existed, _cancellationToken);
            }

            // Assert
            FakeAggregateRoot expected = await DbContext
                                         .FakeAggregateRoots
                                         .SingleAsync(f => f.Id == existed.Id, _cancellationToken);

            expected.Name.ShouldBe(updatedName);
        }
        public async Task SaveAsync_NoEvents_Throws()
        {
            var id = Unified.NewCode();
            var executionContext = new AggregateExecutionContext(Fixtures.Pipelines.FakeCreateCommand());
            var aggregateRoot    = new FakeAggregateRoot(id, executionContext);

            await store.Awaiting(s => s.SaveAsync(aggregateRoot)).Should().ThrowAsync <InvalidOperationException>();
        }
Exemplo n.º 5
0
        public void Equals_DifferentClassesSameIds_ReturnsFalse()
        {
            var id    = new FakeDomainId(Guid.NewGuid());
            var fake1 = new FakeAggregateRoot(id);
            var fake2 = new DifferentFakeAggregateRoot(id);

            Assert.False(fake1 == fake2);
        }
Exemplo n.º 6
0
        public void Equals_SameClassesSameIds_ReturnsTrue()
        {
            var id    = new FakeDomainId(Guid.NewGuid());
            var fake1 = new FakeAggregateRoot(id);
            var fake2 = new FakeAggregateRoot(id);

            Assert.Equal(fake1, fake2);
        }
Exemplo n.º 7
0
        public void FakeAggregate_AfterCreating_ShouldHaveOneUncommitedEvent()
        {
            var aggregateId = Guid.NewGuid();

            var fakeAggregate = new FakeAggregateRoot(aggregateId);

            fakeAggregate.GetUncommitedEvents().Should().NotBeEmpty().And.HaveCount(1);
            fakeAggregate.GetUncommitedEvents().Single().Should().BeAssignableTo <FakeAggregateCreatedEvent>();
        }
Exemplo n.º 8
0
            public Task Handle(FakeCommand command, CancellationToken cancellationToken)
            {
                var unitOfWork    = _context.GetUnitOfWork <string, object>();
                var aggregateRoot = new FakeAggregateRoot();

                unitOfWork.Attach(new AggregateRootEntity <string, object>("some_id", aggregateRoot, 5));
                _action.Invoke(aggregateRoot);
                return(Task.CompletedTask);
            }
Exemplo n.º 9
0
        public void Apply_PassNullAsEvent_ShouldThrowException()
        {
            // Arrange
            FakeAggregateRoot aggregateRoot = Mock.Of <FakeAggregateRoot>();

            // Act & Assert
            Action action = () => aggregateRoot.ApplyNull();

            action.Should().Throw <ArgumentNullException>()
            .Which.ParamName.Should().Be("event");
        }
Exemplo n.º 10
0
        private FakeAggregateRoot SetupAggregateRootWithEvents()
        {
            var id = Unified.NewCode();
            var executionContext = new AggregateExecutionContext(Fixtures.Pipelines.FakeCreateCommand());
            var aggregateRoot    = new FakeAggregateRoot(id, executionContext);

            aggregateRoot.Create(FixtureUtils.String());
            aggregateRoot.Update(FixtureUtils.String());

            return(aggregateRoot);
        }
Exemplo n.º 11
0
        public void Apply_UnhandledEvent_ShouldThrowException()
        {
            // Arrange
            FakeAggregateRoot aggregateRoot = Mock.Of <FakeAggregateRoot>();

            // Act & Assert
            Action action = () => aggregateRoot.ApplyC();

            action.Should().Throw <UnhandledEventException>()
            .WithMessage("Unhandled event EventC");
        }
Exemplo n.º 12
0
        public void FakeAggregate_AfterAppliedFakeMethod_ShouldIncreaseVersion()
        {
            var aggregateId         = Guid.NewGuid();
            var fakeAggregate       = new FakeAggregateRoot(aggregateId);
            var versionBeforeChange = fakeAggregate.Version;

            fakeAggregate.FakeMethod();

            Assert.Equal(versionBeforeChange, fakeAggregate.Version - 1);
            fakeAggregate.Version.Should().Be(2);
            fakeAggregate.GetUncommitedEvents().Should().NotBeEmpty().And.HaveCount(2);
        }
        /// <summary>
        /// Ensures that is fake aggregate root
        /// </summary>
        protected FakeAggregateRoot EnsureHasFakeAggregateRoot()
        {
            var fakeAggregateRoot = new FakeAggregateRoot(GenerateRandomName());

            FakeAggregateRoot entry = _dbContext
                                      .Set <FakeAggregateRoot>()
                                      .Add(fakeAggregateRoot);

            _dbContext.SaveChanges();

            return(entry);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Ensures that is fake aggregate root
        /// </summary>
        public Preconditions EnsureRandomFake()
        {
            var fakeAggregateRoot = new FakeAggregateRoot(GenerateRandomName());

            _dbContext
            .Set <FakeAggregateRoot>()
            .Add(fakeAggregateRoot);

            _dbContext.SaveChanges();

            return(this);
        }
Exemplo n.º 15
0
        public async Task Can_add_and_commit_conflict()
        {
            // Arrange
            FakeAggregateRoot added = base.EnsureHasFakeAggregateRoot();
            var newFake             = new FakeAggregateRoot("newFake");

            // Act
            int actual = await _repository.AddAsync(newFake, _cancellationToken);

            // Assert
            actual.ShouldNotBe(added.Id);
        }
Exemplo n.º 16
0
        public async Task Can_get_by_id()
        {
            // Arrange
            const int         fakeAggregateRootId = 1;
            FakeAggregateRoot expected            = base.EnsureHasFakeAggregateRoot();

            // Act
            FakeAggregateRoot actual = await _repository.GetByIdAsync(
                fakeAggregateRootId,
                _cancellationToken);

            // Assert
            actual.ShouldNotBeNull();
            _fakeAggregateRootEqualityComparer.Equals(actual, expected).ShouldBeTrue();
        }
Exemplo n.º 17
0
        public async Task Can_get_by_id_not_found()
        {
            // Arrange
            const int notFoundId = 11;

            base.EnsureHasFakeAggregateRoot();

            // Act
            FakeAggregateRoot actual = await _repository.GetByIdAsync(
                notFoundId,
                _cancellationToken);

            // Assert
            actual.ShouldBeNull();
        }
        public async Task SaveAsync_HasEvents_Stores()
        {
            var id = Unified.NewCode();
            var executionContext = new AggregateExecutionContext(Fixtures.Pipelines.FakeCreateCommand());
            var aggregateRoot    = new FakeAggregateRoot(id, executionContext);

            aggregateRoot.Create(FixtureUtils.String());
            aggregateRoot.Update(FixtureUtils.String());

            await store.SaveAsync(aggregateRoot);

            var events = await store.GetAsync(aggregateRoot.Id, 0);

            events.Should().BeEquivalentTo(aggregateRoot.Events, options => options.ForMessage());
        }
Exemplo n.º 19
0
        /// <summary>
        /// Ensures that there is fake aggregate roots with the specified count
        /// </summary>
        /// <param name="count">Count of fake aggregate roots</param>
        public Preconditions EnsureFakes(int count)
        {
            for (int i = 0; i < count; i++)
            {
                var fakeAggregateRoot = new FakeAggregateRoot(GenerateRandomName());

                _dbContext
                .Set <FakeAggregateRoot>()
                .Add(fakeAggregateRoot);
            }

            _dbContext.SaveChanges();

            return(this);
        }
Exemplo n.º 20
0
        public async Task Cannot_Commit_Entity_With_Same_Revision()
        {
            var fakeId       = Guid.NewGuid();
            var fakeDomainId = new FakeDomainId(fakeId);

            var fakeAggregate = FakeAggregateRoot.CreateAggregateRoot(fakeDomainId);
            await repository.Save(fakeAggregate, Guid.NewGuid());

            var aggregateFromRepository = await repository.GetById <FakeAggregateRoot>(fakeDomainId);

            var exception = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                    repository.Save(aggregateFromRepository, Guid.NewGuid()));

            Assert.Equal(
                "Aggregate has a wrong Version. Expected: 2 - Current: 1",
                exception.Message);
        }
        /// <summary>
        /// Ensures that there is fake aggregate roots with the specified count
        /// </summary>
        /// <param name="count">Count of fake aggregate roots</param>
        protected IReadOnlyCollection <FakeAggregateRoot> EnsureHasFakeAggregateRoots(int count)
        {
            for (int i = 0; i < count; i++)
            {
                var fakeAggregateRoot = new FakeAggregateRoot(GenerateRandomName());

                _dbContext
                .Set <FakeAggregateRoot>()
                .Add(fakeAggregateRoot);
            }

            _dbContext.SaveChanges();

            return(_dbContext
                   .Set <FakeAggregateRoot>()
                   .ToList());
        }
Exemplo n.º 22
0
        public async Task Can_Save_Aggregate_Within_Repository()
        {
            var fakeId       = Guid.NewGuid();
            var fakeDomainId = new FakeDomainId(fakeId);

            var fakeAggregate = FakeAggregateRoot.CreateAggregateRoot(fakeDomainId);
            await repository.Save(fakeAggregate, Guid.NewGuid());

            var aggregateFromRepository = await repository.GetById <FakeAggregateRoot>(fakeDomainId);

            aggregateFromRepository.DoSomething();
            await repository.Save(fakeAggregate, Guid.NewGuid());

            aggregateFromRepository = await repository.GetById <FakeAggregateRoot>(fakeDomainId);

            Assert.Equal(fakeAggregate, aggregateFromRepository);
        }
Exemplo n.º 23
0
        public void Apply_PassKnownEvents_ShouldHaveChanges()
        {
            // Arrange
            FakeAggregateRoot aggregateRoot = Mock.Of <FakeAggregateRoot>();

            // Act
            aggregateRoot.ApplyBA();

            // Assert
            var changeTracker = (IAggregateRootChangeTracker <object>)aggregateRoot;

            changeTracker.HasChanges.Should().BeTrue();
            object[] changes = changeTracker.GetChanges().ToArray();
            changes.Should().HaveCount(2);
            changes[0].Should().BeOfType <EventB>();
            changes[1].Should().BeOfType <EventA>();
        }
Exemplo n.º 24
0
        public async Task Add_NewAggregateRoot_ShouldAttachToUnitOfWork()
        {
            // Arrange
            var commandHandlingContext = new CommandHandlingContext();
            var unitOfWork             = commandHandlingContext.CreateUnitOfWork <string, object>();
            var identifier             = Guid.NewGuid().ToString("N");
            var aggregateRoot          = new FakeAggregateRoot();
            var repository             = new Repository <FakeAggregateRoot>(NewEventStoreMock.Object, commandHandlingContext);

            // Act
            await repository.Add(identifier, aggregateRoot);

            // Assert
            unitOfWork.TryGet(identifier, out var aggregateRootEntityFromUnitOfWork).Should().BeTrue();
            aggregateRootEntityFromUnitOfWork.Identifier.Should().Be(identifier);
            aggregateRootEntityFromUnitOfWork.AggregateRoot.Should().Be(aggregateRoot);
        }
Exemplo n.º 25
0
        public void Add_AggregateRootAlreadyKnownByUnitOfWork_ShouldThrowException()
        {
            // Arrange
            var commandHandlingContext = new CommandHandlingContext();
            var unitOfWork             = commandHandlingContext.CreateUnitOfWork <string, object>();
            var identifier             = Guid.NewGuid().ToString("N");
            var aggregateRoot          = new FakeAggregateRoot();

            unitOfWork.Attach(new AggregateRootEntity <string, object>(identifier, new FakeAggregateRoot(), 1));
            var repository = new Repository <FakeAggregateRoot>(NewEventStoreMock.Object, commandHandlingContext);

            // Act / Assert
            Func <Task> action = () => repository.Add(identifier, aggregateRoot);

            action.Should().Throw <AggregateRootAlreadyExistsException <string> >()
            .WithMessage($"Exception for aggregate root with identifier '{identifier}': Aggregate root already attached");
        }
Exemplo n.º 26
0
        public async Task Can_update_and_commit()
        {
            // Arrange
            FakeAggregateRoot existed     = base.EnsureHasFakeAggregateRoot();
            const string      updatedName = "updatedName";

            // Act
            existed.ChangeName(updatedName);

            await _repository.UpdateAsync(existed, _cancellationToken);

            // Assert
            FakeAggregateRoot expected = await _dbContext
                                         .Set <FakeAggregateRoot>()
                                         .SingleAsync(f => f.Id == existed.Id, _cancellationToken);

            expected.Name.ShouldBe(updatedName);
        }
Exemplo n.º 27
0
        public async Task Get_AggregateRootAlreadyAttachedToUnitOfWork_ShouldReturnAggregateRootFromUnitOfWork()
        {
            // Arrange
            var commandHandlingContext = new CommandHandlingContext();
            var unitOfWork = commandHandlingContext.CreateUnitOfWork <string, object>(); var identifier = Guid.NewGuid().ToString("N");
            var aggregateRoot = new FakeAggregateRoot();

            unitOfWork.Attach(new AggregateRootEntity <string, object>(identifier, aggregateRoot, 1));
            var eventStoreMock = NewEventStoreMock;
            var repository     = new Repository <FakeAggregateRoot>(eventStoreMock.Object, commandHandlingContext);

            // Act
            var aggregateRootFromRepository = await repository.Get(identifier);

            // Assert
            aggregateRootFromRepository.Should().Be(aggregateRoot);
            eventStoreMock.Verify(x => x.GetEvents(identifier, 1), Times.Never);
        }
Exemplo n.º 28
0
        public async Task Can_add_and_commit()
        {
            // Arrange
            const string addedName = "expectedName";
            var          added     = new FakeAggregateRoot(addedName);

            // Act
            await _repository.AddAsync(added, _cancellationToken);

            await _dbContext.SaveChangesAsync();

            // Assert
            FakeAggregateRoot expected = await _dbContext
                                         .Set <FakeAggregateRoot>()
                                         .FirstAsync(f => f.Name == added.Name, _cancellationToken);

            expected.ShouldNotBeNull();
            expected.Name.ShouldBe(addedName);
        }
Exemplo n.º 29
0
        public void Add_AggregateRootAlreadyKnownByEventStore_ShouldThrowException()
        {
            // Arrange
            var commandHandlingContext = new CommandHandlingContext();

            commandHandlingContext.CreateUnitOfWork <string, object>();
            var identifier     = Guid.NewGuid().ToString("N");
            var aggregateRoot  = new FakeAggregateRoot();
            var eventStoreMock = NewEventStoreMock;

            eventStoreMock.Setup(x => x.Contains(identifier)).ReturnsAsync(true);
            var repository = new Repository <FakeAggregateRoot>(eventStoreMock.Object, commandHandlingContext);

            // Act / Assert
            Func <Task> action = () => repository.Add(identifier, aggregateRoot);

            action.Should().Throw <AggregateRootAlreadyExistsException <string> >()
            .WithMessage($"Exception for aggregate root with identifier '{identifier}': Aggregate root already attached");
        }
Exemplo n.º 30
0
        public async Task Contains_NewAggregateRootAttachedToUnitOfWork_ShouldReturnTrue()
        {
            // Arrange
            var commandHandlingContext = new CommandHandlingContext();

            commandHandlingContext.CreateUnitOfWork <string, object>();
            var newIdentifier = Guid.NewGuid().ToString("N");
            var aggregateRoot = new FakeAggregateRoot();
            var unitOfWork    = commandHandlingContext.GetUnitOfWork <string, object>();

            unitOfWork.Attach(new AggregateRootEntity <string, object>(newIdentifier, aggregateRoot, 1));
            var repository = new Repository <FakeAggregateRoot>(NewEventStoreMock.Object, commandHandlingContext);

            // Act
            var result = await repository.Contains(newIdentifier);

            // Assert
            result.Should().BeTrue();
        }