Nieuwe tabel OfferingsCategories (unieke naam, case-insensitief) en nullable Offering.CategoryId met FK Restrict (migratie AddOfferingCategories). Routes onder /offerings/categories zijn AdminOnly; verwijderen van een gebruikte categorie geeft 409. Het aanbod neemt categoryId (moet bestaan, anders 400) en geeft category (naam) en categoryId terug; niets wordt geseed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
140 lines
5.0 KiB
C#
140 lines
5.0 KiB
C#
using FluentAssertions;
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using SlpModularCms.Modules.Offerings.Data.Entities;
|
|
using SlpModularCms.Modules.Offerings.Models;
|
|
using SlpModularCms.Modules.Offerings.Repositories;
|
|
using SlpModularCms.Modules.Offerings.Services;
|
|
|
|
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
|
|
|
public class OfferingsServiceCategoryTests
|
|
{
|
|
private readonly IOfferingRepository _repo = Substitute.For<IOfferingRepository>();
|
|
private readonly IOfferingCategoryRepository _categories = Substitute.For<IOfferingCategoryRepository>();
|
|
|
|
public OfferingsServiceCategoryTests()
|
|
{
|
|
_repo.BeginTransactionAsync().Returns(Substitute.For<IDbContextTransaction>());
|
|
_repo.GetMaxDisplayOrderAsync().Returns(-1);
|
|
}
|
|
|
|
private OfferingsService CreateSut()
|
|
=> new(_repo, _categories, Substitute.For<ILogger<OfferingsService>>());
|
|
|
|
private static OfferingCategory Category(string name) => new() { Id = Guid.NewGuid(), Name = name };
|
|
|
|
private static CreateOfferingRequest Create(Guid? categoryId) => new(
|
|
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", CategoryId: categoryId);
|
|
|
|
private static UpdateOfferingRequest Update(Guid? categoryId) => new(
|
|
"Existing", "Description", "€ 100", "per month", ["Feature 1"], "Contact", CategoryId: categoryId);
|
|
|
|
private static Offering Existing(OfferingCategory? category) => new()
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Slug = "existing",
|
|
Title = "Existing",
|
|
Description = "Description",
|
|
Price = "€ 100",
|
|
PriceNote = "per month",
|
|
Features = ["Feature 1"],
|
|
CtaLabel = "Contact",
|
|
CategoryId = category?.Id,
|
|
Category = category,
|
|
};
|
|
|
|
[Fact]
|
|
public async Task CreateAsync_StoresTheCategory_AndReturnsIdAndName()
|
|
{
|
|
var websites = Category("Websites");
|
|
_categories.GetByIdAsync(websites.Id).Returns(websites);
|
|
|
|
var dto = await CreateSut().CreateAsync(Create(websites.Id), Guid.NewGuid());
|
|
|
|
dto.CategoryId.Should().Be(websites.Id.ToString());
|
|
dto.Category.Should().Be("Websites");
|
|
await _repo.Received(1).AddAsync(Arg.Is<Offering>(o => o.CategoryId == websites.Id));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateAsync_WithoutCategory_HasNullCategoryFields()
|
|
{
|
|
var dto = await CreateSut().CreateAsync(Create(null), Guid.NewGuid());
|
|
|
|
dto.CategoryId.Should().BeNull();
|
|
dto.Category.Should().BeNull();
|
|
await _categories.DidNotReceive().GetByIdAsync(Arg.Any<Guid>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateAsync_Throws_AndSavesNothing_WhenTheCategoryDoesNotExist()
|
|
{
|
|
_categories.GetByIdAsync(Arg.Any<Guid>()).Returns((OfferingCategory?)null);
|
|
|
|
var act = () => CreateSut().CreateAsync(Create(Guid.NewGuid()), Guid.NewGuid());
|
|
|
|
await act.Should().ThrowAsync<OfferingCategoryInvalidException>();
|
|
await _repo.DidNotReceive().AddAsync(Arg.Any<Offering>());
|
|
await _repo.DidNotReceive().SaveChangesAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_MovesTheOfferingToAnotherCategory()
|
|
{
|
|
var websites = Category("Websites");
|
|
var onderhoud = Category("Onderhoud");
|
|
var target = Existing(websites);
|
|
_repo.GetByIdAsync(target.Id).Returns(target);
|
|
_categories.GetByIdAsync(onderhoud.Id).Returns(onderhoud);
|
|
|
|
var dto = await CreateSut().UpdateAsync(target.Id, Update(onderhoud.Id), Guid.NewGuid());
|
|
|
|
dto!.Category.Should().Be("Onderhoud");
|
|
target.CategoryId.Should().Be(onderhoud.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_ClearsTheCategory_WhenNull()
|
|
{
|
|
var target = Existing(Category("Websites"));
|
|
_repo.GetByIdAsync(target.Id).Returns(target);
|
|
|
|
var dto = await CreateSut().UpdateAsync(target.Id, Update(null), Guid.NewGuid());
|
|
|
|
dto!.CategoryId.Should().BeNull();
|
|
dto.Category.Should().BeNull();
|
|
target.CategoryId.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_Throws_AndChangesNothing_WhenTheCategoryDoesNotExist()
|
|
{
|
|
var websites = Category("Websites");
|
|
var target = Existing(websites);
|
|
_repo.GetByIdAsync(target.Id).Returns(target);
|
|
_categories.GetByIdAsync(Arg.Any<Guid>()).Returns((OfferingCategory?)null);
|
|
|
|
var act = () => CreateSut().UpdateAsync(target.Id, Update(Guid.NewGuid()), Guid.NewGuid());
|
|
|
|
await act.Should().ThrowAsync<OfferingCategoryInvalidException>();
|
|
target.CategoryId.Should().Be(websites.Id);
|
|
await _repo.DidNotReceive().SaveChangesAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetOfferingsAsync_CarriesTheCategoryName_SoARenameShowsUpImmediately()
|
|
{
|
|
var websites = Category("Websites");
|
|
_repo.GetAllAsync().Returns([Existing(websites)]);
|
|
var sut = CreateSut();
|
|
|
|
(await sut.GetOfferingsAsync())[0].Category.Should().Be("Websites");
|
|
|
|
websites.Name = "Webdesign";
|
|
|
|
(await sut.GetOfferingsAsync())[0].Category.Should().Be("Webdesign");
|
|
}
|
|
}
|