-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started creating unit testing methods for all the related solution folders.
- Loading branch information
Showing
15 changed files
with
721 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...rney.Tests/FleetJourney.Application.Tests/Extensions/ApplicationServiceExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using FleetJourney.Application.Attributes; | ||
using FleetJourney.Application.Extensions; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace FleetJourney.Application.Tests.Extensions; | ||
|
||
public sealed class ApplicationServiceExtensionsTests | ||
{ | ||
private interface IExampleService | ||
{ | ||
void DoSomething(); | ||
} | ||
|
||
private sealed class ExampleService : IExampleService | ||
{ | ||
public void DoSomething() | ||
{ | ||
} | ||
} | ||
|
||
[Fact] | ||
public void AddApplicationService_ShouldRegisterTypes_WhenInterfaceTypeProvided() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
var interfaceType = typeof(IExampleService); | ||
|
||
// Act | ||
services.AddApplicationService(interfaceType); | ||
|
||
// Assert | ||
services.Should().Contain(x => x.ServiceType == interfaceType); | ||
services.Should().OnlyHaveUniqueItems(x => x.ServiceType); | ||
services.Should().OnlyHaveUniqueItems(x => x.ImplementationType); | ||
} | ||
|
||
[Fact] | ||
public void AddApplicationService_ShouldNotRegisterTypesWithCachingDecoratorAttribute_WhenInterfaceTypeProvided() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
var interfaceType = typeof(IExampleService); | ||
|
||
// Act | ||
services.AddApplicationService(interfaceType); | ||
|
||
// Assert | ||
services.Where(x => x.ServiceType == interfaceType) | ||
.Should().NotContain(x => x.ImplementationType!.GetCustomAttributes(typeof(CachingDecorator), true).Any()); | ||
} | ||
|
||
[Fact] | ||
public void AddApplicationService_ShouldRegisterTypes_WhenGenericTypeProvided() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
services.AddApplicationService<IExampleService>(); | ||
|
||
// Assert | ||
services.Should().Contain(x => x.ServiceType == typeof(IExampleService)); | ||
services.Should().OnlyHaveUniqueItems(x => x.ServiceType); | ||
services.Should().OnlyHaveUniqueItems(x => x.ImplementationType); | ||
} | ||
|
||
[Fact] | ||
public void AddApplicationService_ShouldNotRegisterTypesWithCachingDecoratorAttribute_WhenGenericTypeProvided() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
services.AddApplicationService<IExampleService>(); | ||
|
||
// Assert | ||
services.Where(x => x.ServiceType == typeof(IExampleService)) | ||
.Should().NotContain(x => x.ImplementationType!.GetCustomAttributes(typeof(CachingDecorator), true).Any()); | ||
} | ||
|
||
[Fact] | ||
public void AddApplicationService_ShouldThrowArgumentNullException_WhenInterfaceTypeIsNull() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
Action act = () => services.AddApplicationService(null!); | ||
|
||
// Assert | ||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void AddApplicationService_ShouldThrowArgumentNullException_WhenGenericTypeIsNull() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
Action act = () => services.AddApplicationService(null!); | ||
|
||
// Assert | ||
act.Should().Throw<ArgumentNullException>(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
FleetJourney.Tests/FleetJourney.Application.Tests/Extensions/CacheServiceExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using FleetJourney.Application.Extensions; | ||
using FleetJourney.Application.Services.Abstractions; | ||
using Moq; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace FleetJourney.Application.Tests.Extensions; | ||
|
||
public sealed class CacheServiceExtensionsTests | ||
{ | ||
[Fact] | ||
public async Task RemoveCachesAsync_ShouldRemoveCaches() | ||
{ | ||
// Arrange | ||
var cacheServiceMock = new Mock<ICacheService<object>>(); | ||
var cancellationToken = CancellationToken.None; | ||
var cacheKeys = new[] { "cacheKey1", "cacheKey2", "cacheKey3" }; | ||
|
||
// Act | ||
await cacheServiceMock.Object.RemoveCachesAsync(cancellationToken, cacheKeys); | ||
|
||
// Assert | ||
foreach (var cacheKey in cacheKeys) | ||
{ | ||
cacheServiceMock.Verify(x => x.RemoveAsync(cacheKey, cancellationToken), Times.Once); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
FleetJourney.Tests/FleetJourney.Application.Tests/FleetJourney.Application.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.5" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="Moq" Version="4.18.4" /> | ||
<PackageReference Include="Testcontainers.MySql" Version="3.2.0" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Repositories\" /> | ||
<Folder Include="Services\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\FleetJourney.Application\FleetJourney.Application.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
130 changes: 130 additions & 0 deletions
130
FleetJourney.Tests/FleetJourney.Application.Tests/Helpers/CacheKeysTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using FleetJourney.Application.Helpers; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace FleetJourney.Application.Tests.Helpers; | ||
|
||
public sealed class CacheKeysTests | ||
{ | ||
[Theory] | ||
[InlineData("employees-all")] | ||
public void Employees_GetAll_Should_Return_Correct_Key(string expectedKey) | ||
{ | ||
// Act | ||
var key = CacheKeys.Employees.GetAll; | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("employees-", "")] | ||
public void Employees_Get_Should_Return_Correct_Key(string prefix, string suffix) | ||
{ | ||
// Arrange | ||
var employeeId = Guid.NewGuid(); | ||
var expectedKey = $"{prefix}{employeeId}{suffix}"; | ||
|
||
// Act | ||
var key = CacheKeys.Employees.Get(employeeId); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("employees-email-", "test@example.com")] | ||
public void Employees_GetByEmail_Should_Return_Correct_Key(string prefix, string email) | ||
{ | ||
// Arrange | ||
var expectedKey = $"{prefix}{email}"; | ||
|
||
// Act | ||
var key = CacheKeys.Employees.GetByEmail(email); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("cars-all")] | ||
public void CarPool_GetAll_Should_Return_Correct_Key(string expectedKey) | ||
{ | ||
// Act | ||
var key = CacheKeys.CarPool.GetAll; | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("cars-", "")] | ||
public void CarPool_Get_Should_Return_Correct_Key(string prefix, string suffix) | ||
{ | ||
// Arrange | ||
var carId = Guid.NewGuid(); | ||
var expectedKey = $"{prefix}{carId}{suffix}"; | ||
|
||
// Act | ||
var key = CacheKeys.CarPool.Get(carId); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("trips-all")] | ||
public void Trips_GetAll_Should_Return_Correct_Key(string expectedKey) | ||
{ | ||
// Act | ||
var key = CacheKeys.Trips.GetAll; | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("trips-", "-all")] | ||
public void Trips_GetAllByEmployeeId_Should_Return_Correct_Key(string prefix, string suffix) | ||
{ | ||
// Arrange | ||
var employeeId = Guid.NewGuid(); | ||
var expectedKey = $"{prefix}{employeeId}{suffix}"; | ||
|
||
// Act | ||
var key = CacheKeys.Trips.GetAllByEmployeeId(employeeId); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("trips-", "")] | ||
public void Trips_Get_Should_Return_Correct_Key(string prefix, string suffix) | ||
{ | ||
// Arrange | ||
var tripId = Guid.NewGuid(); | ||
var expectedKey = $"{prefix}{tripId}{suffix}"; | ||
|
||
// Act | ||
var key = CacheKeys.Trips.Get(tripId); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
|
||
[Theory] | ||
[InlineData("trips-", "")] | ||
public void Trips_GetByCarId_Should_Return_Correct_Key(string prefix, string suffix) | ||
{ | ||
// Arrange | ||
var carId = Guid.NewGuid(); | ||
var expectedKey = $"{prefix}{carId}{suffix}"; | ||
|
||
// Act | ||
var key = CacheKeys.Trips.GetByCarId(carId); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...tJourney.Tests/FleetJourney.Application.Tests/Helpers/PrefixKeyVaultSecretManagerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Azure.Security.KeyVault.Secrets; | ||
using FleetJourney.Application.Helpers; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace FleetJourney.Application.Tests.Helpers; | ||
|
||
public sealed class PrefixKeyVaultSecretManagerTests | ||
{ | ||
[Theory] | ||
[InlineData("prefix")] | ||
public void Load_Should_Return_True_For_Secret_With_Correct_Prefix(string prefix) | ||
{ | ||
// Arrange | ||
var secret = new SecretProperties("prefix-secret-name"); | ||
|
||
// Act | ||
var manager = new PrefixKeyVaultSecretManager(prefix); | ||
var result = manager.Load(secret); | ||
|
||
// Assert | ||
result.Should().BeTrue(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("prefix")] | ||
public void Load_Should_Return_False_For_Secret_With_Incorrect_Prefix(string prefix) | ||
{ | ||
// Arrange | ||
var secret = new SecretProperties("other-prefix-secret-name"); | ||
|
||
// Act | ||
var manager = new PrefixKeyVaultSecretManager(prefix); | ||
var result = manager.Load(secret); | ||
|
||
// Assert | ||
result.Should().BeFalse(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("prefix", "prefix-secret-name", "secret-name")] | ||
[InlineData("prefix", "prefix-secret--name", "secret:name")] | ||
public void GetKey_Should_Return_Correct_Key(string prefix, string secretName, string expectedKey) | ||
{ | ||
// Arrange | ||
var secret = new KeyVaultSecret(secretName, string.Empty); | ||
|
||
// Act | ||
var manager = new PrefixKeyVaultSecretManager(prefix); | ||
var key = manager.GetKey(secret); | ||
|
||
// Assert | ||
key.Should().Be(expectedKey); | ||
} | ||
} |
Oops, something went wrong.