Skip to content

Commit 75c27d3

Browse files
committed
Additional test for cascading changes
1 parent 61d9535 commit 75c27d3

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes.Models;
7+
using EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes.Triggers;
8+
using Microsoft.EntityFrameworkCore;
9+
10+
namespace EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes
11+
{
12+
public class ApplicationDbContext : DbContext
13+
{
14+
readonly string _databaseName;
15+
16+
public ApplicationDbContext(string databaseName)
17+
{
18+
_databaseName = databaseName;
19+
}
20+
21+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
22+
{
23+
optionsBuilder
24+
.UseInMemoryDatabase(_databaseName)
25+
.UseTriggers(triggerOptions => {
26+
triggerOptions.AddTrigger<SoftDelete>();
27+
});
28+
}
29+
30+
protected override void OnModelCreating(ModelBuilder modelBuilder)
31+
{
32+
modelBuilder.Entity<Branch>()
33+
.HasOne(x => x.Parent)
34+
.WithMany(x => x.Children)
35+
.HasForeignKey(x => x.ParentId)
36+
.OnDelete(DeleteBehavior.Cascade);
37+
}
38+
39+
public DbSet<Branch> Branches => Set<Branch>();
40+
}
41+
42+
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes.Models;
7+
using ScenarioTests;
8+
using Xunit;
9+
10+
namespace EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes
11+
{
12+
public partial class CascadingSoftDeletesTestScenario
13+
{
14+
[Scenario(NamingPolicy = ScenarioTestMethodNamingPolicy.Test)]
15+
public void Scenario(ScenarioContext scenario)
16+
{
17+
var dbContext = new ApplicationDbContext(scenario.TargetName);
18+
19+
var level0 = new Branch();
20+
var level1 = new Branch() { Parent = level0 };
21+
var level2 = new Branch() { Parent = level1 };
22+
23+
dbContext.AddRange(level0, level1, level2);
24+
{
25+
var result = dbContext.SaveChanges();
26+
Assert.Equal(3, result);
27+
}
28+
29+
scenario.Fact("1. Soft delete works on the depest level", () => {
30+
dbContext.Remove(level2);
31+
var result = dbContext.SaveChanges();
32+
33+
Assert.Equal(1, result);
34+
Assert.NotNull(level2.DeletedOn);
35+
});
36+
37+
scenario.Fact("2. Soft delete cascades at least 1 level deep", () => {
38+
dbContext.Remove(level1);
39+
var result = dbContext.SaveChanges();
40+
41+
Assert.Equal(2, result);
42+
Assert.NotNull(level1.DeletedOn);
43+
Assert.NotNull(level2.DeletedOn);
44+
});
45+
46+
scenario.Fact("3. Soft delete cascades multiple levels deep", () => {
47+
dbContext.Remove(level0);
48+
var result = dbContext.SaveChanges();
49+
50+
Assert.Equal(3, result);
51+
Assert.NotNull(level0.DeletedOn);
52+
Assert.NotNull(level1.DeletedOn);
53+
Assert.NotNull(level2.DeletedOn);
54+
});
55+
}
56+
}
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.VisualBasic;
7+
8+
namespace EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes.Models
9+
{
10+
public class Branch
11+
{
12+
public int Id { get; set; }
13+
public int? ParentId { get; set; }
14+
public DateTime? DeletedOn { get; set; }
15+
16+
public Branch Parent { get; set; }
17+
public ICollection<Branch> Children { get; set; }
18+
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using EntityFrameworkCore.Triggered.Extensions;
7+
using EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes.Models;
8+
9+
namespace EntityFrameworkCore.Triggered.IntegrationTests.CascadingSoftDeletes.Triggers
10+
{
11+
public class SoftDelete : Trigger<Branch>
12+
{
13+
readonly ApplicationDbContext _applicationDbContext;
14+
15+
public SoftDelete(ApplicationDbContext applicationDbContext)
16+
{
17+
_applicationDbContext = applicationDbContext;
18+
}
19+
20+
public override void BeforeSave(ITriggerContext<Branch> context)
21+
{
22+
if (context.ChangeType is ChangeType.Deleted)
23+
{
24+
_applicationDbContext.Entry(context.Entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
25+
context.Entity.DeletedOn = DateTime.UtcNow;
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)