Skip to content

Commit 5cccc09

Browse files
committed
#283 Catching the case where non-existing data is being deleted and on SaveChanges() an exception was being thrown.
Adding tests for WithoutData with provided Set.
1 parent 0536950 commit 5cccc09

File tree

2 files changed

+113
-3
lines changed

2 files changed

+113
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public IAndDbContextBuilder WithSet<TDbContext, TEntity>(Action<DbSet<TEntity>>
8383
/// <inheritdoc />
8484
public IAndDbContextBuilder WithoutEntities(IEnumerable<object> entities)
8585
=> this.WithoutEntities(dbContext => dbContext.RemoveRange(entities));
86-
86+
8787
/// <inheritdoc />
8888
public IAndDbContextBuilder WithoutEntities<TDbContext>(IEnumerable<object> entities)
8989
where TDbContext : DbContext
@@ -113,7 +113,15 @@ public IAndDbContextBuilder WithoutEntities<TDbContext>(Action<TDbContext> dbCon
113113

114114
var dbContext = this.TestContext.GetDbContext<TDbContext>();
115115
dbContextSetup(dbContext);
116-
dbContext.SaveChanges();
116+
117+
try
118+
{
119+
dbContext.SaveChanges();
120+
}
121+
catch (DbUpdateConcurrencyException)
122+
{
123+
// Intentional silent fail.
124+
}
117125

118126
return this;
119127
}
@@ -132,7 +140,15 @@ public IAndDbContextBuilder WithoutSet<TDbContext, TEntity>(Action<DbSet<TEntity
132140

133141
var dbContext = this.TestContext.GetDbContext<TDbContext>();
134142
entitySetup(dbContext.Set<TEntity>());
135-
dbContext.SaveChanges();
143+
144+
try
145+
{
146+
dbContext.SaveChanges();
147+
}
148+
catch (DbUpdateConcurrencyException)
149+
{
150+
// Intentional silent fail.
151+
}
136152

137153
return this;
138154
}

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,5 +578,99 @@ public void WithoutDataReturnsCorrectDataWhenAllTableEntitiesAreDeleted()
578578
.ShouldReturn()
579579
.NotFound();
580580
}
581+
582+
[Fact]
583+
public void WithoutDataReturnsCorrectDataWhenDeletingNonExistingObjects()
584+
{
585+
MyApplication
586+
.StartsFrom<TestStartup>()
587+
.WithServices(services => services.AddDbContext<CustomDbContext>());
588+
589+
var models = new List<CustomModel> {
590+
new CustomModel
591+
{
592+
Id = 1,
593+
Name = "Test"
594+
}};
595+
596+
MyController<DbContextController>
597+
.Instance()
598+
.WithoutData(models)
599+
.Calling(c => c.GetAll())
600+
.ShouldReturn()
601+
.NotFound();
602+
}
603+
604+
[Fact]
605+
public void WithoutSetReturnsCorrectDataWhenWholeRangeIsRemoved()
606+
{
607+
MyApplication
608+
.StartsFrom<TestStartup>()
609+
.WithServices(services => services.AddDbContext<CustomDbContext>());
610+
611+
var models = new List<CustomModel> {
612+
new CustomModel
613+
{
614+
Id = 1,
615+
Name = "Test"
616+
}};
617+
618+
MyController<DbContextController>
619+
.Instance()
620+
.WithData(models)
621+
.WithoutData(data =>
622+
data.WithoutSet<CustomModel>(
623+
cm => cm.RemoveRange(models)))
624+
.Calling(c => c.GetAll())
625+
.ShouldReturn()
626+
.NotFound();
627+
}
628+
629+
[Fact]
630+
public void WithoutSetReturnsCorrectDataWhenPartialEntitiesAreRemoved()
631+
{
632+
MyApplication
633+
.StartsFrom<TestStartup>()
634+
.WithServices(services => services.AddDbContext<CustomDbContext>());
635+
636+
var models = new List<CustomModel> {
637+
new CustomModel
638+
{
639+
Id = 1,
640+
Name = "Test1"
641+
},
642+
new CustomModel
643+
{
644+
Id = 2,
645+
Name = "Test2"
646+
}};
647+
648+
MyController<DbContextController>
649+
.Instance()
650+
.WithData(models)
651+
.WithoutData(data =>
652+
data.WithoutSet<CustomModel>(
653+
cm => cm.Remove(models.Last())))
654+
.Calling(c => c.GetAll())
655+
.ShouldReturn()
656+
.Ok(ok => ok
657+
.WithModelOfType<List<CustomModel>>()
658+
.Passing(mdls => mdls.Count == 1));
659+
}
660+
661+
[Fact]
662+
public void WithoutSetReturnsCorrectDataWhenDeletingNonExistingObjects()
663+
{
664+
MyApplication
665+
.StartsFrom<TestStartup>()
666+
.WithServices(services => services.AddDbContext<CustomDbContext>());
667+
668+
MyController<DbContextController>
669+
.Instance()
670+
.WithoutData(data => data.WithoutSet<CustomModel>(cm => cm.Remove(new CustomModel())))
671+
.Calling(c => c.GetAll())
672+
.ShouldReturn()
673+
.NotFound();
674+
}
581675
}
582676
}

0 commit comments

Comments
 (0)