Skip to content

Integration tests for post service and some fixes of the project structure and CI #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 11, 2024
16 changes: 8 additions & 8 deletions .github/workflows/post.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ name: Post Service CI/CD Pipeline
on:
push:
paths:
- backend/src/PostService/**
- backend/src/**
- .github/workflows/post.yml
branches:
- master
- develop
tags:
- 'v*'
pull_request:
branches:
- master
- develop
types: [opened, synchronize, reopened]

jobs:
build:
Expand All @@ -26,16 +24,18 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
dotnet-version: '8.x'

- name: Restore dependencies
run: dotnet restore ./backend/FitnessApp.sln
run: dotnet restore ./FitnessApp.sln

- name: Build project
run: dotnet build --configuration Release ./backend/FitnessApp.sln --no-restore
run: dotnet build --configuration Release ./FitnessApp.sln --no-restore

- name: Run tests
run: dotnet test ./backend/FitnessApp.sln --no-restore --verbosity normal
run: |
dotnet test ./backend/tests/PostService/PostService.Domain.Tests/PostService.Domain.Tests.csproj --configuration Release --no-build --verbosity normal
dotnet test ./backend/tests/PostService/PostService.Application.Tests/PostService.Application.Tests.csproj --configuration Release --no-build --verbosity normal

docker:
needs: build
Expand Down
38 changes: 25 additions & 13 deletions backend/FitnessApp.sln → FitnessApp.sln
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostService", "PostService", "{84AE3F00-9A59-4A8D-A737-67AB74E5D1FD}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostService", "backend\PostService", "{84AE3F00-9A59-4A8D-A737-67AB74E5D1FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Api", "src\PostService\PostService.Api\PostService.Api.csproj", "{2E44C019-ED30-4671-A878-D2F7B6071024}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Api", "backend\src\PostService\PostService.Api\PostService.Api.csproj", "{2E44C019-ED30-4671-A878-D2F7B6071024}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Domain", "src\PostService\PostService.Domain\PostService.Domain.csproj", "{BC83A789-36C2-42E7-B630-6BC503375C30}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Domain", "backend\src\PostService\PostService.Domain\PostService.Domain.csproj", "{BC83A789-36C2-42E7-B630-6BC503375C30}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Infrastructure", "src\PostService\PostService.Infrastructure\PostService.Infrastructure.csproj", "{49301846-33F7-4C75-A1F9-6A0FEBA5EADD}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Infrastructure", "backend\src\PostService\PostService.Infrastructure\PostService.Infrastructure.csproj", "{49301846-33F7-4C75-A1F9-6A0FEBA5EADD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Persistence", "src\PostService\PostService.Persistence\PostService.Persistence.csproj", "{0E631CB2-47C2-458A-B163-43CE50473B8A}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Persistence", "backend\src\PostService\PostService.Persistence\PostService.Persistence.csproj", "{0E631CB2-47C2-458A-B163-43CE50473B8A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Application", "src\PostService\PostService.Application\PostService.Application.csproj", "{4FD56528-F5F1-4BF8-AC46-A5E54D158AA2}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Application", "backend\src\PostService\PostService.Application\PostService.Application.csproj", "{4FD56528-F5F1-4BF8-AC46-A5E54D158AA2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B7E4E4CF-E7A9-4DAC-807E-DFF17553BAE8}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "backend\src", "{B7E4E4CF-E7A9-4DAC-807E-DFF17553BAE8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{112E9C63-BF62-49C1-AD19-03855535225B}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "backend\Shared", "{112E9C63-BF62-49C1-AD19-03855535225B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared.Application", "src\Shared\Shared.Application\Shared.Application.csproj", "{824BB290-EC26-471B-AEA6-1395B4C23177}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared.Application", "backend\src\Shared\Shared.Application\Shared.Application.csproj", "{824BB290-EC26-471B-AEA6-1395B4C23177}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{2B7719FF-D7B4-4EFA-B3C5-2240EC19A1A5}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "backend\tests", "{2B7719FF-D7B4-4EFA-B3C5-2240EC19A1A5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostService", "tests\PostService", "{885CB230-66EA-4743-8162-6A240AA960A4}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostService", "backend\tests\PostService", "{885CB230-66EA-4743-8162-6A240AA960A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Domain.Tests", "tests\PostService\PostService.Domain.Tests\PostService.Domain.Tests.csproj", "{9CD34775-6A28-4C93-9BFC-C15F54C3FA21}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Domain.Tests", "backend\tests\PostService\PostService.Domain.Tests\PostService.Domain.Tests.csproj", "{9CD34775-6A28-4C93-9BFC-C15F54C3FA21}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Application.Tests", "tests\PostService\PostService.Application.Tests\PostService.Application.Tests.csproj", "{5325FC87-0538-4AE1-8979-F9597F3FF466}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PostService.Application.Tests", "backend\tests\PostService\PostService.Application.Tests\PostService.Application.Tests.csproj", "{5325FC87-0538-4AE1-8979-F9597F3FF466}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{D5A12D19-86E3-4E83-8BF0-D9AF8C2477F3}"
ProjectSection(SolutionItems) = preProject
.github\workflows\post.yml = .github\workflows\post.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution-items", "solution-items", "{CE086396-E7FB-46F7-87A9-4570DBF43597}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Fitness App

## TODO

- Fill readme properly
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using PostService.Application.Commands.AddPost;

namespace PostService.Application;

Expand All @@ -9,6 +10,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
{
services.AddValidatorsFromAssembly(typeof(DependencyInjection).Assembly);

services.AddScoped<AddPostCommandHandler>();

return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Net;
using FluentAssertions;
using PostService.Application.Commands.AddPost;
using PostService.Application.DTOs;
using PostService.Domain.Enums;
using Xunit;

namespace PostService.Application.Tests.CommandHandlerTests.PostTests;

public class AddPostTests(TestFixture fixture) : TestBase(fixture)
{
[Fact]
public async Task HandleAsync_ShouldAddPostCorrectly()
{
// Arrange
var title = "Title";
var description = "Description";
var contentUrl = "https://example.com";
var contentType = ContentType.Image;

var createPost = new CreatePostDto(title, description, contentUrl, contentType);
var userId = Guid.NewGuid();

var command = new AddPostCommand(createPost, userId);

// Act
var result = await Fixture.AddPostCommandHandler.HandleAsync(command);

// Assert
result.IsSuccess.Should().BeTrue();
result.StatusCode.Should().Be(HttpStatusCode.OK);
result.Response.Should().NotBeNull();
result.Response.Title.Should().Be(title);
result.Response.Description.Should().Be(description);
result.Response.ContentUrl.Should().Be(contentUrl);
result.Response.ContentType.Should().Be(contentType);
result.Response.LikeCount.Should().Be(0);
result.Response.CommentCount.Should().Be(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,26 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Testcontainers" Version="4.0.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="4.0.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.9.2" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\PostService\PostService.Application\PostService.Application.csproj" />
<ProjectReference Include="..\..\..\src\PostService\PostService.Persistence\PostService.Persistence.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Xunit;

namespace PostService.Application.Tests;

public class TestBase : IClassFixture<TestFixture>, IAsyncLifetime
{
protected readonly TestFixture Fixture;

protected TestBase(TestFixture fixture)
{
Fixture = fixture;
}

public async Task InitializeAsync()
{
await Fixture.PostDbContextFixture.Database.EnsureCreatedAsync();
}

public Task DisposeAsync()
{
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using PostService.Application.Commands.AddPost;
using PostService.Persistence;
using Testcontainers.PostgreSql;

namespace PostService.Application.Tests;

public class TestFixture
{
private readonly TestStartup _testStartup = new();
public readonly PostDbContext PostDbContextFixture;

public readonly AddPostCommandHandler AddPostCommandHandler;

private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder()
.WithImage("postgres:15-alpine")
.Build();

public TestFixture()
{
_postgreSqlContainer.StartAsync().Wait();

var connectionString = _postgreSqlContainer.GetConnectionString();
var serviceProvider = _testStartup.Initialize(connectionString);

PostDbContextFixture = serviceProvider.GetRequiredService<PostDbContext>();

AddPostCommandHandler = serviceProvider.GetRequiredService<AddPostCommandHandler>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using PostService.Application.Commands.AddPost;
using PostService.Application.DTOs;
using PostService.Persistence;

namespace PostService.Application.Tests;

public class TestStartup
{
public ServiceProvider Initialize(string connectionString)
{
var serviceCollection = new ServiceCollection()
.AddDbContext<PostDbContext>(options =>
{
options.UseNpgsql(connectionString);
})
.AddScoped<IValidator<CreatePostDto>, InlineValidator<CreatePostDto>>()
.AddScoped<AddPostCommandHandler>();

return serviceCollection.BuildServiceProvider();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down