-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
CommandInterceptionSqlServerTest.cs
207 lines (167 loc) · 7.36 KB
/
CommandInterceptionSqlServerTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
namespace Microsoft.EntityFrameworkCore;
#nullable disable
public abstract class CommandInterceptionSqlServerTestBase : CommandInterceptionTestBase
{
protected CommandInterceptionSqlServerTestBase(InterceptionSqlServerFixtureBase fixture)
: base(fixture)
{
}
public override async Task<string> Intercept_query_passively(bool async, bool inject)
{
AssertSql(
"""
SELECT [s].[Id], [s].[Type] FROM [Singularity] AS [s]
""",
await base.Intercept_query_passively(async, inject));
return null;
}
protected override async Task<string> QueryMutationTest<TInterceptor>(bool async, bool inject)
{
AssertSql(
"""
SELECT [s].[Id], [s].[Type] FROM [Brane] AS [s]
""",
await base.QueryMutationTest<TInterceptor>(async, inject));
return null;
}
public override async Task<string> Intercept_query_to_replace_execution(bool async, bool inject)
{
AssertSql(
"""
SELECT [s].[Id], [s].[Type] FROM [Singularity] AS [s]
""",
await base.Intercept_query_to_replace_execution(async, inject));
return null;
}
[ConditionalTheory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(true, true)]
public virtual async Task<string> Intercept_query_to_get_statistics(bool async, bool inject) // Issue #23535
{
var (context, interceptor) = await CreateContextAsync<StatisticsCommandInterceptor>(inject);
using (context)
{
using (async
? await context.Database.BeginTransactionAsync()
: context.Database.BeginTransaction())
{
var connection = (SqlConnection)context.Database.GetDbConnection();
var message = "";
connection.InfoMessage += ConnectionOnInfoMessage;
using var listener = Fixture.SubscribeToDiagnosticListener(context.ContextId);
var results = async
? await context.Set<Singularity>().ToListAsync()
: context.Set<Singularity>().ToList();
AssertNormalOutcome(context, interceptor, async, CommandSource.LinqQuery);
Assert.True(interceptor.DataReaderClosingCalled);
Assert.True(interceptor.DataReaderDisposingCalled);
Assert.Contains("Scan count", message);
results[0].Type = "Big Hole Bang";
_ = async
? await context.SaveChangesAsync()
: context.SaveChanges();
AssertNormalOutcome(context, interceptor, async, CommandSource.SaveChanges);
Assert.True(interceptor.DataReaderClosingCalled);
Assert.True(interceptor.DataReaderDisposingCalled);
Assert.Contains("Scan count", message);
AssertExecutedEvents(listener);
connection.InfoMessage -= ConnectionOnInfoMessage;
void ConnectionOnInfoMessage(object sender, SqlInfoMessageEventArgs args)
{
Assert.Same(connection, sender);
message = args.Message;
}
}
}
return interceptor.CommandText;
}
protected class StatisticsCommandInterceptor : CommandInterceptorBase
{
public StatisticsCommandInterceptor()
: base(DbCommandMethod.ExecuteReader)
{
}
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result)
{
command.CommandText = "SET STATISTICS IO ON;" + command.CommandText;
return base.ReaderExecuting(command, eventData, result);
}
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = default)
{
command.CommandText = "SET STATISTICS IO ON;" + command.CommandText;
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
}
public override InterceptionResult DataReaderClosing(
DbCommand command,
DataReaderClosingEventData eventData,
InterceptionResult result)
{
eventData.DataReader.NextResult();
return base.DataReaderClosing(command, eventData, result);
}
public override async ValueTask<InterceptionResult> DataReaderClosingAsync(
DbCommand command,
DataReaderClosingEventData eventData,
InterceptionResult result)
{
await eventData.DataReader.NextResultAsync();
return await base.DataReaderClosingAsync(command, eventData, result);
}
}
public abstract class InterceptionSqlServerFixtureBase : InterceptionFixtureBase
{
protected override string StoreName
=> "CommandInterception";
protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;
protected override IServiceCollection InjectInterceptors(
IServiceCollection serviceCollection,
IEnumerable<IInterceptor> injectedInterceptors)
=> base.InjectInterceptors(serviceCollection.AddEntityFrameworkSqlServer(), injectedInterceptors);
}
public class CommandInterceptionSqlServerTest(CommandInterceptionSqlServerTest.InterceptionSqlServerFixture fixture)
: CommandInterceptionSqlServerTestBase(fixture), IClassFixture<CommandInterceptionSqlServerTest.InterceptionSqlServerFixture>
{
public class InterceptionSqlServerFixture : InterceptionSqlServerFixtureBase
{
protected override bool ShouldSubscribeToDiagnosticListener
=> false;
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
{
new SqlServerDbContextOptionsBuilder(base.AddOptions(builder))
.ExecutionStrategy(d => new SqlServerExecutionStrategy(d));
return builder;
}
}
}
public class CommandInterceptionWithDiagnosticsSqlServerTest(
CommandInterceptionWithDiagnosticsSqlServerTest.InterceptionSqlServerFixture fixture)
: CommandInterceptionSqlServerTestBase(fixture),
IClassFixture<CommandInterceptionWithDiagnosticsSqlServerTest.InterceptionSqlServerFixture>
{
public class InterceptionSqlServerFixture : InterceptionSqlServerFixtureBase
{
protected override bool ShouldSubscribeToDiagnosticListener
=> true;
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
{
new SqlServerDbContextOptionsBuilder(base.AddOptions(builder))
.ExecutionStrategy(d => new SqlServerExecutionStrategy(d));
return builder;
}
}
}
}