Skip to content

Commit

Permalink
Implemented Optimistic Concurrency with ESDB exercise solution
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 2, 2024
1 parent 3eb99cb commit b710a4b
Show file tree
Hide file tree
Showing 23 changed files with 435 additions and 350 deletions.
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 @@ -21,7 +21,7 @@ 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);

Expand All @@ -35,9 +35,9 @@ 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]
[InlineData("immutable")]
Expand All @@ -46,15 +46,15 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
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]
[InlineData("immutable")]
Expand All @@ -63,14 +63,14 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
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);

Expand All @@ -81,14 +81,14 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
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);

Expand All @@ -99,10 +99,10 @@ public class AddProductItemToShoppingCartTests(ApiSpecification<Program> api):
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 @@ -20,7 +20,7 @@ public class CancelShoppingCartTests(ApiSpecification<Program> api):
.When(
DELETE,
URI(ShoppingCartUrl(apiPrefix, ClientId, NotExistingShoppingCartId)),
HEADERS(IF_MATCH(0))
HEADERS(IF_MATCH(-1))
)
.Then(NOT_FOUND);

Expand All @@ -31,14 +31,14 @@ public class CancelShoppingCartTests(ApiSpecification<Program> api):
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]
[InlineData("immutable")]
Expand All @@ -47,13 +47,13 @@ public class CancelShoppingCartTests(ApiSpecification<Program> api):
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);

Expand All @@ -64,13 +64,13 @@ public class CancelShoppingCartTests(ApiSpecification<Program> api):
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);

Expand All @@ -81,11 +81,11 @@ public class CancelShoppingCartTests(ApiSpecification<Program> api):
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 @@ -20,7 +20,7 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
.When(
POST,
URI(ConfirmShoppingCartUrl(apiPrefix, ClientId, NotExistingShoppingCartId)),
HEADERS(IF_MATCH(0))
HEADERS(IF_MATCH(-1))
)
.Then(NOT_FOUND);

Expand All @@ -33,7 +33,7 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
.When(
POST,
URI(ctx => ConfirmShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())),
HEADERS(IF_MATCH(1))
HEADERS(IF_MATCH(0))
)
.Then(CONFLICT);

Expand All @@ -44,14 +44,14 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
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]
[InlineData("immutable")]
Expand All @@ -60,13 +60,13 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
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);

Expand All @@ -77,13 +77,13 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
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);

Expand All @@ -94,11 +94,11 @@ public class ConfirmShoppingCartTests(ApiSpecification<Program> api):
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class OpenShoppingCartTests(ApiSpecification<Program> api):
.When(POST, URI(ShoppingCartsUrl(apiPrefix, ClientId)))
.Then(
CREATED_WITH_DEFAULT_HEADERS(locationHeaderPrefix: ShoppingCartsUrl(apiPrefix, ClientId)),
RESPONSE_ETAG_HEADER(1)
RESPONSE_ETAG_HEADER(0)
);

[Theory]
Expand All @@ -28,7 +28,7 @@ public class OpenShoppingCartTests(ApiSpecification<Program> api):
public Task ReturnsOpenedShoppingCart(string apiPrefix) =>
api.Given(OpenedShoppingCart(apiPrefix, ClientId))
.When(GET, URI(ctx => ShoppingCartUrl(apiPrefix, ClientId, ctx.GetCreatedId<Guid>())))
.Then(OK, RESPONSE_ETAG_HEADER(1));
.Then(OK, RESPONSE_ETAG_HEADER(0));

private readonly Guid ClientId = Guid.NewGuid();
}
Loading

0 comments on commit b710a4b

Please sign in to comment.