Skip to content

Commit dfc4eb0

Browse files
committed
Implemented testing
1 parent 8ebd07a commit dfc4eb0

17 files changed

+309
-59
lines changed

.github/workflows/dotnet.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,28 @@ jobs:
2727
- name: Test
2828
run: dotnet test --no-build --verbosity normal
2929

30-
deploy:
30+
test:
3131
needs: [build]
32+
runs-on: ubuntu-latest
33+
defaults:
34+
run:
35+
working-directory: "./Forums.Test"
36+
steps:
37+
- uses: actions/checkout@v3
38+
- name: Setup .NET
39+
uses: actions/setup-dotnet@v3
40+
with:
41+
dotnet-version: 6.0.x
42+
- name: Restore dependencies
43+
run: dotnet restore
44+
- name: Test
45+
run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage"
46+
- name: Codecov
47+
uses: codecov/[email protected]
48+
49+
50+
deploy:
51+
needs: [build, test]
3252
runs-on: ubuntu-latest
3353
steps:
3454
- name: Checkout

Forums.API.sln

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ VisualStudioVersion = 17.3.32804.467
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E55534E5-3740-433D-9286-C73BB94B80A7}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Forums", "Forums\Forums.csproj", "{B32A812D-6725-4E30-A6A3-75C746BAE10E}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Forums", "Forums\Forums.csproj", "{B32A812D-6725-4E30-A6A3-75C746BAE10E}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumsService.Application", "ForumsService.API\ForumsService.Application.csproj", "{3277C567-8A09-4BBB-9588-96DFBF1A5D93}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForumsService.Application", "ForumsService.API\ForumsService.Application.csproj", "{3277C567-8A09-4BBB-9588-96DFBF1A5D93}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumsService.Domain", "ForumsService.Domain\ForumsService.Domain.csproj", "{890E3992-0D7E-4090-B7ED-658A2EA9207D}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForumsService.Domain", "ForumsService.Domain\ForumsService.Domain.csproj", "{890E3992-0D7E-4090-B7ED-658A2EA9207D}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForumsService.Infrastructure", "ForumsService.Infrastructure\ForumsService.Infrastructure.csproj", "{24838E55-59E1-4B37-A8CA-740AE56265BA}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForumsService.Infrastructure", "ForumsService.Infrastructure\ForumsService.Infrastructure.csproj", "{24838E55-59E1-4B37-A8CA-740AE56265BA}"
1515
EndProject
1616
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}"
1717
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Forums.Test", "Forums.Test\Forums.Test.csproj", "{60329483-CF6E-4CE9-9BAD-AF3B4AE4ECF8}"
19+
ProjectSection(ProjectDependencies) = postProject
20+
{24838E55-59E1-4B37-A8CA-740AE56265BA} = {24838E55-59E1-4B37-A8CA-740AE56265BA}
21+
{3277C567-8A09-4BBB-9588-96DFBF1A5D93} = {3277C567-8A09-4BBB-9588-96DFBF1A5D93}
22+
{890E3992-0D7E-4090-B7ED-658A2EA9207D} = {890E3992-0D7E-4090-B7ED-658A2EA9207D}
23+
EndProjectSection
24+
EndProject
1825
Global
1926
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2027
Debug|Any CPU = Debug|Any CPU
@@ -41,6 +48,10 @@ Global
4148
{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}.Debug|Any CPU.Build.0 = Debug|Any CPU
4249
{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}.Release|Any CPU.ActiveCfg = Release|Any CPU
4350
{4FEA7726-C49A-4E6D-8A14-FD2BF2E35187}.Release|Any CPU.Build.0 = Release|Any CPU
51+
{60329483-CF6E-4CE9-9BAD-AF3B4AE4ECF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
52+
{60329483-CF6E-4CE9-9BAD-AF3B4AE4ECF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
53+
{60329483-CF6E-4CE9-9BAD-AF3B4AE4ECF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
54+
{60329483-CF6E-4CE9-9BAD-AF3B4AE4ECF8}.Release|Any CPU.Build.0 = Release|Any CPU
4455
EndGlobalSection
4556
GlobalSection(SolutionProperties) = preSolution
4657
HideSolutionNode = FALSE

Forums.Test/Forums.Test.csproj

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
7+
88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="AutoFixture" Version="4.17.0" />
13-
<PackageReference Include="AutoMapper" Version="12.0.0" />
14-
<PackageReference Include="Bogus" Version="34.0.2" />
15-
<PackageReference Include="MediatR" Version="11.0.0" />
16-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.11" />
17-
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="7.0.0" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
19-
<PackageReference Include="Moq" Version="4.18.2" />
20-
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
21-
<PackageReference Include="xunit" Version="2.4.1" />
22-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
12+
<PackageReference Include="AutoFixture" Version="4.17.0" />
13+
<PackageReference Include="AutoMapper" Version="12.0.0" />
14+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.8" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
16+
<PackageReference Include="Moq" Version="4.18.2" />
17+
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
18+
<PackageReference Include="Testcontainers" Version="2.1.0" />
19+
<PackageReference Include="xunit" Version="2.4.2" />
20+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
2321
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2422
<PrivateAssets>all</PrivateAssets>
2523
</PackageReference>
@@ -30,9 +28,8 @@
3028
</ItemGroup>
3129

3230
<ItemGroup>
33-
<ProjectReference Include="..\Forums.API\ForumsService.API\ForumsService.Application.csproj" />
34-
<ProjectReference Include="..\Forums.API\ForumsService.Domain\ForumsService.Domain.csproj" />
35-
<ProjectReference Include="..\Forums.API\ForumsService.Infrastructure\ForumsService.Infrastructure.csproj" />
31+
<ProjectReference Include="..\ForumsService.Domain\ForumsService.Domain.csproj" />
32+
<ProjectReference Include="..\ForumsService.Infrastructure\ForumsService.Infrastructure.csproj" />
3633
</ItemGroup>
3734

3835
</Project>

Forums.Test/Integration.Testing/ControllerTesting.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Forums.Test.Model.Testing;
2+
using ForumsService.Application.Interface;
3+
using ForumsService.Domain.Entities;
4+
using Moq;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Forums.Test.Setup
12+
{
13+
public interface IGuidService { Guid NewGuid(); }
14+
public static class MockForumRepository
15+
{
16+
public static Mock<IQueryForumRepository> GetForumsRepository()
17+
{
18+
var mockGuidService = new Mock<IGuidService>();
19+
20+
var forumstypes = new List<ForumsDTO>
21+
{
22+
new ForumsDTO() { Id = new Guid("1c565daf-3eaa-4b60-bc11-d0a96fce249e"), Title = "Is the Star-Wars - Rise of Empire movie good?", Description = "I don't think this is a good movie at all! It contains a lot of bad filming.", Amountoflikes = 4, DateOfAdded = DateTime.Now, MovieId = 42543, Ownership = new Guid("1c565daf-3eaa-4b60-bc11-d0a96fce249e"), Reported = false },
23+
new ForumsDTO() { Id = new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8"), Title = "Harry Potter & The Deathly Hallows Part 1 - Hagrid?!", Description = "Just Hagrid in the movie?!.", Amountoflikes = 245, DateOfAdded = DateTime.MaxValue, MovieId = 5345, Ownership = new Guid("48aff7a7-702c-40d7-bbb0-417c0d775c08"), Reported = false }
24+
};
25+
26+
var mockRepo = new Mock<IQueryForumRepository>();
27+
28+
mockRepo.Setup(r => r.GetAll()).ReturnsAsync(forumstypes);
29+
mockRepo.Setup(r => r.Get(new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8"))).ReturnsAsync(new ForumsDTO{ Id = new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8"), Title = "Harry Potter & The Deathly Hallows Part 1 - Hagrid?!", Description = "Just Hagrid in the movie?!.", Amountoflikes = 245, DateOfAdded = DateTime.MaxValue, MovieId = 5345, Ownership = new Guid("48aff7a7-702c-40d7-bbb0-417c0d775c08"), Reported = false });
30+
31+
return mockRepo;
32+
33+
}
34+
public static Mock<ICommandForumRepository> CommandForumRepository()
35+
{
36+
var mockGuidService = new Mock<IGuidService>();
37+
38+
var forumstypes = new List<ForumsDTO>
39+
{
40+
new ForumsDTO() { Id = new Guid("1c565daf-3eaa-4b60-bc11-d0a96fce249e"), Title = "Is the Star-Wars - Rise of Empire movie good?", Description = "I don't think this is a good movie at all! It contains a lot of bad filming.", Amountoflikes = 4, DateOfAdded = DateTime.Now, MovieId = 42543, Ownership = new Guid("1c565daf-3eaa-4b60-bc11-d0a96fce249e"), Reported = false },
41+
new ForumsDTO() { Id = new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8"), Title = "Harry Potter & The Deathly Hallows Part 1 - Hagrid?!", Description = "Just Hagrid in the movie?!.", Amountoflikes = 245, DateOfAdded = DateTime.MaxValue, MovieId = 5345, Ownership = new Guid("48aff7a7-702c-40d7-bbb0-417c0d775c08"), Reported = false }
42+
};
43+
44+
var mockRepo = new Mock<ICommandForumRepository>();
45+
46+
mockRepo.Setup(x => x.CreateForum(It.IsAny<ForumsDTO>())).ReturnsAsync((ForumsDTO dto) =>
47+
{
48+
forumstypes.Add(dto);
49+
return dto;
50+
});
51+
52+
return mockRepo;
53+
54+
}
55+
}
56+
}

Forums.Test/Setup/MockUnitOfWork.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using ForumsService.Application.Interface;
2+
using Moq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Forums.Test.Setup
10+
{
11+
public static class MockUnitOfWork
12+
{
13+
public static Mock<IUnitOfWork> GetUnitOfWork()
14+
{
15+
var mockUow = new Mock<IUnitOfWork>();
16+
var mockLeaveTypeRepo = MockForumRepository.GetForumsRepository();
17+
18+
mockUow.Setup(r => r.QueryForumRepository).Returns(mockLeaveTypeRepo.Object);
19+
20+
return mockUow;
21+
}
22+
}
23+
}

Forums.Test/UnitTest/QueryTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Forums.Test.Setup;
2+
using ForumsService.Application.Interface;
3+
using ForumsService.Application.Query.GetAllForums;
4+
using ForumsService.Application.Query.GetForum;
5+
using ForumsService.Domain.Entities;
6+
using Moq;
7+
8+
9+
namespace Forums.Test.UnitTest
10+
{
11+
public class QueryTests
12+
{
13+
private readonly Mock<IQueryForumRepository> _mockRepo;
14+
public QueryTests() => _mockRepo = MockForumRepository.GetForumsRepository();
15+
16+
[Fact]
17+
public async Task GetAllForums()
18+
{
19+
//Arrange
20+
var handler = new GetAllForumsHandler(_mockRepo.Object);
21+
22+
//Act
23+
var result = await handler.Handle(new GetAllForumsQuery(), CancellationToken.None);
24+
25+
//Assert
26+
Assert.Equal(2, result.Count());
27+
}
28+
29+
[Fact]
30+
public async Task GetSingleForum()
31+
{
32+
//Arrange
33+
var handler = new GetForumHandler(_mockRepo.Object);
34+
var expected = new ForumsDTO() { Id = new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8"), Title = "Harry Potter & The Deathly Hallows Part 1 - Hagrid?!", Description = "Just Hagrid in the movie?!.", Amountoflikes = 245, DateOfAdded = DateTime.MaxValue, MovieId = 5345, Ownership = new Guid("48aff7a7-702c-40d7-bbb0-417c0d775c08"), Reported = false };
35+
36+
//Act
37+
var result = await handler.Handle(new GetForumQuery(new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8")), CancellationToken.None);
38+
39+
//Assert
40+
Assert.Equal(expected.ToString(), result.ToString());
41+
}
42+
43+
[Fact]
44+
public async Task GetSingleForum_WithoutId()
45+
{
46+
//Arrange
47+
var handler = new GetForumHandler(_mockRepo.Object);
48+
var expected = new ForumsDTO() { Id = new Guid("208e2274-db97-4ac4-b17c-27d10abca7a8"), Title = "Harry Potter & The Deathly Hallows Part 1 - Hagrid?!", Description = "Just Hagrid in the movie?!.", Amountoflikes = 245, DateOfAdded = DateTime.MaxValue, MovieId = 5345, Ownership = new Guid("48aff7a7-702c-40d7-bbb0-417c0d775c08"), Reported = false };
49+
50+
//Act
51+
var result = await handler.Handle(new GetForumQuery(new Guid()), CancellationToken.None);
52+
53+
//Assert
54+
Assert.Null(result);
55+
}
56+
}
57+
}

Forums.Test/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;

Forums/Controllers/ForumController.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ public ForumController(IMediator mediator)
2020
{
2121
_mediator = mediator;
2222
}
23+
[HttpGet("/test")]
24+
public string TestMethod()
25+
{
26+
return "Test Alive!";
27+
}
2328

24-
2529
[HttpGet]
2630
public async Task<ActionResult<IEnumerable<ForumsViewModel>>> GetAll()
2731
{
@@ -73,7 +77,7 @@ public async Task<ActionResult> Create(POSTViewModel forumViewModel)
7377
[HttpPut]
7478
public async Task<ActionResult> Update(ForumsViewModel forum)
7579
{
76-
var dto = new ForumsDTO() { Id = forum.Id, Title = forum.Title, Description = forum.Description, Amountoflikes = forum.Amountoflikes, DateOfAdded = forum.Dateofadded, MovieId = forum.MovieId, Ownership = forum.OwnerId, Reported = forum.Reported};
80+
var dto = new ForumsDTO() { Id = forum.Id, Title = forum.Title, Description = forum.Description, Amountoflikes = forum.Amountoflikes, DateOfAdded = forum.DateOfAdded, MovieId = forum.MovieId, Ownership = forum.Ownership, Reported = forum.Reported};
7781
var command = new UpdateForumCommand(dto);
7882
var response = await _mediator.Send(command);
7983
if (response == null)

Forums/Forums.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24+
<ProjectReference Include="..\Forums.Test\Forums.Test.csproj" />
2425
<ProjectReference Include="..\ForumsService.API\ForumsService.Application.csproj" />
2526
<ProjectReference Include="..\ForumsService.Infrastructure\ForumsService.Infrastructure.csproj" />
2627
</ItemGroup>

Forums/Models/ForumsViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public class ForumsViewModel
66
public string Title { get; set; }
77
public string Description { get; set; }
88

9-
public DateTime Dateofadded { get; set; }
10-
public Guid OwnerId { get; set; }
9+
public DateTime DateOfAdded { get; set; }
10+
public Guid Ownership { get; set; }
1111

1212
public bool Reported { get; set; }
1313
public int Amountoflikes { get; set; }
@@ -18,8 +18,8 @@ public ForumsViewModel(Guid id, string title, string description, Guid ownership
1818
Id = id;
1919
Title = title;
2020
Description = description;
21-
OwnerId = ownership;
22-
Dateofadded = dateofadded;
21+
Ownership = ownership;
22+
DateOfAdded = dateofadded;
2323
Reported = reported;
2424
Amountoflikes = amountoflikes;
2525
MovieId = movieId;

Forums/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@
5151
app.MapControllers();
5252

5353
app.Run();
54+
55+
public partial class Program { }

ForumsService.API/ForumsService.Application.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="AutoMapper" Version="12.0.0" />
1011
<PackageReference Include="MediatR" Version="11.0.0" />
1112
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
1213
</ItemGroup>

ForumsService.API/Interface/IQueryForumRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ namespace ForumsService.Application.Interface
1010
public interface IQueryForumRepository
1111
{
1212
Task<IEnumerable<ForumsDTO>> GetAll();
13-
Task<ForumsDTO?> Get(Guid id);
13+
Task<ForumsDTO> Get(Guid id);
1414
}
1515
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ForumsService.Application.Interface
8+
{
9+
public interface IUnitOfWork : IDisposable
10+
{
11+
ICommandForumRepository CommandForumRepository{ get; }
12+
IQueryForumRepository QueryForumRepository { get; }
13+
Task Save();
14+
}
15+
}

0 commit comments

Comments
 (0)