123 lines
3.7 KiB
C#
123 lines
3.7 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Application;
|
|
using EnvelopeGenerator.Application.Dto.Receiver;
|
|
using EnvelopeGenerator.Application.Histories.Commands;
|
|
using EnvelopeGenerator.Application.Histories.Queries;
|
|
using EnvelopeGenerator.Domain;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Infrastructure;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
namespace EnvelopeGenerator.Tests.Application;
|
|
|
|
[TestFixture]
|
|
public class HistoryTests
|
|
{
|
|
private IHost _host;
|
|
|
|
private IMediator Mediator => _host.Services.GetRequiredService<IMediator>();
|
|
|
|
[SetUp]
|
|
public async Task Setup()
|
|
{
|
|
_host = 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();
|
|
|
|
// set receivers
|
|
var _rcvRepo = _host.Services.GetRequiredService<IRepository<Receiver>>();
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_host.Dispose();
|
|
}
|
|
|
|
[Test]
|
|
public async Task CreateHistory_And_ReadHistory_Should_Work()
|
|
{
|
|
// Arrange
|
|
var createCmd = new CreateHistoryCommand
|
|
{
|
|
EnvelopeId = 1,
|
|
UserReference = "UserA",
|
|
Status = Constants.EnvelopeStatus.EnvelopeCreated,
|
|
Comment = "First create"
|
|
};
|
|
|
|
// Act
|
|
var id = await Mediator.Send(createCmd);
|
|
|
|
// Assert
|
|
Assert.That(id, Is.Not.Null);
|
|
|
|
// ReadHistory query
|
|
var query = new ReadHistoryQuery(1);
|
|
var result = await Mediator.Send(query);
|
|
|
|
Assert.That(result, Is.Not.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReadHistory_Should_Filter_By_Status()
|
|
{
|
|
// Arrange
|
|
var createCmd1 = new CreateHistoryCommand
|
|
{
|
|
EnvelopeId = 2,
|
|
UserReference = "UserX",
|
|
Status = Constants.EnvelopeStatus.EnvelopeCreated
|
|
};
|
|
|
|
var createCmd2 = new CreateHistoryCommand
|
|
{
|
|
EnvelopeId = 2,
|
|
UserReference = "UserX",
|
|
Status = Constants.EnvelopeStatus.EnvelopePartlySigned
|
|
};
|
|
|
|
await Mediator.Send(createCmd1);
|
|
await Mediator.Send(createCmd2);
|
|
|
|
// Act
|
|
var result = await Mediator.Send(new ReadHistoryQuery(2, Constants.EnvelopeStatus.EnvelopePartlySigned));
|
|
|
|
// Assert
|
|
Assert.That(result, Has.Exactly(1).Items);
|
|
Assert.That(result, Has.All.Matches<EnvelopeGenerator.Application.Dto.EnvelopeHistory.EnvelopeHistoryDto>(
|
|
r => r.Status == Constants.EnvelopeStatus.EnvelopePartlySigned));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ReadHistory_Should_Return_Empty_When_No_Record()
|
|
{
|
|
// Act
|
|
var result = await Mediator.Send(new ReadHistoryQuery(999));
|
|
|
|
// Assert
|
|
Assert.That(result, Is.Empty);
|
|
}
|
|
} |