Skip to content

Commit ad8e2a3

Browse files
committed
Basic integration test
1 parent 23c3200 commit ad8e2a3

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

test/EntityFrameworkCore.Triggered.IntegrationTests/ApplicationDbContext.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ namespace EntityFrameworkCore.Triggered.IntegrationTests
1010
{
1111
public class ApplicationDbContext : DbContext
1212
{
13+
readonly string _databaseName;
14+
15+
public ApplicationDbContext(string databaseName)
16+
{
17+
_databaseName = databaseName;
18+
}
19+
1320
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1421
{
15-
optionsBuilder.UseInMemoryDatabase("test");
22+
optionsBuilder.UseInMemoryDatabase(_databaseName);
1623
optionsBuilder.UseTriggers(triggerOptions => {
1724
triggerOptions.AddAssemblyTriggers();
1825
});

test/EntityFrameworkCore.Triggered.IntegrationTests/Models/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public class User
1212

1313
public string UserName { get; set; }
1414

15-
public DateTime DeletedDate { get; set; }
15+
public DateTime? DeletedDate { get; set; }
1616
}
1717
}

test/EntityFrameworkCore.Triggered.IntegrationTests/TestScenario.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,52 @@ public void TestScenario1(ScenarioContext scenario)
1818
{
1919
const int sampleUsersCount = 100;
2020

21-
var dbContext = new ApplicationDbContext();
21+
var dbContext = new ApplicationDbContext(scenario.TargetName);
2222

2323
// step 1: Populate database with users
24-
{
25-
dbContext.Users.AddRange(Enumerable.Range(0, sampleUsersCount).Select(x => new User {
26-
UserName = $"user{x}"
27-
}));
24+
dbContext.Users.AddRange(Enumerable.Range(0, sampleUsersCount).Select(x => new User {
25+
UserName = $"user{x}"
26+
}));
2827

29-
dbContext.SaveChanges();
30-
}
28+
dbContext.SaveChanges();
3129

3230
scenario.Fact("Database saved all users", () => {
3331
var usersCount = dbContext.Users.Count();
3432
Assert.Equal(sampleUsersCount, usersCount);
3533
});
34+
35+
// step 2: Delete the last 50 users
36+
dbContext.RemoveRange(
37+
dbContext.Users.Where(x => x.Id > sampleUsersCount / 2)
38+
);
39+
40+
dbContext.SaveChanges();
41+
42+
scenario.Fact("We should still have all our users", () => {
43+
var usersCount = dbContext.Users.Count();
44+
Assert.Equal(sampleUsersCount, usersCount);
45+
});
46+
47+
scenario.Fact("However, half of them will have been soft deleted", () => {
48+
var usersCount = dbContext.Users.Where(x => x.DeletedDate != null).Count();
49+
Assert.Equal(sampleUsersCount / 2, usersCount);
50+
});
51+
52+
// step 3: Undo deletion of our users
53+
{
54+
var users = dbContext.Users.Where(x => x.DeletedDate != null);
55+
foreach (var user in users)
56+
{
57+
user.DeletedDate = null;
58+
}
59+
60+
dbContext.SaveChanges();
61+
}
62+
63+
scenario.Fact("Ensure that all users are restored", () => {
64+
var usersCount = dbContext.Users.Where(x => x.DeletedDate != null).Count();
65+
Assert.Equal(0, usersCount);
66+
});
3667
}
3768
}
3869
}

0 commit comments

Comments
 (0)