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
29 changes: 29 additions & 0 deletions .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: .NET CI Pipeline

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore --configuration Release

- name: Run tests
run: dotnet test --no-build --configuration Release --verbosity normal
6 changes: 6 additions & 0 deletions FinancialTracker.API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialTracker", "FinancialTracker\FinancialTracker.csproj", "{BBECDC23-AC86-4437-8F43-FB9221F4724F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FinancialTracker.Tests", "FinancialTracker.Tests\FinancialTracker.Tests.csproj", "{129BAEA0-4BA6-468A-B0F1-ED748AE9D316}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{BBECDC23-AC86-4437-8F43-FB9221F4724F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBECDC23-AC86-4437-8F43-FB9221F4724F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBECDC23-AC86-4437-8F43-FB9221F4724F}.Release|Any CPU.Build.0 = Release|Any CPU
{129BAEA0-4BA6-468A-B0F1-ED748AE9D316}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{129BAEA0-4BA6-468A-B0F1-ED748AE9D316}.Debug|Any CPU.Build.0 = Debug|Any CPU
{129BAEA0-4BA6-468A-B0F1-ED748AE9D316}.Release|Any CPU.ActiveCfg = Release|Any CPU
{129BAEA0-4BA6-468A-B0F1-ED748AE9D316}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions FinancialTracker.API.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=c3e35030_002D771d_002D4674_002D85e8_002D2b16dd384d0f/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
55 changes: 55 additions & 0 deletions FinancialTracker.Tests/Controllers/CategoriesControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using FinancialTracker.Controllers;
using FinancialTracker.DTOs.CategoryDtos;
using FinancialTracker.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;

namespace FinancialTracker.Tests.Controllers;

public class CategoriesControllerTests
{
private readonly Mock<ICategoryService> _categoryServiceMock;
private readonly Mock<ILogger<CategoriesController>> _loggerMock;
private readonly CategoriesController _controller;

public CategoriesControllerTests()
{
_categoryServiceMock = new Mock<ICategoryService>();
_loggerMock = new Mock<ILogger<CategoriesController>>();
_controller = new CategoriesController(_categoryServiceMock.Object, _loggerMock.Object);
}

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

_categoryServiceMock
.Setup(service => service.AddAsync(createDto))
.ReturnsAsync(expectedCategory);

var result = await _controller.Add(createDto);

var createdAtResult = Assert.IsType<CreatedAtActionResult>(result.Result);
Assert.Equal(201, createdAtResult.StatusCode);
Assert.Equal(expectedCategory, createdAtResult.Value);
}

[Fact]
public async Task Add_DuplicateCategory_ReturnsConflict()
{
var createDto = new CategoryCreateDto { Name = "Продукты" };
const string errorMessage = "Категория с именем 'Продукты' уже существует";

_categoryServiceMock
.Setup(service => service.AddAsync(createDto))
.ThrowsAsync(new InvalidOperationException(errorMessage));

var result = await _controller.Add(createDto);

var conflictResult = Assert.IsType<ConflictObjectResult>(result.Result);
Assert.Equal(409, conflictResult.StatusCode);
}
}
27 changes: 27 additions & 0 deletions FinancialTracker.Tests/FinancialTracker.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FinancialTracker\FinancialTracker.csproj" />
</ItemGroup>

</Project>