add test and outbound test

This commit is contained in:
zhougf committed 2026-06-01 23:30:36 +08:00
1 parent 38ecbf82e5
commit 7f621dd553
23 files changed
+1301 -26

No files matched your search

@@ -0,0 +1,165 @@
using System.Linq.Expressions;
using WmsLite.Api.Common;
using WmsLite.Api.Dtos.Common;
using WmsLite.Api.Dtos.Inventory;
using WmsLite.Api.Enums;
using WmsLite.Api.Infrastructure.Auth;
using WmsLite.Api.Models;
using WmsLite.Api.Repositories.Interfaces;
using WmsLite.Api.Services;
namespace WmsLite.Api.Tests;
public class InventoryAllocationServiceTests
{
[Fact]
public async Task AllocateAsync_AllocatesAcrossFifoCandidates()
{
var repo = new FakeInventoryRepository([
NewInventory(1, "L-1", qty: 5, createTime: new DateTime(2026, 1, 1)),
NewInventory(2, "L-2", qty: 8, createTime: new DateTime(2026, 1, 2)),
]);
var service = new InventoryAllocationService(repo);
var result = await service.AllocateAsync(NewRequest(10));
Assert.Equal(2, result.Count);
Assert.Equal(1, result[0].Inventory.Id);
Assert.Equal(5, result[0].Qty);
Assert.Equal(2, result[1].Inventory.Id);
Assert.Equal(5, result[1].Qty);
}
[Fact]
public async Task AllocateAsync_RespectsLocationConstraint()
{
var repo = new FakeInventoryRepository([
NewInventory(1, "L-1", qty: 5),
NewInventory(2, "L-2", qty: 8),
]);
var service = new InventoryAllocationService(repo);
var result = await service.AllocateAsync(NewRequest(6, locationCode: "L-2"));
var item = Assert.Single(result);
Assert.Equal(2, item.Inventory.Id);
Assert.Equal(6, item.Qty);
}
[Fact]
public async Task AllocateAsync_ThrowsWhenAvailableQtyIsNotEnough()
{
var repo = new FakeInventoryRepository([
NewInventory(1, "L-1", qty: 5, pendingOutboundQty: 3),
]);
var service = new InventoryAllocationService(repo);
await Assert.ThrowsAsync<BusinessException>(() => service.AllocateAsync(NewRequest(3)));
}
private static InventoryAllocationRequest NewRequest(decimal qty, string? locationCode = null)
{
return new InventoryAllocationRequest
{
TenantId = "default",
OwnerCode = "default",
InventoryKind = InventoryKind.Material,
MaterialCode = "MAT-1",
WarehouseCode = "WH-1",
AreaCode = "A-1",
LocationCode = locationCode,
Qty = qty
};
}
private static Inventory NewInventory(
long id,
string locationCode,
decimal qty,
decimal pendingOutboundQty = 0,
DateTime? createTime = null)
{
return new Inventory
{
Id = id,
TenantId = "default",
OwnerCode = "default",
InventoryKind = InventoryKind.Material,
MaterialCode = "MAT-1",
WarehouseCode = "WH-1",
AreaCode = "A-1",
ZoneCode = "Z-1",
LocationCode = locationCode,
Qty = qty,
PendingOutboundQty = pendingOutboundQty,
Unit = "pcs",
CreateTime = createTime
};
}
private sealed class FakeInventoryRepository : IInventoryRepository
{
private readonly List<Inventory> _items;
public FakeInventoryRepository(IEnumerable<Inventory> items)
{
_items = items.ToList();
}
public Task<List<Inventory>> GetAllocatableAsync(
string tenantId,
string ownerCode,
string warehouseCode,
string areaCode,
InventoryKind inventoryKind,
string? materialCode,
string? containerCode,
string? locationCode = null)
{
var items = _items
.Where(x => !x.IsDeleted
&& x.TenantId == tenantId
&& x.OwnerCode == ownerCode
&& x.WarehouseCode == warehouseCode
&& x.AreaCode == areaCode
&& x.InventoryKind == inventoryKind
&& x.MaterialCode == materialCode
&& x.Qty - x.PendingOutboundQty - x.FrozenQty > 0)
.Where(x => string.IsNullOrWhiteSpace(locationCode) || x.LocationCode == locationCode)
.OrderBy(x => x.CreateTime)
.ThenBy(x => x.Id)
.ToList();
return Task.FromResult(items);
}
public Task<Inventory?> GetByStockKeyAsync(string tenantId, string ownerCode, string warehouseCode, string areaCode, string zoneCode, string locationCode, InventoryKind inventoryKind, string? materialCode, string? containerCode)
=> Task.FromResult<Inventory?>(null);
public Task<Inventory?> GetByIdAsync(long id)
=> Task.FromResult<Inventory?>(null);
public Task<Inventory?> GetByIdAsync(long id, string tenantId, string ownerCode)
=> Task.FromResult<Inventory?>(null);
public Task<List<Inventory>> GetListAsync(Expression<Func<Inventory, bool>> predicate)
=> Task.FromResult(new List<Inventory>());
public Task<PagedResult<Inventory>> GetPagedListAsync(BasePagedQuery query)
=> Task.FromResult(new PagedResult<Inventory>());
public Task<PagedResult<Inventory>> GetPagedListAsync(InventoryPagedQuery query)
=> Task.FromResult(new PagedResult<Inventory>());
public Task<long> InsertAsync(Inventory entity)
=> Task.FromResult(entity.Id);
public Task<int> UpdateAsync(Inventory entity)
=> Task.FromResult(1);
public Task<int> SoftDeleteAsync(long id)
=> Task.FromResult(0);
public Task<int> SoftDeleteAsync(long id, string tenantId, string ownerCode)
=> Task.FromResult(0);
}
}
@@ -0,0 +1,214 @@
using System.Linq.Expressions;
using WmsLite.Api.Common;
using WmsLite.Api.Dtos.Common;
using WmsLite.Api.Dtos.Inventory;
using WmsLite.Api.Enums;
using WmsLite.Api.Infrastructure.Auth;
using WmsLite.Api.Models;
using WmsLite.Api.Repositories.Interfaces;
using WmsLite.Api.Services;
namespace WmsLite.Api.Tests;
public class InventoryMovementServiceTests
{
[Fact]
public async Task PendingOutboundAsync_ReservesAvailableQtyAndWritesTransaction()
{
var inventory = NewInventory(qty: 10);
var (_, transactionRepository, service) = CreateService();
await service.PendingOutboundAsync(inventory, 4, "SO-1", "pick");
Assert.Equal(10, inventory.Qty);
Assert.Equal(4, inventory.PendingOutboundQty);
Assert.Equal(InventoryStatus.PendingOutbound, inventory.InventoryStatus);
var tx = Assert.Single(transactionRepository.Entities);
Assert.Equal(InventoryMovementType.PendingOutbound, tx.MovementType);
Assert.Equal(10, tx.BeforeQty);
Assert.Equal(10, tx.AfterQty);
Assert.Equal(0, tx.BeforePendingOutboundQty);
Assert.Equal(4, tx.AfterPendingOutboundQty);
Assert.Equal(4, tx.ChangeQty);
Assert.Equal("SO-1", tx.BizNo);
Assert.Equal("pick", tx.Attrs?["remark"]);
}
[Fact]
public async Task ConfirmOutboundAsync_DecrementsPendingAndStockQty()
{
var inventory = NewInventory(qty: 10, pendingOutboundQty: 4);
var (_, transactionRepository, service) = CreateService();
await service.ConfirmOutboundAsync(inventory, 4, "SO-1");
Assert.Equal(6, inventory.Qty);
Assert.Equal(0, inventory.PendingOutboundQty);
Assert.Equal(InventoryStatus.Available, inventory.InventoryStatus);
var tx = Assert.Single(transactionRepository.Entities);
Assert.Equal(InventoryMovementType.OutboundConfirm, tx.MovementType);
Assert.Equal(10, tx.BeforeQty);
Assert.Equal(6, tx.AfterQty);
Assert.Equal(4, tx.BeforePendingOutboundQty);
Assert.Equal(0, tx.AfterPendingOutboundQty);
Assert.Equal(-4, tx.ChangeQty);
}
[Fact]
public async Task ConfirmInboundAsync_MovesPendingInboundToStockQty()
{
var inventory = NewInventory(qty: 2, pendingInboundQty: 5);
var (_, transactionRepository, service) = CreateService();
await service.ConfirmInboundAsync(inventory, 3, "IN-1");
Assert.Equal(5, inventory.Qty);
Assert.Equal(2, inventory.PendingInboundQty);
Assert.Equal(InventoryStatus.PendingInbound, inventory.InventoryStatus);
var tx = Assert.Single(transactionRepository.Entities);
Assert.Equal(InventoryMovementType.InboundConfirm, tx.MovementType);
Assert.Equal(2, tx.BeforeQty);
Assert.Equal(5, tx.AfterQty);
Assert.Equal(5, tx.BeforePendingInboundQty);
Assert.Equal(2, tx.AfterPendingInboundQty);
Assert.Equal(3, tx.ChangeQty);
}
[Fact]
public async Task PendingOutboundAsync_ThrowsWhenAvailableQtyIsNotEnough()
{
var inventory = NewInventory(qty: 10, pendingOutboundQty: 7, frozenQty: 2);
var (inventoryRepository, transactionRepository, service) = CreateService();
await Assert.ThrowsAsync<BusinessException>(
() => service.PendingOutboundAsync(inventory, 2, "SO-2"));
Assert.Equal(7, inventory.PendingOutboundQty);
Assert.Empty(inventoryRepository.UpdatedEntities);
Assert.Empty(transactionRepository.Entities);
}
private static (FakeInventoryRepository InventoryRepository, FakeInventoryTransactionRepository TransactionRepository, InventoryMovementService Service) CreateService()
{
var inventoryRepository = new FakeInventoryRepository();
var transactionRepository = new FakeInventoryTransactionRepository();
var service = new InventoryMovementService(
inventoryRepository,
transactionRepository,
new FakeCurrentUserContext());
return (inventoryRepository, transactionRepository, service);
}
private static Inventory NewInventory(decimal qty, decimal pendingInboundQty = 0, decimal pendingOutboundQty = 0, decimal frozenQty = 0)
{
return new Inventory
{
Id = 1001,
TenantId = "default",
OwnerCode = "default",
InventoryKind = InventoryKind.Material,
MaterialCode = "MAT-001",
WarehouseCode = "WH-1",
AreaCode = "A-1",
ZoneCode = "Z-1",
LocationCode = "L-1",
Qty = qty,
PendingInboundQty = pendingInboundQty,
PendingOutboundQty = pendingOutboundQty,
FrozenQty = frozenQty,
InventoryStatus = InventoryStatus.Available,
Unit = "pcs",
Attrs = new Dictionary<string, object> { ["lot"] = "L202606" },
};
}
private sealed class FakeCurrentUserContext : ICurrentUserContext
{
public bool IsAuthenticated => true;
public string UserId => "tester";
public string TenantId => "default";
public string OwnerCode => "default";
public string? DisplayName => "Tester";
}
private sealed class FakeInventoryTransactionRepository : IInventoryTransactionRepository
{
public List<InventoryTransaction> Entities { get; } = [];
public Task<long> InsertAsync(InventoryTransaction entity)
{
entity.Id = Entities.Count + 1;
Entities.Add(entity);
return Task.FromResult(entity.Id);
}
public Task<PagedResult<InventoryTransaction>> GetPagedListAsync(InventoryTransactionPagedQuery query)
=> Task.FromResult(new PagedResult<InventoryTransaction>());
}
private sealed class FakeInventoryRepository : IInventoryRepository
{
public List<Inventory> InsertedEntities { get; } = [];
public List<Inventory> UpdatedEntities { get; } = [];
public Task<Inventory?> GetByStockKeyAsync(
string tenantId,
string ownerCode,
string warehouseCode,
string areaCode,
string zoneCode,
string locationCode,
InventoryKind inventoryKind,
string? materialCode,
string? containerCode)
=> Task.FromResult<Inventory?>(null);
public Task<List<Inventory>> GetAllocatableAsync(
string tenantId,
string ownerCode,
string warehouseCode,
string areaCode,
InventoryKind inventoryKind,
string? materialCode,
string? containerCode,
string? locationCode = null)
=> Task.FromResult(new List<Inventory>());
public Task<Inventory?> GetByIdAsync(long id)
=> Task.FromResult<Inventory?>(null);
public Task<Inventory?> GetByIdAsync(long id, string tenantId, string ownerCode)
=> Task.FromResult<Inventory?>(null);
public Task<List<Inventory>> GetListAsync(Expression<Func<Inventory, bool>> predicate)
=> Task.FromResult(new List<Inventory>());
public Task<PagedResult<Inventory>> GetPagedListAsync(BasePagedQuery query)
=> Task.FromResult(new PagedResult<Inventory>());
public Task<PagedResult<Inventory>> GetPagedListAsync(InventoryPagedQuery query)
=> Task.FromResult(new PagedResult<Inventory>());
public Task<long> InsertAsync(Inventory entity)
{
entity.Id = entity.Id == 0 ? InsertedEntities.Count + 1 : entity.Id;
InsertedEntities.Add(entity);
return Task.FromResult(entity.Id);
}
public Task<int> UpdateAsync(Inventory entity)
{
UpdatedEntities.Add(entity);
return Task.FromResult(1);
}
public Task<int> SoftDeleteAsync(long id)
=> Task.FromResult(0);
public Task<int> SoftDeleteAsync(long id, string tenantId, string ownerCode)
=> Task.FromResult(0);
}
}
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WmsLite.Api\WmsLite.Api.csproj" />
</ItemGroup>
</Project>