135 lines
4.3 KiB
C#
135 lines
4.3 KiB
C#
using Bogus;
|
|
using EnvelopeGenerator.Application;
|
|
using EnvelopeGenerator.Application.Envelopes.Commands;
|
|
using EnvelopeGenerator.Application.Receivers.Commands;
|
|
using EnvelopeGenerator.Application.Users.Commands;
|
|
using EnvelopeGenerator.Infrastructure;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace EnvelopeGenerator.Tests.Application;
|
|
|
|
public class Fake
|
|
{
|
|
public static readonly Faker Provider = new("de");
|
|
|
|
public static Host CreateHost() => Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder()
|
|
.ConfigureAppConfiguration((context, config) =>
|
|
{
|
|
// add appsettings.json
|
|
config.SetBasePath(Directory.GetCurrentDirectory());
|
|
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
|
})
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
IConfiguration configuration = context.Configuration;
|
|
|
|
// add Application and Infrastructure services
|
|
#pragma warning disable CS0618
|
|
services.AddEnvelopeGeneratorServices(configuration);
|
|
services.AddEnvelopeGeneratorInfrastructureServices(
|
|
(sp, options) => options.UseInMemoryDatabase("EnvelopeGeneratorTestDb"),
|
|
context.Configuration
|
|
);
|
|
#pragma warning restore CS0618
|
|
})
|
|
.Build()
|
|
.ToFake();
|
|
|
|
public class Host : IHost
|
|
{
|
|
#region Root
|
|
private readonly IHost _root;
|
|
|
|
public Host(IHost root)
|
|
{
|
|
_root = root;
|
|
}
|
|
|
|
public IServiceProvider Services => _root.Services;
|
|
|
|
public void Dispose()
|
|
{
|
|
_root.Dispose();
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancel = default)
|
|
{
|
|
return _root.StartAsync(cancel);
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken cancel = default)
|
|
{
|
|
return _root.StopAsync(cancel);
|
|
}
|
|
#endregion
|
|
|
|
public IMediator Mediator => Services.GetRequiredService<IMediator>();
|
|
|
|
#region Sample Receivers
|
|
public List<(int Id, string EmailAddress)> _sampleReceivers = new();
|
|
|
|
public IEnumerable<(int Id, string EmailAddress)> SampleReceivers => _sampleReceivers;
|
|
|
|
public async Task AddSampleReceivers()
|
|
{
|
|
var mediator = Mediator;
|
|
foreach (var cmd in Provider.CreateReceiverCommands())
|
|
{
|
|
var (Id, _) = await mediator.Send(cmd);
|
|
_sampleReceivers.Add((Id, cmd.EmailAddress));
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
public static class Extensions
|
|
{
|
|
public static Fake.Host ToFake(this IHost host) => new(host);
|
|
|
|
#region Receiver Command
|
|
public static CreateReceiverCommand CreateReceiverCommand(this Faker fake) => new()
|
|
{
|
|
EmailAddress = fake.Internet.Email(),
|
|
};
|
|
|
|
public static List<CreateReceiverCommand> CreateReceiverCommands(this Faker fake, int minCount = 10, int maxCount = 20)
|
|
=> Enumerable.Range(0, fake.Random.Number(minCount, maxCount))
|
|
.Select(_ => fake.CreateReceiverCommand())
|
|
.ToList();
|
|
#endregion
|
|
|
|
#region Envelope Command
|
|
public static CreateEnvelopeCommand CreateEnvelopeCommand(this Faker fake, int userId) => new()
|
|
{
|
|
Message = fake.Lorem.Paragraph(fake.Random.Number(2, 5)),
|
|
Title = fake.Lorem.Paragraph(fake.Random.Number(1, 2)),
|
|
UserId = userId
|
|
};
|
|
|
|
public static List<CreateEnvelopeCommand> CreateEnvelopeCommands(this Faker fake, params int[] userIDs)
|
|
=> Enumerable.Range(0, userIDs.Length)
|
|
.Select(fake.CreateEnvelopeCommand)
|
|
.ToList();
|
|
#endregion
|
|
|
|
#region User Command
|
|
public static CreateUserCommand CreateUserCommand(this Faker fake) => new()
|
|
{
|
|
Prename = fake.Name.FirstName(),
|
|
Name = fake.Name.LastName(),
|
|
Username = fake.Internet.UserName(),
|
|
Shortname = fake.Random.String2(3, 8),
|
|
Email = fake.Internet.Email()
|
|
};
|
|
|
|
public static List<CreateUserCommand> CreateUserCommands(this Faker fake, int minCount = 10, int maxCount = 20)
|
|
=> Enumerable.Range(0, fake.Random.Number(minCount, maxCount))
|
|
.Select(_ => fake.CreateUserCommand())
|
|
.ToList();
|
|
#endregion
|
|
} |