mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
8.5 KiB
242 lines
8.5 KiB
// ==========================================================================
|
|
// AggregateHandlerTests.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using FakeItEasy;
|
|
using Squidex.Infrastructure.Commands.TestHelpers;
|
|
using Squidex.Infrastructure.EventSourcing;
|
|
using Squidex.Infrastructure.Log;
|
|
using Squidex.Infrastructure.States;
|
|
using Squidex.Infrastructure.Tasks;
|
|
using Xunit;
|
|
|
|
namespace Squidex.Infrastructure.Commands
|
|
{
|
|
public class AggregateHandlerTests
|
|
{
|
|
private readonly ISemanticLog log = A.Fake<ISemanticLog>();
|
|
private readonly IServiceProvider serviceProvider = A.Fake<IServiceProvider>();
|
|
private readonly IStore store = A.Fake<IStore>();
|
|
private readonly IStateFactory stateFactory = A.Fake<IStateFactory>();
|
|
private readonly IPersistence<object> persistence = A.Fake<IPersistence<object>>();
|
|
private readonly Envelope<IEvent> event1 = new Envelope<IEvent>(new MyEvent());
|
|
private readonly Envelope<IEvent> event2 = new Envelope<IEvent>(new MyEvent());
|
|
private readonly CommandContext context;
|
|
private readonly Guid domainObjectId = Guid.NewGuid();
|
|
private readonly MyDomainObject domainObject = new MyDomainObject();
|
|
private readonly AggregateHandler sut;
|
|
|
|
public sealed class MyEvent : IEvent
|
|
{
|
|
}
|
|
|
|
public AggregateHandlerTests()
|
|
{
|
|
context = new CommandContext(new MyCommand { AggregateId = domainObjectId });
|
|
|
|
A.CallTo(() => store.WithSnapshots<MyDomainObject, object>(domainObjectId.ToString(), A<Func<object, Task>>.Ignored))
|
|
.Returns(persistence);
|
|
|
|
A.CallTo(() => stateFactory.CreateAsync<MyDomainObject>(domainObjectId.ToString()))
|
|
.Returns(Task.FromResult(domainObject));
|
|
|
|
A.CallTo(() => stateFactory.GetSingleAsync<MyDomainObject>(domainObjectId.ToString()))
|
|
.Returns(Task.FromResult(domainObject));
|
|
|
|
sut = new AggregateHandler(stateFactory, serviceProvider, log);
|
|
|
|
domainObject.ActivateAsync(domainObjectId.ToString(), store).Wait();
|
|
}
|
|
|
|
[Fact]
|
|
public Task Create_with_task_should_throw_exception_if_not_aggregate_command()
|
|
{
|
|
return Assert.ThrowsAnyAsync<ArgumentException>(() => sut.CreateAsync<MyDomainObject>(new CommandContext(A.Dummy<ICommand>()), x => TaskHelper.False));
|
|
}
|
|
|
|
[Fact]
|
|
public Task Create_synced_with_task_should_throw_exception_if_not_aggregate_command()
|
|
{
|
|
return Assert.ThrowsAnyAsync<ArgumentException>(() => sut.CreateSyncedAsync<MyDomainObject>(new CommandContext(A.Dummy<ICommand>()), x => TaskHelper.False));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_with_task_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.CreateAsync<MyDomainObject>(context, async x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
await Task.Yield();
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntityCreatedResult<Guid>>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_synced_with_task_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.CreateSyncedAsync<MyDomainObject>(context, async x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
await Task.Yield();
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntityCreatedResult<Guid>>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.CreateAsync<MyDomainObject>(context, x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntityCreatedResult<Guid>>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Create_synced_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.CreateSyncedAsync<MyDomainObject>(context, x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntityCreatedResult<Guid>>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public Task Update_with_task_should_throw_exception_if_not_aggregate_command()
|
|
{
|
|
return Assert.ThrowsAnyAsync<ArgumentException>(() => sut.UpdateAsync<MyDomainObject>(new CommandContext(A.Dummy<ICommand>()), x => TaskHelper.False));
|
|
}
|
|
|
|
[Fact]
|
|
public Task Update_synced_with_task_should_throw_exception_if_not_aggregate_command()
|
|
{
|
|
return Assert.ThrowsAnyAsync<ArgumentException>(() => sut.UpdateSyncedAsync<MyDomainObject>(new CommandContext(A.Dummy<ICommand>()), x => TaskHelper.False));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_with_task_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.UpdateAsync<MyDomainObject>(context, async x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
await Task.Yield();
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntitySavedResult>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_synced_with_task_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.UpdateSyncedAsync<MyDomainObject>(context, async x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
await Task.Yield();
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntitySavedResult>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.UpdateAsync<MyDomainObject>(context, x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntitySavedResult>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_synced_should_create_domain_object_and_save()
|
|
{
|
|
MyDomainObject passedDomainObject = null;
|
|
|
|
await sut.UpdateSyncedAsync<MyDomainObject>(context, x =>
|
|
{
|
|
x.RaiseEvent(new MyEvent());
|
|
|
|
passedDomainObject = x;
|
|
});
|
|
|
|
Assert.Equal(domainObject, passedDomainObject);
|
|
Assert.NotNull(context.Result<EntitySavedResult>());
|
|
|
|
A.CallTo(() => persistence.WriteEventsAsync(A<IEnumerable<Envelope<IEvent>>>.Ignored))
|
|
.MustHaveHappened();
|
|
}
|
|
}
|
|
}
|
|
|