mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 09:01:18 +03:00
nice refactorings
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
using static PracticeCalendar.UnitTests.Integration.Testing;
|
||||
|
||||
namespace PracticeCalendar.UnitTests.Integration
|
||||
{
|
||||
public class BaseTest
|
||||
{
|
||||
public BaseTest()
|
||||
{
|
||||
ResetState();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PracticeCalendar.Infrastructure.Persistence;
|
||||
|
||||
namespace PracticeCalendar.UnitTests.Integration
|
||||
{
|
||||
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
|
||||
{
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.ConfigureAppConfiguration(configurationBuilder =>
|
||||
{
|
||||
var integrationConfig = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
configurationBuilder.AddConfiguration(integrationConfig);
|
||||
});
|
||||
|
||||
builder.ConfigureServices((builder, services) =>
|
||||
{
|
||||
services.Remove<DbContextOptions<ApplicationDbContext>>();
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseInMemoryDatabase("InMemoryDbForTesting")
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using FluentAssertions;
|
||||
using PracticeCalendar.Application.PracticeEvents.Commands;
|
||||
using PracticeCalendar.Application.PracticeEvents.Queries;
|
||||
|
||||
using static PracticeCalendar.UnitTests.Integration.Testing;
|
||||
|
||||
namespace PracticeCalendar.UnitTests.Integration.PracticeEvents
|
||||
{
|
||||
public class CreatePracticeEventsTest : BaseTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task ShouldCreatePracticeEvent()
|
||||
{
|
||||
await RunBeforeAnyTests();
|
||||
|
||||
var query = new CreatePracticeEventCommand(new PracticeEventDto
|
||||
{
|
||||
Title = "Some title",
|
||||
Description = "Some desc",
|
||||
StartTime = DateTime.Now,
|
||||
EndTime = DateTime.Now,
|
||||
Attendees = {
|
||||
new AttendeeDto
|
||||
{
|
||||
Name = "Claudiu F",
|
||||
EmailAddress = "claudiuf@somewhere.com"
|
||||
},
|
||||
new AttendeeDto
|
||||
{
|
||||
Name = "Claudiu F 2",
|
||||
EmailAddress = "claudiuf2@somewhere.com"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var result = await SendAsync(query);
|
||||
|
||||
result.Should().NotBeNull();
|
||||
result.Id.Should().NotBe(0);
|
||||
result.Attendees.Count.Should().Be(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using FluentAssertions;
|
||||
using PracticeCalendar.Application.PracticeEvents.Queries.GetPracticeEvents;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
|
||||
using static PracticeCalendar.UnitTests.Integration.Testing;
|
||||
|
||||
namespace PracticeCalendar.UnitTests.Integration.PracticeEvents
|
||||
{
|
||||
public class GetPracticeEventsTest : BaseTest
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldReturnZeroResult()
|
||||
{
|
||||
await RunBeforeAnyTests();
|
||||
|
||||
var query = new GetPracticeEventsQuery();
|
||||
|
||||
var result = await SendAsync(query);
|
||||
|
||||
result.Count.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldReturnAllListsAndItems()
|
||||
{
|
||||
await RunBeforeAnyTests();
|
||||
|
||||
await AddAsync(new PracticeEvent("Test Event", "Event description")
|
||||
{
|
||||
Id = 1,
|
||||
Attendees = {
|
||||
new Attendee("Claudiu F", "claudiuf@busybee.com")
|
||||
{
|
||||
Id = 1
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var query = new GetPracticeEventsQuery();
|
||||
|
||||
var result = await SendAsync(query);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().Attendees.Should().HaveCount(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace PracticeCalendar.UnitTests.Integration
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection Remove<TService>(this IServiceCollection services)
|
||||
{
|
||||
var serviceDescriptor = services.FirstOrDefault(d =>
|
||||
d.ServiceType == typeof(TService));
|
||||
|
||||
if (serviceDescriptor != null)
|
||||
{
|
||||
services.Remove(serviceDescriptor);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PracticeCalendar.Infrastructure.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
|
||||
namespace PracticeCalendar.UnitTests.Integration
|
||||
{
|
||||
public partial class Testing
|
||||
{
|
||||
private static WebApplicationFactory<Program> _factory = null!;
|
||||
private static IConfiguration _configuration = null!;
|
||||
private static IServiceScopeFactory _scopeFactory = null!;
|
||||
|
||||
public static async Task RunBeforeAnyTests()
|
||||
{
|
||||
_factory = new CustomWebApplicationFactory();
|
||||
_scopeFactory = _factory.Services.GetRequiredService<IServiceScopeFactory>();
|
||||
_configuration = _factory.Services.GetRequiredService<IConfiguration>();
|
||||
}
|
||||
|
||||
public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
|
||||
var mediator = scope.ServiceProvider.GetRequiredService<ISender>();
|
||||
|
||||
return await mediator.Send(request);
|
||||
}
|
||||
|
||||
public static async Task<TEntity?> FindAsync<TEntity>(params object[] keyValues)
|
||||
where TEntity : class
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
return await context.FindAsync<TEntity>(keyValues);
|
||||
}
|
||||
|
||||
public static async Task AddAsync<TEntity>(TEntity entity)
|
||||
where TEntity : class
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
context.Add(entity);
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public static async Task<int> CountAsync<TEntity>() where TEntity : class
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
|
||||
return await context.Set<TEntity>().CountAsync();
|
||||
}
|
||||
|
||||
public static void ResetState()
|
||||
{
|
||||
if (_scopeFactory != null)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
context.Set<Attendee>().RemoveRange(context.Set<Attendee>().ToList());
|
||||
context.Set<PracticeEvent>().RemoveRange(context.Set<PracticeEvent>().ToList());
|
||||
context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user