Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Nov 29, 2024
1 parent c0014fe commit 2533b94
Show file tree
Hide file tree
Showing 36 changed files with 72 additions and 82 deletions.
3 changes: 3 additions & 0 deletions CleanAspCore.Api.TestUtils/CleanAspCore.Api.TestUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<ItemGroup>
<PackageReference Include="Bogus" Version="35.6.1"/>
<PackageReference Include="NUnit" Version="4.2.2"/>
<PackageReference Include="Testcontainers.MsSql" Version="4.0.0"/>
<PackageReference Include="Respawn" Version="6.2.1"/>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Extensions.ObjectPool;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal sealed class DatabasePool
public sealed class DatabasePool
{
private readonly ObjectPool<IDatabase> _pool;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal static class DbContextExtensions
public static class DbContextExtensions
{
public static MigrationScript[] GenerateMigrationScripts(this DbContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal sealed class DbContextMigrationInitializer<TDbContext> : IDatabaseInitializer
public sealed class DbContextMigrationInitializer<TDbContext> : IDatabaseInitializer
where TDbContext : DbContext, new()
{
private int _counter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Extensions.Hosting;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal interface IDatabase
public interface IDatabase
{
string ConnectionString { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Extensions.Hosting;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal interface IDatabaseInitializer
public interface IDatabaseInitializer
{
void Initialize(IHost app);
string GetUniqueDataBaseName();
Expand Down
6 changes: 6 additions & 0 deletions CleanAspCore.Api.TestUtils/DataBaseSetup/MigrationScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

public sealed record MigrationScript(string FromMigration, string ToMigration, string UpScript, string DownScript)
{
public override string ToString() => ToMigration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using DotNet.Testcontainers.Containers;
using Testcontainers.MsSql;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal static class MsSqlContainerExtensions
public static class MsSqlContainerExtensions
{
public static async Task<ExecResult> ExecScriptAsync(this MsSqlContainer container, string scriptContent, string database, CancellationToken ct = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Respawn;
using Testcontainers.MsSql;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal sealed class MsSqlDatabase : IDatabase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Respawn;
using Testcontainers.MsSql;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal sealed class MsSqlDatabasePoolPolicy : IPooledObjectPolicy<IDatabase>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.ObjectPool;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal sealed class PooledDatabase : IDisposable
public sealed class PooledDatabase : IDisposable
{
private readonly IDatabase _database;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
using Respawn;
using Testcontainers.MsSql;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal static class ServiceCollectionExtensions
public static class ServiceCollectionExtensions
{
public static void RegisterSharedDatabaseServices(this IServiceCollection services)
{
services.AddSingleton<DatabasePool>();
}

public static void RegisterPostgreSqlContainer(this IServiceCollection services)
public static void RegisterSqlContainer(this IServiceCollection services)
{
services.RegisterSharedDatabaseServices();
services.AddTransient<RespawnerOptions>(c => new RespawnerOptions { DbAdapter = DbAdapter.SqlServer });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using Microsoft.Extensions.Logging;
using Testcontainers.MsSql;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal sealed class SqlMigrator
public sealed class SqlMigrator
{
private readonly MsSqlContainer _container;
private readonly ILogger _logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.DataBaseSetup;

internal static class TaskExtensions
{
Expand Down
5 changes: 5 additions & 0 deletions CleanAspCore.Api.TestUtils/Fakers/FakerConfigurator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Bogus;

namespace CleanAspCore.Api.TestUtils.Fakers;

public delegate Faker<T> FakerConfigurator<T>(Faker<T> t) where T : class;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.Extensions.Logging;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.Logging;

internal static class LoggingBuilderExtensions
public static class LoggingBuilderExtensions
{
public static ILoggingBuilder AddNunitLogging(this ILoggingBuilder services)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.Logging;

internal sealed class NunitLogger(TextWriter output, string name) : ILogger, IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using NUnit.Framework;

namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils.Logging;

internal sealed class NunitLoggerProvider : ILoggerProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanAspCore.Api.Tests.TestSetup;
namespace CleanAspCore.Api.TestUtils;

internal sealed record TestScenario<TInput>
public sealed record TestScenario<TInput>
{
public TInput Input { get; }
private string Name { get; }
Expand Down
4 changes: 0 additions & 4 deletions CleanAspCore.Api.Tests/CleanAspCore.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0"/>

<PackageReference Include="Testcontainers.MsSql" Version="4.0.0"/>
<PackageReference Include="Respawn" Version="6.2.1"/>

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
<PackageReference Include="NUnit" Version="4.2.2"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0"/>
Expand Down
1 change: 1 addition & 0 deletions CleanAspCore.Api.Tests/Data/MigrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using CleanAspCore.Api.TestUtils.DataBaseSetup;
using CleanAspCore.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Departments;
namespace CleanAspCore.Api.Tests.Endpoints.Departments;

internal sealed class AddDepartmentsTests : TestBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Departments;
namespace CleanAspCore.Api.Tests.Endpoints.Departments;

internal sealed class GetDepartmentByIdTests : TestBase
{
Expand Down
29 changes: 14 additions & 15 deletions CleanAspCore.Api.Tests/Endpoints/Employees/CreateEmployeeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CleanAspCore.Api.Endpoints.Employees;
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Employees;

Expand Down Expand Up @@ -30,20 +29,20 @@ public async Task CreateEmployee_IsAdded()

private static readonly TestScenario<(FakerConfigurator<CreateEmployeeRequest>, string[])>[] _validationCases =
[
new("FirstName is null",
(x => x.RuleFor(y => y.FirstName, (string?)null), ["FirstName"])),
new("LastName is null",
(x => x.RuleFor(y => y.LastName, (string?)null), ["LastName"])),
new("Gender is null",
(x => x.RuleFor(y => y.Gender, (string?)null), ["Gender"])),
new("Email is null",
(x => x.RuleFor(y => y.Email, (string?)null), ["Email"])),
new("Invalid email",
(x => x.RuleFor(y => y.Email, "this is not a valid email address"), ["Email"])),
new("Job does not exist",
(x => x.RuleFor(y => y.JobId, Guid.NewGuid()), ["JobId"])),
new("Department does not exist",
(x => x.RuleFor(y => y.DepartmentId, Guid.NewGuid()), ["DepartmentId"])),
new((string)"FirstName is null",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.FirstName, (string?)null), ["FirstName"])),
new((string)"LastName is null",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.LastName, (string?)null), ["LastName"])),
new((string)"Gender is null",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.Gender, (string?)null), ["Gender"])),
new((string)"Email is null",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.Email, (string?)null), ["Email"])),
new((string)"Invalid email",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.Email, "this is not a valid email address"), ["Email"])),
new((string)"Job does not exist",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.JobId, Guid.NewGuid()), ["JobId"])),
new((string)"Department does not exist",
((FakerConfigurator<CreateEmployeeRequest>, string[]))(x => x.RuleFor(y => y.DepartmentId, Guid.NewGuid()), ["DepartmentId"])),
];

[TestCaseSource(nameof(_validationCases))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Employees;
namespace CleanAspCore.Api.Tests.Endpoints.Employees;

internal sealed class DeleteEmployeeByIdTests : TestBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Employees;
namespace CleanAspCore.Api.Tests.Endpoints.Employees;

internal sealed class GetEmployeeByIdTests : TestBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Employees;
namespace CleanAspCore.Api.Tests.Endpoints.Employees;

internal sealed class GetEmployees : TestBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CleanAspCore.Api.Endpoints.Employees;
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Employees;

Expand Down
2 changes: 0 additions & 2 deletions CleanAspCore.Api.Tests/Endpoints/Jobs/CreateJobTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Jobs;

internal sealed class CreateJobTests : TestBase
Expand Down
4 changes: 1 addition & 3 deletions CleanAspCore.Api.Tests/Endpoints/Jobs/GetJobByIdTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using CleanAspCore.Api.TestUtils.Fakers;

namespace CleanAspCore.Api.Tests.Endpoints.Jobs;
namespace CleanAspCore.Api.Tests.Endpoints.Jobs;

internal sealed class GetJobByIdTests : TestBase
{
Expand Down
6 changes: 4 additions & 2 deletions CleanAspCore.Api.Tests/GlobalSetup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CleanAspCore.Data;
using CleanAspCore.Api.TestUtils.DataBaseSetup;
using CleanAspCore.Api.TestUtils.Logging;
using CleanAspCore.Data;
using Microsoft.Extensions.DependencyInjection;

[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
Expand All @@ -19,7 +21,7 @@ public void RunBeforeAnyTests()
var services = new ServiceCollection();

services.AddLogging(x => x.AddNunitLogging());
services.RegisterPostgreSqlContainer();
services.RegisterSqlContainer();
services.AddScoped<TestWebApi>();
services.RegisterMigrationInitializer<HrContext>();
_serviceProvider = services.BuildServiceProvider();
Expand Down
2 changes: 2 additions & 0 deletions CleanAspCore.Api.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
global using System.Net;
global using System.Net.Http.Json;
global using CleanAspCore.Api.Tests.TestSetup;
global using CleanAspCore.Api.TestUtils;
global using CleanAspCore.Api.TestUtils.Fakers;
global using FluentAssertions;
global using NUnit.Framework;
5 changes: 0 additions & 5 deletions CleanAspCore.Api.Tests/TestSetup/FakerConfigurator.cs

This file was deleted.

6 changes: 0 additions & 6 deletions CleanAspCore.Api.Tests/TestSetup/MigrationScript.cs

This file was deleted.

1 change: 1 addition & 0 deletions CleanAspCore.Api.Tests/TestSetup/TestWebApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http.Headers;
using System.Security.Claims;
using CleanAspCore.Api.TestUtils.DataBaseSetup;
using CleanAspCore.Data;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
Expand Down
2 changes: 1 addition & 1 deletion CleanAspCore.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using CleanAspCore.Core.Common.Telemetry;
using CleanAspCore.Data;

var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateSlimBuilder(args);

builder.AddOpenApiServices();
builder.AddAuthServices();
Expand Down

0 comments on commit 2533b94

Please sign in to comment.