Skip to content

Commit

Permalink
Add unit tests for application
Browse files Browse the repository at this point in the history
  • Loading branch information
baratgabor committed Feb 26, 2021
1 parent 94938a2 commit f4de475
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/backend-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ jobs:
path-to-lcov: tests/WebApi.UnitTests/.coverage/coverage.info
parallel: true

- name: ApplicationUnitTests Report Upload
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: ApplicationUnitTests
path-to-lcov: tests/Application.UnitTests/.coverage/coverage.info
parallel: true

- name: Conclude Coverage Upload
uses: coverallsapp/github-action@master
with:
Expand Down
9 changes: 8 additions & 1 deletion MyWarehouse.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleData", "src\SampleDat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Domain", "src\Domain\Domain.csproj", "{DB2A0D38-6829-4413-8EE9-511BEAFDA5D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi.UnitTests", "tests\WebApi.UnitTests\WebApi.UnitTests.csproj", "{E79457E5-05D1-4DD0-B52F-22276205EF45}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.UnitTests", "tests\WebApi.UnitTests\WebApi.UnitTests.csproj", "{E79457E5-05D1-4DD0-B52F-22276205EF45}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application.UnitTests", "tests\Application.UnitTests\Application.UnitTests.csproj", "{FC7A5BBD-5FAB-4DE8-9A31-AF9351418E06}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -72,6 +74,10 @@ Global
{E79457E5-05D1-4DD0-B52F-22276205EF45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E79457E5-05D1-4DD0-B52F-22276205EF45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E79457E5-05D1-4DD0-B52F-22276205EF45}.Release|Any CPU.Build.0 = Release|Any CPU
{FC7A5BBD-5FAB-4DE8-9A31-AF9351418E06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC7A5BBD-5FAB-4DE8-9A31-AF9351418E06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC7A5BBD-5FAB-4DE8-9A31-AF9351418E06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC7A5BBD-5FAB-4DE8-9A31-AF9351418E06}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -86,6 +92,7 @@ Global
{FB414F9F-BA98-4FFB-9821-A3D70C1DE1DA} = {BD794B73-A20A-45A8-B38D-9318D181615F}
{DB2A0D38-6829-4413-8EE9-511BEAFDA5D7} = {BD794B73-A20A-45A8-B38D-9318D181615F}
{E79457E5-05D1-4DD0-B52F-22276205EF45} = {B2315480-4D42-4643-B60E-DE823C32A92D}
{FC7A5BBD-5FAB-4DE8-9A31-AF9351418E06} = {B2315480-4D42-4643-B60E-DE823C32A92D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5FECF7DF-777D-49DD-AFB4-D98906EA079B}
Expand Down
18 changes: 9 additions & 9 deletions src/Application/Common/AppOperationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
namespace MyWarehouse.Application.Common
{
//TODO: Complete 'operation result' scaffolding to return validation problems this way instead of throwing exceptions.
public class AppOperationResult
{
public bool OperationCompleted => Problems == null | !Problems.Any();
public IDictionary<string, string[]> Problems { get; }
}
//public class AppOperationResult
//{
// public bool OperationCompleted => Problems == null | !Problems.Any();
// public IDictionary<string, string[]> Problems { get; }
//}

public class AppOperationResult<T> : AppOperationResult
{
public T Result { get; init; }
}
//public class AppOperationResult<T> : AppOperationResult
//{
// public T Result { get; init; }
//}
}
36 changes: 18 additions & 18 deletions src/Application/Common/Behaviors/ErrorHandlerBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

namespace MyWarehouse.Application.Common.Behaviors
{
public class ErrorHandlerBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
public ErrorHandlerBehavior()
{
// TODO: Implement logging as an application (instead of API).
//public class ErrorHandlerBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
//{
// public ErrorHandlerBehavior()
// {

}
// }

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
try
{
return await next();
}
catch (Exception e)
{
throw;
// TODO: Implement logging as an application (instead of API).
}
}
}
// public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
// {
// try
// {
// return await next();
// }
// catch (Exception e)
// {
// throw;
// }
// }
//}
}
32 changes: 32 additions & 0 deletions tests/Application.UnitTests/Application.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>

<AssemblyName>MyWarehouse.Application.UnitTests</AssemblyName>

<RootNamespace>MyWarehouse.Application.UnitTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

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

<ItemGroup>
<Folder Include="Common\Behaviors\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using FluentAssertions;
using MyWarehouse.Application.Common.Dependencies.DataAccess.Repositories.Common;
using MyWarehouse.Application.Common.Exceptions;
using NUnit.Framework;
using System;

namespace MyWarehouse.Application.UnitTests.Common.Dependencies.DataAccess.Repositories.Common
{
public class ListQueryModelTests
{
[Test]
public void Instantiation_SetsDefaultValues()
{
var sut = new ListQueryModel<int>();

sut.PageIndex.Should().Be(1);
sut.PageSize.Should().BeGreaterThan(1);
sut.OrderBy.Should().Be("id");
sut.Filter.Should().BeNull();
}

[Test]
public void ThrowFilterIncorrectException_ReturnsFilterValidationError()
{
var sut = new ListQueryModel<int>();

FluentActions.Invoking(()
=> sut.ThrowFilterIncorrectException(new Exception()))
.Should().ThrowExactly<InputValidationException>()
.And.Errors.Keys.Should().Contain(nameof(ListQueryModel<int>.Filter));
}

[Test]
public void ThrowOrderByIncorrectException_ReturnsOrderByValidationError()
{
var sut = new ListQueryModel<int>();

FluentActions.Invoking(()
=> sut.ThrowOrderByIncorrectException(new Exception()))
.Should().ThrowExactly<InputValidationException>()
.And.Errors.Keys.Should().Contain(nameof(ListQueryModel<int>.OrderBy));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using FluentAssertions;
using FluentValidation.Results;
using MyWarehouse.Application.Common.Exceptions;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MyWarehouse.Application.UnitTests.Common.Exceptions
{
public class InputValidationExceptionTests
{

[Test]
public void Instantiation_Blank_SetsProperties()
{
var innerException = new Exception();

var sut = new InputValidationException(innerException);

sut.InnerException.Should().Be(innerException);
sut.Message.Should().Contain("validation");
sut.Errors.Should().NotBeNull();
}

[Test]
public void Instantiation_WithValidationFailures_SetsProperties()
{
var failures = new List<ValidationFailure>()
{
new ValidationFailure("Prop", "Prop not good, mister."),
new ValidationFailure("Prop", "Prop really not good, mister.")
};

var sut = new InputValidationException(failures);

sut.InnerException.Should().BeNull();
sut.Message.Should().Contain("validation");
sut.Errors.Should().HaveCount(1).And.ContainKey("Prop");
sut.Errors.First().Should().BeEquivalentTo(
new {
Key = "Prop",
Value = new[] {
failures[0].ErrorMessage,
failures[1].ErrorMessage
}
},
because: "Validations failured are expected to be grouped by property name."
);
}

[Test]
public void Instantiation_WithTuples_SetsProperties()
{
var failureTuples = new[]
{
("Prop", "Prop not good, mister."),
("Prop", "Prop really not good, mister.")
};

var sut = new InputValidationException(failureTuples);

sut.InnerException.Should().BeNull();
sut.Message.Should().Contain("validation");
sut.Errors.Should().HaveCount(1).And.ContainKey("Prop");
sut.Errors.First().Should().BeEquivalentTo(
new
{
Key = "Prop",
Value = new[] {
failureTuples[0].Item2,
failureTuples[1].Item2
}
},
because: "Validations failured are expected to be grouped by property name."
);
}
}
}

0 comments on commit f4de475

Please sign in to comment.