Aanbod: categorieën als CMS-data met CRUD-routes en categoryId op het aanbod
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>
This commit is contained in:
+110
@@ -0,0 +1,110 @@
|
||||
using System.Reflection;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SlpModularCms.Modules.Offerings.Controllers;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Controllers;
|
||||
|
||||
public class OfferingCategoriesControllerTests
|
||||
{
|
||||
private readonly IOfferingCategoriesService _service = Substitute.For<IOfferingCategoriesService>();
|
||||
|
||||
private OfferingCategoriesController CreateSut() => new(_service);
|
||||
|
||||
private static OfferingCategoryDto Dto(string name = "Websites")
|
||||
=> new(Guid.NewGuid().ToString(), name, 0, 0);
|
||||
|
||||
[Fact]
|
||||
public async Task List_Returns200()
|
||||
{
|
||||
_service.ListAsync().Returns([Dto()]);
|
||||
|
||||
var result = await CreateSut().List();
|
||||
|
||||
result.Should().BeOfType<OkObjectResult>().Which.StatusCode.Should().Be(200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_Returns201_WithLocation()
|
||||
{
|
||||
var dto = Dto();
|
||||
_service.CreateAsync("Websites").Returns(dto);
|
||||
|
||||
var result = await CreateSut().Create(new OfferingCategoryRequest("Websites"));
|
||||
|
||||
var created = result.Should().BeOfType<CreatedResult>().Subject;
|
||||
created.Location.Should().Be($"/api/v1/offerings/categories/{dto.Id}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_Returns409_WhenTheNameIsTaken()
|
||||
{
|
||||
_service.CreateAsync(Arg.Any<string>()).ThrowsAsync(new OfferingCategoryNameConflictException("Websites"));
|
||||
|
||||
var result = await CreateSut().Create(new OfferingCategoryRequest("Websites"));
|
||||
|
||||
result.Should().BeOfType<ConflictObjectResult>().Which.StatusCode.Should().Be(409);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Rename_Returns200_404_And409()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
_service.RenameAsync(id, "Nieuw").Returns(Dto("Nieuw"));
|
||||
(await CreateSut().Rename(id, new OfferingCategoryRequest("Nieuw")))
|
||||
.Should().BeOfType<OkObjectResult>();
|
||||
|
||||
_service.RenameAsync(id, "Nieuw").Returns((OfferingCategoryDto?)null);
|
||||
(await CreateSut().Rename(id, new OfferingCategoryRequest("Nieuw")))
|
||||
.Should().BeOfType<NotFoundResult>();
|
||||
|
||||
_service.RenameAsync(id, "Nieuw").ThrowsAsync(new OfferingCategoryNameConflictException("Nieuw"));
|
||||
(await CreateSut().Rename(id, new OfferingCategoryRequest("Nieuw")))
|
||||
.Should().BeOfType<ConflictObjectResult>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delete_Returns204_404_And409WhenInUse()
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
_service.DeleteAsync(id).Returns(true);
|
||||
(await CreateSut().Delete(id)).Should().BeOfType<NoContentResult>();
|
||||
|
||||
_service.DeleteAsync(id).Returns(false);
|
||||
(await CreateSut().Delete(id)).Should().BeOfType<NotFoundResult>();
|
||||
|
||||
_service.DeleteAsync(id).ThrowsAsync(new OfferingCategoryInUseException(2));
|
||||
var conflict = await CreateSut().Delete(id);
|
||||
var problem = conflict.Should().BeOfType<ConflictObjectResult>().Subject.Value
|
||||
.Should().BeOfType<ProblemDetails>().Subject;
|
||||
problem.Status.Should().Be(409);
|
||||
problem.Title.Should().Contain("2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EveryAction_RequiresAdminOnly_IsNeverAnonymous_AndHasNoAdminSegment()
|
||||
{
|
||||
var type = typeof(OfferingCategoriesController);
|
||||
type.GetCustomAttribute<AuthorizeAttribute>()!.Policy.Should().Be("AdminOnly");
|
||||
type.GetCustomAttribute<RouteAttribute>()!.Template.Should().Be("offerings/categories");
|
||||
|
||||
var actions = type
|
||||
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
||||
.Where(m => m.GetCustomAttributes<HttpMethodAttribute>().Any())
|
||||
.ToList();
|
||||
|
||||
actions.Should().HaveCount(4);
|
||||
foreach (var action in actions)
|
||||
{
|
||||
action.GetCustomAttribute<AllowAnonymousAttribute>().Should().BeNull(action.Name);
|
||||
(action.GetCustomAttribute<HttpMethodAttribute>()!.Template ?? string.Empty)
|
||||
.Should().NotContainEquivalentOf("admin");
|
||||
}
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
using System.Security.Claims;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SlpModularCms.Modules.Offerings.Controllers;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Controllers;
|
||||
|
||||
public class OfferingsControllerCategoryTests
|
||||
{
|
||||
private readonly IOfferingsService _service = Substitute.For<IOfferingsService>();
|
||||
|
||||
private OfferingsController CreateSut() => new(_service)
|
||||
{
|
||||
ControllerContext = new ControllerContext
|
||||
{
|
||||
HttpContext = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(
|
||||
[new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString())], "TestAuth")),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
private static void AssertInvalidCategoryId(IActionResult result)
|
||||
{
|
||||
var bad = result.Should().BeOfType<BadRequestObjectResult>().Subject;
|
||||
var problem = bad.Value.Should().BeOfType<ValidationProblemDetails>().Subject;
|
||||
problem.Status.Should().Be(400);
|
||||
problem.Errors["categoryId"].Should().Equal("invalid");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Create_Returns400_WithCategoryIdError_WhenTheCategoryDoesNotExist()
|
||||
{
|
||||
_service.CreateAsync(Arg.Any<CreateOfferingRequest>(), Arg.Any<Guid>())
|
||||
.ThrowsAsync(new OfferingCategoryInvalidException());
|
||||
|
||||
var result = await CreateSut().Create(
|
||||
new CreateOfferingRequest("T", "D", "p", "n", ["f"], "c", CategoryId: Guid.NewGuid()));
|
||||
|
||||
AssertInvalidCategoryId(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Update_Returns400_WithCategoryIdError_WhenTheCategoryDoesNotExist()
|
||||
{
|
||||
_service.UpdateAsync(Arg.Any<Guid>(), Arg.Any<UpdateOfferingRequest>(), Arg.Any<Guid>())
|
||||
.ThrowsAsync(new OfferingCategoryInvalidException());
|
||||
|
||||
var result = await CreateSut().Update(
|
||||
Guid.NewGuid(), new UpdateOfferingRequest("T", "D", "p", "n", ["f"], "c", CategoryId: Guid.NewGuid()));
|
||||
|
||||
AssertInvalidCategoryId(result);
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SlpModularCms.Modules.Offerings.Data;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
using SlpModularCms.Modules.Offerings.Repositories;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Repositories;
|
||||
|
||||
public class OfferingCategoryRepositoryTests : IDisposable
|
||||
{
|
||||
private readonly OfferingsDbContext _context;
|
||||
private readonly OfferingCategoryRepository _sut;
|
||||
|
||||
public OfferingCategoryRepositoryTests()
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<OfferingsDbContext>()
|
||||
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
||||
.Options;
|
||||
_context = new OfferingsDbContext(options);
|
||||
_sut = new OfferingCategoryRepository(_context);
|
||||
}
|
||||
|
||||
public void Dispose() => _context.Dispose();
|
||||
|
||||
private static OfferingCategory Category(string name, int order) => new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = name,
|
||||
SortOrder = order,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
private static Offering Offering(string title, Guid? categoryId, bool deleted = false) => new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Slug = title.ToLowerInvariant(),
|
||||
Title = title,
|
||||
Description = "d",
|
||||
Price = "p",
|
||||
PriceNote = "n",
|
||||
Features = ["f"],
|
||||
CtaLabel = "c",
|
||||
CategoryId = categoryId,
|
||||
IsDeleted = deleted,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task ListAsync_OrdersBySortOrderThenName_AndCountsOnlyLiveOfferings()
|
||||
{
|
||||
var onderhoud = Category("Onderhoud", 1);
|
||||
var websites = Category("Websites", 0);
|
||||
await _sut.AddAsync(onderhoud);
|
||||
await _sut.AddAsync(websites);
|
||||
_context.Offerings.AddRange(
|
||||
Offering("A", websites.Id),
|
||||
Offering("B", websites.Id),
|
||||
Offering("Gone", websites.Id, deleted: true),
|
||||
Offering("C", null));
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var result = await _sut.ListAsync();
|
||||
|
||||
result.Select(r => (r.Category.Name, r.OfferingCount)).Should().Equal(("Websites", 2), ("Onderhoud", 0));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("websites")]
|
||||
[InlineData("WEBSITES")]
|
||||
[InlineData("Websites")]
|
||||
public async Task ExistsByNameAsync_IsCaseInsensitive(string probe)
|
||||
{
|
||||
await _sut.AddAsync(Category("Websites", 0));
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
(await _sut.ExistsByNameAsync(probe)).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExistsByNameAsync_IgnoresTheCategoryBeingRenamed()
|
||||
{
|
||||
var websites = Category("Websites", 0);
|
||||
await _sut.AddAsync(websites);
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
(await _sut.ExistsByNameAsync("websites", excludeId: websites.Id)).Should().BeFalse();
|
||||
(await _sut.ExistsByNameAsync("Onderhoud")).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetMaxSortOrderAsync_ReturnsMinusOne_WhenEmpty_ElseTheHighest()
|
||||
{
|
||||
(await _sut.GetMaxSortOrderAsync()).Should().Be(-1);
|
||||
|
||||
await _sut.AddAsync(Category("A", 3));
|
||||
await _sut.AddAsync(Category("B", 7));
|
||||
await _sut.SaveChangesAsync();
|
||||
|
||||
(await _sut.GetMaxSortOrderAsync()).Should().Be(7);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ClearCategoryOnDeletedOfferingsAsync_OnlyReleasesSoftDeletedOfferings()
|
||||
{
|
||||
var websites = Category("Websites", 0);
|
||||
await _sut.AddAsync(websites);
|
||||
var live = Offering("Live", websites.Id);
|
||||
var gone = Offering("Gone", websites.Id, deleted: true);
|
||||
_context.Offerings.AddRange(live, gone);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
await _sut.ClearCategoryOnDeletedOfferingsAsync(websites.Id);
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
|
||||
var all = await _context.Offerings.IgnoreQueryFilters().ToListAsync();
|
||||
all.Single(o => o.Title == "Live").CategoryId.Should().Be(websites.Id);
|
||||
all.Single(o => o.Title == "Gone").CategoryId.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountOfferingsAsync_IgnoresSoftDeletedOfferings()
|
||||
{
|
||||
var websites = Category("Websites", 0);
|
||||
await _sut.AddAsync(websites);
|
||||
_context.Offerings.AddRange(Offering("Live", websites.Id), Offering("Gone", websites.Id, deleted: true));
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
(await _sut.CountOfferingsAsync(websites.Id)).Should().Be(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Modules.Offerings.Data.Entities;
|
||||
using SlpModularCms.Modules.Offerings.Repositories;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Services;
|
||||
|
||||
public class OfferingCategoriesServiceTests
|
||||
{
|
||||
private readonly IOfferingCategoryRepository _repo = Substitute.For<IOfferingCategoryRepository>();
|
||||
|
||||
public OfferingCategoriesServiceTests()
|
||||
{
|
||||
_repo.BeginTransactionAsync().Returns(Substitute.For<IDbContextTransaction>());
|
||||
_repo.GetMaxSortOrderAsync().Returns(-1);
|
||||
}
|
||||
|
||||
private OfferingCategoriesService CreateSut()
|
||||
=> new(_repo, Substitute.For<ILogger<OfferingCategoriesService>>());
|
||||
|
||||
private static OfferingCategory Category(string name, int order = 0) => new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = name,
|
||||
SortOrder = order,
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task ListAsync_ReturnsNamesInRepositoryOrder_WithCounts()
|
||||
{
|
||||
_repo.ListAsync().Returns([
|
||||
new CategoryWithCount(Category("Websites", 0), 2),
|
||||
new CategoryWithCount(Category("Onderhoud", 1), 0),
|
||||
]);
|
||||
|
||||
var result = await CreateSut().ListAsync();
|
||||
|
||||
result.Select(c => (c.Name, c.OfferingCount)).Should().Equal(("Websites", 2), ("Onderhoud", 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAsync_TrimsTheName_AppendsToTheEnd_AndSaves()
|
||||
{
|
||||
_repo.GetMaxSortOrderAsync().Returns(4);
|
||||
|
||||
var dto = await CreateSut().CreateAsync(" Websites ");
|
||||
|
||||
dto.Name.Should().Be("Websites");
|
||||
dto.SortOrder.Should().Be(5);
|
||||
dto.OfferingCount.Should().Be(0);
|
||||
await _repo.Received(1).AddAsync(Arg.Is<OfferingCategory>(c => c.Name == "Websites"));
|
||||
await _repo.Received(1).SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateAsync_Throws_AndSavesNothing_WhenTheNameIsTaken()
|
||||
{
|
||||
_repo.ExistsByNameAsync("Websites").Returns(true);
|
||||
|
||||
var act = () => CreateSut().CreateAsync("Websites");
|
||||
|
||||
await act.Should().ThrowAsync<OfferingCategoryNameConflictException>();
|
||||
await _repo.DidNotReceive().AddAsync(Arg.Any<OfferingCategory>());
|
||||
await _repo.DidNotReceive().SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RenameAsync_ReturnsNull_WhenUnknown()
|
||||
{
|
||||
_repo.GetByIdAsync(Arg.Any<Guid>()).Returns((OfferingCategory?)null);
|
||||
|
||||
var result = await CreateSut().RenameAsync(Guid.NewGuid(), "Nieuw");
|
||||
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RenameAsync_ChangesTheName_AndReportsTheUsage()
|
||||
{
|
||||
var category = Category("Oud");
|
||||
_repo.GetByIdAsync(category.Id).Returns(category);
|
||||
_repo.CountOfferingsAsync(category.Id).Returns(3);
|
||||
|
||||
var dto = await CreateSut().RenameAsync(category.Id, " Nieuw ");
|
||||
|
||||
category.Name.Should().Be("Nieuw");
|
||||
dto!.Name.Should().Be("Nieuw");
|
||||
dto.OfferingCount.Should().Be(3);
|
||||
await _repo.Received(1).SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RenameAsync_Throws_WhenAnotherCategoryHasTheName()
|
||||
{
|
||||
var category = Category("Oud");
|
||||
_repo.GetByIdAsync(category.Id).Returns(category);
|
||||
_repo.ExistsByNameAsync("Websites", category.Id).Returns(true);
|
||||
|
||||
var act = () => CreateSut().RenameAsync(category.Id, "Websites");
|
||||
|
||||
await act.Should().ThrowAsync<OfferingCategoryNameConflictException>();
|
||||
category.Name.Should().Be("Oud");
|
||||
await _repo.DidNotReceive().SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_ReturnsFalse_WhenUnknown()
|
||||
{
|
||||
_repo.GetByIdAsync(Arg.Any<Guid>()).Returns((OfferingCategory?)null);
|
||||
|
||||
(await CreateSut().DeleteAsync(Guid.NewGuid())).Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_Throws_AndDeletesNothing_WhileOfferingsUseTheCategory()
|
||||
{
|
||||
var category = Category("Websites");
|
||||
_repo.GetByIdAsync(category.Id).Returns(category);
|
||||
_repo.CountOfferingsAsync(category.Id).Returns(2);
|
||||
|
||||
var act = () => CreateSut().DeleteAsync(category.Id);
|
||||
|
||||
await act.Should().ThrowAsync<OfferingCategoryInUseException>();
|
||||
_repo.DidNotReceive().Remove(Arg.Any<OfferingCategory>());
|
||||
await _repo.DidNotReceive().SaveChangesAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteAsync_ReleasesSoftDeletedOfferings_ThenRemovesTheCategory()
|
||||
{
|
||||
var category = Category("Websites");
|
||||
_repo.GetByIdAsync(category.Id).Returns(category);
|
||||
_repo.CountOfferingsAsync(category.Id).Returns(0);
|
||||
|
||||
var deleted = await CreateSut().DeleteAsync(category.Id);
|
||||
|
||||
deleted.Should().BeTrue();
|
||||
Received.InOrder(() =>
|
||||
{
|
||||
_repo.ClearCategoryOnDeletedOfferingsAsync(category.Id);
|
||||
_repo.Remove(category);
|
||||
_repo.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class OfferingsServiceOrderTests
|
||||
_repo.BeginTransactionAsync().Returns(Substitute.For<IDbContextTransaction>());
|
||||
}
|
||||
|
||||
private OfferingsService CreateSut() => new(_repo, Substitute.For<ILogger<OfferingsService>>());
|
||||
private OfferingsService CreateSut() => new(_repo, Substitute.For<IOfferingCategoryRepository>(), Substitute.For<ILogger<OfferingsService>>());
|
||||
|
||||
private static Offering New(int order) => new()
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ public class OfferingsServiceTests
|
||||
_repo.BeginTransactionAsync().Returns(Substitute.For<IDbContextTransaction>());
|
||||
}
|
||||
|
||||
private OfferingsService CreateSut() => new(_repo, _logger);
|
||||
private OfferingsService CreateSut() => new(_repo, Substitute.For<IOfferingCategoryRepository>(), _logger);
|
||||
|
||||
private static CreateOfferingRequest CreateRequest(bool featured = false) => new(
|
||||
"Title", "Description", "€ 100", "per month", ["Feature 1"], "Contact", featured);
|
||||
|
||||
Reference in New Issue
Block a user