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(); }
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); }
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>(); }
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); }
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); }
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>(); }
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); }
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"); }
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); }
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"); }
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); }
/// <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); }
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); }
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(); }
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()); }
/// <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); }
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()); }
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); }
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>(); }
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); }
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"); }
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); }
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); }
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); }
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"); }
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(); }