-
-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Marten application layer tests
- Loading branch information
1 parent
b5d23c2
commit 245385c
Showing
42 changed files
with
2,197 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,67 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Numerics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Core.Validation; | ||
|
||
public static class ValidationExtensions | ||
{ | ||
public static T AssertNotNull<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T NotNull<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : struct | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException(paramName); | ||
|
||
return (T)value; | ||
return value.Value; | ||
} | ||
|
||
public static string AssertNotEmpty(this string? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T NotNull<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : class | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException(paramName); | ||
|
||
return value; | ||
} | ||
|
||
public static string NotEmpty(this string? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
=> !string.IsNullOrWhiteSpace(value) ? value : throw new ArgumentOutOfRangeException(paramName); | ||
|
||
public static T AssertNotEmpty<T>(this T value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static Guid NotEmpty(this Guid? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
=> value!= null && value != Guid.Empty ? value.Value : throw new ArgumentOutOfRangeException(paramName); | ||
|
||
|
||
public static T NotEmpty<T>(this T value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : struct | ||
=> AssertNotEmpty((T?)value, paramName); | ||
=> NotEmpty((T?)value, paramName); | ||
|
||
public static T AssertNotEmpty<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T NotEmpty<T>(this T? value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : struct | ||
{ | ||
var notNullValue = value.AssertNotNull(paramName); | ||
var notNullValue = value.NotNull(paramName); | ||
|
||
if (Equals(notNullValue, default(T))) | ||
throw new ArgumentOutOfRangeException(paramName); | ||
|
||
return notNullValue; | ||
} | ||
|
||
public static T AssertGreaterOrEqualThan<T>(this T value, T valueToCompare, [CallerArgumentExpression("value")] string? paramName = null) | ||
public static T GreaterOrEqualThan<T>(this T value, T valueToCompare, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : IComparable<T> | ||
{ | ||
if (value.CompareTo(valueToCompare) < 0) | ||
throw new ArgumentOutOfRangeException(paramName); | ||
|
||
return value; | ||
|
||
} | ||
|
||
public static T Positive<T>(this T value, [CallerArgumentExpression("value")] string? paramName = null) | ||
where T : INumber<T> | ||
{ | ||
if (value == null || value.CompareTo(Convert.ChangeType(0, typeof(T))) <= 0) | ||
throw new ArgumentOutOfRangeException(paramName); | ||
|
||
return value; | ||
} | ||
} |
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
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
1 change: 0 additions & 1 deletion
1
...MeetingsManagement/MeetingsSearch.IntegrationTests/MeetingsSearch.IntegrationTests.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
32 changes: 32 additions & 0 deletions
32
...nToEventSourcing/08-ApplicationLogic.Marten.Tests/08-ApplicationLogic.Marten.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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>ApplicationLogic.Marten.Tests</RootNamespace> | ||
<AssemblyName>ApplicationLogic.Marten.Tests</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageReference Include="NSubstitute" Version="5.1.0" /> | ||
<PackageReference Include="xunit" Version="2.7.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.4" /> | ||
<PackageReference Include="Ogooreck" Version="0.8.0" /> | ||
<PackageReference Include="Bogus" Version="35.5.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\08-ApplicationLogic.Marten\08-ApplicationLogic.Marten.csproj" /> | ||
</ItemGroup> | ||
|
||
<Import Project="..\..\..\Tests.Build.props" /> | ||
|
||
</Project> |
107 changes: 107 additions & 0 deletions
107
...tSourcing/08-ApplicationLogic.Marten.Tests/Incidents/AddProductItemToShoppingCartTests.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,107 @@ | ||
using System.Net; | ||
using ApplicationLogic.Marten.Immutable.ShoppingCarts; | ||
using Bogus; | ||
using Ogooreck.API; | ||
using Xunit; | ||
using static Ogooreck.API.ApiSpecification; | ||
using static ApplicationLogic.Marten.Tests.Incidents.Scenarios; | ||
using static ApplicationLogic.Marten.Tests.Incidents.Fixtures; | ||
|
||
namespace ApplicationLogic.Marten.Tests.Incidents; | ||
|
||
public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api): | ||
IClassFixture<ApiSpecification<Program>> | ||
{ | ||
[Theory] | ||
[InlineData("immutable")] | ||
[InlineData("mutable")] | ||
[InlineData("mixed")] | ||
public Task CantAddProductItemToNotExistingShoppingCart(string apiPrefix) => | ||
api.Given() | ||
.When( | ||
POST, | ||
URI(ShoppingCartProductItems(apiPrefix, ClientId, NotExistingShoppingCartId)), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(NOT_FOUND); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
[InlineData("mutable")] | ||
[InlineData("mixed")] | ||
public Task AddsProductItemToEmptyShoppingCart(string apiPrefix) => | ||
api.Given(OpenedShoppingCart(apiPrefix, ClientId)) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(NO_CONTENT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
[InlineData("mutable")] | ||
[InlineData("mixed")] | ||
public Task AddsProductItemToNonEmptyShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem) | ||
) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(NO_CONTENT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
[InlineData("mutable")] | ||
[InlineData("mixed")] | ||
public Task CantAddProductItemToConfirmedShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenConfirmed(apiPrefix, ClientId) | ||
) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
[InlineData("mutable")] | ||
[InlineData("mixed")] | ||
public Task CantAddProductItemToCanceledShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem), | ||
ThenCanceled(apiPrefix, ClientId) | ||
) | ||
.When( | ||
POST, | ||
URI(ctx => ShoppingCartProductItems(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())), | ||
BODY(new AddProductRequest(ProductItem)) | ||
) | ||
.Then(CONFLICT); | ||
|
||
[Theory] | ||
[InlineData("immutable")] | ||
[InlineData("mutable")] | ||
[InlineData("mixed")] | ||
public Task ReturnsNonEmptyShoppingCart(string apiPrefix) => | ||
api.Given( | ||
OpenedShoppingCart(apiPrefix, ClientId), | ||
WithProductItem(apiPrefix, ClientId, ProductItem) | ||
) | ||
.When(GET, URI(ctx => ShoppingCart(apiPrefix, ClientId, ctx.GetCreatedId<Guid>()))) | ||
.Then(OK); | ||
|
||
private static readonly Faker Faker = new(); | ||
private readonly Guid NotExistingShoppingCartId = Guid.NewGuid(); | ||
private readonly Guid ClientId = Guid.NewGuid(); | ||
private readonly ProductItemRequest ProductItem = new(Guid.NewGuid(), Faker.Random.Number(1, 500)); | ||
} |
Oops, something went wrong.