Repository navigation
Expand file tree
/
Copy pathNullableNavigationTests.cs
More file actions
334 lines (280 loc) · 10.2 KB
/
Copy pathNullableNavigationTests.cs
File metadata and controls
334 lines (280 loc) · 10.2 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
public class NullableNavigationTests
{
[Test]
public async Task Include()
{
await using var data = await BuildData(nameof(Include));
var cars = await data.Cars
.Include(_ => _.Owner)
.OrderBy(_ => _.Id)
.ToListAsync();
await Verify(cars);
}
[Test]
public async Task IgnoreNavigationProperties()
{
await using var data = await BuildData(nameof(IgnoreNavigationProperties));
var cars = await data.Cars
.Include(_ => _.Owner)
.OrderBy(_ => _.Id)
.ToListAsync();
await Verify(cars)
.IgnoreNavigationProperties(data);
}
[Test]
public async Task SuppressNull()
{
await using var data = await BuildData(nameof(SuppressNull));
var cars = await data.Cars
.Where(_ => _.Owner!.Name == "owner")
.ToListAsync();
await Verify(cars);
}
[Test]
public async Task RedundantNullCheck()
{
await using var data = await BuildData(nameof(RedundantNullCheck));
#region RedundantNullCheck
await ThrowsTask(() =>
data.Cars
.Where(_ => _.Owner != null && _.Owner.Name == "owner")
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task RedundantNullCheckReversed()
{
await using var data = await BuildData(nameof(RedundantNullCheckReversed));
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Cars
.Where(_ => _.Owner!.Id > 0 && null != _.Owner)
.ToListAsync());
}
// a null Owner matches `!=`, so the check changes the result
[Test]
public async Task NullCheckKeptForNotEqual()
{
await using var data = await BuildData(nameof(NullCheckKeptForNotEqual));
var cars = await data.Cars
.Where(_ => _.Owner != null && _.Owner.Name != "other")
.ToListAsync();
await Assert.That(cars).Count().IsEqualTo(1);
}
// a null Owner matches when the variable is null, so the check changes the result
[Test]
public async Task NullCheckKeptForVariable()
{
await using var data = await BuildData(nameof(NullCheckKeptForVariable));
string? name = null;
var cars = await data.Cars
.Where(_ => _.Owner != null && _.Owner.Name == name)
.ToListAsync();
await Assert.That(cars).IsEmpty();
}
[Test]
public async Task RedundantNullCheckNullableScalar()
{
await using var data = await BuildData(nameof(RedundantNullCheckNullableScalar));
#region RedundantNullCheckNullableScalar
await ThrowsTask(() =>
data.Cars
.Where(_ => _.OwnerId != null && _.OwnerId > 0)
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task RedundantNullCheckHasValue()
{
await using var data = await BuildData(nameof(RedundantNullCheckHasValue));
await ThrowsTask(() =>
data.Cars
.Where(_ => _.OwnerId.HasValue && _.OwnerId.Value == 1)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task RedundantNullCheckString()
{
await using var data = await BuildData(nameof(RedundantNullCheckString));
await ThrowsTask(() =>
data.Cars
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
.Where(_ => _.Model != null &&
_.Model == "with owner")
.ToListAsync())
.IgnoreStackTrace();
}
// a null OwnerId matches `!=`, so the check changes the result
[Test]
public async Task NullableScalarCheckKeptForNotEqual()
{
await using var data = await BuildData(nameof(NullableScalarCheckKeptForNotEqual));
var cars = await data.Cars
.Where(_ => _.OwnerId != null && _.OwnerId != 2)
.ToListAsync();
await Assert.That(cars).Count().IsEqualTo(1);
}
[Test]
public async Task RedundantConditionalNullCheck()
{
await using var data = await BuildData(nameof(RedundantConditionalNullCheck));
#region RedundantConditionalNullCheck
await ThrowsTask(() =>
data.Cars
.OrderBy(_ => _.Owner == null)
.ThenBy(_ => _.Owner == null ? null : _.Owner.Name)
.ToListAsync())
.IgnoreStackTrace();
#endregion
}
[Test]
public async Task RedundantConditionalNullCheckNotEqual()
{
await using var data = await BuildData(nameof(RedundantConditionalNullCheckNotEqual));
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Cars
.Select(_ => _.Owner != null ? _.Owner.Name : null)
.ToListAsync());
}
[Test]
public async Task RedundantConditionalNullCheckInWhere()
{
await using var data = await BuildData(nameof(RedundantConditionalNullCheckInWhere));
await Assert.ThrowsExactlyAsync<Exception>(() =>
data.Cars
.Where(_ => (null == _.Owner ? null : _.Owner.Name) == "owner")
.ToListAsync());
}
// the member is null when the navigation is, so it returns the same values as the conditional
[Test]
public async Task ConditionalNullCheckRemoved()
{
await using var data = await BuildData(nameof(ConditionalNullCheckRemoved));
var names = await data.Cars
.OrderBy(_ => _.Id)
.Select(_ => _.Owner!.Name)
.ToListAsync();
await Assert.That(names).IsEquivalentTo(["owner", null]);
}
// a null Owner gives "none", not null, so the check changes the result
[Test]
public async Task ConditionalNullCheckKeptForNonNullFallback()
{
await using var data = await BuildData(nameof(ConditionalNullCheckKeptForNonNullFallback));
var names = await data.Cars
.OrderBy(_ => _.Id)
.Select(_ => _.Owner == null ? "none" : _.Owner.Name)
.ToListAsync();
await Assert.That(names).IsEquivalentTo(["owner", "none"]);
}
[Test]
public async Task RedundantConditionalNullCheckValueType()
{
await using var data = await BuildData(nameof(RedundantConditionalNullCheckValueType));
await ThrowsTask(() =>
data.Cars
.Select(_ => _.Owner == null ? (int?) null : _.Owner.Id)
.ToListAsync())
.IgnoreStackTrace();
}
// the converted member is null when the navigation is, so it returns the same values as the conditional
[Test]
public async Task ConditionalNullCheckRemovedValueType()
{
await using var data = await BuildData(nameof(ConditionalNullCheckRemovedValueType));
var ids = await data.Cars
.OrderBy(_ => _.Id)
.Select(_ => (int?) _.Owner!.Id)
.ToListAsync();
await Assert.That(ids).IsEquivalentTo([1, (int?) null]);
}
[Test]
public async Task RedundantConditionalNullCheckHasValue()
{
await using var data = await BuildData(nameof(RedundantConditionalNullCheckHasValue));
await ThrowsTask(() =>
data.Cars
// ReSharper disable once MergeConditionalExpression
.Select(_ => _.OwnerId.HasValue ? _.OwnerId.Value : (int?) null)
.ToListAsync())
.IgnoreStackTrace();
}
// EF returns an empty collection for a null navigation, with or without the check, so the check never gives null
[Test]
public async Task RedundantConditionalNullCheckCollection()
{
await using var data = await BuildData(nameof(RedundantConditionalNullCheckCollection));
await ThrowsTask(() =>
data.Cars
.Select(_ => _.Owner == null ? null : _.Owner.Cars)
.ToListAsync())
.IgnoreStackTrace();
}
[Test]
public async Task ConditionalNullCheckRemovedCollection()
{
await using var data = await BuildData(nameof(ConditionalNullCheckRemovedCollection));
var cars = await data.Cars
.OrderBy(_ => _.Id)
.Select(_ => _.Owner!.Cars)
.ToListAsync();
await Assert.That(cars[1]).IsEmpty();
}
[Test]
public async Task AllData()
{
await using var data = await BuildData(nameof(AllData));
await Verify(data.AllData())
.AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.Objects);
}
static async Task<NullableNavigationDbContext> BuildData(string name)
{
var builder = new DbContextOptionsBuilder<NullableNavigationDbContext>();
builder.UseInMemoryDatabase(nameof(NullableNavigationTests) + name);
builder.ThrowOnAntiPatterns();
var data = new NullableNavigationDbContext(builder.Options);
var owner = new Owner
{
Id = 1,
Name = "owner"
};
data.AddRange(
new Car
{
Id = 1,
Model = "with owner",
Owner = owner
},
new Car
{
Id = 2,
Model = "without owner"
});
await data.SaveChangesAsync();
data.ChangeTracker.Clear();
return data;
}
public class NullableNavigationDbContext(DbContextOptions options) :
DbContext(options)
{
public DbSet<Car> Cars { get; set; } = null!;
public DbSet<Owner> Owners { get; set; } = null!;
}
public class Car
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public required string Model { get; set; }
public int? OwnerId { get; set; }
public Owner? Owner { get; set; }
}
public class Owner
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public required string Name { get; set; }
public List<Car> Cars { get; set; } = [];
}
}