Skip to content

Commit 6825c83

Browse files
committed
Added test for removing from many-to-many relationship with composite key
1 parent 02d19b5 commit 6825c83

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/Car.cs

+3
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ public override string? Id
4747

4848
[HasOne]
4949
public Dealership? Dealership { get; set; }
50+
51+
[HasMany]
52+
public ISet<Dealership> PreviousDealerships { get; set; } = new HashSet<Dealership>();
5053
}

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/CompositeDbContext.cs

+4
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ protected override void OnModelCreating(ModelBuilder builder)
3434
builder.Entity<Dealership>()
3535
.HasMany(dealership => dealership.Inventory)
3636
.WithOne(car => car.Dealership!);
37+
38+
builder.Entity<Car>()
39+
.HasMany(car => car.PreviousDealerships)
40+
.WithMany(dealership => dealership.SoldCars);
3741
}
3842
}

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/CompositeKeyTests.cs

+48
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,52 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
508508
carInDatabase.Should().BeNull();
509509
});
510510
}
511+
512+
[Fact]
513+
public async Task Can_remove_from_ManyToMany_relationship()
514+
{
515+
// Arrange
516+
Dealership existingDealership = _fakers.Dealership.Generate();
517+
existingDealership.SoldCars = _fakers.Car.Generate(2).ToHashSet();
518+
519+
await _testContext.RunOnDatabaseAsync(async dbContext =>
520+
{
521+
await dbContext.ClearTableAsync<Car>();
522+
dbContext.Dealerships.Add(existingDealership);
523+
await dbContext.SaveChangesAsync();
524+
});
525+
526+
var requestBody = new
527+
{
528+
data = new[]
529+
{
530+
new
531+
{
532+
type = "cars",
533+
id = existingDealership.SoldCars.ElementAt(1).StringId
534+
}
535+
}
536+
};
537+
538+
string route = $"/dealerships/{existingDealership.StringId}/relationships/soldCars";
539+
540+
// Act
541+
(HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecuteDeleteAsync<string>(route, requestBody);
542+
543+
// Assert
544+
httpResponse.ShouldHaveStatusCode(HttpStatusCode.NoContent);
545+
546+
responseDocument.Should().BeEmpty();
547+
548+
await _testContext.RunOnDatabaseAsync(async dbContext =>
549+
{
550+
Dealership dealershipInDatabase = await dbContext.Dealerships.Include(dealership => dealership.SoldCars).FirstWithIdAsync(existingDealership.Id);
551+
552+
dealershipInDatabase.SoldCars.ShouldHaveCount(1);
553+
dealershipInDatabase.SoldCars.Single().Id.Should().Be(existingDealership.SoldCars.ElementAt(0).Id);
554+
555+
List<Car> carsInDatabase = await dbContext.Cars.ToListAsync();
556+
carsInDatabase.ShouldHaveCount(2);
557+
});
558+
}
511559
}

test/JsonApiDotNetCoreTests/IntegrationTests/CompositeKeys/Dealership.cs

+3
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ public sealed class Dealership : Identifiable<int>
1313

1414
[HasMany]
1515
public ISet<Car> Inventory { get; set; } = new HashSet<Car>();
16+
17+
[HasMany]
18+
public ISet<Car> SoldCars { get; set; } = new HashSet<Car>();
1619
}

0 commit comments

Comments
 (0)