Skip to content

Commit

Permalink
Updated optimistic concurrency exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 2, 2024
1 parent a8cad2b commit f3509c1
Show file tree
Hide file tree
Showing 32 changed files with 420 additions and 469 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"profiles": {
"Helpdesk.Api": {
"ApplicationLogic.Marten": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"profiles": {
"Helpdesk.Api": {
"ApplicationLogic.EventStoreDB": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"profiles": {
"Helpdesk.Api": {
"OptimisticConcurrency.Marten": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
using Oakton;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

[assembly: TestFramework("OptimisticConcurrency.Marten.Tests.AssemblyFixture", "OptimisticConcurrency.Marten.Tests")]
[assembly: CollectionBehavior(DisableTestParallelization = true)]

namespace OptimisticConcurrency.EventStoreDB.Tests;

public sealed class AssemblyFixture : XunitTestFramework
{
public AssemblyFixture(IMessageSink messageSink)
:base(messageSink)
{
OaktonEnvironment.AutoStartHost = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{
[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantAddProductItemToNotExistingShoppingCart(string apiPrefix) =>
Expand All @@ -22,13 +21,12 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
POST,
URI(ShoppingCartProductItemsUrl(apiPrefix, ClientId, NotExistingShoppingCartId)),
BODY(new AddProductRequest(ProductItem)),
HEADERS(IF_MATCH(0))
HEADERS(IF_MATCH(-1))
)
.Then(NOT_FOUND);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task AddsProductItemToEmptyShoppingCart(string apiPrefix) =>
Expand All @@ -37,78 +35,74 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
POST,
URI(ctx => ShoppingCartProductItemsUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
BODY(new AddProductRequest(ProductItem)),
HEADERS(IF_MATCH(1))
HEADERS(IF_MATCH(0))
)
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(2));
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(1));

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task AddsProductItemToNonEmptyShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1)
WithProductItem(apiPrefix, ClientId, ProductItem, 0)
)
.When(
POST,
URI(ctx => ShoppingCartProductItemsUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
BODY(new AddProductRequest(ProductItem)),
HEADERS(IF_MATCH(2))
HEADERS(IF_MATCH(1))
)
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(3));
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(2));

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantAddProductItemToConfirmedShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenConfirmed(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenConfirmed(apiPrefix, ClientId, 1)
)
.When(
POST,
URI(ctx => ShoppingCartProductItemsUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
BODY(new AddProductRequest(ProductItem)),
HEADERS(IF_MATCH(3))
HEADERS(IF_MATCH(1))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantAddProductItemToCanceledShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenCanceled(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenCanceled(apiPrefix, ClientId, 1)
)
.When(
POST,
URI(ctx => ShoppingCartProductItemsUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
BODY(new AddProductRequest(ProductItem)),
HEADERS(IF_MATCH(3))
HEADERS(IF_MATCH(2))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task ReturnsNonEmptyShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1)
WithProductItem(apiPrefix, ClientId, ProductItem, 0)
)
.When(GET, URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())))
.Then(OK, RESPONSE_ETAG_HEADER(2));
.Then(OK, RESPONSE_ETAG_HEADER(1));

private static readonly Faker Faker = new();
private readonly Guid NotExistingShoppingCartId = Guid.NewGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,80 @@ public class CancelShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{
[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantCancelNotExistingShoppingCart(string apiPrefix) =>
api.Given()
.When(
DELETE,
URI(ShoppingCartUrl(apiPrefix, ClientId, NotExistingShoppingCartId)),
HEADERS(IF_MATCH(0))
HEADERS(IF_MATCH(-1))
)
.Then(NOT_FOUND);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CancelsNonEmptyShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1)
WithProductItem(apiPrefix, ClientId, ProductItem, 0)
)
.When(
DELETE,
URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(2))
HEADERS(IF_MATCH(1))
)
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(3));
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(2));

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantCancelAlreadyCanceledShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenCanceled(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenCanceled(apiPrefix, ClientId, 1)
)
.When(
DELETE,
URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(3))
HEADERS(IF_MATCH(2))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantCancelConfirmedShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenConfirmed(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenConfirmed(apiPrefix, ClientId, 1)
)
.When(
DELETE,
URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(3))
HEADERS(IF_MATCH(2))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task ReturnsNonEmptyShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenCanceled(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenCanceled(apiPrefix, ClientId, 1)
)
.When(GET, URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())))
.Then(OK, RESPONSE_ETAG_HEADER(3));
.Then(OK, RESPONSE_ETAG_HEADER(2));

private static readonly Faker Faker = new();
private readonly Guid NotExistingShoppingCartId = Guid.NewGuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,99 +12,93 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
IClassFixture<ApiSpecification<Program>>
{
[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantConfirmNotExistingShoppingCart(string apiPrefix) =>
api.Given()
.When(
POST,
URI(ConfirmShoppingCartUrl(apiPrefix, ClientId, NotExistingShoppingCartId)),
HEADERS(IF_MATCH(0))
HEADERS(IF_MATCH(-1))
)
.Then(NOT_FOUND);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantConfirmEmptyShoppingCart(string apiPrefix) =>
api.Given(OpenedShoppingCart(apiPrefix, ClientId))
.When(
POST,
URI(ctx => ConfirmShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(1))
HEADERS(IF_MATCH(0))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task ConfirmsNonEmptyShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1)
WithProductItem(apiPrefix, ClientId, ProductItem, 0)
)
.When(
POST,
URI(ctx => ConfirmShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(2))
HEADERS(IF_MATCH(1))
)
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(3));
.Then(NO_CONTENT, RESPONSE_ETAG_HEADER(2));

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantConfirmAlreadyConfirmedShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenConfirmed(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenConfirmed(apiPrefix, ClientId, 1)
)
.When(
POST,
URI(ctx => ConfirmShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(3))
HEADERS(IF_MATCH(2))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task CantConfirmCanceledShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenCanceled(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenCanceled(apiPrefix, ClientId, 1)
)
.When(
POST,
URI(ctx => ConfirmShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(3))
HEADERS(IF_MATCH(2))
)
.Then(CONFLICT);

[Theory]
[Trait("Category", "SkipCI")]
[InlineData("immutable")]
[Trait("Category", "SkipCI")][InlineData("immutable")]
[InlineData("mutable")]
[InlineData("mixed")]
public Task ReturnsNonEmptyShoppingCart(string apiPrefix) =>
api.Given(
OpenedShoppingCart(apiPrefix, ClientId),
WithProductItem(apiPrefix, ClientId, ProductItem, 1),
ThenConfirmed(apiPrefix, ClientId, 2)
WithProductItem(apiPrefix, ClientId, ProductItem, 0),
ThenConfirmed(apiPrefix, ClientId, 1)
)
.When(GET, URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())))
.Then(OK, RESPONSE_ETAG_HEADER(3));
.Then(OK, RESPONSE_ETAG_HEADER(2));

private static readonly Faker Faker = new();
private readonly Guid NotExistingShoppingCartId = Guid.NewGuid();
Expand Down
Loading

0 comments on commit f3509c1

Please sign in to comment.