Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions FinancialTracker.Tests/Controllers/CategoriesControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,169 @@ public async Task Add_DuplicateCategory_ReturnsConflict()
var conflictResult = Assert.IsType<ConflictObjectResult>(result.Result);
Assert.Equal(409, conflictResult.StatusCode);
}

[Fact]
public async Task GetAll_WithCategories_ReturnsOkResult()
{
List<CategoryResponseDto> categories =
[
new() { Id = 1, Name = "Продукты"},
new() { Id = 2, Name = "Транспорт"},
new() { Id = 3, Name = "Разное"},
];

_categoryServiceMock
.Setup(service => service.GetAllAsync())
.ReturnsAsync(categories);

var result = await _controller.GetAll();

var okResult = Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(200, okResult.StatusCode);

var actualCategories = Assert.IsType<List<CategoryResponseDto>>(okResult.Value);
Assert.Equal(categories.Count, actualCategories.Count);
Assert.Equal(categories[0].Name, actualCategories[0].Name);
Assert.Equal(categories[1].Name, actualCategories[1].Name);
Assert.Equal(categories[2].Name, actualCategories[2].Name);
}

[Fact]
public async Task GetAll_EmptyList_ReturnsOkWithEmptyList()
{
var emptyList = new List<CategoryResponseDto>();

_categoryServiceMock
.Setup(service => service.GetAllAsync())
.ReturnsAsync(emptyList);

var result = await _controller.GetAll();

var okResult = Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(200, okResult.StatusCode);

var actualCategories = Assert.IsType<List<CategoryResponseDto>>(okResult.Value);
Assert.Empty(actualCategories);
}

[Fact]
public async Task GetAll_ServiceThrowsException_ReturnsServerError()
{
const string errorMessage = "Внутренняя ошибка сервера при получении категорий";

_categoryServiceMock
.Setup(service => service.GetAllAsync())
.ThrowsAsync(new Exception(errorMessage));

var result = await _controller.GetAll();

var statusCodeResult = Assert.IsType<ObjectResult>(result.Result);
Assert.Equal(500, statusCodeResult.StatusCode);
}

[Fact]
public async Task GetById_ExistingCategory_ReturnsOk()
{
var categoryId = 1;
var expectedCategory = new CategoryResponseDto { Id = categoryId, Name = "Продукты" };

_categoryServiceMock
.Setup(service => service.GetByIdAsync(categoryId))
.ReturnsAsync(expectedCategory);

var result = await _controller.GetById(categoryId);

var okResult = Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(200, okResult.StatusCode);

var actualCategory = Assert.IsType<CategoryResponseDto>(okResult.Value);
Assert.Equal(expectedCategory.Id, actualCategory.Id);
Assert.Equal(expectedCategory.Name, actualCategory.Name);
}

[Fact]
public async Task GetById_NonExistingCategory_ReturnsNotFound()
{
const int categoryId = 999;

_categoryServiceMock
.Setup(service => service.GetByIdAsync(categoryId))
.ReturnsAsync((CategoryResponseDto)null!);

var result = await _controller.GetById(categoryId);

var notFoundResult = Assert.IsType<NotFoundObjectResult>(result.Result);
Assert.Equal(404, notFoundResult.StatusCode);
}

[Fact]
public async Task Update_ValidCategory_ReturnsOk()
{
var updateDto = new CategoryUpdateDto { Id = 1, Name = "Продукты обновленные" };
var expectedCategory = new CategoryResponseDto { Id = 1, Name = "Продукты обновленные" };

_categoryServiceMock
.Setup(service => service.UpdateAsync(updateDto))
.ReturnsAsync(expectedCategory);

var result = await _controller.Update(updateDto);

var okResult = Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(200, okResult.StatusCode);

var actualCategory = Assert.IsType<CategoryResponseDto>(okResult.Value);
Assert.Equal(expectedCategory.Id, actualCategory.Id);
Assert.Equal(expectedCategory.Name, actualCategory.Name);
}

[Fact]
public async Task Update_DuplicateCategoryName_ReturnsConflict()
{
var updateDto = new CategoryUpdateDto { Id = 1, Name = "Дубликат" };
const string errorMessage = "Попытка обновления категории на дублирующее имя: 1 -> Дубликат";

_categoryServiceMock
.Setup(service => service.UpdateAsync(updateDto))
.ThrowsAsync(new InvalidOperationException(errorMessage));

var result = await _controller.Update(updateDto);

var conflictResult = Assert.IsType<ConflictObjectResult>(result.Result);
Assert.Equal(409, conflictResult.StatusCode);
}

[Fact]
public async Task Delete_ExistingCategory_ReturnsOk()
{
const int categoryId = 1;
var expectedCategory = new CategoryResponseDto { Id = categoryId, Name = "Продукты" };

_categoryServiceMock
.Setup(service => service.DeleteAsync(categoryId))
.ReturnsAsync(expectedCategory);

var result = await _controller.Delete(categoryId);

var okResult = Assert.IsType<OkObjectResult>(result.Result);
Assert.Equal(200, okResult.StatusCode);

var actualCategory = Assert.IsType<CategoryResponseDto>(okResult.Value);
Assert.Equal(expectedCategory.Id, actualCategory.Id);
Assert.Equal(expectedCategory.Name, actualCategory.Name);
}

[Fact]
public async Task Delete_NonExistingCategory_ReturnsNotFound()
{
const int categoryId = 999;

_categoryServiceMock
.Setup(service => service.DeleteAsync(categoryId))
.ReturnsAsync((CategoryResponseDto)null!);

var result = await _controller.Delete(categoryId);

var notFoundResult = Assert.IsType<NotFoundObjectResult>(result.Result);
Assert.Equal(404, notFoundResult.StatusCode);
}
}
Loading