Skip to content

Commit

Permalink
test: ✅ add some test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdihadeli committed Sep 8, 2024
1 parent 4773337 commit a13b01f
Show file tree
Hide file tree
Showing 48 changed files with 465 additions and 371 deletions.
314 changes: 157 additions & 157 deletions .github/workflows/app-version.yml

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions food-delivery-microservices.sln
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodDelivery.Services.Ident
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodDelivery.Services.Identity.TestShared", "tests\Services\Identity\FoodDelivery.Services.Identity.TestShared\FoodDelivery.Services.Identity.TestShared.csproj", "{D12A7726-3B12-4CCD-A3F4-4ABDD28BD44C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodDelivery.Services.Identity.DependencyTests", "tests\Services\Identity\FoodDelivery.Services.Identity.DependencyTests\FoodDelivery.Services.Identity.DependencyTests.csproj", "{12AB9357-D6EE-404A-AADB-7D31BBFAFCD7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -812,6 +814,10 @@ Global
{D12A7726-3B12-4CCD-A3F4-4ABDD28BD44C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D12A7726-3B12-4CCD-A3F4-4ABDD28BD44C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D12A7726-3B12-4CCD-A3F4-4ABDD28BD44C}.Release|Any CPU.Build.0 = Release|Any CPU
{12AB9357-D6EE-404A-AADB-7D31BBFAFCD7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12AB9357-D6EE-404A-AADB-7D31BBFAFCD7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12AB9357-D6EE-404A-AADB-7D31BBFAFCD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12AB9357-D6EE-404A-AADB-7D31BBFAFCD7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -968,6 +974,7 @@ Global
{D9A193CB-020A-4367-A954-6D4EDAD9E327} = {A9B8E9D0-8E3C-4495-B8FE-CBBAE3D46E62}
{84A67508-47F8-41D8-95D8-3493FF35554A} = {A9B8E9D0-8E3C-4495-B8FE-CBBAE3D46E62}
{D12A7726-3B12-4CCD-A3F4-4ABDD28BD44C} = {A9B8E9D0-8E3C-4495-B8FE-CBBAE3D46E62}
{12AB9357-D6EE-404A-AADB-7D31BBFAFCD7} = {A9B8E9D0-8E3C-4495-B8FE-CBBAE3D46E62}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AD0585D6-CBA4-4818-86D8-0D914F18E390}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

namespace BuildingBlocks.Core.Extensions.ServiceCollection;

Expand Down Expand Up @@ -233,29 +234,44 @@ params Assembly[] assembliesToScan
)
{
var scanAssemblies = assembliesToScan.Length != 0 ? assembliesToScan : [Assembly.GetExecutingAssembly(),];
var exceptions = new List<string>();

// for resolving scoped based dependencies without errors
using var scope = rootServiceProvider.CreateScope();
var sp = scope.ServiceProvider;

foreach (var serviceDescriptor in services)
{
// Skip services that are not typically resolved directly or are special cases
if (
serviceDescriptor.ServiceType == typeof(IHostedService)
|| serviceDescriptor.ServiceType == typeof(IApplicationLifetime)
)
{
continue;
}

try
{
var serviceType = serviceDescriptor.ServiceType;
if (scanAssemblies.Contains(serviceType.Assembly))
sp.GetRequiredService(serviceType);
{
// Attempt to resolve the service
var service = sp.GetService(serviceType);

// Assert: Check that the service was resolved if it's not meant to be optional
if (
serviceDescriptor.ImplementationInstance == null
&& serviceDescriptor.ImplementationFactory == null
)
{
service.NotBeNull();
}
}
}
catch (System.Exception e)
catch (System.Exception ex)
{
exceptions.Add($"Unable to resolve '{serviceDescriptor.ServiceType.FullName}', detail: {e.Message}");
throw new($"Failed to resolve service {serviceDescriptor.ServiceType.FullName}: {ex.Message}", ex);
}
}

if (exceptions.Count != 0)
{
throw new System.Exception(string.Join("\n", exceptions));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static T NotBeNull<T>([NotNull] this T? argument, System.Exception except
return argument;
}

public static string NotBeEmpty(
public static string NotBeInvalid(
this string argument,
[CallerArgumentExpression("argument")] string? argumentName = null
)
Expand Down Expand Up @@ -74,7 +74,7 @@ public static string NotBeNullOrWhiteSpace(
return argument;
}

public static Guid NotBeEmpty(
public static Guid NotBeInvalid(
this Guid argument,
[CallerArgumentExpression("argument")] string? argumentName = null
)
Expand All @@ -87,7 +87,7 @@ public static Guid NotBeEmpty(
return argument;
}

public static Guid NotBeEmpty(
public static Guid NotBeInvalid(
[NotNull] this Guid? argument,
[CallerArgumentExpression("argument")] string? argumentName = null
)
Expand All @@ -97,15 +97,15 @@ public static Guid NotBeEmpty(
throw new ValidationException(message: $"{argumentName} cannot be null or empty.");
}

return argument.Value.NotBeEmpty();
return argument.Value.NotBeInvalid();
}

public static int NotBeNegativeOrZero(
this int argument,
[CallerArgumentExpression("argument")] string? argumentName = null
)
{
if (argument == 0)
if (argument <= 0)
{
throw new ValidationException($"{argumentName} cannot be zero.");
}
Expand All @@ -131,9 +131,9 @@ public static long NotBeNegativeOrZero(
[CallerArgumentExpression("argument")] string? argumentName = null
)
{
if (argument == 0)
if (argument <= 0)
{
throw new ValidationException($"{argumentName} cannot be zero.");
throw new ValidationException($"{argumentName} cannot be negative or zero.");
}

return argument;
Expand All @@ -157,9 +157,9 @@ public static decimal NotBeNegativeOrZero(
[CallerArgumentExpression("argument")] string? argumentName = null
)
{
if (argument == 0)
if (argument <= 0)
{
throw new ValidationException($"{argumentName} cannot be zero.");
throw new ValidationException($"{argumentName} cannot be negative or zero.");
}

return argument;
Expand All @@ -183,7 +183,7 @@ public static double NotBeNegativeOrZero(
[CallerArgumentExpression("argument")] string? argumentName = null
)
{
if (argument == 0)
if (argument <= 0)
{
throw new ValidationException($"{argumentName} cannot be zero.");
}
Expand Down Expand Up @@ -277,29 +277,31 @@ public static TEnum NotBeEmptyOrNull<TEnum>(
throw new ValidationException(message: $"{argumentName} cannot be null or empty.");
}

enumValue.NotBeEmpty();
enumValue.NotBeInvalid();

return enumValue;
}

public static TEnum NotBeEmpty<TEnum>(
public static TEnum NotBeInvalid<TEnum>(
[NotNull] this TEnum enumValue,
[CallerArgumentExpression("enumValue")] string? argumentName = null
)
where TEnum : Enum
{
enumValue.NotBeNull();
if (enumValue.Equals(default(TEnum)))

// returns `true` if `enumValue` corresponds to one of the defined values in `TEnum`
if (!Enum.IsDefined(typeof(TEnum), enumValue))
{
throw new ValidationException(
$"The value of '{argumentName}' cannot be the default value of '{typeof(TEnum).Name}' enum."
$"The value of '{argumentName}' is not valid for enum of '{typeof(TEnum).Name}'."
);
}

return enumValue;
}

public static void NotBeEmpty(
public static void NotBeInvalid(
this DateTime dateTime,
[CallerArgumentExpression("dateTime")] string? argumentName = null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public record MachineInstanceInfo : IMachineInstanceInfo
public MachineInstanceInfo(Guid clientId, string? clientGroup)
{
clientGroup.NotBeNullOrWhiteSpace();
clientId.NotBeEmpty();
clientId.NotBeInvalid();

ClientId = clientId;
ClientGroup = clientGroup;
Expand Down
11 changes: 11 additions & 0 deletions src/BuildingBlocks/BuildingBlocks.Core/Web/Environments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace BuildingBlocks.Core.Web;

public static class Environments
{
public const string Development = "Development";
public const string Staging = "Staging";
public const string Production = "Production";
public const string Test = "Test";
public const string Docker = "Docker";
public const string DependencyTest = "DependencyTest";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace BuildingBlocks.Core.Web.Extensions;

public static class HostEnvironmentExtensions
{
public static bool IsTest(this IHostEnvironment env) => env.IsEnvironment("test");
public static bool IsTest(this IHostEnvironment env) => env.IsEnvironment("Test");

public static bool IsDocker(this IHostEnvironment env) => env.IsEnvironment("docker");
public static bool IsDependencyTest(this IHostEnvironment env) => env.IsEnvironment("DependencyTest");

public static bool IsDocker(this IHostEnvironment env) => env.IsEnvironment("Docker");
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,9 @@

var app = builder.Build();

if (app.Environment.IsDevelopment() || app.Environment.IsTest())
if (app.Environment.IsDependencyTest())
{
app.Services.ValidateDependencies(
builder.Services,
typeof(CatalogsMetadata).Assembly,
Assembly.GetExecutingAssembly()
);
return;
}

/*----------------- Module Middleware Setup ------------------*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ public static CreateProduct Of(
Name.Of(name),
Price.Of(price),
Stock.Of(stock, restockThreshold, maxStockThreshold),
status.NotBeEmpty(),
status.NotBeInvalid(),
productType,
Dimensions.Of(width, height, depth),
Size.Of(size),
color.NotBeEmpty(),
color.NotBeInvalid(),
CategoryId.Of(categoryId),
SupplierId.Of(supplierId),
BrandId.Of(brandId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public static ProductCreated Of(
stock.Available,
stock.RestockThreshold,
stock.MaxStockThreshold,
status.NotBeEmpty(),
status.NotBeInvalid(),
dimensions.Width,
dimensions.Height,
dimensions.Depth,
size,
color.NotBeEmpty(),
color.NotBeInvalid(),
categoryId,
supplierId,
brandId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ public void ChangeProductDetail(
name.NotBeNull();
Name = name;

status.NotBeEmpty();
status.NotBeInvalid();
ProductStatus = status;

productType.NotBeEmpty();
productType.NotBeInvalid();
ProductType = productType;

dimensions.NotBeNull();
Expand All @@ -149,7 +149,7 @@ public void ChangeProductDetail(
size.NotBeNull();
Size = size;

color.NotBeEmpty();
color.NotBeInvalid();
Color = color;

// input validation will do in the command and our value objects, here we just do business validation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace FoodDelivery.Services.Customers.Api;

public class CustomersApiMetadata { }
public class CustomersApiMetadata;
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,9 @@

var app = builder.Build();

if (app.Environment.IsDevelopment() || app.Environment.IsTest())
if (app.Environment.IsDependencyTest())
{
// app.Services.ValidateDependencies(
// builder.Services,
// typeof(CustomersMetadata).Assembly,
// Assembly.GetExecutingAssembly()
// );
return;
}

/*----------------- Module Middleware Setup ------------------*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FoodDelivery.Services.Customers.Customers.Features.UpdatingCustomer.v1;
using FoodDelivery.Services.Customers.Customers.Features.UpdatingCustomer.v1.Events.Domain;
using FoodDelivery.Services.Customers.Customers.Features.UpdatingCustomer.v1.Read.Mongo;
using FoodDelivery.Services.Customers.Customers.Models.Reads;
using Riok.Mapperly.Abstractions;

namespace FoodDelivery.Services.Customers.Customers;
Expand Down Expand Up @@ -112,16 +113,28 @@ internal static partial class CustomersModuleMapping
)]
internal static partial UpdateCustomerRead ToUpdateCustomerRead(this Models.Customer customer);

[MapProperty(nameof(UpdateCustomerRead.CustomerId), nameof(Models.Reads.Customer.CustomerId))]
[MapProperty(nameof(UpdateCustomerRead.Id), nameof(Models.Reads.Customer.Id))]
[MapProperty(nameof(UpdateCustomerRead.OccurredOn), nameof(Models.Reads.Customer.Created))]
internal static partial Models.Reads.Customer ToCustomer(this UpdateCustomerRead updateCustomerRead);

// https://mapperly.riok.app/docs/configuration/existing-target/
[MapProperty(nameof(UpdateCustomerRead.CustomerId), nameof(Models.Reads.Customer.CustomerId))]
[MapProperty(nameof(UpdateCustomerRead.Id), nameof(Models.Reads.Customer.Id))]
[MapProperty(nameof(UpdateCustomerRead.OccurredOn), nameof(Models.Reads.Customer.Created))]
internal static partial void ToCustomer(this UpdateCustomerRead updateCustomerRead, Models.Reads.Customer customer);
// Todo: doesn't map correctly
internal static Models.Reads.Customer ToCustomer(this UpdateCustomerRead updateCustomerRead)
{
return new Customer
{
Created = updateCustomerRead.OccurredOn,
Email = updateCustomerRead.Email,
CustomerId = updateCustomerRead.CustomerId,
IdentityId = updateCustomerRead.IdentityId,
FirstName = updateCustomerRead.FirstName,
LastName = updateCustomerRead.LastName,
FullName = updateCustomerRead.FullName,
PhoneNumber = updateCustomerRead.PhoneNumber,
Country = updateCustomerRead.Country,
City = updateCustomerRead.City,
DetailAddress = updateCustomerRead.DetailAddress,
Nationality = updateCustomerRead.Nationality,
BirthDate = updateCustomerRead.BirthDate,
Id = updateCustomerRead.Id
};
}

[MapperIgnoreTarget(nameof(UpdateCustomerRead.Id))]
[MapProperty(nameof(CustomerUpdated.Id), nameof(UpdateCustomerRead.CustomerId))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BuildingBlocks.Abstractions.Commands;
using BuildingBlocks.Core.Domain.ValueObjects;
using BuildingBlocks.Core.Exception.Types;
using BuildingBlocks.Core.Extensions;
using BuildingBlocks.Core.IdsGenerator;
using BuildingBlocks.Validation.Extensions;
Expand Down Expand Up @@ -58,6 +59,8 @@ public async Task<CreateCustomerResult> Handle(CreateCustomer command, Cancellat
throw new CustomerAlreadyExistsException($"Customer with email '{command.Email}' already exists.");

var identityUser = await identityApiClient.GetUserByEmailAsync(command.Email, cancellationToken);
if (identityUser is null)
throw new NotFoundAppException($"user with email {command.Email} not found.");

var customer = Customer.Create(
CustomerId.Of(command.Id),
Expand Down
Loading

0 comments on commit a13b01f

Please sign in to comment.