feat(api): add integration tests for the DELETE
This commit is contained in:
@@ -42,7 +42,162 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
private readonly SqliteConnection _eventStoreDbContextSqlite = new(ConnectionString);
|
||||
private readonly SqliteConnection _writeDbContextSqlite = new(ConnectionString);
|
||||
|
||||
#region POST /api/customer/
|
||||
#region GET: /api/customer/
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsHttpStatus200Ok_When_GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var queryModels = new Faker<CustomerQueryModel>()
|
||||
.UsePrivateConstructor()
|
||||
.RuleFor(queryModel => queryModel.Id, faker => faker.Random.Guid())
|
||||
.RuleFor(queryModel => queryModel.FirstName, faker => faker.Person.FirstName)
|
||||
.RuleFor(queryModel => queryModel.LastName, faker => faker.Person.LastName)
|
||||
.RuleFor(queryModel => queryModel.Email, faker => faker.Person.Email)
|
||||
.RuleFor(queryModel => queryModel.Gender, faker => faker.PickRandom<EGender>().ToString())
|
||||
.RuleFor(queryModel => queryModel.DateOfBirth, faker => faker.Person.DateOfBirth)
|
||||
.Generate(10);
|
||||
|
||||
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
|
||||
readOnlyRepository.GetAllAsync().Returns(queryModels);
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(services =>
|
||||
{
|
||||
services.RemoveAll<ICustomerReadOnlyRepository>();
|
||||
services.AddScoped(_ => readOnlyRepository);
|
||||
});
|
||||
|
||||
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
|
||||
|
||||
// Act
|
||||
using var act = await httpClient.GetAsync(Endpoint);
|
||||
|
||||
// Assert (HTTP)
|
||||
act.Should().NotBeNull();
|
||||
act.IsSuccessStatusCode.Should().BeTrue();
|
||||
act.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
// Assert (HTTP Content Response)
|
||||
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse<IEnumerable<CustomerQueryModel>>>();
|
||||
response.Should().NotBeNull();
|
||||
response.Success.Should().BeTrue();
|
||||
response.StatusCode.Should().Be(StatusCodes.Status200OK);
|
||||
response.Errors.Should().BeEmpty();
|
||||
response.Result.Should().NotBeNullOrEmpty()
|
||||
.And.OnlyHaveUniqueItems()
|
||||
.And.HaveCount(queryModels.Count)
|
||||
.And.AllSatisfy(model =>
|
||||
{
|
||||
model.Id.Should().NotBeEmpty();
|
||||
model.FirstName.Should().NotBeNullOrWhiteSpace();
|
||||
model.LastName.Should().NotBeNullOrWhiteSpace();
|
||||
model.Email.Should().NotBeNullOrWhiteSpace();
|
||||
model.Gender.Should().NotBeNullOrWhiteSpace();
|
||||
model.FullName.Should().NotBeNullOrWhiteSpace();
|
||||
});
|
||||
|
||||
await readOnlyRepository.Received(1).GetAllAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DELETE: /api/customer/{id}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsHttpStatus200Ok_When_Delete_ValidRequest()
|
||||
{
|
||||
// Arrange
|
||||
var customer = new Faker<Customer>()
|
||||
.CustomInstantiator(faker =>
|
||||
{
|
||||
var emailResult = Email.Create(faker.Person.Email);
|
||||
return new Customer(
|
||||
faker.Person.FirstName,
|
||||
faker.Person.LastName,
|
||||
faker.PickRandom<EGender>(),
|
||||
emailResult.Value,
|
||||
faker.Person.DateOfBirth);
|
||||
})
|
||||
.Generate();
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServiceScope: serviceScope =>
|
||||
{
|
||||
var writeDbContext = serviceScope.ServiceProvider.GetRequiredService<WriteDbContext>();
|
||||
writeDbContext.Customers.Add(customer);
|
||||
writeDbContext.SaveChanges();
|
||||
});
|
||||
|
||||
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
|
||||
|
||||
// Act
|
||||
using var act = await httpClient.DeleteAsync($"{Endpoint}/{customer.Id}");
|
||||
|
||||
// Assert (HTTP)
|
||||
act.Should().NotBeNull();
|
||||
act.IsSuccessStatusCode.Should().BeTrue();
|
||||
act.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
// Assert (HTTP Content Response)
|
||||
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse>();
|
||||
response.Should().NotBeNull();
|
||||
response.Success.Should().BeTrue();
|
||||
response.StatusCode.Should().Be(StatusCodes.Status200OK);
|
||||
response.Errors.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsHttpStatus400BadRequest_When_Delete_InvalidRequest()
|
||||
{
|
||||
// Arrange
|
||||
await using var webApplicationFactory = InitializeWebAppFactory();
|
||||
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
|
||||
|
||||
// Act
|
||||
using var act = await httpClient.DeleteAsync($"{Endpoint}/{Guid.Empty}");
|
||||
|
||||
// Assert (HTTP)
|
||||
act.Should().NotBeNull();
|
||||
act.IsSuccessStatusCode.Should().BeFalse();
|
||||
act.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
|
||||
// Assert (HTTP Content Response)
|
||||
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse>();
|
||||
response.Should().NotBeNull();
|
||||
response.Success.Should().BeFalse();
|
||||
response.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
|
||||
response.Errors.Should().NotBeNullOrEmpty().And.OnlyHaveUniqueItems();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsStatus404NotFound_When_Delete_NonExistingCustomer()
|
||||
{
|
||||
// Arrange
|
||||
var customerId = Guid.NewGuid();
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory();
|
||||
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
|
||||
|
||||
// Act
|
||||
using var act = await httpClient.DeleteAsync($"{Endpoint}/{customerId}");
|
||||
|
||||
// Assert (HTTP)
|
||||
act.Should().NotBeNull();
|
||||
act.IsSuccessStatusCode.Should().BeFalse();
|
||||
act.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
|
||||
// Assert (HTTP Content Response)
|
||||
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse>();
|
||||
response.Should().NotBeNull();
|
||||
response.Success.Should().BeFalse();
|
||||
response.StatusCode.Should().Be(StatusCodes.Status404NotFound);
|
||||
response.Errors.Should().NotBeNullOrEmpty()
|
||||
.And.OnlyHaveUniqueItems()
|
||||
.And.AllSatisfy(error => error.Message.Should().Be($"No customer found by Id: {customerId}"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region POST: /api/customer/
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsHttpStatus200Ok_When_Post_ValidRequest()
|
||||
@@ -113,19 +268,19 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
{
|
||||
// Arrange
|
||||
var customer = new Faker<Customer>()
|
||||
.CustomInstantiator(faker =>
|
||||
{
|
||||
var emailResult = Email.Create(faker.Person.Email);
|
||||
return new Customer(
|
||||
.CustomInstantiator(faker =>
|
||||
{
|
||||
var emailResult = Email.Create(faker.Person.Email);
|
||||
return new Customer(
|
||||
faker.Person.FirstName,
|
||||
faker.Person.LastName,
|
||||
faker.PickRandom<EGender>(),
|
||||
emailResult.Value,
|
||||
faker.Person.DateOfBirth);
|
||||
})
|
||||
.Generate();
|
||||
})
|
||||
.Generate();
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServiceScope: (serviceScope) =>
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServiceScope: serviceScope =>
|
||||
{
|
||||
var writeDbContext = serviceScope.ServiceProvider.GetRequiredService<WriteDbContext>();
|
||||
writeDbContext.Customers.Add(customer);
|
||||
@@ -165,66 +320,7 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
|
||||
#endregion
|
||||
|
||||
#region GET /api/customer/
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsHttpStatus200Ok_When_GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var queryModels = new Faker<CustomerQueryModel>()
|
||||
.UsePrivateConstructor()
|
||||
.RuleFor(queryModel => queryModel.Id, faker => faker.Random.Guid())
|
||||
.RuleFor(queryModel => queryModel.FirstName, faker => faker.Person.FirstName)
|
||||
.RuleFor(queryModel => queryModel.LastName, faker => faker.Person.LastName)
|
||||
.RuleFor(queryModel => queryModel.Email, faker => faker.Person.Email)
|
||||
.RuleFor(queryModel => queryModel.Gender, faker => faker.PickRandom<EGender>().ToString())
|
||||
.RuleFor(queryModel => queryModel.DateOfBirth, faker => faker.Person.DateOfBirth)
|
||||
.Generate(10);
|
||||
|
||||
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
|
||||
readOnlyRepository.GetAllAsync().Returns(queryModels);
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
|
||||
{
|
||||
services.RemoveAll<ICustomerReadOnlyRepository>();
|
||||
services.AddScoped(_ => readOnlyRepository);
|
||||
});
|
||||
|
||||
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
|
||||
|
||||
// Act
|
||||
using var act = await httpClient.GetAsync(Endpoint);
|
||||
|
||||
// Assert (HTTP)
|
||||
act.Should().NotBeNull();
|
||||
act.IsSuccessStatusCode.Should().BeTrue();
|
||||
act.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
// Assert (HTTP Content Response)
|
||||
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse<IEnumerable<CustomerQueryModel>>>();
|
||||
response.Should().NotBeNull();
|
||||
response.Success.Should().BeTrue();
|
||||
response.StatusCode.Should().Be(StatusCodes.Status200OK);
|
||||
response.Errors.Should().BeEmpty();
|
||||
response.Result.Should().NotBeNullOrEmpty()
|
||||
.And.OnlyHaveUniqueItems()
|
||||
.And.HaveCount(queryModels.Count)
|
||||
.And.AllSatisfy(model =>
|
||||
{
|
||||
model.Id.Should().NotBeEmpty();
|
||||
model.FirstName.Should().NotBeNullOrWhiteSpace();
|
||||
model.LastName.Should().NotBeNullOrWhiteSpace();
|
||||
model.Email.Should().NotBeNullOrWhiteSpace();
|
||||
model.Gender.Should().NotBeNullOrWhiteSpace();
|
||||
model.FullName.Should().NotBeNullOrWhiteSpace();
|
||||
});
|
||||
|
||||
await readOnlyRepository.Received(1).GetAllAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GET /api/customer/{id}
|
||||
#region GET: /api/customer/{id}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_ReturnsHttpStatus200Ok_When_GetById_ValidRequest()
|
||||
@@ -243,7 +339,7 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
|
||||
readOnlyRepository.GetByIdAsync(Arg.Any<Guid>()).Returns(queryModel);
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(services =>
|
||||
{
|
||||
services.RemoveAll<ICustomerReadOnlyRepository>();
|
||||
services.AddScoped(_ => readOnlyRepository);
|
||||
@@ -278,13 +374,13 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_eturnsHttpStatus400BadRequest_When_GetById_InvalidRequest()
|
||||
public async Task Should_ReturnsHttpStatus400BadRequest_When_GetById_InvalidRequest()
|
||||
{
|
||||
// Arrange
|
||||
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
|
||||
readOnlyRepository.GetByIdAsync(Arg.Any<Guid>()).Returns((CustomerQueryModel)null);
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(services =>
|
||||
{
|
||||
services.RemoveAll<ICustomerReadOnlyRepository>();
|
||||
services.AddScoped(_ => readOnlyRepository);
|
||||
@@ -320,7 +416,7 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
|
||||
readOnlyRepository.GetByIdAsync(Arg.Any<Guid>()).Returns((CustomerQueryModel)null);
|
||||
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
|
||||
await using var webApplicationFactory = InitializeWebAppFactory(services =>
|
||||
{
|
||||
services.RemoveAll<ICustomerReadOnlyRepository>();
|
||||
services.AddScoped(_ => readOnlyRepository);
|
||||
@@ -397,8 +493,12 @@ public class CustomersControllerTests : IAsyncLifetime
|
||||
services.RemoveAll<NoSqlDbContext>();
|
||||
services.RemoveAll<ISynchronizeDb>();
|
||||
|
||||
services.AddDbContext<WriteDbContext>(options => options.UseSqlite(_writeDbContextSqlite));
|
||||
services.AddDbContext<EventStoreDbContext>(options => options.UseSqlite(_eventStoreDbContextSqlite));
|
||||
services.AddDbContext<WriteDbContext>(
|
||||
options => options.UseSqlite(_writeDbContextSqlite));
|
||||
|
||||
services.AddDbContext<EventStoreDbContext>(
|
||||
options => options.UseSqlite(_eventStoreDbContextSqlite));
|
||||
|
||||
services.AddSingleton(_ => Substitute.For<IReadDbContext>());
|
||||
services.AddSingleton(_ => Substitute.For<ISynchronizeDb>());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user