-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathAdHocMiscellaneousQueryRelationalTestBase.cs
321 lines (260 loc) · 11.1 KB
/
AdHocMiscellaneousQueryRelationalTestBase.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System.ComponentModel.DataAnnotations.Schema;
using NameSpace1;
namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class AdHocMiscellaneousQueryRelationalTestBase : AdHocMiscellaneousQueryTestBase
{
protected TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;
protected void ClearLog()
=> TestSqlLoggerFactory.Clear();
protected void AssertSql(params string[] expected)
=> TestSqlLoggerFactory.AssertBaseline(expected);
#region 2951
[ConditionalFact]
public async Task Query_when_null_key_in_database_should_throw()
{
var contextFactory = await InitializeAsync<Context2951>(
onConfiguring: o => o.EnableDetailedErrors(),
seed: Seed2951);
using var context = contextFactory.CreateContext();
Assert.Equal(
RelationalStrings.ErrorMaterializingPropertyNullReference(nameof(Context2951.ZeroKey2951), "Id", typeof(int)),
(await Assert.ThrowsAsync<InvalidOperationException>(() => context.ZeroKeys.ToListAsync())).Message);
}
protected abstract Task Seed2951(Context2951 context);
protected class Context2951(DbContextOptions options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<ZeroKey2951>().ToTable("ZeroKey", t => t.ExcludeFromMigrations())
.Property(z => z.Id).ValueGeneratedNever();
public DbSet<ZeroKey2951> ZeroKeys { get; set; }
public class ZeroKey2951
{
public int Id { get; set; }
}
}
#endregion
#region 11818
[ConditionalFact]
public virtual async Task GroupJoin_Anonymous_projection_GroupBy_Aggregate_join_elimination()
{
var contextFactory = await InitializeAsync<Context11818>(
onConfiguring:
o => o.ConfigureWarnings(w => w.Log(CoreEventId.FirstWithoutOrderByAndFilterWarning)));
using (var context = contextFactory.CreateContext())
{
var query = (from e in context.Set<Context11818.Entity11818>()
join a in context.Set<Context11818.AnotherEntity11818>()
on e.Id equals a.Id into grouping
from a in grouping.DefaultIfEmpty()
select new { ename = e.Name, aname = a.Name })
.GroupBy(g => g.aname)
.Select(
g => new { g.Key, cnt = g.Count() + 5 })
.ToList();
Assert.Empty(query);
}
using (var context = contextFactory.CreateContext())
{
var query = (from e in context.Set<Context11818.Entity11818>()
join a in context.Set<Context11818.AnotherEntity11818>()
on e.Id equals a.Id into grouping
from a in grouping.DefaultIfEmpty()
join m in context.Set<Context11818.MaumarEntity11818>()
on e.Id equals m.Id into grouping2
from m in grouping2.DefaultIfEmpty()
select new { aname = a.Name, mname = m.Name })
.GroupBy(
g => new { g.aname, g.mname })
.Select(
g => new { MyKey = g.Key.aname, cnt = g.Count() + 5 })
.ToList();
Assert.Empty(query);
}
using (var context = contextFactory.CreateContext())
{
var query = (from e in context.Set<Context11818.Entity11818>()
join a in context.Set<Context11818.AnotherEntity11818>()
on e.Id equals a.Id into grouping
from a in grouping.DefaultIfEmpty()
join m in context.Set<Context11818.MaumarEntity11818>()
on e.Id equals m.Id into grouping2
from m in grouping2.DefaultIfEmpty()
select new { aname = a.Name, mname = m.Name })
.OrderBy(g => g.aname)
.GroupBy(g => new { g.aname, g.mname })
.Select(g => new { MyKey = g.Key.aname, cnt = g.Key.mname }).FirstOrDefault();
Assert.Null(query);
}
}
protected class Context11818(DbContextOptions options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity11818>().ToTable("Table");
modelBuilder.Entity<AnotherEntity11818>().ToTable("Table");
modelBuilder.Entity<MaumarEntity11818>().ToTable("Table");
modelBuilder.Entity<Entity11818>()
.HasOne<AnotherEntity11818>()
.WithOne()
.HasForeignKey<AnotherEntity11818>(b => b.Id);
modelBuilder.Entity<Entity11818>()
.HasOne<MaumarEntity11818>()
.WithOne()
.HasForeignKey<MaumarEntity11818>(b => b.Id);
}
public class Entity11818
{
public int Id { get; set; }
public string Name { get; set; }
}
public class AnotherEntity11818
{
public int Id { get; set; }
public string Name { get; set; }
public bool Exists { get; set; }
}
public class MaumarEntity11818
{
public int Id { get; set; }
public string Name { get; set; }
public bool Exists { get; set; }
}
}
#endregion
#region 23981
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Multiple_different_entity_type_from_different_namespaces(bool async)
{
var contextFactory = await InitializeAsync<Context23981>();
using var context = contextFactory.CreateContext();
//var good1 = context.Set<NameSpace1.TestQuery>().FromSqlRaw(@"SELECT 1 AS MyValue").ToList(); // OK
//var good2 = context.Set<NameSpace2.TestQuery>().FromSqlRaw(@"SELECT 1 AS MyValue").ToList(); // OK
var bad = context.Set<TestQuery>().FromSqlRaw(@"SELECT cast(null as int) AS MyValue").ToList(); // Exception
}
protected class Context23981(DbContextOptions options) : DbContext(options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var mb = modelBuilder.Entity(typeof(TestQuery));
mb.HasBaseType((Type)null);
mb.HasNoKey();
mb.ToTable((string)null);
mb = modelBuilder.Entity(typeof(NameSpace2.TestQuery));
mb.HasBaseType((Type)null);
mb.HasNoKey();
mb.ToTable((string)null);
}
}
#endregion
#region 27954
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task StoreType_for_UDF_used(bool async)
{
var contextFactory = await InitializeAsync<Context27954>();
using var context = contextFactory.CreateContext();
var date = new DateTime(2012, 12, 12);
var query1 = context.Set<Context27954.MyEntity>().Where(x => x.SomeDate == date);
var query2 = context.Set<Context27954.MyEntity>().Where(x => Context27954.MyEntity.Modify(x.SomeDate) == date);
if (async)
{
await query1.ToListAsync();
await Assert.ThrowsAnyAsync<Exception>(() => query2.ToListAsync());
}
else
{
query1.ToList();
Assert.ThrowsAny<Exception>(() => query2.ToList());
}
}
protected class Context27954(DbContextOptions options) : DbContext(options)
{
public DbSet<MyEntity> MyEntities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder
.HasDbFunction(typeof(MyEntity).GetMethod(nameof(MyEntity.Modify)))
.HasName("ModifyDate")
.HasStoreType("datetime")
.HasSchema("dbo");
public class MyEntity
{
public int Id { get; set; }
[Column(TypeName = "datetime")]
public DateTime SomeDate { get; set; }
public static DateTime Modify(DateTime date)
=> throw new NotSupportedException();
}
}
#endregion
#region 35025
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Select_base_with_ComplexTypes_in_TPC(bool async)
{
var contextFactory = await InitializeAsync<Context35025>();
using var context = contextFactory.CreateContext();
var query1 = context.Events;
var count = 0;
if (async)
{
count = (await query1.ToListAsync()).Count;
}
else
{
count = query1.ToList().Count;
}
Assert.Equal(0, count);
}
protected class Context35025(DbContextOptions options) : DbContext(options)
{
public DbSet<EventBase> Events { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EventBase>(builder =>
{
builder.ComplexProperty(e => e.Knowledge).IsRequired();
builder.UseTpcMappingStrategy();
});
modelBuilder.Entity<EventWithIdentification>();
modelBuilder.Entity<RealEvent>();
}
public abstract class EventBase
{
public int Id { get; set; }
public Period Knowledge { get; set; }
}
public class EventWithIdentification : EventBase
{
public long ExtraId { get; set; }
}
public class RealEvent : EventBase
{
}
public class Period
{
public DateTimeOffset From { get; set; }
}
}
#endregion
}
}
namespace NameSpace1
{
public class TestQuery
{
public int? MyValue { get; set; }
}
}
namespace NameSpace2
{
public class TestQuery
{
public int MyValue { get; set; }
}
}