Skip to content

Commit

Permalink
test: add tests for repository, tables and new validations
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeHB69 committed Sep 12, 2024
1 parent 05c365a commit f5835d9
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace DistributionCenter.Infraestructure.Validators.Extensions;

using System.Globalization;
using System.Text.RegularExpressions;
using Components.Builders.Interfaces;

public static partial class ValidationExtensions
{
public static IValidationBuilder<double> DecimalSize(
this IValidationBuilder<double> builder,
int decimalQuantity,
string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x =>
{
string number = x.ToString(CultureInfo.InvariantCulture);
return Regex.IsMatch(number, $@"^\d+(\.\d{{{decimalQuantity}}})?$");
}, message);
}

public static IValidationBuilder<int> NumberRange(
this IValidationBuilder<int> builder,
uint? min,
uint? max,
string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x => (x >= (min ?? uint.MinValue) && x <= (max ?? uint.MaxValue)), message);
}

public static IValidationBuilder<int> NonNegatives(this IValidationBuilder<int> builder, string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x => x >= 0, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace DistributionCenter.Infraestructure.Validators.Extensions;

using System.Globalization;
using System.Text.RegularExpressions;
using DistributionCenter.Infraestructure.Validators.Components.Builders.Interfaces;

public static class ValidationExtensions
public static partial class ValidationExtensions
{
public static IValidationBuilder<string> WhenNotNull(this IValidationBuilder<string?> builder)
{
Expand Down Expand Up @@ -49,38 +48,4 @@ public static IValidationBuilder<string> EmailValidator(this IValidationBuilder<

return builder.AddRule(static x => Regex.IsMatch(x, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"), message);
}

public static IValidationBuilder<double> DecimalSize(
this IValidationBuilder<double> builder,
int decimalQuantity,
string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x =>
{
string number = x.ToString(CultureInfo.InvariantCulture);
return Regex.IsMatch(number, $@"^\d+(\.\d{{{decimalQuantity}}})?$");
}, message);
}

public static IValidationBuilder<int> NumberRange(
this IValidationBuilder<int> builder,
uint? min,
uint? max,
string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x => (x >= (min ?? uint.MinValue) && x <= (max ?? uint.MaxValue)), message);
}

public static IValidationBuilder<int> NonNegatives(this IValidationBuilder<int> builder, string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.AddRule(x => x >= 0, message);
}
}
3 changes: 3 additions & 0 deletions test/DistributionCenter.Application.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
global using DistributionCenter.Application.Repositories.Concretes;
global using DistributionCenter.Application.Tables.Components.Information.Concretes;
global using DistributionCenter.Application.Tables.Core.Concretes;
global using Moq;
global using Xunit;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace DistributionCenter.Application.Tests.Repositories.Concretes;

using DistributionCenter.Application.Contexts.Interfaces;
using DistributionCenter.Application.Repositories.Concretes;

public class ClientRepositoryTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace DistributionCenter.Application.Tests.Repositories.Concretes;

using DistributionCenter.Application.Contexts.Interfaces;

public class ProductRepositoryTests
{
[Fact]
public void Constructor_InitializesRepository()
{
// Define Input and Output
Mock<IContext> contextMock = new();
ProductRepository repository = new(contextMock.Object);

// Verify actual result
Assert.NotNull(repository);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace DistributionCenter.Application.Tests.Tables.Components.Information.Concretes;

using DistributionCenter.Application.Tables.Components.Information.Concretes;

public class ClientTableInformationTests
{
private readonly ClientTableInformation _table;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace DistributionCenter.Application.Tests.Tables.Components.Information.Concretes;

public class ProductTableInformationTests
{
private readonly ProductTableInformation _table;

public ProductTableInformationTests()
{
_table = new ProductTableInformation();
}

[Fact]
public void TableName_ReturnsExpectedString()
{
// Define Input and Output
string result;
string expected = "product";

// Execute actual operation
result = _table.TableName;

// Verify actual result
Assert.Equal(expected, result);
}

[Fact]
public void GetByIdFields_ReturnsExpectedString()
{
// Define Input and Output
string result;
string expected =
"id AS Id, name, description, weight_gr AS Weight, is_active AS IsActive, created_at AS CreatedAt, updated_at AS UpdatedAt";

// Execute actual operation
result = _table.GetByIdFields;

// Verify actual result
Assert.Equal(expected, result);
}

[Fact]
public void CreateFields_ReturnsExpectedString()
{
// Define Input and Output
string result;
string expected = "id, name, description, weight_gr, is_active, created_at, updated_at";

// Execute actual operation
result = _table.CreateFields;

// Verify actual result
Assert.Equal(expected, result);
}

[Fact]
public void CreateValues_ReturnsExpectedString()
{
// Define Input and Output
string result;
string expected = "@Id, @Name, @Description, @Weight, @IsActive, @CreatedAt, @UpdatedAt";

// Execute actual operation
result = _table.CreateValues;

// Verify actual result
Assert.Equal(expected, result);
}

[Fact]
public void UpdateFields_ReturnsExpectedString()
{
// Define Input and Output
string result;
string expected =
"name = @Name, description = @Description, weight_gr = @Weight, is_active = @IsActive, updated_at = @UpdatedAt";

// Execute actual operation
result = _table.UpdateFields;

// Verify actual result
Assert.Equal(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace DistributionCenter.Application.Tests.Tables.Core.Concretes;

using DistributionCenter.Application.Tables.Components.Information.Concretes;
using DistributionCenter.Application.Tables.Components.Information.Interfaces;
using DistributionCenter.Application.Tables.Connections.Interfaces;
using DistributionCenter.Application.Tables.Core.Concretes;

public class ClientTableTests
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace DistributionCenter.Application.Tests.Tables.Core.Concretes;

using Application.Tables.Components.Information.Interfaces;
using Application.Tables.Connections.Interfaces;

public class ProductTableTests
{
[Fact]
public void GetInformation_ShouldReturnProductTableInformation()
{
// Define Input and Output
Mock<IDbConnectionFactory> mockFactory = new();
ProductTable table = new(mockFactory.Object);

// Execute actual operation
ITableInformation info = table.GetInformation();

// Verify actual result
_ = Assert.IsType<ProductTableInformation>(info);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void CreateProductTest()
// Define Input and output
string expectedName = "Pepsico Zero 2Lts.";
string expectedDescription = "Fresh analcoholic beverage without sugar.";
uint expectedWeight = 15325;
int expectedWeight = 15325;

// Execute actual operation
Product product = new()
Expand All @@ -34,8 +34,8 @@ public void EditProductTest()
string editedName = "Pepsico Zero 3Lts.";
string previousDescription = "Fresh analcoholic beverage without sugar.";
string editedDescription = "Fresh analcoholic beverage without sugar for diabetics.";
uint previousWeight = 234567;
uint editedWeight = 123;
int previousWeight = 234567;
int editedWeight = 123;

Product product = new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void VerifyThatTheDataWasValidatedSuccessfully()
{
Name = "Sh",
Description = "or",
Weight = 532342,
Weight = 5323428,
};

CreateProductDto validDto =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class UpdateProductDtoTests
public void FromEntity_UpdatesAndReturnsCorrectProduct()
{
// Define Input and Output
uint expectedWeight = 100;
int expectedWeight = 100;
Product product =
new()
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public void FromEntity_UpdatesAndReturnsCorrectProduct()
public void FromEntity_UpdatesWithNullsAndReturnsCorrectProduct()
{
// Define Input and Output
uint expectedWeight = 10034;
int expectedWeight = 10034;
Product product =
new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public void VerifyThanProductDescriptionHasLeastThan128Characters()
}

[Fact]
public void VerifyThanProductWeightHasALimitDecimalNumbers()
public void VerifyThanProductWeightHasALimit()
{
// Define Input and Output
CreateProductValidator validator = new();
uint invalidWeight = 1492850;
uint validWeight = 149285;
int invalidWeight = 1492850;
int validWeight = 149285;

// Execute actual operation
CreateProductDto invalidDto = new()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace DistributionCenter.Infraestructure.Tests.Validators;

using Infraestructure.Validators.Components.Builders.Concretes;
using Infraestructure.Validators.Extensions;

public class ValidationExtensionsTests
{
[Fact]
public void CheckedThanTheLimitOfTheDecimalNumbersWasValidated()
{
double validNumber = 1345.52;
double invalidNumber = 1345.5242;
ValidationBuilder<double> validations = new();
_ = validations.DecimalSize(2, "The number couldn't has more than 2 decimal numbers");

Assert.Empty(validations.Validate(validNumber));
Assert.NotEmpty(validations.Validate(invalidNumber));
}
}

0 comments on commit f5835d9

Please sign in to comment.