Skip to content

Commit dd84392

Browse files
committed
#283, #340 Adding WithoutEntity and Entities by providing model and key/s.
1 parent 4762588 commit dd84392

File tree

3 files changed

+192
-2
lines changed

3 files changed

+192
-2
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,51 @@
66

77
public interface IWithoutDbContextBuilder
88
{
9+
/// <summary>
10+
/// Remove entity by providing its primary key from the registered <see cref="DbContext"/>.
11+
/// </summary>
12+
/// <typeparam name="TEntity">Entity model type for which the provided key is related to.</typeparam>
13+
/// <param name="key">Primary key of entity to remove from the registered <see cref="DbContext"/>.</param>
14+
/// <returns>The same <see cref="DbContext"/> builder.</returns>
15+
IAndWithoutDbContextBuilder WithoutEntityByKey<TEntity>(object key)
16+
where TEntity : class;
17+
18+
/// <summary>
19+
/// Remove entity by providing its primary key from the registered <see cref="DbContext"/>.
20+
/// </summary>
21+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/> to set up.</typeparam>
22+
/// <typeparam name="TEntity">Entity model type for which the provided key is related to.</typeparam>
23+
/// <param name="key">Primary key of entity to remove from the registered <see cref="DbContext"/>.</param>
24+
/// <returns>The same <see cref="DbContext"/> builder.</returns>
25+
IAndWithoutDbContextBuilder WithoutEntityByKey<TDbContext, TEntity>(object key)
26+
where TDbContext : DbContext;
27+
28+
/// <summary>
29+
/// Remove entities by providing their primary keys from the registered <see cref="DbContext"/>.
30+
/// </summary>
31+
/// <typeparam name="TEntity">Entity model type for which the provided key is related to.</typeparam>
32+
/// <param name="keys">Primary keys for entities to remove from the registered <see cref="DbContext"/>.</param>
33+
/// <returns>The same <see cref="DbContext"/> builder.</returns>
34+
IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TEntity>(IEnumerable<object> keys)
35+
where TEntity : class;
36+
37+
/// <summary>
38+
/// Remove entities by providing their primary keys from the registered <see cref="DbContext"/>.
39+
/// </summary>
40+
/// <typeparam name="TDbContext">Type of <see cref="DbContext"/> to set up.</typeparam>
41+
/// <typeparam name="TEntity">Entity model type for which the provided keys are related to.</typeparam>
42+
/// <param name="keys">Primary keys for entities to remove from the registered <see cref="DbContext"/>.</param>
43+
/// <returns>The same <see cref="DbContext"/> builder.</returns>
44+
IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TDbContext, TEntity>(IEnumerable<object> keys)
45+
where TDbContext : DbContext;
46+
947
/// <summary>
1048
/// Remove entity from the registered <see cref="DbContext"/>.
1149
/// </summary>
1250
/// <param name="entity">Entity to remove from the registered <see cref="DbContext"/>.</param>
1351
/// <returns>The same <see cref="DbContext"/> builder.</returns>
1452
IAndWithoutDbContextBuilder WithoutEntity(object entity);
15-
53+
1654
/// <summary>
1755
/// Remove entity from the registered <see cref="DbContext"/>.
1856
/// </summary>

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,52 @@ public WithoutDbContextBuilder(HttpTestContext testContext)
2323
{
2424
}
2525

26+
/// <inheritdoc />
27+
public IAndWithoutDbContextBuilder WithoutEntityByKey<TEntity>(object key)
28+
where TEntity : class
29+
=> this.WithoutEntityByKey<DbContext, TEntity>(key);
30+
31+
/// <inheritdoc />
32+
public IAndWithoutDbContextBuilder WithoutEntityByKey<TDbContext, TEntity>(object key)
33+
where TDbContext : DbContext
34+
{
35+
var dbContext = this.TestContext.GetDbContext<TDbContext>();
36+
37+
var entity = dbContext.Find(typeof(TEntity), key);
38+
if (entity == null)
39+
return this;
40+
41+
dbContext.Remove(entity);
42+
dbContext.SaveChanges();
43+
44+
return this;
45+
}
46+
47+
/// <inheritdoc />
48+
public IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TEntity>(IEnumerable<object> keys)
49+
where TEntity : class
50+
=> this.WithoutEntitiesByKeys<DbContext, TEntity>(keys);
51+
52+
/// <inheritdoc />
53+
public IAndWithoutDbContextBuilder WithoutEntitiesByKeys<TDbContext, TEntity>(IEnumerable<object> keys)
54+
where TDbContext : DbContext
55+
{
56+
var dbContext = this.TestContext.GetDbContext<TDbContext>();
57+
58+
var entityType = typeof(TEntity);
59+
var entities = keys
60+
.Select(key => dbContext.Find(entityType, key))
61+
.Where(entity => entity != null);
62+
63+
if (entities.Any() == false)
64+
return this;
65+
66+
dbContext.RemoveRange(entities);
67+
dbContext.SaveChanges();
68+
69+
return this;
70+
}
71+
2672
/// <inheritdoc />
2773
public IAndWithoutDbContextBuilder WithoutEntity(object entity)
2874
=> this.WithoutEntity<DbContext>(entity);
@@ -31,7 +77,7 @@ public IAndWithoutDbContextBuilder WithoutEntity(object entity)
3177
public IAndWithoutDbContextBuilder WithoutEntity<TDbContext>(object entity)
3278
where TDbContext : DbContext
3379
=> this.WithoutEntities<TDbContext>(entity);
34-
80+
3581
/// <inheritdoc />
3682
public IAndWithoutDbContextBuilder WithoutEntities(IEnumerable<object> entities)
3783
=> this.WithoutEntities(dbContext => dbContext.RemoveRange(entities));

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,5 +705,111 @@ public void WithoutSetReturnsCorrectDataWhenDeletingNonExistingObjects()
705705
.ShouldReturn()
706706
.NotFound();
707707
}
708+
709+
[Fact]
710+
public void WithoutDataByProvidingModelAndKeyInBuilderShouldRemoveTheCorrectObject()
711+
{
712+
MyApplication
713+
.StartsFrom<TestStartup>()
714+
.WithServices(services => services.AddDbContext<CustomDbContext>());
715+
716+
var model = new CustomModel
717+
{
718+
Id = 1,
719+
Name = "Test"
720+
};
721+
722+
MyController<DbContextController>
723+
.Instance()
724+
.WithData(model)
725+
.WithoutData(data => data.WithoutEntityByKey<CustomModel>(model.Id))
726+
.Calling(c => c.Get(model.Id))
727+
.ShouldReturn()
728+
.NotFound();
729+
}
730+
731+
[Fact]
732+
public void WithoutDataByProvidingNonExistingModelAndKeyInBuilderShouldRemoveTheCorrectObject()
733+
{
734+
MyApplication
735+
.StartsFrom<TestStartup>()
736+
.WithServices(services => services.AddDbContext<CustomDbContext>());
737+
738+
var model = new CustomModel
739+
{
740+
Id = 1,
741+
Name = "Test"
742+
};
743+
744+
var keyToRemove = int.MaxValue;
745+
MyController<DbContextController>
746+
.Instance()
747+
.WithData(model)
748+
.WithoutData(data => data.WithoutEntityByKey<CustomModel>(keyToRemove))
749+
.Calling(c => c.Get(model.Id))
750+
.ShouldReturn()
751+
.Ok(ok => ok
752+
.WithModelOfType<CustomModel>()
753+
.Passing(cm => cm.Name.Equals(model.Name)));
754+
}
755+
756+
[Fact]
757+
public void WithoutDataByProvidingModelAndMultpleKeysInBuilderShouldRemoveTheCorrectObjects()
758+
{
759+
MyApplication
760+
.StartsFrom<TestStartup>()
761+
.WithServices(services => services.AddDbContext<CustomDbContext>());
762+
763+
var models = new List<CustomModel>
764+
{
765+
new CustomModel
766+
{
767+
Id = 1,
768+
Name = "Test1"
769+
},
770+
new CustomModel
771+
{
772+
Id = 2,
773+
Name = "Test2"
774+
},
775+
};
776+
777+
var keys = models.Select(x => x.Id as object).ToList();
778+
MyController<DbContextController>
779+
.Instance()
780+
.WithData(models)
781+
.WithoutData(data => data.WithoutEntitiesByKeys<CustomModel>(keys))
782+
.Calling(c => c.Get(models[0].Id))
783+
.ShouldReturn()
784+
.NotFound();
785+
}
786+
787+
[Fact]
788+
public void WithoutDataByProvidingNonExistingModelAndMultpleKeysInBuilderShouldRemoveTheCorrectObjects()
789+
{
790+
MyApplication
791+
.StartsFrom<TestStartup>()
792+
.WithServices(services => services.AddDbContext<CustomDbContext>());
793+
794+
var models = new List<CustomModel>
795+
{
796+
new CustomModel
797+
{
798+
Id = 1,
799+
Name = "Test1"
800+
}
801+
};
802+
803+
var keys = new List<object> { int.MaxValue };
804+
MyController<DbContextController>
805+
.Instance()
806+
.WithData(models)
807+
.WithoutData(data => data.WithoutEntitiesByKeys<CustomModel>(keys))
808+
.Calling(c => c.Get(models[0].Id))
809+
.ShouldReturn()
810+
.Ok(ok => ok
811+
.WithModelOfType<CustomModel>()
812+
.Passing(cm => cm.Name.Equals(models[0].Name)));
813+
}
708814
}
709815
}

0 commit comments

Comments
 (0)