using MAX.Framework.Commons;
using Store.Users.Entities;
using Store.Users.Repositories;
using Store.Users.Services;
namespace Store.Users.Tests;
/// 验证主账号解析及无效父账号的继承边界。
public sealed class UserRepositoryTests
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ChildFees_MatchMainAccount_IncludingUnsetValues(bool unset)
{
using var context = new UserTestContext();
decimal? fee = unset ? null : 0.5m;
await context.Db.Update().Set(u => u.PostagePerOrder, fee)
.Set(u => u.RefundPostagePerOrder, fee).Set(u => u.GiftOemAmountPerOrder, fee)
.Set(u => u.SyncLogisticsPerOrder, fee).Set(u => u.ShopPointsPerMonth, 45)
.Where(u => u.Id == context.Main.Id).ExecuteAffrowsAsync();
var main = await context.Query.GetUser("main");
var child = await context.Query.GetUser("child");
Assert.Equal(main.PostagePerOrder, child.PostagePerOrder);
Assert.Equal(main.RefundPostagePerOrder, child.RefundPostagePerOrder);
Assert.Equal(main.GiftOemAmountPerOrder, child.GiftOemAmountPerOrder);
Assert.Equal(main.SyncLogisticsPerOrder, child.SyncLogisticsPerOrder);
Assert.Equal(45, child.ShopPointsPerMonth);
Assert.Equal(30, (await context.Read("child")).ShopPointsPerMonth);
}
[Theory]
[InlineData("main", "main")]
[InlineData("child", "main")]
[InlineData("missing", null)]
public async Task GetMainUser_ResolvesOwner(string userName, string? expected)
{
using var context = new UserTestContext();
var repository = (IUserRepository)Activator.CreateInstance(
typeof(UserManagementService).Assembly.GetType("Store.Users.Repositories.UserRepository", true)!, context.Db)!;
Assert.Equal(expected, (await repository.GetMainUser(userName))?.UserName);
}
[Theory]
[InlineData("missing")]
[InlineData("child")]
[InlineData("")]
public async Task InvalidParent_IsRejectedByBothQueries(string parent)
{
using var context = new UserTestContext();
await context.Db.Update().Set(u => u.ParentUserName, parent)
.Where(u => u.Id == context.Child.Id).ExecuteAffrowsAsync();
var repository = (IUserRepository)Activator.CreateInstance(
typeof(UserManagementService).Assembly.GetType("Store.Users.Repositories.UserRepository", true)!, context.Db)!;
await Assert.ThrowsAsync(() => repository.GetUser("child"));
await Assert.ThrowsAsync(() => repository.GetMainUser("child"));
}
}