Skip to content

Commit 723dc01

Browse files
committed
#283 Adding WithoutEntity (single object) to be removed from the dbcontext.
- TODO: add <TKey>(key) methods for single key and for collection of keys by providing model type.
1 parent 55ca4f8 commit 723dc01

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Contracts/Data/IWithoutDbContextBuilder.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66

77
public interface IWithoutDbContextBuilder
88
{
9+
/// <summary>
10+
/// Remove entity from the registered <see cref="DbContext"/>.
11+
/// </summary>
12+
/// <param name="entity">Entity to remove from the registered <see cref="DbContext"/>.</param>
13+
/// <returns>The same <see cref="DbContext"/> builder.</returns>
14+
IAndWithoutDbContextBuilder WithoutEntity(object entity);
15+
16+
/// <summary>
17+
/// Remove entity from the registered <see cref="DbContext"/>.
18+
/// </summary>
19+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/> to set up.</typeparam>
20+
/// <param name="entity">Entity to remove from the registered <see cref="DbContext"/>.</param>
21+
/// <returns>The same <see cref="DbContext"/> builder.</returns>
22+
IAndWithoutDbContextBuilder WithoutEntity<TDbContext>(object entity)
23+
where TDbContext : DbContext;
24+
925
/// <summary>
1026
/// Remove values from the registered <see cref="DbContext"/>.
1127
/// </summary>

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/Builders/Data/WithoutDbContextBuilder.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public WithoutDbContextBuilder(HttpTestContext testContext)
2323
{
2424
}
2525

26+
/// <inheritdoc />
27+
public IAndWithoutDbContextBuilder WithoutEntity(object entity)
28+
=> this.WithoutEntity<DbContext>(entity);
29+
30+
/// <inheritdoc />
31+
public IAndWithoutDbContextBuilder WithoutEntity<TDbContext>(object entity)
32+
where TDbContext : DbContext
33+
=> this.WithoutEntities<TDbContext>(entity);
34+
2635
/// <inheritdoc />
2736
public IAndWithoutDbContextBuilder WithoutEntities(IEnumerable<object> entities)
2837
=> this.WithoutEntities(dbContext => dbContext.RemoveRange(entities));
@@ -63,7 +72,7 @@ public IAndWithoutDbContextBuilder WithoutEntities<TDbContext>(Action<TDbContext
6372
}
6473
catch (DbUpdateConcurrencyException)
6574
{
66-
// Intentional silent fail.
75+
// Intentional silent fail, when deleting entities that does not exist in the database or have been already deleted.
6776
}
6877

6978
return this;
@@ -90,7 +99,7 @@ public IAndWithoutDbContextBuilder WithoutSet<TDbContext, TEntity>(Action<DbSet<
9099
}
91100
catch (DbUpdateConcurrencyException)
92101
{
93-
// Intentional silent fail.
102+
// Intentional silent fail, when deleting entities that does not exist in the database or have been already deleted.
94103
}
95104

96105
return this;

src/MyTested.AspNetCore.Mvc.EntityFrameworkCore/ComponentBuilderEntityFrameworkCoreExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ public static TBuilder WithData<TBuilder>(
7474
return actualBuilder.Builder;
7575
}
7676

77+
/// <summary>
78+
/// Remove entity from the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> on the tested component.
79+
/// </summary>
80+
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
81+
/// <param name="builder">
82+
/// Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.
83+
/// </param>
84+
/// <param name="entity">
85+
/// Remove entity from the registered <see cref="Microsoft.EntityFrameworkCore.DbContext"/>.
86+
/// </param>
87+
/// <returns>The same component builder.</returns>
88+
public static TBuilder WithoutData<TBuilder>(
89+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
90+
object entity)
91+
where TBuilder : IBaseTestBuilder
92+
=> builder
93+
.WithoutData(data => data
94+
.WithoutEntity(entity));
95+
7796
/// <summary>
7897
/// Remove values from the <see cref="Microsoft.EntityFrameworkCore.DbContext"/> on the tested component.
7998
/// </summary>

test/MyTested.AspNetCore.Mvc.EntityFrameworkCore.Test/BuildersTests/ControllersTests/ControllerBuilderTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,36 @@ public void WithDataThrowCorrectExceptionWhenNoDbContextIsRegistered()
415415

416416
[Fact]
417417
public void WithoutDataParamsReturnsCorrectResultCodeWhenAllProvidedDataIsDelete()
418+
{
419+
MyApplication
420+
.StartsFrom<TestStartup>()
421+
.WithServices(services => services.AddDbContext<CustomDbContext>());
422+
423+
var model1 = new CustomModel
424+
{
425+
Id = 1,
426+
Name = "Test1"
427+
};
428+
429+
var model2 = new CustomModel
430+
{
431+
Id = 2,
432+
Name = "Test2"
433+
};
434+
435+
MyController<DbContextController>
436+
.Instance()
437+
.WithData(model1)
438+
.AndAlso()
439+
.WithData(model2)
440+
.WithoutData(model1, model2)
441+
.Calling(c => c.Get(model1.Id))
442+
.ShouldReturn()
443+
.NotFound();
444+
}
445+
446+
[Fact]
447+
public void WithoutDataWithSingleEntityReturnsCorrectResultWhenAllProvidedDataIsDeleted()
418448
{
419449
MyApplication
420450
.StartsFrom<TestStartup>()

0 commit comments

Comments
 (0)