refactor: AdminApi 剔除租户侧能力
This commit is contained in:
@@ -4,9 +4,13 @@ using TakeoutSaaS.Infrastructure.Dictionary.Persistence;
|
||||
|
||||
namespace TakeoutSaaS.Integration.Tests.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// 集成测试用 SQLite 内存数据库(字典库)。
|
||||
/// </summary>
|
||||
public sealed class DictionarySqliteTestDatabase : IDisposable
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
private readonly TestIdGenerator _idGenerator = new();
|
||||
private bool _initialized;
|
||||
|
||||
public DictionarySqliteTestDatabase()
|
||||
@@ -24,10 +28,14 @@ public sealed class DictionarySqliteTestDatabase : IDisposable
|
||||
public DictionaryDbContext CreateContext(long tenantId, long userId = 0)
|
||||
{
|
||||
EnsureCreated();
|
||||
// 1. AdminApi 不使用租户上下文;tenantId 参数仅用于兼容测试调用方签名
|
||||
_ = tenantId;
|
||||
|
||||
// 2. (空行后) 按需注入当前用户与 ID 生成器
|
||||
return new DictionaryDbContext(
|
||||
Options,
|
||||
new TestTenantProvider(tenantId),
|
||||
userId == 0 ? null : new TestCurrentUserAccessor(userId));
|
||||
userId == 0 ? null : new TestCurrentUserAccessor(userId),
|
||||
_idGenerator);
|
||||
}
|
||||
|
||||
public void EnsureCreated()
|
||||
@@ -37,7 +45,8 @@ public sealed class DictionarySqliteTestDatabase : IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
using var context = new DictionaryDbContext(Options, new TestTenantProvider(1));
|
||||
// 1. 创建并初始化数据库结构
|
||||
using var context = new DictionaryDbContext(Options, idGenerator: _idGenerator);
|
||||
context.Database.EnsureCreated();
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,13 @@ using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
|
||||
namespace TakeoutSaaS.Integration.Tests.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// 集成测试用 SQLite 内存数据库(业务主库)。
|
||||
/// </summary>
|
||||
public sealed class SqliteTestDatabase : IDisposable
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
private readonly TestIdGenerator _idGenerator = new();
|
||||
private bool _initialized;
|
||||
|
||||
public SqliteTestDatabase()
|
||||
@@ -24,7 +28,14 @@ public sealed class SqliteTestDatabase : IDisposable
|
||||
public TakeoutAdminDbContext CreateContext(long tenantId, long userId = 0)
|
||||
{
|
||||
EnsureCreated();
|
||||
return new TakeoutAdminDbContext(Options, new TestTenantProvider(tenantId), new TestCurrentUserAccessor(userId));
|
||||
// 1. AdminApi 不使用租户上下文;tenantId 参数仅用于兼容测试调用方签名
|
||||
_ = tenantId;
|
||||
|
||||
// 2. (空行后) 按需注入当前用户与 ID 生成器
|
||||
return new TakeoutAdminDbContext(
|
||||
Options,
|
||||
userId == 0 ? null : new TestCurrentUserAccessor(userId),
|
||||
_idGenerator);
|
||||
}
|
||||
|
||||
public void EnsureCreated()
|
||||
@@ -34,7 +45,8 @@ public sealed class SqliteTestDatabase : IDisposable
|
||||
return;
|
||||
}
|
||||
|
||||
using var context = new TakeoutAdminDbContext(Options, new TestTenantProvider(1));
|
||||
// 1. 创建并初始化数据库结构
|
||||
using var context = new TakeoutAdminDbContext(Options, idGenerator: _idGenerator);
|
||||
context.Database.EnsureCreated();
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Threading;
|
||||
using TakeoutSaaS.Shared.Abstractions.Ids;
|
||||
|
||||
namespace TakeoutSaaS.Integration.Tests.Fixtures;
|
||||
|
||||
/// <summary>
|
||||
/// 集成测试用雪花 ID 生成器(递增模拟)。
|
||||
/// </summary>
|
||||
public sealed class TestIdGenerator : IIdGenerator
|
||||
{
|
||||
private long _current;
|
||||
|
||||
/// <summary>
|
||||
/// 生成下一个 ID。
|
||||
/// </summary>
|
||||
/// <returns>递增的 long ID。</returns>
|
||||
public long NextId()
|
||||
=> Interlocked.Increment(ref _current);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user