Skip to content

Commit

Permalink
solve all warnings, use container reuse for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed May 9, 2024
1 parent e855cb6 commit 1065dd1
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 50 deletions.
1 change: 1 addition & 0 deletions CleanAspCore.Api.Tests/Data/MigrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public async Task MigrationsUpAndDown_NoErrors2(MigrationScript migration)
upResult.ExitCode.Should().Be(0, $"Error during migration up2: {upResult2.Stderr}");
}

[SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes")]
private sealed class MigrationTestCases : IEnumerable
{
public IEnumerator GetEnumerator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task CreateEmployee_InvalidRequest_ReturnsBadRequest()
{
//Arrange
var createEmployeeRequest = new CreateEmployeeRequestFaker()
.RuleFor(x => x.FirstName, string.Empty)
.RuleFor(x => x.FirstName, (string?)null)
.Generate();

Sut.SeedData(context =>
Expand Down
1 change: 1 addition & 0 deletions CleanAspCore.Api.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
global using FluentAssertions;
global using NUnit.Framework;
global using CleanAspCore.Api.Tests.TestSetup;
global using System.Diagnostics.CodeAnalysis;
5 changes: 1 addition & 4 deletions CleanAspCore.Api.Tests/TestSetup/PooledDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public void EnsureDatabaseIsReadyForTest(IHost host)
{
_database.EnsureInitialized(host);
// Clean the database before and not after the test so that after a test is run you can inspect the database.
Utils.RunWithoutSynchronizationContext(() =>
{
_database.Clean().GetAwaiter().GetResult();
});
_database.Clean().RunSynchronouslyWithoutSynchronizationContext();
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public static void RegisterPostgreSqlContainer(this IServiceCollection services)
});
services.AddTransient<IPooledObjectPolicy<IDatabase>, PostgreSqlDatabasePoolPolicy>();

var container = new PostgreSqlBuilder().Build();
Utils.RunWithoutSynchronizationContext(() => container.StartAsync().Wait());
var container = new PostgreSqlBuilder()
.WithReuse(true)
.Build();
container.StartAsync().RunSynchronouslyWithoutSynchronizationContext();

services.AddSingleton(container);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace CleanAspCore.Api.Tests.TestSetup;

public static class Utils
public static class TaskExtensions
{
public static void RunWithoutSynchronizationContext(Action action)
public static void RunSynchronouslyWithoutSynchronizationContext(this Task task)
{
// Capture the current synchronization context so we can restore it later.
// We don't have to be afraid of other threads here as this is a ThreadStatic.
var synchronizationContext = SynchronizationContext.Current;
try
{
SynchronizationContext.SetSynchronizationContext(null);
action();
task.GetAwaiter().GetResult();
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void ValidateNullableReferences<TModel>(this AbstractValidator<TMo
{
IEnumerable<PropertyInfo> properties = GetNonNullableProperties<TModel>(new NullabilityInfoContext());

validator.ApplyRuleToProperties(new GenericNotNullOrEmptyRule(), properties);
validator.ApplyRuleToProperties(new GenericNotNullRule(), properties);
}

private static IEnumerable<PropertyInfo> GetNonNullableProperties<TModel>(NullabilityInfoContext nullabilityInfoContext) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanAspCore.Extensions.FluentValidation;

public class GenericNotNullOrEmptyRule : IGenericRule
public class GenericNotNullRule : IGenericRule
{
public void ApplyRule<T, TProperty>(IRuleBuilderInitial<T, TProperty> builder) => builder.NotNull().NotEmpty();
public void ApplyRule<T, TProperty>(IRuleBuilderInitial<T, TProperty> builder) => builder.NotNull();
}
16 changes: 8 additions & 8 deletions CleanAspCore/Features/Departments/Endpoints/AddDepartments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public sealed class CreateDepartmentRequest
public required string City { get; init; }
}

public sealed class CreateDepartmentRequestValidator : AbstractValidator<CreateDepartmentRequest>
{
public CreateDepartmentRequestValidator()
{
this.ValidateNullableReferences();
}
}

internal static class AddDepartments
{
public static async Task<CreatedAtRoute> Handle(HrContext context, CreateDepartmentRequest createDepartmentRequest, CancellationToken cancellationToken)
Expand All @@ -29,12 +37,4 @@ public static async Task<CreatedAtRoute> Handle(HrContext context, CreateDepartm
Name = department.Name,
City = department.City
};

private sealed class CreateDepartmentRequestValidator : AbstractValidator<CreateDepartmentRequest>
{
public CreateDepartmentRequestValidator()
{
this.ValidateNullableReferences();
}
}
}
20 changes: 10 additions & 10 deletions CleanAspCore/Features/Employees/Endpoints/AddEmployee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public sealed class CreateEmployeeRequest
public Guid JobId { get; init; }
}

public sealed class CreateEmployeeRequestValidator : AbstractValidator<CreateEmployeeRequest>
{
public CreateEmployeeRequestValidator()
{
this.ValidateNullableReferences();

RuleFor(x => x.Email).EmailAddress();
}
}

internal static class AddEmployee
{
internal static async Task<CreatedAtRoute> Handle([FromBody] CreateEmployeeRequest request, HrContext context, [FromServices] IValidator<CreateEmployeeRequest> validator,
Expand All @@ -38,14 +48,4 @@ internal static async Task<CreatedAtRoute> Handle([FromBody] CreateEmployeeReque
DepartmentId = employee.DepartmentId,
JobId = employee.JobId
};

private sealed class CreateEmployeeRequestValidator : AbstractValidator<CreateEmployeeRequest>
{
public CreateEmployeeRequestValidator()
{
this.ValidateNullableReferences();

RuleFor(x => x.Email).EmailAddress();
}
}
}
20 changes: 10 additions & 10 deletions CleanAspCore/Features/Employees/Endpoints/UpdateEmployeeById.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public sealed class UpdateEmployeeRequest
public Guid? JobId { get; init; }
}

public class UpdateEmployeeRequestValidator : AbstractValidator<UpdateEmployeeRequest>
{
public UpdateEmployeeRequestValidator()
{
this.ValidateNullableReferences();

RuleFor(x => x.Email).EmailAddress();
}
}

internal static class UpdateEmployeeById
{
internal static async Task<Results<NoContent, NotFound>> Handle(
Expand All @@ -42,13 +52,3 @@ internal static async Task<Results<NoContent, NotFound>> Handle(
};
}
}

public class UpdateEmployeeRequestValidator : AbstractValidator<UpdateEmployeeRequest>
{
public UpdateEmployeeRequestValidator()
{
this.ValidateNullableReferences();

RuleFor(x => x.Email).EmailAddress();
}
}
16 changes: 8 additions & 8 deletions CleanAspCore/Features/Jobs/Endpoints/AddJobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public sealed class CreateJobRequest
public required string Name { get; init; }
}

public sealed class CreateJobRequestValidator : AbstractValidator<CreateJobRequest>
{
public CreateJobRequestValidator()
{
this.ValidateNullableReferences();
}
}

internal static class AddJobs
{
internal static async Task<CreatedAtRoute> Handle([FromBody] CreateJobRequest createJobRequest, HrContext context, CancellationToken cancellationToken)
Expand All @@ -27,12 +35,4 @@ internal static async Task<CreatedAtRoute> Handle([FromBody] CreateJobRequest cr
Id = Guid.NewGuid(),
Name = createJobRequest.Name
};

private sealed class CreateJobRequestValidator : AbstractValidator<CreateJobRequest>
{
public CreateJobRequestValidator()
{
this.ValidateNullableReferences();
}
}
}
2 changes: 1 addition & 1 deletion CleanAspCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme);

builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), includeInternalTypes: true);
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
builder.Services.AddDbContext<HrContext>();

var app = builder.Build();
Expand Down

0 comments on commit 1065dd1

Please sign in to comment.