-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
340 additions
and
0 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
22 changes: 22 additions & 0 deletions
22
test/JsonApiDotNetCoreTests/IntegrationTests/Experiments/Customer.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,22 @@ | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.Resources; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace JsonApiDotNetCoreTests.IntegrationTests.Experiments; | ||
|
||
[UsedImplicitly(ImplicitUseTargetFlags.Members)] | ||
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.Experiments")] | ||
public sealed class Customer : Identifiable<long> | ||
{ | ||
[Attr] | ||
public string Name { get; set; } = null!; | ||
|
||
[HasOne] | ||
public Order? FirstOrder { get; set; } | ||
|
||
[HasOne] | ||
public Order? LastOrder { get; set; } | ||
|
||
[HasMany] | ||
public ISet<Order> Orders { get; set; } = new HashSet<Order>(); | ||
} |
57 changes: 57 additions & 0 deletions
57
test/JsonApiDotNetCoreTests/IntegrationTests/Experiments/ExperimentsDbContext.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,57 @@ | ||
using JetBrains.Annotations; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
// @formatter:wrap_chained_method_calls chop_always | ||
|
||
namespace JsonApiDotNetCoreTests.IntegrationTests.Experiments; | ||
|
||
[UsedImplicitly(ImplicitUseTargetFlags.Members)] | ||
public sealed class ExperimentsDbContext : DbContext | ||
{ | ||
public DbSet<Customer> Customers => Set<Customer>(); | ||
public DbSet<Order> Orders => Set<Order>(); | ||
public DbSet<ShoppingBasket> ShoppingBaskets => Set<ShoppingBasket>(); | ||
|
||
public ExperimentsDbContext(DbContextOptions<ExperimentsDbContext> options) | ||
: base(options) | ||
{ | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder builder) | ||
{ | ||
// https://stackoverflow.com/questions/54326165/ef-core-why-clientsetnull-is-default-ondelete-behavior-for-optional-relations | ||
// https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete | ||
|
||
builder.Entity<Customer>() | ||
.HasMany(customer => customer.Orders) | ||
.WithOne(order => order.Customer); | ||
|
||
builder.Entity<Customer>() | ||
.HasOne(customer => customer.FirstOrder) | ||
.WithOne() | ||
.HasForeignKey<Customer>("FirstOrderId") | ||
.OnDelete(DeleteBehavior.ClientSetNull); | ||
//.OnDelete(DeleteBehavior.SetNull); | ||
|
||
builder.Entity<Customer>() | ||
.HasOne(customer => customer.LastOrder) | ||
.WithOne() | ||
.HasForeignKey<Customer>("LastOrderId") | ||
.OnDelete(DeleteBehavior.ClientSetNull); | ||
//.OnDelete(DeleteBehavior.SetNull); | ||
|
||
builder.Entity<Order>() | ||
.HasOne(order => order.Parent) | ||
.WithOne() | ||
.HasForeignKey<Order>("ParentOrderId") | ||
.OnDelete(DeleteBehavior.ClientSetNull); | ||
//.OnDelete(DeleteBehavior.SetNull); | ||
|
||
builder.Entity<ShoppingBasket>() | ||
.HasOne(shoppingBasket => shoppingBasket.CurrentOrder) | ||
.WithOne() | ||
.HasForeignKey<ShoppingBasket>("CurrentOrderId") | ||
.OnDelete(DeleteBehavior.ClientSetNull); | ||
//.OnDelete(DeleteBehavior.SetNull); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
test/JsonApiDotNetCoreTests/IntegrationTests/Experiments/ExperimentsFakers.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,29 @@ | ||
using Bogus; | ||
using TestBuildingBlocks; | ||
|
||
// @formatter:wrap_chained_method_calls chop_always | ||
// @formatter:keep_existing_linebreaks true | ||
|
||
namespace JsonApiDotNetCoreTests.IntegrationTests.Experiments; | ||
|
||
internal sealed class ExperimentsFakers : FakerContainer | ||
{ | ||
private readonly Lazy<Faker<Customer>> _lazyCustomerFaker = new(() => | ||
new Faker<Customer>() | ||
.UseSeed(GetFakerSeed()) | ||
.RuleFor(customer => customer.Name, faker => faker.Person.FullName)); | ||
|
||
private readonly Lazy<Faker<Order>> _lazyOrderFaker = new(() => | ||
new Faker<Order>() | ||
.UseSeed(GetFakerSeed()) | ||
.RuleFor(order => order.Amount, faker => faker.Finance.Amount())); | ||
|
||
private readonly Lazy<Faker<ShoppingBasket>> _lazyShoppingBasketFaker = new(() => | ||
new Faker<ShoppingBasket>() | ||
.UseSeed(GetFakerSeed()) | ||
.RuleFor(shoppingBasket => shoppingBasket.ProductCount, faker => faker.Random.Int(0, 5))); | ||
|
||
public Faker<Customer> Customer => _lazyCustomerFaker.Value; | ||
public Faker<Order> Order => _lazyOrderFaker.Value; | ||
public Faker<ShoppingBasket> ShoppingBasket => _lazyShoppingBasketFaker.Value; | ||
} |
73 changes: 73 additions & 0 deletions
73
test/JsonApiDotNetCoreTests/IntegrationTests/Experiments/ExperimentsTests.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,73 @@ | ||
using System.Net; | ||
using FluentAssertions; | ||
using FluentAssertions.Common; | ||
using FluentAssertions.Extensions; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCoreTests.IntegrationTests.SoftDeletion; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TestBuildingBlocks; | ||
using Xunit; | ||
|
||
namespace JsonApiDotNetCoreTests.IntegrationTests.Experiments; | ||
|
||
public sealed class ExperimentsTests : IClassFixture<IntegrationTestContext<TestableStartup<ExperimentsDbContext>, ExperimentsDbContext>> | ||
{ | ||
private readonly IntegrationTestContext<TestableStartup<ExperimentsDbContext>, ExperimentsDbContext> _testContext; | ||
private readonly ExperimentsFakers _fakers = new(); | ||
|
||
public ExperimentsTests(IntegrationTestContext<TestableStartup<ExperimentsDbContext>, ExperimentsDbContext> testContext) | ||
{ | ||
_testContext = testContext; | ||
|
||
testContext.UseController<CustomersController>(); | ||
testContext.UseController<OrdersController>(); | ||
testContext.UseController<ShoppingBasketsController>(); | ||
|
||
testContext.ConfigureServicesAfterStartup(services => | ||
{ | ||
services.AddSingleton<ISystemClock>(new FrozenSystemClock | ||
{ | ||
UtcNow = 1.January(2005).ToDateTimeOffset() | ||
}); | ||
|
||
services.AddResourceService<SoftDeletionAwareResourceService<Company, int>>(); | ||
services.AddResourceService<SoftDeletionAwareResourceService<Department, int>>(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_delete_resource() | ||
{ | ||
// Arrange | ||
Order existingOrder = _fakers.Order.Generate(); | ||
existingOrder.Customer = _fakers.Customer.Generate(); | ||
existingOrder.Parent = _fakers.Order.Generate(); | ||
existingOrder.Parent.Customer = existingOrder.Customer; | ||
|
||
List<ShoppingBasket> existingBaskets = _fakers.ShoppingBasket.Generate(3); | ||
existingBaskets[0].CurrentOrder = existingOrder; | ||
existingBaskets[1].CurrentOrder = existingOrder; | ||
|
||
await _testContext.RunOnDatabaseAsync(async dbContext => | ||
{ | ||
dbContext.Orders.Add(existingOrder); | ||
await dbContext.SaveChangesAsync(); | ||
|
||
existingOrder.Customer.FirstOrder = existingOrder; | ||
existingOrder.Customer.LastOrder = existingOrder; | ||
dbContext.ShoppingBaskets.AddRange(existingBaskets); | ||
await dbContext.SaveChangesAsync(); | ||
}); | ||
|
||
string route = $"/orders/{existingOrder.StringId}"; | ||
|
||
// Act | ||
(HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync<string>(route); | ||
|
||
// Assert | ||
httpResponse.ShouldHaveStatusCode(HttpStatusCode.NoContent); | ||
|
||
responseDocument.Should().BeEmpty(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
test/JsonApiDotNetCoreTests/IntegrationTests/Experiments/Order.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,19 @@ | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.Resources; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace JsonApiDotNetCoreTests.IntegrationTests.Experiments; | ||
|
||
[UsedImplicitly(ImplicitUseTargetFlags.Members)] | ||
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.Experiments")] | ||
public sealed class Order : Identifiable<long> | ||
{ | ||
[Attr] | ||
public decimal Amount { get; set; } | ||
|
||
[HasOne] | ||
public Customer Customer { get; set; } = null!; | ||
|
||
[HasOne] | ||
public Order? Parent { get; set; } | ||
} |
16 changes: 16 additions & 0 deletions
16
test/JsonApiDotNetCoreTests/IntegrationTests/Experiments/ShoppingBasket.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,16 @@ | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.Resources; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace JsonApiDotNetCoreTests.IntegrationTests.Experiments; | ||
|
||
[UsedImplicitly(ImplicitUseTargetFlags.Members)] | ||
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.Experiments")] | ||
public sealed class ShoppingBasket : Identifiable<long> | ||
{ | ||
[Attr] | ||
public int ProductCount { get; set; } | ||
|
||
[HasOne] | ||
public Order? CurrentOrder { get; set; } | ||
} |