-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathInMemoryRecordingTests.cs
More file actions
456 lines (367 loc) · 12.8 KB
/
Copy pathInMemoryRecordingTests.cs
File metadata and controls
456 lines (367 loc) · 12.8 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
public class InMemoryRecordingTests
{
// ReSharper disable once UnusedVariable
static void Build(string databaseName)
{
#region EnableRecordingInMemory
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(databaseName);
builder.EnableRecording();
var data = new SampleDbContext(builder.Options);
#endregion
}
[Test]
public async Task RecordingInMemory()
{
await using var data = BuildData();
#region RecordingInMemory
Recording.Start();
data.Add(
new Company
{
Id = 1,
Name = "Title"
});
await data.SaveChangesAsync();
await data
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();
await Verify();
#endregion
}
[Test]
public async Task Parameters()
{
await using var data = BuildData();
Recording.Start();
var name = "Title";
var ids = new[] { 1, 2, 4, 6 };
await data
.Companies
.Where(_ => _.Name == name && ids.Contains(_.Id))
.ToListAsync();
await Verify();
}
[Test]
public async Task Include()
{
await using var data = BuildData();
Recording.Start();
await data
.Companies
.Include(_ => _.Employees)
.OrderBy(_ => _.Name)
.ThenBy(_ => _.Id)
.ToListAsync();
await Verify();
}
[Test]
public async Task Scalar()
{
await using var data = BuildData();
Recording.Start();
await data.Companies.CountAsync();
await data.Companies.AnyAsync(_ => _.Name == "Title");
await Verify();
}
[Test]
public async Task Sync()
{
await using var data = BuildData();
Recording.Start();
data.Add(
new Company
{
Id = 1,
Name = "Title"
});
await data.SaveChangesAsync();
data
.Companies
.Where(_ => _.Name == "Title")
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
.ToList();
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
data.Companies.Count();
await Verify();
}
[Test]
public async Task Modified()
{
await using var data = BuildData();
data.Add(
new Company
{
Id = 1,
Name = "Before"
});
await data.SaveChangesAsync();
Recording.Start();
var company = await data.Companies.SingleAsync();
company.Name = "After";
await data.SaveChangesAsync();
await Verify();
}
[Test]
public async Task Deleted()
{
await using var data = BuildData();
var company = new Company
{
Id = 1,
Name = "Company"
};
data.Add(company);
data.Add(
new Employee
{
Id = 2,
Name = "Employee",
Company = company
});
await data.SaveChangesAsync();
Recording.Start();
// cascades to the employee
data.Remove(company);
await data.SaveChangesAsync();
await Verify();
}
// the interceptor runs after the one added by EnableRecording
[Test]
public async Task ChangedByLaterInterceptor()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(ChangedByLaterInterceptor));
builder.EnableRecording();
builder.AddInterceptors(new UpperCaseNameInterceptor());
await using var data = new SampleDbContext(builder.Options);
Recording.Start();
data.Add(
new Company
{
Id = 1,
Name = "Title"
});
await data.SaveChangesAsync();
await Verify();
}
class UpperCaseNameInterceptor :
SaveChangesInterceptor
{
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData data, InterceptionResult<int> result, Cancel cancel = default)
{
foreach (var entry in data.Context!.ChangeTracker.Entries<Company>())
{
entry.Entity.Name = entry.Entity.Name.ToUpperInvariant();
}
return new(result);
}
}
[Test]
public async Task NoChanges()
{
await using var data = BuildData();
Recording.Start();
await data.SaveChangesAsync();
await Assert.That(Recording.Stop()).IsEmpty();
}
[Test]
public async Task DisableRecording()
{
await using var data = BuildData();
Recording.Start();
await data
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();
data.DisableRecording();
await data
.Companies
.Where(_ => _.Name == "Disabled")
.ToListAsync();
data.Add(
new Company
{
Id = 1,
Name = "Disabled"
});
await data.SaveChangesAsync();
await Verify();
}
// a pooled context keeps its InstanceId, so disabling was carried over to every later lease
[Test]
public async Task DisableRecordingPooled()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(DisableRecordingPooled));
builder.EnableRecording();
var factory = new PooledDbContextFactory<SampleDbContext>(builder.Options);
SampleDbContext disabled;
await using (var data = await factory.CreateDbContextAsync())
{
disabled = data;
data.DisableRecording();
}
await using (var data = await factory.CreateDbContextAsync())
{
await Assert.That(data).IsSameReferenceAs(disabled);
Recording.Start();
await data
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();
await Verify();
}
}
[Test]
public async Task Identifier()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(Identifier));
builder.EnableRecording(nameof(InMemoryRecordingTests) + nameof(Identifier));
await using var data = new SampleDbContext(builder.Options);
Recording.Start(nameof(InMemoryRecordingTests) + nameof(Identifier));
data.Add(
new Company
{
Id = 1,
Name = "Title"
});
await data.SaveChangesAsync();
await data
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();
var entries = Recording.Stop(nameof(InMemoryRecordingTests) + nameof(Identifier));
await Verify(entries);
}
[Test]
public async Task MultiDbContexts()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(MultiDbContexts));
builder.EnableRecording();
await using var data1 = new SampleDbContext(builder.Options);
Recording.Start();
data1.Add(
new Company
{
Id = 1,
Name = "Title"
});
await data1.SaveChangesAsync();
await using var data2 = new SampleDbContext(builder.Options);
await data2
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();
await Verify();
}
// the query compiler is registered before the provider adds its services
[Test]
public async Task EnableRecordingBeforeProvider()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.EnableRecording();
builder.UseInMemoryDatabase(nameof(EnableRecordingBeforeProvider));
await using var data = new SampleDbContext(builder.Options);
Recording.Start();
await data
.Companies
.Where(_ => _.Name == "Title")
.ToListAsync();
await Verify();
}
// EF does not apply extension services to an internal service provider, so nothing is recorded and anti-patterns
// are not detected. But it also rejects ReplaceService, and AddInterceptors with a singleton interceptor, there,
// which EnableRecording must not use
[Test]
public async Task InternalServiceProvider()
{
var provider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(InternalServiceProvider));
builder.UseInternalServiceProvider(provider);
builder.EnableRecording();
await using var data = new SampleDbContext(builder.Options);
await data.Companies.ToListAsync();
}
// at the cost of the InMemory queries not being recorded
[Test]
public async Task KeepsReplacedQueryCompiler()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(KeepsReplacedQueryCompiler));
((IDbContextOptionsBuilderInfrastructure) builder).AddOrUpdateExtension(new QueryCompilerExtension());
builder.EnableRecording();
await using var data = new SampleDbContext(builder.Options);
await Assert.That(data.GetService<IQueryCompiler>()).IsTypeOf<QueryCompiler>();
}
// a library that adds, rather than replaces, its IQueryCompiler leaves EF's default as the first registration.
// Replacing that would remove the default and put RecordingQueryCompiler last, displacing the library's.
[Test]
public async Task KeepsAddedQueryCompiler()
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(nameof(KeepsAddedQueryCompiler));
((IDbContextOptionsBuilderInfrastructure) builder).AddOrUpdateExtension(new AddQueryCompilerExtension());
builder.EnableRecording();
await using var data = new SampleDbContext(builder.Options);
await Assert.That(data.GetService<IQueryCompiler>()).IsTypeOf<QueryCompiler>();
}
class AddQueryCompilerExtension :
IDbContextOptionsExtension
{
public DbContextOptionsExtensionInfo Info => field ??= new ExtensionInfo(this);
public void ApplyServices(IServiceCollection services) =>
services.AddScoped<IQueryCompiler>(_ => ActivatorUtilities.CreateInstance<QueryCompiler>(_));
public void Validate(IDbContextOptions options)
{
}
class ExtensionInfo(IDbContextOptionsExtension extension) :
DbContextOptionsExtensionInfo(extension)
{
public override bool IsDatabaseProvider => false;
public override string LogFragment => "";
public override int GetServiceProviderHashCode() => 0;
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) =>
other is ExtensionInfo;
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
}
}
}
// mimics a library, for example EntityFrameworkCore.Projectables, that replaces IQueryCompiler
class QueryCompilerExtension :
IDbContextOptionsExtension
{
public DbContextOptionsExtensionInfo Info => field ??= new ExtensionInfo(this);
public void ApplyServices(IServiceCollection services) =>
services.Replace(ServiceDescriptor.Scoped<IQueryCompiler>(_ => ActivatorUtilities.CreateInstance<QueryCompiler>(_)));
public void Validate(IDbContextOptions options)
{
}
class ExtensionInfo(IDbContextOptionsExtension extension) :
DbContextOptionsExtensionInfo(extension)
{
public override bool IsDatabaseProvider => false;
public override string LogFragment => "";
public override int GetServiceProviderHashCode() => 0;
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other) =>
other is ExtensionInfo;
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
}
}
}
static SampleDbContext BuildData([CallerMemberName] string databaseName = "")
{
var builder = new DbContextOptionsBuilder<SampleDbContext>();
builder.UseInMemoryDatabase(databaseName);
builder.EnableRecording();
return new(builder.Options);
}
}