This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEndToEndTest.cs
279 lines (228 loc) · 10.4 KB
/
EndToEndTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Linq;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using EFCore.TimeTraveler;
using EFCore.TimeTravelerTests.DataAccess;
using EFCore.TimeTravelerTests.Entities;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
namespace EFCore.TimeTravelerTests
{
/// <summary>
/// One large end-to-end test
/// </summary>
[TestFixture]
internal class EndToEndTest
{
[SetUp]
public async Task ResetDb()
{
await DatabaseSetupFixture.ResetDb();
}
private static AutofacServiceProvider ServiceProvider => DatabaseSetupFixture.ServiceProvider;
private static async
Task<(Guid appleId, DateTime unripeAppleTime, DateTime ripeAppleTime, DateTime rottenAppleTime)>
SimulateLifecycleOfMyApple()
{
var appleId = Guid.NewGuid();
await SetInitialFruitStatus(appleId);
var unripeAppleTime = TestHelper.UtcNow;
await UpdateFruitStatus(appleId, FruitStatus.Ripe);
var ripeAppleTime = TestHelper.UtcNow;
await UpdateFruitStatus(appleId, FruitStatus.Rotten, new[] {"Moe"});
await GiveMoeFriendsAndWeapons(appleId);
var rottenAppleTime = TestHelper.UtcNow;
await UpdateFruitStatus(appleId, FruitStatus.Fuzzy, new[] {"Hairy", "Curly"});
await GiveHairyAndCurlyFriendsAndWeapons(appleId);
return (appleId, unripeAppleTime, ripeAppleTime, rottenAppleTime);
}
private static async Task AssertWormsNavigationPropertyAtRottenAppleTime(DateTime rottenAppleTime, Guid appleId)
{
using (TemporalQuery.AsOf(rottenAppleTime))
{
using var localScope = ServiceProvider.CreateScope();
var context = localScope.ServiceProvider.GetService<ApplicationDbContext>();
var rottenAppleWorms = await context.Apples
.Where(a => a.Id == appleId)
.SelectMany(a => a.Worms)
.Include(worm => worm.Weapons)
.AsNoTracking().ToListAsync();
rottenAppleWorms
.Should()
.BeEquivalentTo(new[] {new {Name = "Moe", Weapons = new[] {new {Name = "Bazooka"}}}},
options => options.ExcludingMissingMembers());
}
}
private static async Task AssertWormlessFruitStatusAtTime(
FruitStatus expectedFruitStatus,
DateTime appleTime,
Guid appleId)
{
using (TemporalQuery.AsOf(appleTime))
{
var apple = await GetApple(appleId);
apple
.Should()
.BeEquivalentTo(new {FruitStatus = expectedFruitStatus, Worms = Array.Empty<Worm>()},
options => options.ExcludingMissingMembers());
}
}
private static async Task GiveMoeFriendsAndWeapons(Guid appleId)
{
using var localScope = ServiceProvider.CreateScope();
var context = localScope.ServiceProvider.GetService<ApplicationDbContext>();
var moe = await context.Apples
.Where(a => a.Id == appleId)
.SelectMany(a => a.Worms)
.SingleAsync();
moe.Weapons.Add(new WormWeapon {Name = "Bazooka"});
moe.FriendshipsA.AddRange(new[]
{
new WormFriendship
{
WormA = moe,
WormB = new Worm {Name = "John", Apple = new Apple {FruitStatus = FruitStatus.Ripe}}
},
new WormFriendship
{
WormA = moe,
WormB = new Worm {Name = "Ringo", Apple = new Apple {FruitStatus = FruitStatus.Rotten}}
}
});
await context.SaveChangesAsync();
}
private static async Task GiveHairyAndCurlyFriendsAndWeapons(Guid appleId)
{
using var localScope = ServiceProvider.CreateScope();
var context = localScope.ServiceProvider.GetService<ApplicationDbContext>();
var worms = await context.Apples
.Where(a => a.Id == appleId)
.SelectMany(a => a.Worms)
.Include(worm => worm.Weapons)
.Include(worm => worm.FriendshipsA)
.ThenInclude(friendship => friendship.WormB)
.ToListAsync();
var curly = worms.Single(worm => worm.Name == "Curly");
var hairy = worms.Single(worm => worm.Name == "Hairy");
hairy.Weapons.Add(new WormWeapon {Name = "Super Banana Bomb"});
hairy.Weapons.Add(new WormWeapon {Name = "Holy Hand Grenade"});
var joan = new Worm {Name = "Joan", Apple = new Apple {FruitStatus = FruitStatus.Unripe}};
joan.Weapons.Add(new WormWeapon {Name = "Dodgy Phone Battery"});
joan.Weapons.Add(new WormWeapon {Name = "Angry Concrete Donkey"});
hairy.FriendshipsA.AddRange(new[]
{
new WormFriendship {WormA = hairy, WormB = curly}, new WormFriendship {WormA = hairy, WormB = joan},
new WormFriendship
{
WormA = hairy,
WormB = new Worm {Name = "Starr", Apple = new Apple {FruitStatus = FruitStatus.Fuzzy}}
}
});
var moe = worms.Single(worm => worm.Name == "Moe");
moe.Weapons.First().Name = "Bazooka Pie";
moe.FriendshipsA.RemoveAll(friendship => friendship.WormB.Name == "Ringo");
await context.SaveChangesAsync();
}
private static async Task<Apple> GetApple(Guid appleId)
{
using var localScope = ServiceProvider.CreateScope();
var context = localScope.ServiceProvider.GetService<ApplicationDbContext>();
return await context.Apples
.Include(apple => apple.Worms)
.ThenInclude(worm => worm.Weapons)
.Include(apple => apple.Worms)
.ThenInclude(worm => worm.FriendshipsA)
.ThenInclude(friendship => friendship.WormB)
.ThenInclude(worm => worm.Weapons)
.Include(apple => apple.Worms)
.ThenInclude(worm => worm.FriendshipsB)
.ThenInclude(friendship => friendship.WormA)
.ThenInclude(worm => worm.Weapons)
.Where(a => a.Id == appleId)
.AsNoTracking()
.SingleAsync();
}
private static async Task SetInitialFruitStatus(Guid appleId)
{
using var localScope = ServiceProvider.CreateScope();
var context = localScope.ServiceProvider.GetService<ApplicationDbContext>();
var myApple = new Apple {Id = appleId, FruitStatus = FruitStatus.Unripe};
context.Apples.Add(myApple);
await context.SaveChangesAsync();
}
private static async Task UpdateFruitStatus(Guid appleId, FruitStatus fruitStatus, string[] worms = null)
{
using var localScope = ServiceProvider.CreateScope();
worms ??= Array.Empty<string>();
var context = localScope.ServiceProvider.GetService<ApplicationDbContext>();
var myApple = await context.Apples.Where(a => a.Id == appleId).SingleAsync();
myApple.FruitStatus = fruitStatus;
myApple.Worms.AddRange(worms.Select(wormName => new Worm {Name = wormName}));
await context.SaveChangesAsync();
}
[Test]
public async Task TemporalQuery_Should_AllowAllTemporalEntitiesToTimeTravel()
{
var (appleId, unripeAppleTime, ripeAppleTime, rottenAppleTime) = await SimulateLifecycleOfMyApple();
// Validate current state of my apple
var currentApple = await GetApple(appleId);
currentApple
.Should()
.BeEquivalentTo(
new
{
FruitStatus = FruitStatus.Fuzzy,
Worms = new object[]
{
new
{
Name = "Hairy",
Weapons = new[]
{
new {Name = "Holy Hand Grenade"}, new {Name = "Super Banana Bomb"}
},
FriendsNames = new[] {"Joan", "Starr", "Curly"}
},
new {Name = "Curly"},
new
{
Name = "Moe",
Weapons = new[] {new {Name = "Bazooka Pie"}},
FriendsNames = new[] {"John"}
}
}
}, options => options.ExcludingMissingMembers());
// Query the state of my apple a prior "system time" when the apple was rotten
using (TemporalQuery.AsOf(rottenAppleTime))
{
var rottenApple = await GetApple(appleId);
rottenApple
.Should()
.BeEquivalentTo(
new
{
FruitStatus = FruitStatus.Rotten,
Worms = new object[]
{
new
{
Name = "Moe",
Weapons = new[] {new {Name = "Bazooka"}},
FriendsNames = new[] {"John", "Ringo"}
}
}
},
options => options.ExcludingMissingMembers());
}
await Task.WhenAll(new[]
{
AssertWormlessFruitStatusAtTime(FruitStatus.Ripe, ripeAppleTime, appleId),
AssertWormlessFruitStatusAtTime(FruitStatus.Unripe, unripeAppleTime, appleId),
AssertWormsNavigationPropertyAtRottenAppleTime(rottenAppleTime, appleId)
});
}
}
}