using Data.SeaHavenIndustries; using FluentAssertions; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Implementation; using Xunit; namespace Api.SeaHavenIndustries.Tests; public class VendorDataServiceTests { private static ApplicationDbContext NewContext() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; return new ApplicationDbContext(options); } private static Vendor VendorWithJobs( string company, string city, string state, string trades, int jobCount, bool isActive = true) { return new Vendor { CompanyName = company, ContactName = $"{company} Technician", City = city, State = state, TradeSpecialties = trades, IsActive = isActive, IsDeleted = false, Dispatches = Enumerable.Range(1, jobCount) .Select(workOrderId => new Dispatch { WorkOrderId = workOrderId }) .ToList() }; } private static async Task SeedBoundaryVendors(ApplicationDbContext context) { context.Vendors.AddRange( VendorWithJobs("Under Co", "Chicago", "IL", "Plumbing, HVAC", 49), VendorWithJobs("Fifty Co", "Chicago", "IL", "Electrical", 50), VendorWithJobs("Hundred Co", "Dallas", "TX", "Plumbing", 100), VendorWithJobs("One Fifty Co", "Dallas", "TX", "Roofing", 150)); await context.SaveChangesAsync(); } [Theory] [InlineData("under-50", "Under Co")] [InlineData("50-99", "Fifty Co")] [InlineData("100-149", "Hundred Co")] [InlineData("150-plus", "One Fifty Co")] public async Task GetPagedAsync_AppliesExactJobBucketBoundaries( string bucket, string expectedCompany) { await using var context = NewContext(); await SeedBoundaryVendors(context); var service = new VendorDataService(context); var (items, totalCount) = await service.GetPagedAsync( 1, 12, isActive: true, jobBuckets: new[] { bucket }); totalCount.Should().Be(1); items.Single().CompanyName.Should().Be(expectedCompany); } [Fact] public async Task GetPagedAsync_UsesOrWithinFacetAndAndAcrossFacets() { await using var context = NewContext(); await SeedBoundaryVendors(context); var service = new VendorDataService(context); var (items, totalCount) = await service.GetPagedAsync( 1, 12, isActive: true, companies: new[] { "Under Co", "Hundred Co" }, trades: new[] { "HVAC", "Plumbing" }, locations: new[] { "Chicago, IL" }, jobBuckets: new[] { "under-50", "100-149" }); totalCount.Should().Be(1); items.Single().CompanyName.Should().Be("Under Co"); } [Fact] public async Task GetPagedAsync_StatusAndCountAreIndependentOfInactiveHistory() { await using var context = NewContext(); context.Vendors.AddRange( VendorWithJobs("Active Co", "Chicago", "IL", "Plumbing", 1), VendorWithJobs("Inactive Co", "Chicago", "IL", "Plumbing", 1, false)); await context.SaveChangesAsync(); var service = new VendorDataService(context); var active = await service.GetPagedAsync(1, 1, isActive: true); var inactive = await service.GetPagedAsync(1, 1, isActive: false); active.TotalCount.Should().Be(1); inactive.TotalCount.Should().Be(1); active.Items.Single().CompanyName.Should().Be("Active Co"); inactive.Items.Single().CompanyName.Should().Be("Inactive Co"); } }