Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: Replace Dawn.Guard with Throw and refactor domain validations #80

Merged
merged 7 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public void Configure(EntityTypeBuilder<Company> builder)

builder.Property(x => x.Name)
.IsRequired()
.HasMaxLength(100);
.HasMaxLength(Company.NameLength);

builder.Property(x => x.Email)
.IsRequired()
.HasMaxLength(255);
.HasMaxLength(Company.EmailLength);

builder.Property(x => x.WebSiteUrl)
.IsRequired(false)
.HasMaxLength(300);
.HasMaxLength(Company.WebSiteUrlLength);

builder.Property(x => x.Version)
.IsRowVersion();
Expand All @@ -39,19 +39,19 @@ public void Configure(EntityTypeBuilder<Company> builder)
{
b.Property(x => x.Street)
.IsRequired(false)
.HasMaxLength(100);
.HasMaxLength(Address.StreetLength);

b.Property(x => x.City)
.IsRequired(false)
.HasMaxLength(100);
.HasMaxLength(Address.CityLength);

b.Property(x => x.County)
.IsRequired(false)
.HasMaxLength(100);
.HasMaxLength(Address.CountyLength);

b.Property(x => x.PostCode)
.IsRequired(false)
.HasMaxLength(10);
.HasMaxLength(Address.PostCodeLength);

b.HasOne(x => x.Country)
.WithMany()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public override void Configure(EntityTypeBuilder<Country> builder)

builder.Property(x => x.Name)
.IsRequired()
.HasMaxLength(100);
.HasMaxLength(Country.NameLength);

if (CanRunSeed)
builder.HasData(CountrySeed.GetCountries());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public void Configure(EntityTypeBuilder<File> builder)

builder.Property(x => x.Name)
.IsRequired()
.HasMaxLength(300);
.HasMaxLength(File.NameLength);

builder.Property(x => x.Extension)
.IsRequired()
.HasMaxLength(20);
.HasMaxLength(File.ExtensionLength);

builder.Property(x => x.ContentType)
.IsRequired()
.HasMaxLength(50);
.HasMaxLength(File.ContentTypeLength);

builder.Property(x => x.Size)
.IsRequired();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public void Configure(EntityTypeBuilder<Product> builder)

builder.Property(x => x.Title)
.IsRequired()
.HasMaxLength(100);
.HasMaxLength(Product.TitleLength);

builder.Property(x => x.Description)
.IsRequired()
.HasMaxLength(500);
.HasMaxLength(Product.DescriptionLength);

builder.Property(x => x.Price)
.IsRequired()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using FluentAssertions;
using AutoFixture;
using FluentAssertions;
using Monaco.Template.Backend.Application.Features.Company;
using Monaco.Template.Backend.Application.Infrastructure.Context;
using Monaco.Template.Backend.Common.Tests;
using Monaco.Template.Backend.Common.Tests.Factories;
using Monaco.Template.Backend.Domain.Tests.Factories;
using Moq;
using System.Diagnostics.CodeAnalysis;
using Xunit;
Expand All @@ -14,18 +15,23 @@ namespace Monaco.Template.Backend.Application.Tests.Features.Company;
public class CreateCompanyHandlerTests
{
private readonly Mock<AppDbContext> _dbContextMock = new();
private static readonly CreateCompany.Command Command = new(It.IsAny<string>(), // Name
It.IsAny<string>(), // Email
It.IsAny<string>(), // WebsiteUrl
It.IsAny<string>(), // Street
It.IsAny<string>(), // City
It.IsAny<string>(), // County
It.IsAny<string>(), // PostCode
It.IsAny<Guid>()); // CountryId
private static readonly CreateCompany.Command Command;

static CreateCompanyHandlerTests()
{
var fixture = new Fixture();
Command = new(fixture.Create<string>(), // Name
fixture.Create<string>(), // Email
fixture.Create<string>(), // WebsiteUrl
fixture.Create<string>(), // Street
fixture.Create<string>(), // City
fixture.Create<string>(), // County
fixture.Create<string>(), // PostCode
fixture.Create<Guid>()); // CountryId
}

[Theory(DisplayName = "Create new company succeeds")]
[AnonymousData]
[AutoDomainData]
public async Task CreateNewCompanySucceeds(Domain.Model.Country country)
{
_dbContextMock.CreateAndSetupDbSetMock(new List<Domain.Model.Company>(), out var companyDbSetMock)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using FluentAssertions;
using AutoFixture;
using FluentAssertions;
using FluentValidation;
using FluentValidation.TestHelper;
using Monaco.Template.Backend.Application.Features.Company;
using Monaco.Template.Backend.Application.Infrastructure.Context;
using Monaco.Template.Backend.Common.Tests;
using Monaco.Template.Backend.Common.Tests.Factories;
using Monaco.Template.Backend.Domain.Model;
using Monaco.Template.Backend.Domain.Tests.Factories;
using Moq;
using System.Diagnostics.CodeAnalysis;
using Xunit;
Expand All @@ -16,14 +18,20 @@ namespace Monaco.Template.Backend.Application.Tests.Features.Company;
public class CreateCompanyValidatorTests
{
private readonly Mock<AppDbContext> _dbContextMock = new();
private static readonly CreateCompany.Command Command = new(It.IsAny<string>(), // Name
It.IsAny<string>(), // Email
It.IsAny<string>(), // WebsiteUrl
It.IsAny<string>(), // Street
It.IsAny<string>(), // City
It.IsAny<string>(), // County
It.IsAny<string>(), // PostCode
It.IsAny<Guid>()); // CountryId
private static readonly CreateCompany.Command Command;

static CreateCompanyValidatorTests()
{
var fixture = new Fixture();
Command = new(fixture.Create<string>(), // Name
fixture.Create<string>(), // Email
fixture.Create<string>(), // WebsiteUrl
fixture.Create<string>(), // Street
fixture.Create<string>(), // City
fixture.Create<string>(), // County
fixture.Create<string>(), // PostCode
fixture.Create<Guid>()); // CountryId
}

[Fact(DisplayName = "Validator's rule level cascade mode is 'Stop'")]
public void ValidatorRuleLevelCascadeModeIsStop()
Expand All @@ -34,14 +42,12 @@ public void ValidatorRuleLevelCascadeModeIsStop()
}

[Fact(DisplayName = "Name being valid does not generate validation error")]
public async Task NameDoesNotGenerateErrorWhenValid()
public async Task NameValidDoesNotGenerateError()
{
var command = Command with { Name = new string(It.IsAny<char>(), 100) };

_dbContextMock.CreateAndSetupDbSetMock(new List<Domain.Model.Company>());

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.Name));
var validationResult = await sut.TestValidateAsync(Command, s => s.IncludeProperties(x => x.Name));

validationResult.ShouldNotHaveValidationErrorFor(x => x.Name);
}
Expand All @@ -63,20 +69,20 @@ public async Task NameIsEmptyGeneratesError()
[Fact(DisplayName = "Name with long value generates validation error")]
public async Task NameWithLongValueGeneratesError()
{
var command = Command with { Name = new string(It.IsAny<char>(), 101) };
var command = Command with { Name = new string(It.IsAny<char>(), Domain.Model.Company.NameLength + 1) };

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.Name));

validationResult.ShouldHaveValidationErrorFor(x => x.Name)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", 100)
.WithMessageArgument("MaxLength", Domain.Model.Company.NameLength)
.Should()
.HaveCount(1);
}

[Theory(DisplayName = "Name which already exists generates validation error")]
[AnonymousData]
[AutoDomainData]
public async Task NameAlreadyExistsGeneratesError(Domain.Model.Company company)
{
var command = Command with { Name = company.Name };
Expand Down Expand Up @@ -120,7 +126,7 @@ public async Task EmailIsEmptyGeneratesError()
}

[Theory(DisplayName = "Email being invalid generates validation error")]
[AnonymousData]
[AutoDomainData]
public async Task EmailAddressIsInvalidGeneratesError(string email)
{
var command = Command with { Email = email };
Expand All @@ -134,17 +140,38 @@ public async Task EmailAddressIsInvalidGeneratesError(string email)
.HaveCount(1);
}

[Theory(DisplayName = "Email with long value generates validation error")]
[AutoDomainData]
public async Task EmailWithLongValueGeneratesError(string emailDomain)
{
var command = Command with
{
Email = string.Join("@",
new string(It.IsAny<char>(), Domain.Model.Company.EmailLength),
emailDomain)
};

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.Email));

validationResult.ShouldHaveValidationErrorFor(x => x.Email)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", Domain.Model.Company.EmailLength)
.Should()
.HaveCount(1);
}

[Fact(DisplayName = "Website URL with long value generates validation error")]
public async Task WebsiteUrlWithLongValueGeneratesError()
{
var command = Command with { WebSiteUrl = new string(It.IsAny<char>(), 301) };
var command = Command with { WebSiteUrl = new string(It.IsAny<char>(), Domain.Model.Company.WebSiteUrlLength + 1) };

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.WebSiteUrl));

validationResult.ShouldHaveValidationErrorFor(x => x.WebSiteUrl)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", 300)
.WithMessageArgument("MaxLength", Domain.Model.Company.WebSiteUrlLength)
.Should()
.HaveCount(1);
}
Expand All @@ -163,14 +190,14 @@ public async Task WebsiteUrlWithEmptyValueDoesNotGenerateError()
[Fact(DisplayName = "Street with long value generates validation error")]
public async Task StreetWithLongValueGeneratesError()
{
var command = Command with { Street = new string(It.IsAny<char>(), 101) };
var command = Command with { Street = new string(It.IsAny<char>(), Address.StreetLength + 1) };

var validator = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await validator.TestValidateAsync(command, s => s.IncludeProperties(x => x.Street));

validationResult.ShouldHaveValidationErrorFor(x => x.Street)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", 100)
.WithMessageArgument("MaxLength", Address.StreetLength)
.Should()
.HaveCount(1);
}
Expand All @@ -189,14 +216,14 @@ public async Task StreetWithEmptyValueDoesNotGenerateError()
[Fact(DisplayName = "City with long value generates validation error")]
public async Task CityWithLongValueGeneratesError()
{
var command = Command with { City = new string(It.IsAny<char>(), 101) };
var command = Command with { City = new string(It.IsAny<char>(), Address.CityLength + 1) };

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.City));

validationResult.ShouldHaveValidationErrorFor(x => x.City)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", 100)
.WithMessageArgument("MaxLength", Address.CityLength)
.Should()
.HaveCount(1);
}
Expand All @@ -215,14 +242,14 @@ public async Task CityWithEmptyValueDoesNotGenerateError()
[Fact(DisplayName = "County with long value generates validation error")]
public async Task CountyWithLongValueGeneratesError()
{
var command = Command with { County = new string(It.IsAny<char>(), 101) };
var command = Command with { County = new string(It.IsAny<char>(), Address.CountyLength + 1) };

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.County));

validationResult.ShouldHaveValidationErrorFor(x => x.County)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", 100)
.WithMessageArgument("MaxLength", Address.CountyLength)
.Should()
.HaveCount(1);
}
Expand All @@ -241,14 +268,14 @@ public async Task CountyWithEmptyValueDoesNotGenerateError()
[Fact(DisplayName = "Postcode with long value generates validation error")]
public async Task PostcodeWithLongValueGeneratesError()
{
var command = Command with { PostCode = new string(It.IsAny<char>(), 11) };
var command = Command with { PostCode = new string(It.IsAny<char>(), Address.PostCodeLength + 1) };

var sut = new CreateCompany.Validator(_dbContextMock.Object);
var validationResult = await sut.TestValidateAsync(command, s => s.IncludeProperties(x => x.PostCode));

validationResult.ShouldHaveValidationErrorFor(x => x.PostCode)
.WithErrorCode("MaximumLengthValidator")
.WithMessageArgument("MaxLength", 10)
.WithMessageArgument("MaxLength", Address.PostCodeLength)
.Should()
.HaveCount(1);
}
Expand All @@ -265,7 +292,7 @@ public async Task PostcodeWithEmptyValueDoesNotGenerateError()
}

[Theory(DisplayName = "Country being valid does not generate validation error")]
[AnonymousData(true)]
[AutoDomainData(true)]
public async Task CountryIsValidDoesNotGenerateError(Domain.Model.Country country)
{
var command = Command with { CountryId = country.Id };
Expand Down Expand Up @@ -328,7 +355,7 @@ public async Task CountryWithNullValueGeneratesErrorWhenAddressFieldsPresent()
}

[Theory(DisplayName = "Country that doesn't exist generates validation error")]
[AnonymousData]
[AutoDomainData]
public async Task CountryMustExistValidation(Domain.Model.Country country)
{
var command = Command with { CountryId = Guid.NewGuid() };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
using Monaco.Template.Backend.Application.Services.Contracts;
#endif
using Monaco.Template.Backend.Common.Tests;
using Monaco.Template.Backend.Common.Tests.Factories;
#if (filesSupport)
using Monaco.Template.Backend.Domain.Model;
#endif
using Moq;
using System.Diagnostics.CodeAnalysis;
using Monaco.Template.Backend.Domain.Tests.Factories;
using Xunit;

namespace Monaco.Template.Backend.Application.Tests.Features.Company;
Expand All @@ -23,13 +23,13 @@ namespace Monaco.Template.Backend.Application.Tests.Features.Company;
public class DeleteCompanyHandlerTests
{
private readonly Mock<AppDbContext> _dbContextMock = new();
private static readonly DeleteCompany.Command Command = new(It.IsAny<Guid>());
private static readonly DeleteCompany.Command Command = new(new Fixture().Create<Guid>());
#if (filesSupport)
private readonly Mock<IFileService> _fileServiceMock = new();
#endif

[Theory(DisplayName = "Delete company succeeds")]
[AnonymousData(true)]
[AutoDomainData(true)]
#if (filesSupport)
public async Task DeleteCompanySucceeds(IFixture fixture, Domain.Model.Product[] products)
#else
Expand Down
Loading