2026-07-17 10:36:32 -03:00
|
|
|
using Api.SeaHavenIndustries.Controllers;
|
|
|
|
|
using Data.SeaHavenIndustries;
|
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-07-24 17:35:34 -03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2026-07-17 10:36:32 -03:00
|
|
|
using Moq;
|
|
|
|
|
using SeaHaven.DataServices.Implementation;
|
|
|
|
|
using SeaHaven.Services.DTOs;
|
|
|
|
|
using SeaHaven.Services.Implementation;
|
|
|
|
|
using SeaHaven.Services.Validation;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
|
|
|
|
|
|
public class LocationControllerSitesTests
|
|
|
|
|
{
|
|
|
|
|
private static ApplicationDbContext NewContext()
|
|
|
|
|
{
|
|
|
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
|
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
|
|
|
|
.Options;
|
|
|
|
|
return new ApplicationDbContext(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static LocationController NewController(ApplicationDbContext ctx)
|
|
|
|
|
{
|
|
|
|
|
var dataService = new LocationDataService(ctx);
|
|
|
|
|
var service = new LocationService(
|
|
|
|
|
dataService,
|
2026-08-26 14:03:12 -03:00
|
|
|
new AccountDataService(ctx),
|
2026-07-17 10:36:32 -03:00
|
|
|
Mock.Of<ICreateLocationValidation>(),
|
|
|
|
|
Mock.Of<IUpdateLocationValidation>());
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
var controller = new LocationController(service, Mock.Of<ILogger<LocationController>>())
|
2026-07-17 10:36:32 -03:00
|
|
|
{
|
|
|
|
|
ControllerContext = new ControllerContext
|
|
|
|
|
{
|
|
|
|
|
HttpContext = new DefaultHttpContext()
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return controller;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task SeedSitesAsync(ApplicationDbContext ctx)
|
|
|
|
|
{
|
|
|
|
|
ctx.Locations.AddRange(
|
|
|
|
|
new Locations { Id = 1, Name = "STL8", City = "St. Louis", State = "MO" },
|
|
|
|
|
new Locations { Id = 2, Name = "BK5", City = "Dallas", State = "TX" },
|
|
|
|
|
new Locations { Id = 3, Name = "IND9", City = "Indianapolis", State = "IN" },
|
|
|
|
|
new Locations { Id = 4, Name = "DFW8", City = "Dallas", State = "TX" });
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSites_ReturnsIdCodeCityState_OrderedByCode()
|
|
|
|
|
{
|
|
|
|
|
await using var ctx = NewContext();
|
|
|
|
|
await SeedSitesAsync(ctx);
|
|
|
|
|
var controller = NewController(ctx);
|
|
|
|
|
|
|
|
|
|
var result = await controller.GetSites();
|
|
|
|
|
|
|
|
|
|
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
|
|
|
|
var data = ok.Value.Should().BeAssignableTo<IReadOnlyList<SiteOptionDTO>>().Subject;
|
|
|
|
|
|
|
|
|
|
data.Should().HaveCount(4);
|
|
|
|
|
data.Select(s => s.Code).Should().ContainInOrder("BK5", "DFW8", "IND9", "STL8");
|
|
|
|
|
data[0].Should().BeEquivalentTo(new SiteOptionDTO
|
|
|
|
|
{
|
|
|
|
|
Id = 2,
|
|
|
|
|
Code = "BK5",
|
|
|
|
|
City = "Dallas",
|
|
|
|
|
State = "TX"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSites_FiltersByCode()
|
|
|
|
|
{
|
|
|
|
|
await using var ctx = NewContext();
|
|
|
|
|
await SeedSitesAsync(ctx);
|
|
|
|
|
var controller = NewController(ctx);
|
|
|
|
|
|
|
|
|
|
var result = await controller.GetSites("bk5");
|
|
|
|
|
|
|
|
|
|
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
|
|
|
|
var data = ok.Value.Should().BeAssignableTo<IReadOnlyList<SiteOptionDTO>>().Subject;
|
|
|
|
|
|
|
|
|
|
data.Should().ContainSingle();
|
|
|
|
|
data[0].Code.Should().Be("BK5");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetSites_FiltersByCity()
|
|
|
|
|
{
|
|
|
|
|
await using var ctx = NewContext();
|
|
|
|
|
await SeedSitesAsync(ctx);
|
|
|
|
|
var controller = NewController(ctx);
|
|
|
|
|
|
|
|
|
|
var result = await controller.GetSites("Dallas");
|
|
|
|
|
|
|
|
|
|
var ok = result.Should().BeOfType<OkObjectResult>().Subject;
|
|
|
|
|
var data = ok.Value.Should().BeAssignableTo<IReadOnlyList<SiteOptionDTO>>().Subject;
|
|
|
|
|
|
|
|
|
|
data.Should().HaveCount(2);
|
|
|
|
|
data.Select(s => s.Code).Should().BeEquivalentTo("BK5", "DFW8");
|
|
|
|
|
}
|
|
|
|
|
}
|