forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonSharedPrimitiveCollectionsQueryRelationalTestBase.cs
More file actions
305 lines (256 loc) · 12.3 KB
/
NonSharedPrimitiveCollectionsQueryRelationalTestBase.cs
File metadata and controls
305 lines (256 loc) · 12.3 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.EntityFrameworkCore.Query;
#nullable disable
public abstract class NonSharedPrimitiveCollectionsQueryRelationalTestBase(NonSharedFixture fixture)
: NonSharedPrimitiveCollectionsQueryTestBase(fixture)
{
// On relational databases, byte[] gets mapped to a special binary data type, which isn't queryable as a regular primitive collection.
[ConditionalFact]
public override Task Array_of_byte()
=> AssertTranslationFailed(() => TestArray((byte)1, (byte)2));
protected abstract DbContextOptionsBuilder SetParameterizedCollectionMode(
DbContextOptionsBuilder optionsBuilder,
ParameterTranslationMode parameterizedCollectionMode);
[ConditionalFact]
public virtual async Task Column_collection_inside_json_owned_entity()
{
var contextFactory = await InitializeAsync<TestContext>(
onModelCreating: mb => mb.Entity<TestOwner>().OwnsOne(t => t.Owned, b => b.ToJson()),
seed: context =>
{
context.AddRange(
new TestOwner { Owned = new TestOwned { Strings = ["foo", "bar"] } },
new TestOwner { Owned = new TestOwned { Strings = ["baz"] } });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var result = await context.Set<TestOwner>().SingleAsync(o => o.Owned.Strings.Count() == 2);
Assert.Equivalent(new[] { "foo", "bar" }, result.Owned.Strings);
result = await context.Set<TestOwner>().SingleAsync(o => o.Owned.Strings[1] == "bar");
Assert.Equivalent(new[] { "foo", "bar" }, result.Owned.Strings);
}
protected static IEnumerable<object[]> ParameterTranslationModeValues()
=> Enum.GetValues<ParameterTranslationMode>().Select<ParameterTranslationMode, object[]>(x => [x]);
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Count_with_column_predicate_with_default_mode(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ids = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => ids.Count(i => i > c.Id) == 1).Select(x => x.Id).ToListAsync();
Assert.Equivalent(new[] { 100 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Contains_with_default_mode(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 2 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ints = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => ints.Contains(c.Id)).Select(x => x.Id).ToListAsync();
Assert.Equivalent(new[] { 2 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Count_with_column_predicate_with_default_mode_EF_Constant(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ids = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => EF.Constant(ids).Count(i => i > c.Id) == 1).Select(x => x.Id).ToListAsync();
Assert.Equivalent(new[] { 100 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Contains_with_default_mode_EF_Constant(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 2 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ints = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => EF.Constant(ints).Contains(c.Id)).Select(x => x.Id).ToListAsync();
Assert.Equivalent(new[] { 2 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Count_with_column_predicate_with_default_mode_EF_Parameter(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ids = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => EF.Parameter(ids).Count(i => i > c.Id) == 1).Select(x => x.Id)
.ToListAsync();
Assert.Equivalent(new[] { 100 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Contains_with_default_mode_EF_Parameter(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 2 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ints = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => EF.Parameter(ints).Contains(c.Id)).Select(x => x.Id).ToListAsync();
Assert.Equivalent(new[] { 2 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Count_with_column_predicate_with_default_mode_EF_MultipleParameters(
ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ids = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => EF.MultipleParameters(ids).Count(i => i > c.Id) == 1).Select(x => x.Id)
.ToListAsync();
Assert.Equivalent(new[] { 100 }, result);
}
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_Contains_with_default_mode_EF_MultipleParameters(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 2 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ints = new[] { 2, 999 };
var result = await context.Set<TestEntity>().Where(c => EF.MultipleParameters(ints).Contains(c.Id)).Select(x => x.Id).ToListAsync();
Assert.Equivalent(new[] { 2 }, result);
}
[ConditionalFact]
public virtual async Task Parameter_collection_Contains_parameter_bucketization()
{
var contextFactory = await InitializeAsync<TestContext>(
onConfiguring: b => SetParameterizedCollectionMode(b, ParameterTranslationMode.MultipleParameters),
seed: context =>
{
context.AddRange(
new TestEntity { Id = 1 },
new TestEntity { Id = 2 },
new TestEntity { Id = 100 });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
var ints = new[] { 2, 999, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
var result = await context.Set<TestEntity>().Where(c => ints.Contains(c.Id)).Select(c => c.Id).ToListAsync();
Assert.Equivalent(new[] { 2 }, result);
}
protected class TestOwner
{
public int Id { get; set; }
public TestOwned Owned { get; set; }
}
[Owned]
protected class TestOwned
{
public string[] Strings { get; set; }
}
protected TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
protected void ClearLog()
=> TestSqlLoggerFactory.Clear();
protected void AssertSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected);
// #38008
[ConditionalTheory, MemberData(nameof(ParameterTranslationModeValues))]
public virtual async Task Parameter_collection_of_enum_Cast_from_different_enum_type(ParameterTranslationMode mode)
{
var contextFactory = await InitializeAsync<Context38008>(
onConfiguring: b => SetParameterizedCollectionMode(b, mode),
seed: context =>
{
context.AddRange(
new Context38008.TestEntity38008 { Id = 1, Status = Context38008.EntityEnum.Clean },
new Context38008.TestEntity38008 { Id = 2, Status = Context38008.EntityEnum.Malware });
return context.SaveChangesAsync();
});
await using var context = contextFactory.CreateContext();
// Cast<EntityEnum>() returns a lazy IEnumerable whose boxed values retain the ViewModelEnum runtime type.
var filter = new[] { Context38008.ViewModelEnum.Malware }.Cast<Context38008.EntityEnum>();
var result = await context.Set<Context38008.TestEntity38008>()
.Where(a => filter.Any(f => f == a.Status))
.Select(a => a.Id)
.ToListAsync();
Assert.Equivalent(new[] { 2 }, result);
}
protected class Context38008(DbContextOptions options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<TestEntity38008>().Property(e => e.Id).ValueGeneratedNever();
public class TestEntity38008
{
public int Id { get; set; }
public EntityEnum Status { get; set; }
}
[Flags]
public enum EntityEnum
{
Clean = 1,
Malware = 2
}
[Flags]
public enum ViewModelEnum
{
Clean = 1,
Malware = 2
}
}
}