solution files
All checks were successful
.NET Test / test (push) Successful in 6m47s

This commit is contained in:
2025-02-01 20:34:34 +04:00
parent 14937c6964
commit 54ea0925dd
30 changed files with 1388 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddBehaviour
{
public class TestCommand {}
public class TestCommandResult {}
[ExcludeFromCodeCoverage]
public class NonGenericTestOpenBehaviour : IDispatchBehaviour<TestCommand, TestCommandResult>
{
public ValueTask<TestCommandResult> Handle(TestCommand request, DispatchFurtherDelegate<TestCommandResult> next, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
[Theory]
[InlineData(ServiceLifetime.Transient)]
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Singleton)]
public void WhenCalled_ShouldRegisterDispatchBehaviour(ServiceLifetime lifetime)
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddBehaviour<NonGenericTestOpenBehaviour>(lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IDispatchBehaviour<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(NonGenericTestOpenBehaviour)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[ExcludeFromCodeCoverage]
public class InvalidTestBehaviour : IDispatchBehaviour
{
public Type RequestType => throw new NotImplementedException();
public Type ResponseType => throw new NotImplementedException();
}
[Fact]
public void WhenCalledWithInvalidType_ShouldThrow()
{
// Given
ServiceCollection services = new();
// When
// Then
Should.Throw<InvalidOperationException>(() => services.AddCqrs(opt => opt
.AddBehaviour<InvalidTestBehaviour>())
);
}
}

View File

@@ -0,0 +1,124 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddCommandHandler
{
public class TestCommand {}
public class TestCommandResult {}
[ExcludeFromCodeCoverage]
public class TestCommandHandler : ICommandHandler<TestCommand, TestCommandResult>
{
public ValueTask<TestCommandResult> Handle(TestCommand command, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
[Theory]
[InlineData(ServiceLifetime.Transient)]
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Singleton)]
public void WhenCalled_ShouldRegisterCommandHandler(ServiceLifetime lifetime)
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt.AddCommandHandler<TestCommandHandler>(lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(TestCommandHandler)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterCommandHandlerOnce()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddCommandHandler<TestCommandHandler>()
.AddCommandHandler<TestCommandHandler>()
.AddCommandHandler<TestCommandHandler>());
services.AddCqrs(opt => opt
.AddCommandHandler<TestCommandHandler>());
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(TestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
public class SecondTestCommand {}
public class SecondTestCommandResult {}
[ExcludeFromCodeCoverage]
public class SecondTestCommandHandler : ICommandHandler<SecondTestCommand, SecondTestCommandResult>
{
public ValueTask<SecondTestCommandResult> Handle(SecondTestCommand command, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
public class ThirdTestCommand {}
public class ThirdTestCommandResult {}
[ExcludeFromCodeCoverage]
public class ThirdTestCommandHandler : ICommandHandler<ThirdTestCommand, ThirdTestCommandResult>
{
public ValueTask<ThirdTestCommandResult> Handle(ThirdTestCommand command, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterAllCommandHandlers()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddCommandHandler<TestCommandHandler>()
.AddCommandHandler<SecondTestCommandHandler>());
services.AddCqrs(opt => opt
.AddCommandHandler<ThirdTestCommandHandler>());
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(TestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<SecondTestCommand, SecondTestCommandResult>)
&& descriptor.ImplementationType == typeof(SecondTestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<ThirdTestCommand, ThirdTestCommandResult>)
&& descriptor.ImplementationType == typeof(ThirdTestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
}

View File

@@ -0,0 +1,89 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddOpenBehaviour
{
[ExcludeFromCodeCoverage]
public class TestOpenBehaviour<TRequest, TResponse> : IDispatchBehaviour<TRequest, TResponse>
where TRequest: notnull
{
public ValueTask<TResponse> Handle(TRequest request, DispatchFurtherDelegate<TResponse> next, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
[Theory]
[InlineData(ServiceLifetime.Transient)]
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Singleton)]
public void WhenCalled_ShouldRegisterOpenDispatchBehaviour(ServiceLifetime lifetime)
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddOpenBehaviour(typeof(TestOpenBehaviour<,>), lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IDispatchBehaviour<,>)
&& descriptor.ImplementationType == typeof(TestOpenBehaviour<,>)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[ExcludeFromCodeCoverage]
public class InvalidOpenBehaviour : IDispatchBehaviour
{
public Type RequestType => throw new NotImplementedException();
public Type ResponseType => throw new NotImplementedException();
}
[Fact]
public void WhenCalledWithInvalidType_ShouldThrow()
{
// Given
ServiceCollection services = new();
// When
var invalidOpenDispatchBehaviourType = typeof(InvalidOpenBehaviour);
// Then
Should.Throw<ArgumentException>(() => services.AddCqrs(opt => opt
.AddOpenBehaviour(invalidOpenDispatchBehaviourType))
);
}
public class TestCommand {}
public class TestCommandResult {}
[ExcludeFromCodeCoverage]
public class NonGenericTestOpenBehaviour : IDispatchBehaviour<TestCommand, TestCommandResult>
{
public ValueTask<TestCommandResult> Handle(TestCommand request, DispatchFurtherDelegate<TestCommandResult> next, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
[Fact]
public void WhenCalledWithNonGenericType_ShouldThrow()
{
// Given
ServiceCollection services = new();
// When
var nonGenericOpenDispatchBehaviourType = typeof(NonGenericTestOpenBehaviour);
// Then
Should.Throw<ArgumentException>(() => services.AddCqrs(opt => opt
.AddOpenBehaviour(nonGenericOpenDispatchBehaviourType))
);
}
}

View File

@@ -0,0 +1,124 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddQueryHandler
{
public class TestQuery {}
public class TestQueryResult {}
[ExcludeFromCodeCoverage]
public class TestQueryHandler : IQueryHandler<TestQuery, TestQueryResult>
{
public ValueTask<TestQueryResult> Handle(TestQuery Query, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
[Theory]
[InlineData(ServiceLifetime.Transient)]
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Singleton)]
public void WhenCalled_ShouldRegisterQueryHandler(ServiceLifetime lifetime)
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt.AddQueryHandler<TestQueryHandler>(lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryHandler<TestQuery, TestQueryResult>)
&& descriptor.ImplementationType == typeof(TestQueryHandler)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterQueryHandlerOnce()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddQueryHandler<TestQueryHandler>()
.AddQueryHandler<TestQueryHandler>()
.AddQueryHandler<TestQueryHandler>());
services.AddCqrs(opt => opt
.AddQueryHandler<TestQueryHandler>());
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryHandler<TestQuery, TestQueryResult>)
&& descriptor.ImplementationType == typeof(TestQueryHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
public class SecondTestQuery {}
public class SecondTestQueryResult {}
[ExcludeFromCodeCoverage]
public class SecondTestQueryHandler : IQueryHandler<SecondTestQuery, SecondTestQueryResult>
{
public ValueTask<SecondTestQueryResult> Handle(SecondTestQuery Query, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
public class ThirdTestQuery {}
public class ThirdTestQueryResult {}
[ExcludeFromCodeCoverage]
public class ThirdTestQueryHandler : IQueryHandler<ThirdTestQuery, ThirdTestQueryResult>
{
public ValueTask<ThirdTestQueryResult> Handle(ThirdTestQuery Query, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterAllQueryHandlers()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddQueryHandler<TestQueryHandler>()
.AddQueryHandler<SecondTestQueryHandler>());
services.AddCqrs(opt => opt
.AddQueryHandler<ThirdTestQueryHandler>());
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryHandler<TestQuery, TestQueryResult>)
&& descriptor.ImplementationType == typeof(TestQueryHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryHandler<SecondTestQuery, SecondTestQueryResult>)
&& descriptor.ImplementationType == typeof(SecondTestQueryHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryHandler<ThirdTestQuery, ThirdTestQueryResult>)
&& descriptor.ImplementationType == typeof(ThirdTestQueryHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
}

View File

@@ -0,0 +1,63 @@
using Microsoft.Extensions.DependencyInjection;
namespace Cqrs.Tests.CqrsServicesExtensionsTests;
public class AddCqrs
{
[Fact]
public void WhenCalled_ShouldRegisterDispatcherClasses()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs();
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandDispatcher)
&& descriptor.ImplementationType == typeof(CommandDispatcherImpl)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryDispatcher)
&& descriptor.ImplementationType == typeof(QueryDispatcherImpl)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterDispatcherClassesOnce()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs();
services.AddCqrs();
services.AddCqrs();
services.AddCqrs();
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandDispatcher)
&& descriptor.ImplementationType == typeof(CommandDispatcherImpl)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(IQueryDispatcher)
&& descriptor.ImplementationType == typeof(QueryDispatcherImpl)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
}