2023-12-29 11:46:17 +05:00
|
|
|
using System.Net;
|
|
|
|
using AsbCloudApp.Data;
|
2024-01-21 09:38:07 +05:00
|
|
|
using AsbCloudDb.Model;
|
|
|
|
using AsbCloudDb.Model.ProcessMaps;
|
2024-01-19 17:49:09 +05:00
|
|
|
using AsbCloudWebApi.IntegrationTests.Clients;
|
2023-12-29 11:46:17 +05:00
|
|
|
using AsbCloudWebApi.IntegrationTests.TestFakers;
|
|
|
|
using Mapster;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.IntegrationTests.Controllers;
|
|
|
|
|
|
|
|
public class AdminDepositControllerTests : BaseIntegrationTest
|
|
|
|
{
|
2024-01-19 17:49:09 +05:00
|
|
|
protected IAdminDepositClient client;
|
|
|
|
|
|
|
|
public AdminDepositControllerTests(WebAppFactoryFixture factory)
|
|
|
|
: base(factory)
|
|
|
|
{
|
|
|
|
client = factory.GetAuthorizedHttpClient<IAdminDepositClient>();
|
|
|
|
}
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task InsertAsync_ReturnsSuccess_WhenNewItemIsValid()
|
|
|
|
{
|
2024-01-21 09:38:07 +05:00
|
|
|
//arrange
|
|
|
|
var dbset = dbContext.Set<Deposit>();
|
|
|
|
dbset.RemoveRange(dbset);
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
|
|
|
var expectedDto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//act
|
2024-01-19 17:49:09 +05:00
|
|
|
var response = await client.InsertAsync(expectedDto);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//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()
|
|
|
|
{
|
2024-01-21 09:38:07 +05:00
|
|
|
//arrange
|
|
|
|
var dbset = dbContext.Set<Deposit>();
|
|
|
|
dbset.RemoveRange(dbset);
|
|
|
|
dbContext.SaveChanges();
|
2023-12-29 11:46:17 +05:00
|
|
|
|
2024-01-21 09:38:07 +05:00
|
|
|
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
|
|
|
|
|
|
|
|
//act
|
|
|
|
var responce = await client.InsertRangeAsync(new[] { dto });
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//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
|
2024-01-19 17:49:09 +05:00
|
|
|
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//act
|
2024-01-19 17:49:09 +05:00
|
|
|
var response = await client.UpdateAsync(dto);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//assert
|
|
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task UpdateAsync_ReturnsSuccess_WhenUpdatedItemIsValid()
|
|
|
|
{
|
|
|
|
//arrange
|
2024-01-19 17:49:09 +05:00
|
|
|
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
|
|
|
|
var insertResponse = await client.InsertAsync(dto);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
dto.Id = insertResponse.Content;
|
|
|
|
dto.Caption = "Test";
|
|
|
|
dto.Latitude = 50;
|
|
|
|
dto.Longitude = 50;
|
|
|
|
dto.Timezone = new SimpleTimezoneDto
|
|
|
|
{
|
|
|
|
IsOverride = true,
|
|
|
|
Hours = 12
|
|
|
|
};
|
|
|
|
|
|
|
|
//act
|
2024-01-19 17:49:09 +05:00
|
|
|
var response = await client.UpdateAsync(dto);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//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
|
2024-01-19 17:49:09 +05:00
|
|
|
var entity = await dbContext.Deposits.FirstAsync();
|
|
|
|
var expected = entity.Adapt<DepositBaseDto>();
|
2023-12-29 11:46:17 +05:00
|
|
|
|
2024-01-19 17:49:09 +05:00
|
|
|
//act
|
|
|
|
var response = await client.GetOrDefaultAsync(entity.Id);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//assert
|
|
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
|
|
Assert.NotNull(response.Content);
|
|
|
|
|
2024-01-19 17:49:09 +05:00
|
|
|
MatchHelper.Match(expected, response.Content);
|
2023-12-29 11:46:17 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetOrDefaultAsync_ReturnsNoContent_WhenIdIsInvalid()
|
|
|
|
{
|
|
|
|
//arrange
|
|
|
|
const int id = 0;
|
|
|
|
|
|
|
|
//act
|
2024-01-19 17:49:09 +05:00
|
|
|
var responce = await client.GetOrDefaultAsync(id);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//assert
|
|
|
|
Assert.Equal(HttpStatusCode.NoContent, responce.StatusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public async Task GetAllAsync_ReturnsSuccess()
|
|
|
|
{
|
|
|
|
//act
|
2024-01-19 17:49:09 +05:00
|
|
|
var response = await client.GetAllAsync();
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//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
|
2024-01-19 17:49:09 +05:00
|
|
|
var dto = EntitiesFaker.Deposit.Generate().Adapt<DepositBaseDto>();
|
|
|
|
var insertResponse = await client.InsertAsync(dto);
|
2023-12-29 11:46:17 +05:00
|
|
|
var id = insertResponse.Content;
|
|
|
|
|
|
|
|
//act
|
2024-01-19 17:49:09 +05:00
|
|
|
var response = await client.DeleteAsync(id);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//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
|
2024-01-19 17:49:09 +05:00
|
|
|
var response = await client.DeleteAsync(id);
|
2023-12-29 11:46:17 +05:00
|
|
|
|
|
|
|
//assert
|
|
|
|
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
|
|
|
}
|
|
|
|
}
|