26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
135 lines
4.9 KiB
C#
135 lines
4.9 KiB
C#
using JdeScoping.Api.Controllers;
|
|
using JdeScoping.Core.Interfaces;
|
|
using JdeScoping.Core.Models;
|
|
using JdeScoping.Core.Models.Inventory;
|
|
using JdeScoping.Core.Models.Organization;
|
|
using JdeScoping.Core.ViewModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NSubstitute;
|
|
using Shouldly;
|
|
|
|
namespace JdeScoping.Api.Tests.Controllers;
|
|
|
|
public class LookupControllerTests
|
|
{
|
|
private readonly ILotFinderRepository _repository;
|
|
private readonly LookupController _controller;
|
|
|
|
public LookupControllerTests()
|
|
{
|
|
_repository = Substitute.For<ILotFinderRepository>();
|
|
_controller = new LookupController(_repository);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindItems_ReturnsOrderedResults()
|
|
{
|
|
// Arrange
|
|
var items = new List<Item>
|
|
{
|
|
new Item { ItemNumber = "ITEM-002", Description = "Second Item" },
|
|
new Item { ItemNumber = "ITEM-001", Description = "First Item" }
|
|
};
|
|
_repository.SearchItemsAsync("ITEM", Arg.Any<CancellationToken>())
|
|
.Returns(items);
|
|
|
|
// Act
|
|
var result = await _controller.FindItems("ITEM", CancellationToken.None);
|
|
|
|
// Assert
|
|
result.Result.ShouldBeOfType<OkObjectResult>();
|
|
var okResult = (OkObjectResult)result.Result!;
|
|
var viewModels = okResult.Value.ShouldBeAssignableTo<IEnumerable<ItemViewModel>>()!.ToList();
|
|
viewModels.Count.ShouldBe(2);
|
|
viewModels[0].ItemNumber.ShouldBe("ITEM-001"); // Ordered by ItemNumber
|
|
viewModels[1].ItemNumber.ShouldBe("ITEM-002");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindProfitCenters_ReturnsOrderedResults()
|
|
{
|
|
// Arrange
|
|
var centers = new List<ProfitCenter>
|
|
{
|
|
new ProfitCenter { Code = "PC2", Description = "Center 2" },
|
|
new ProfitCenter { Code = "PC1", Description = "Center 1" }
|
|
};
|
|
_repository.SearchProfitCentersAsync("PC", Arg.Any<CancellationToken>())
|
|
.Returns(centers);
|
|
|
|
// Act
|
|
var result = await _controller.FindProfitCenters("PC", CancellationToken.None);
|
|
|
|
// Assert
|
|
result.Result.ShouldBeOfType<OkObjectResult>();
|
|
var okResult = (OkObjectResult)result.Result!;
|
|
var viewModels = okResult.Value.ShouldBeAssignableTo<IEnumerable<ProfitCenterViewModel>>()!.ToList();
|
|
viewModels.Count.ShouldBe(2);
|
|
viewModels[0].Code.ShouldBe("PC1"); // Ordered by Code
|
|
viewModels[1].Code.ShouldBe("PC2");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindWorkCenters_ReturnsOrderedResults()
|
|
{
|
|
// Arrange
|
|
var centers = new List<WorkCenter>
|
|
{
|
|
new WorkCenter { Code = "WC2", Description = "Center 2" },
|
|
new WorkCenter { Code = "WC1", Description = "Center 1" }
|
|
};
|
|
_repository.SearchWorkCentersAsync("WC", Arg.Any<CancellationToken>())
|
|
.Returns(centers);
|
|
|
|
// Act
|
|
var result = await _controller.FindWorkCenters("WC", CancellationToken.None);
|
|
|
|
// Assert
|
|
result.Result.ShouldBeOfType<OkObjectResult>();
|
|
var okResult = (OkObjectResult)result.Result!;
|
|
var viewModels = okResult.Value.ShouldBeAssignableTo<IEnumerable<WorkCenterViewModel>>()!.ToList();
|
|
viewModels.Count.ShouldBe(2);
|
|
viewModels[0].Code.ShouldBe("WC1"); // Ordered by Code
|
|
viewModels[1].Code.ShouldBe("WC2");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindOperators_ReturnsOrderedResults()
|
|
{
|
|
// Arrange
|
|
var users = new List<JdeUser>
|
|
{
|
|
new JdeUser { AddressNumber = 1, UserId = "user1", FullName = "Zebra, Alice" },
|
|
new JdeUser { AddressNumber = 2, UserId = "user2", FullName = "Adams, Bob" }
|
|
};
|
|
_repository.SearchUsersAsync("user", Arg.Any<CancellationToken>())
|
|
.Returns(users);
|
|
|
|
// Act
|
|
var result = await _controller.FindOperators("user", CancellationToken.None);
|
|
|
|
// Assert
|
|
result.Result.ShouldBeOfType<OkObjectResult>();
|
|
var okResult = (OkObjectResult)result.Result!;
|
|
var viewModels = okResult.Value.ShouldBeAssignableTo<IEnumerable<JdeUserViewModel>>()!.ToList();
|
|
viewModels.Count.ShouldBe(2);
|
|
viewModels[0].FullName.ShouldBe("Adams, Bob"); // Ordered by FullName
|
|
viewModels[1].FullName.ShouldBe("Zebra, Alice");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task FindItems_WithNullQuery_PassesEmptyString()
|
|
{
|
|
// Arrange
|
|
_repository.SearchItemsAsync(string.Empty, Arg.Any<CancellationToken>())
|
|
.Returns(new List<Item>());
|
|
|
|
// Act
|
|
var result = await _controller.FindItems(null!, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.Result.ShouldBeOfType<OkObjectResult>();
|
|
await _repository.Received(1).SearchItemsAsync(string.Empty, Arg.Any<CancellationToken>());
|
|
}
|
|
}
|