Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
139 lines
3.9 KiB
C#
139 lines
3.9 KiB
C#
using FluentAssertions;
|
|
using Fengling.Member.Domain.Aggregates.PointsModel;
|
|
|
|
namespace Fengling.Member.Domain.Tests.Aggregates.PointsModel;
|
|
|
|
public class PointsAccountTests
|
|
{
|
|
[Fact]
|
|
public void Create_WithValidIds_ShouldCreateAccount()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
|
|
account.MemberId.Should().Be(1);
|
|
account.TenantId.Should().Be(100);
|
|
account.TotalPoints.Should().Be(0);
|
|
account.FrozenPoints.Should().Be(0);
|
|
account.AvailablePoints.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_WithInvalidMemberId_ShouldThrowException()
|
|
{
|
|
var action = () => PointsAccount.Create(0, 100);
|
|
|
|
action.Should().Throw<ArgumentException>()
|
|
.WithMessage("*会员ID必须大于0*");
|
|
}
|
|
|
|
[Fact]
|
|
public void Create_WithInvalidTenantId_ShouldThrowException()
|
|
{
|
|
var action = () => PointsAccount.Create(1, 0);
|
|
|
|
action.Should().Throw<ArgumentException>()
|
|
.WithMessage("*租户ID必须大于0*");
|
|
}
|
|
|
|
[Fact]
|
|
public void AddPoints_ShouldIncreaseTotalPoints()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
|
|
account.AddPoints(100, "REGISTER", "source_001", "注册赠送");
|
|
|
|
account.TotalPoints.Should().Be(100);
|
|
account.AvailablePoints.Should().Be(100);
|
|
account.Transactions.Should().HaveCount(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddPoints_WithInvalidPoints_ShouldThrowException()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
|
|
var action = () => account.AddPoints(0, "REGISTER", "source_001");
|
|
|
|
action.Should().Throw<ArgumentException>()
|
|
.WithMessage("*积分必须大于0*");
|
|
}
|
|
|
|
[Fact]
|
|
public void DeductPoints_ShouldDecreaseTotalPoints()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
account.AddPoints(100, "REGISTER", "source_001");
|
|
|
|
var result = account.DeductPoints(30, "EXCHANGE", "order_001");
|
|
|
|
result.Should().BeTrue();
|
|
account.TotalPoints.Should().Be(70);
|
|
account.AvailablePoints.Should().Be(70);
|
|
}
|
|
|
|
[Fact]
|
|
public void DeductPoints_WithInsufficientPoints_ShouldReturnFalse()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
account.AddPoints(50, "REGISTER", "source_001");
|
|
|
|
var result = account.DeductPoints(100, "EXCHANGE", "order_001");
|
|
|
|
result.Should().BeFalse();
|
|
account.TotalPoints.Should().Be(50);
|
|
}
|
|
|
|
[Fact]
|
|
public void FreezePoints_ShouldIncreaseFrozenPoints()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
account.AddPoints(100, "REGISTER", "source_001");
|
|
|
|
var result = account.FreezePoints(30);
|
|
|
|
result.Should().BeTrue();
|
|
account.FrozenPoints.Should().Be(30);
|
|
account.AvailablePoints.Should().Be(70);
|
|
}
|
|
|
|
[Fact]
|
|
public void FreezePoints_WithInsufficientPoints_ShouldReturnFalse()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
account.AddPoints(50, "REGISTER", "source_001");
|
|
|
|
var result = account.FreezePoints(100);
|
|
|
|
result.Should().BeFalse();
|
|
account.FrozenPoints.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void UnfreezePoints_ShouldDecreaseFrozenPoints()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
account.AddPoints(100, "REGISTER", "source_001");
|
|
account.FreezePoints(50);
|
|
|
|
var result = account.UnfreezePoints(30);
|
|
|
|
result.Should().BeTrue();
|
|
account.FrozenPoints.Should().Be(20);
|
|
account.AvailablePoints.Should().Be(80);
|
|
}
|
|
|
|
[Fact]
|
|
public void UseFrozenPoints_ShouldDecreaseBothFrozenAndTotalPoints()
|
|
{
|
|
var account = PointsAccount.Create(1, 100);
|
|
account.AddPoints(100, "REGISTER", "source_001");
|
|
account.FreezePoints(50);
|
|
|
|
var result = account.UseFrozenPoints(30);
|
|
|
|
result.Should().BeTrue();
|
|
account.FrozenPoints.Should().Be(20);
|
|
account.TotalPoints.Should().Be(70);
|
|
}
|
|
}
|