GFramework/GFramework.Core.Tests/Architectures/ArchitectureServicesTests.cs
GeWuYou 81897ce2ac docs(source-generators): 添加源码生成器文档
- 新增 AutoRegisterExportedCollections 生成器文档
- 新增 AutoScene 生成器文档
- 新增 AutoUiPage 生成器文档
- 新增完整的源码生成器索引文档
- 详细介绍各生成器的使用方法和参数说明
- 提供生成代码示例和诊断信息说明
- 包含性能优势和使用示例章节
2026-04-15 17:00:49 +08:00

477 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Environment;
using GFramework.Core.Abstractions.Events;
using GFramework.Core.Abstractions.Ioc;
using GFramework.Core.Abstractions.Model;
using GFramework.Core.Abstractions.Query;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Abstractions.Utility;
using GFramework.Core.Architectures;
using GFramework.Core.Command;
using GFramework.Core.Environment;
using GFramework.Core.Events;
using GFramework.Core.Ioc;
using GFramework.Core.Query;
using GFramework.Cqrs.Abstractions.Cqrs;
using ICommand = GFramework.Core.Abstractions.Command.ICommand;
namespace GFramework.Core.Tests.Architectures;
/// <summary>
/// ArchitectureServices类的单元测试
/// 测试内容包括:
/// - 服务容器初始化
/// - 所有服务实例创建Container, EventBus, CommandExecutor, QueryExecutor
/// - SetContext方法 - 设置上下文
/// - SetContext方法 - 重复设置上下文
/// - GetContext方法 - 获取已设置上下文
/// - GetContext方法 - 未设置上下文时返回null
/// - 上下文传播到容器
/// - IArchitectureServices接口实现验证
/// - 服务独立性验证(多个实例)
/// </summary>
[TestFixture]
public class ArchitectureServicesTests
{
[SetUp]
public void SetUp()
{
_services = new ArchitectureServices();
_context = new TestArchitectureContextV3();
}
private TestArchitectureContextV3? _context;
private ArchitectureServices? _services;
private void RegisterBuiltInServices()
{
_services!.ModuleManager.RegisterBuiltInModules(_services.Container);
}
/// <summary>
/// 测试构造函数初始化容器
/// </summary>
[Test]
public void Constructor_Should_Initialize_Container()
{
Assert.That(_services!.Container, Is.Not.Null);
}
[Test]
public void Container_Should_Be_Instance_Of_IocContainer()
{
Assert.That(_services!.Container, Is.InstanceOf<IIocContainer>());
Assert.That(_services.Container, Is.InstanceOf<MicrosoftDiContainer>());
}
/// <summary>
/// 测试注册内置服务后EventBus可用
/// </summary>
[Test]
public void After_RegisterBuiltInModules_EventBus_Should_Be_Available()
{
RegisterBuiltInServices();
Assert.That(_services!.EventBus, Is.InstanceOf<IEventBus>());
Assert.That(_services.EventBus, Is.InstanceOf<EventBus>());
}
/// <summary>
/// 测试注册内置服务后CommandExecutor可用
/// </summary>
[Test]
public void After_RegisterBuiltInModules_CommandExecutor_Should_Be_Available()
{
RegisterBuiltInServices();
Assert.That(_services!.CommandExecutor, Is.InstanceOf<ICommandExecutor>());
Assert.That(_services.CommandExecutor, Is.InstanceOf<CommandExecutor>());
}
/// <summary>
/// 测试注册内置服务后QueryExecutor可用
/// </summary>
[Test]
public void After_RegisterBuiltInModules_QueryExecutor_Should_Be_Available()
{
RegisterBuiltInServices();
Assert.That(_services!.QueryExecutor, Is.InstanceOf<IQueryExecutor>());
Assert.That(_services.QueryExecutor, Is.InstanceOf<QueryExecutor>());
}
/// <summary>
/// 测试注册内置服务后AsyncQueryExecutor可用
/// </summary>
[Test]
public void After_RegisterBuiltInModules_AsyncQueryExecutor_Should_Be_Available()
{
RegisterBuiltInServices();
Assert.That(_services!.AsyncQueryExecutor, Is.InstanceOf<IAsyncQueryExecutor>());
Assert.That(_services.AsyncQueryExecutor, Is.InstanceOf<AsyncQueryExecutor>());
}
/// <summary>
/// 测试未注册服务时EventBus为null
/// </summary>
[Test]
public void Without_RegisterBuiltInModules_EventBus_Should_Be_Null()
{
Assert.That(_services!.EventBus, Is.Null);
}
/// <summary>
/// 测试SetContext设置内部Context字段
/// </summary>
[Test]
public void SetContext_Should_Set_Context_Internal_Field()
{
_services!.SetContext(_context!);
var context = _services.GetContext();
Assert.That(context, Is.SameAs(_context));
}
/// <summary>
/// 测试SetContext将上下文传播到Container
/// </summary>
[Test]
public void SetContext_Should_Propagate_Context_To_Container()
{
_services!.SetContext(_context!);
var containerContext = _services.Container.GetContext();
Assert.That(containerContext, Is.SameAs(_context));
}
/// <summary>
/// 测试GetContext在SetContext后返回上下文
/// </summary>
[Test]
public void GetContext_Should_Return_Context_After_SetContext()
{
_services!.SetContext(_context!);
var context = _services.GetContext();
Assert.That(context, Is.Not.Null);
Assert.That(context, Is.SameAs(_context));
}
/// <summary>
/// 测试GetContext在未设置上下文时返回null
/// </summary>
[Test]
public void GetContext_Should_ReturnNull_When_Context_Not_Set()
{
var context = _services!.GetContext();
Assert.That(context, Is.Null);
}
/// <summary>
/// 测试SetContext替换已存在的上下文
/// </summary>
[Test]
public void SetContext_Should_Replace_Existing_Context()
{
var context1 = new TestArchitectureContextV3 { Id = 1 };
var context2 = new TestArchitectureContextV3 { Id = 2 };
_services!.SetContext(context1);
_services.SetContext(context2);
var context = _services.GetContext();
Assert.That(context, Is.SameAs(context2));
}
/// <summary>
/// 测试ArchitectureServices实现IArchitectureServices接口
/// </summary>
[Test]
public void ArchitectureServices_Should_Implement_IArchitectureServices_Interface()
{
Assert.That(_services, Is.InstanceOf<IArchitectureServices>());
}
/// <summary>
/// 测试多个实例有独立的Container
/// </summary>
[Test]
public void Multiple_Instances_Should_Have_Independent_Container()
{
var services1 = new ArchitectureServices();
var services2 = new ArchitectureServices();
Assert.That(services1.Container, Is.Not.SameAs(services2.Container));
}
/// <summary>
/// 测试多个实例有独立的EventBus
/// </summary>
[Test]
public void Multiple_Instances_Should_Have_Independent_EventBus()
{
var services1 = new ArchitectureServices();
services1.ModuleManager.RegisterBuiltInModules(services1.Container);
var services2 = new ArchitectureServices();
services2.ModuleManager.RegisterBuiltInModules(services2.Container);
Assert.That(services1.EventBus, Is.Not.SameAs(services2.EventBus));
}
/// <summary>
/// 测试多个实例有独立的CommandBus
/// </summary>
[Test]
public void Multiple_Instances_Should_Have_Independent_CommandBus()
{
var services1 = new ArchitectureServices();
services1.ModuleManager.RegisterBuiltInModules(services1.Container);
var services2 = new ArchitectureServices();
services2.ModuleManager.RegisterBuiltInModules(services2.Container);
Assert.That(services1.CommandExecutor, Is.Not.SameAs(services2.CommandExecutor));
}
/// <summary>
/// 测试多个实例有独立的QueryBus
/// </summary>
[Test]
public void Multiple_Instances_Should_Have_Independent_QueryBus()
{
var services1 = new ArchitectureServices();
services1.ModuleManager.RegisterBuiltInModules(services1.Container);
var services2 = new ArchitectureServices();
services2.ModuleManager.RegisterBuiltInModules(services2.Container);
Assert.That(services1.QueryExecutor, Is.Not.SameAs(services2.QueryExecutor));
}
/// <summary>
/// 测试ModuleManager属性不为空
/// </summary>
[Test]
public void ModuleManager_Should_Not_Be_Null()
{
Assert.That(_services!.ModuleManager, Is.Not.Null);
}
}
#region Test Classes
public class TestArchitectureContextV3 : IArchitectureContext
{
private readonly MicrosoftDiContainer _container = new();
private readonly DefaultEnvironment _environment = new();
public int Id { get; init; }
public TService? GetService<TService>() where TService : class
{
return _container.Get<TService>();
}
public IReadOnlyList<TService> GetServices<TService>() where TService : class
{
return _container.GetAll<TService>();
}
public TModel? GetModel<TModel>() where TModel : class, IModel
{
return _container.Get<TModel>();
}
public IReadOnlyList<TModel> GetModels<TModel>() where TModel : class, IModel
{
return _container.GetAll<TModel>();
}
public TSystem? GetSystem<TSystem>() where TSystem : class, ISystem
{
return _container.Get<TSystem>();
}
public IReadOnlyList<TSystem> GetSystems<TSystem>() where TSystem : class, ISystem
{
return _container.GetAll<TSystem>();
}
public TUtility? GetUtility<TUtility>() where TUtility : class, IUtility
{
return _container.Get<TUtility>();
}
public IReadOnlyList<TUtility> GetUtilities<TUtility>() where TUtility : class, IUtility
{
return _container.GetAll<TUtility>();
}
public IReadOnlyList<TService> GetServicesByPriority<TService>() where TService : class
{
return _container.GetAllByPriority<TService>();
}
public IReadOnlyList<TSystem> GetSystemsByPriority<TSystem>() where TSystem : class, ISystem
{
return _container.GetAllByPriority<TSystem>();
}
public IReadOnlyList<TModel> GetModelsByPriority<TModel>() where TModel : class, IModel
{
return _container.GetAllByPriority<TModel>();
}
public IReadOnlyList<TUtility> GetUtilitiesByPriority<TUtility>() where TUtility : class, IUtility
{
return _container.GetAllByPriority<TUtility>();
}
public void SendEvent<TEvent>() where TEvent : new()
{
}
public void SendEvent<TEvent>(TEvent e) where TEvent : class
{
}
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> handler)
{
return new DefaultUnRegister(() => { });
}
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
{
}
public ValueTask<TResponse> SendRequestAsync<TResponse>(IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public TResponse SendRequest<TResponse>(IRequest<TResponse> request)
{
throw new NotImplementedException();
}
/// <summary>
/// 测试桩:异步发送 CQRS 命令并返回响应。
/// </summary>
/// <typeparam name="TResponse">命令响应类型。</typeparam>
/// <param name="command">要发送的命令。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>命令响应任务。</returns>
/// <exception cref="NotImplementedException">该测试桩未实现此成员。</exception>
public ValueTask<TResponse> SendCommandAsync<TResponse>(
GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand<TResponse> command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
/// <summary>
/// 测试桩:同步发送 CQRS 命令并返回响应。
/// </summary>
/// <typeparam name="TResponse">命令响应类型。</typeparam>
/// <param name="command">要发送的命令。</param>
/// <returns>命令响应。</returns>
/// <exception cref="NotImplementedException">该测试桩未实现此成员。</exception>
public TResponse SendCommand<TResponse>(GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand<TResponse> command)
{
throw new NotImplementedException();
}
/// <summary>
/// 测试桩:异步发送 CQRS 查询并返回结果。
/// </summary>
/// <typeparam name="TResponse">查询结果类型。</typeparam>
/// <param name="query">要发送的查询。</param>
/// <param name="cancellationToken">取消令牌。</param>
/// <returns>查询结果任务。</returns>
/// <exception cref="NotImplementedException">该测试桩未实现此成员。</exception>
public ValueTask<TResponse> SendQueryAsync<TResponse>(
GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery<TResponse> query,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
/// <summary>
/// 测试桩:同步发送 CQRS 查询并返回结果。
/// </summary>
/// <typeparam name="TResponse">查询结果类型。</typeparam>
/// <param name="query">要发送的查询。</param>
/// <returns>查询结果。</returns>
/// <exception cref="NotImplementedException">该测试桩未实现此成员。</exception>
public TResponse SendQuery<TResponse>(GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery<TResponse> query)
{
throw new NotImplementedException();
}
public ValueTask PublishAsync<TNotification>(TNotification notification,
CancellationToken cancellationToken = default) where TNotification : INotification
{
throw new NotImplementedException();
}
public IAsyncEnumerable<TResponse> CreateStream<TResponse>(
IStreamRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public ValueTask SendAsync<TCommand>(TCommand command, CancellationToken cancellationToken = default)
where TCommand : IRequest<Unit>
{
throw new NotImplementedException();
}
public ValueTask<TResponse> SendAsync<TResponse>(IRequest<TResponse> command,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public void SendCommand(ICommand command)
{
}
public TResult SendCommand<TResult>(Abstractions.Command.ICommand<TResult> command)
{
return default!;
}
public Task SendCommandAsync(IAsyncCommand command)
{
return Task.CompletedTask;
}
public Task<TResult> SendCommandAsync<TResult>(IAsyncCommand<TResult> command)
{
return (Task<TResult>)Task.CompletedTask;
}
public TResult SendQuery<TResult>(Abstractions.Query.IQuery<TResult> query)
{
return default!;
}
public Task<TResult> SendQueryAsync<TResult>(IAsyncQuery<TResult> query)
{
return (Task<TResult>)Task.CompletedTask;
}
public IEnvironment GetEnvironment()
{
return _environment;
}
}
#endregion