DD.WellWorkover.Cloud/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTests.cs
Степанов Дмитрий 88ce24b8bb Интеграционные тесты
1. Создание инфраструктуры для интегарционных тестов
2. Покрытие контроллера месторождений тестами
2023-12-29 11:46:17 +05:00

186 lines
4.8 KiB
C#

using System.Net;
using AsbCloudApp.Data;
using AsbCloudWebApi.IntegrationTests.TestFakers;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace AsbCloudWebApi.IntegrationTests.Controllers;
public class AdminDepositControllerTests : BaseIntegrationTest
{
public AdminDepositControllerTests(WebAppFactoryFixture factory)
: base(factory)
{
}
[Fact]
public async Task InsertAsync_ReturnsSuccess_WhenNewItemIsValid()
{
//arrange
var expectedDto = DepositTestFaker.GetDeposit().Adapt<DepositBaseDto>();
//act
var response = await adminDepositClient.InsertAsync(expectedDto);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Content > 0);
var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == response.Content);
var actualDto = entity?.Adapt<DepositBaseDto>();
var excludeProps = new[] { nameof(DepositBaseDto.Id) };
MatchHelper.Match(expectedDto, actualDto, excludeProps);
}
[Fact]
public async Task InsertRangeAsync_ReturnsSuccess_WhenAllNewItemsIsValid()
{
//arrange
var dto = DepositTestFaker.GetDeposit().Adapt<DepositBaseDto>();
//act
var responce = await adminDepositClient.InsertRangeAsync(new[] { dto });
//assert
Assert.Equal(HttpStatusCode.OK, responce.StatusCode);
Assert.Equal(1, responce.Content);
var entity = await dbContext.Deposits.OrderBy(d => d.Id).LastOrDefaultAsync();
var deposit = entity?.Adapt<DepositBaseDto>();
var excludeProps = new[] { nameof(DepositBaseDto.Id) };
MatchHelper.Match(dto, deposit, excludeProps);
}
[Fact]
public async Task UpdateAsync_ReturnsBadRequest_WhenUpdatedItemHasInvalidId()
{
//arrange
var dto = DepositTestFaker.GetDeposit().Adapt<DepositBaseDto>();
//act
var response = await adminDepositClient.UpdateAsync(dto);
//assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
[Fact]
public async Task UpdateAsync_ReturnsSuccess_WhenUpdatedItemIsValid()
{
//arrange
var dto = DepositTestFaker.GetDeposit().Adapt<DepositBaseDto>();
var insertResponse = await adminDepositClient.InsertAsync(dto);
dto.Id = insertResponse.Content;
dto.Caption = "Test";
dto.Latitude = 50;
dto.Longitude = 50;
dto.Timezone = new SimpleTimezoneDto
{
IsOverride = true,
Hours = 12
};
//act
var response = await adminDepositClient.UpdateAsync(dto);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Content > 0);
var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == response.Content);
var deposit = entity?.Adapt<DepositBaseDto>();
MatchHelper.Match(dto, deposit);
}
[Fact]
public async Task GetOrDefaultAsync_ReturnsSuccess_WhenIdIsValid()
{
//arrange
var dto = DepositTestFaker.GetDeposit().Adapt<DepositBaseDto>();
var insertResponse = await adminDepositClient.InsertAsync(dto);
var id = insertResponse.Content;
//act
var response = await adminDepositClient.GetOrDefaultAsync(id);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == response.Content.Id);
var deposit = entity?.Adapt<DepositBaseDto>();
var excludeProps = new[] { nameof(DepositBaseDto.Id) };
MatchHelper.Match(dto, deposit, excludeProps);
}
[Fact]
public async Task GetOrDefaultAsync_ReturnsNoContent_WhenIdIsInvalid()
{
//arrange
const int id = 0;
//act
var responce = await adminDepositClient.GetOrDefaultAsync(id);
//assert
Assert.Equal(HttpStatusCode.NoContent, responce.StatusCode);
}
[Fact]
public async Task GetAllAsync_ReturnsSuccess()
{
//act
var response = await adminDepositClient.GetAllAsync();
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
var expectedCount = await dbContext.Deposits.CountAsync();
Assert.Equal(expectedCount, response.Content.Count());
var entity = await dbContext.Deposits.FirstOrDefaultAsync();
var dto = entity?.Adapt<DepositBaseDto>();
MatchHelper.Match(dto, response.Content.FirstOrDefault());
}
[Fact]
public async Task DeleteAsync_ReturnsSuccess_WhenIdIsValid()
{
//arrange
var dto = DepositTestFaker.GetDeposit().Adapt<DepositBaseDto>();
var insertResponse = await adminDepositClient.InsertAsync(dto);
var id = insertResponse.Content;
//act
var response = await adminDepositClient.DeleteAsync(id);
//assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(response.Content > 0);
var entity = await dbContext.Deposits.FirstOrDefaultAsync(d => d.Id == dto.Id);
Assert.Null(entity);
}
[Fact]
public async Task DeleteAsync_ReturnsNoContent_WhenIdIsInvalid()
{
//arrange
const int id = 0;
//act
var response = await adminDepositClient.DeleteAsync(id);
//assert
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
}
}