Skip to content

Commit

Permalink
Added optimistic concurrency exercise and marked application layer te…
Browse files Browse the repository at this point in the history
…sts with trait
  • Loading branch information
oskardudycz committed May 1, 2024
1 parent cedfab8 commit ee4bafb
Show file tree
Hide file tree
Showing 53 changed files with 1,813 additions and 1,203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected MartenTest()
var options = new StoreOptions();
options.Connection(
"PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; DATABASE = 'postgres'; PASSWORD = 'Password12!'; USER ID = 'postgres'");
options.UseDefaultSerialization(nonPublicMembersStorage: NonPublicMembersStorage.All);
options.UseNewtonsoftForSerialization(nonPublicMembersStorage: NonPublicMembersStorage.All);
options.DatabaseSchemaName = options.Events.DatabaseSchemaName = "IntroductionToEventSourcing";

documentStore = new DocumentStore(options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace ApplicationLogic.Marten.Tests.Incidents;
public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -25,7 +27,9 @@ public Task CantAddProductItemToNotExistingShoppingCart(string apiPrefix) =>
)
.Then(NOT_FOUND);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -38,7 +42,9 @@ public Task AddsProductItemToEmptyShoppingCart(string apiPrefix) =>
)
.Then(NO_CONTENT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -54,7 +60,9 @@ public Task AddsProductItemToNonEmptyShoppingCart(string apiPrefix) =>
)
.Then(NO_CONTENT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -71,7 +79,9 @@ public Task CantAddProductItemToConfirmedShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -88,7 +98,9 @@ public Task CantAddProductItemToCanceledShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace ApplicationLogic.Marten.Tests.Incidents;
public class CancelShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -24,7 +26,9 @@ public Task CantCancelNotExistingShoppingCart(string apiPrefix) =>
)
.Then(NOT_FOUND);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -39,7 +43,9 @@ public Task CancelsNonEmptyShoppingCart(string apiPrefix) =>
)
.Then(NO_CONTENT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -55,7 +61,9 @@ public Task CantCancelAlreadyCanceledShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -71,7 +79,9 @@ public Task CantCancelConfirmedShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace ApplicationLogic.Marten.Tests.Incidents;
public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -24,7 +26,9 @@ public Task CantConfirmNotExistingShoppingCart(string apiPrefix) =>
)
.Then(NOT_FOUND);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -36,7 +40,9 @@ public Task CantConfirmEmptyShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -51,7 +57,9 @@ public Task ConfirmsNonEmptyShoppingCart(string apiPrefix) =>
)
.Then(NO_CONTENT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -67,7 +75,9 @@ public Task CantConfirmAlreadyConfirmedShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -83,7 +93,9 @@ public Task CantConfirmCanceledShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace ApplicationLogic.Marten.Tests.Incidents;
public class OpenShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -18,7 +20,9 @@ public Task OpensShoppingCart(string apiPrefix) =>
.When(POST, URI(ShoppingCarts(apiPrefix, ClientId)))
.Then(CREATED_WITH_DEFAULT_HEADERS(locationHeaderPrefix: ShoppingCarts(apiPrefix, ClientId)));


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace ApplicationLogic.Marten.Tests.Incidents;
public class RemoveProductItemFromShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -24,7 +26,9 @@ public Task CantRemoveProductItemFromNotExistingShoppingCart(string apiPrefix) =
)
.Then(NOT_FOUND);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -36,7 +40,9 @@ public Task CantRemoveProductItemFromEmptyShoppingCart(string apiPrefix) =>
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -51,7 +57,9 @@ public Task CanRemoveExistingProductItemFromShoppingCart(string apiPrefix) =>
)
.Then(NO_CONTENT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -66,7 +74,9 @@ public Task CantRemoveNonExistingProductItemFromEmptyShoppingCart(string apiPref
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -82,7 +92,9 @@ public Task CantRemoveExistingProductItemFromCanceledShoppingCart(string apiPref
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand All @@ -98,7 +110,9 @@ public Task CantRemoveExistingProductItemFromConfirmedShoppingCart(string apiPre
)
.Then(CONFLICT);


[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ApplicationLogic.Marten.Core.Entities;
using ApplicationLogic.Marten.Core.Exceptions;
using Marten;

namespace ApplicationLogic.Marten.Core.Marten;
Expand All @@ -8,37 +7,23 @@ public static class DocumentSessionExtensions
{
public static Task Add<T>(this IDocumentSession documentSession, Guid id, object @event, CancellationToken ct)
where T : class
{
documentSession.Events.StartStream<T>(id, @event);
return documentSession.SaveChangesAsync(token: ct);
}
=> throw new NotImplementedException("Document Session Extensions not implemented!");

public static Task Add<T>(this IDocumentSession documentSession, Guid id, object[] events, CancellationToken ct)
where T : class
{
documentSession.Events.StartStream<T>(id, events);
return documentSession.SaveChangesAsync(token: ct);
}
=> throw new NotImplementedException("Document Session Extensions not implemented!");

public static Task GetAndUpdate<T>(
this IDocumentSession documentSession,
Guid id,
Func<T, object[]> handle,
CancellationToken ct
) where T : class =>
documentSession.Events.WriteToAggregate<T>(id, stream =>
stream.AppendMany(handle(stream.Aggregate ?? throw NotFoundException.For<T>(id))), ct);
) where T : class => throw new NotImplementedException("Document Session Extensions not implemented!");

public static Task GetAndUpdate<T>(
this IDocumentSession documentSession,
Guid id,
Action<T> handle,
CancellationToken ct
) where T : class, IAggregate =>
documentSession.Events.WriteToAggregate<T>(id, stream =>
{
var aggregate = stream.Aggregate ?? throw NotFoundException.For<T>(id);
handle(aggregate);
stream.AppendMany(aggregate.DequeueUncommittedEvents());
}, ct);
) where T : class, IAggregate => throw new NotImplementedException("Document Session Extensions not implemented!");
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
.AddImmutableShoppingCarts()
.AddMarten(options =>
{
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "Workshop_ShoppingCarts";
var schemaName = Environment.GetEnvironmentVariable("SchemaName") ?? "Workshop_Application_ShoppingCarts";
options.Events.DatabaseSchemaName = schemaName;
options.DatabaseSchemaName = schemaName;
options.Connection(builder.Configuration.GetConnectionString("ShoppingCarts") ??
Expand Down
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>
Loading

0 comments on commit ee4bafb

Please sign in to comment.