WMSlite/WmsLite.Api.Tests/InventoryAllocationServiceTests.cs

166 lines
5.6 KiB
C#

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);
}
}