Skip to content

Commit cc91adf

Browse files
committed
Add IDbProfiler shimming example to tests
See #316
1 parent bee2740 commit cc91adf

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tests/MiniProfiler.Tests/DbProfilerTests.cs

+34-1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,39 @@ public async Task TrackingOptionsAsync(bool track)
292292
CheckConnectionTracking(track, profiler, cmdString, true);
293293
}
294294

295+
[Fact]
296+
public void ShimProfiler()
297+
{
298+
var options = new MiniProfilerTestOptions {};
299+
var profiler = options.StartProfiler("Shimming");
300+
var currentDbProfiler = new CurrentDbProfiler(() => profiler);
301+
302+
const string cmdString = "Select 1";
303+
GetUnopenedConnection(currentDbProfiler).Query(cmdString);
304+
305+
CheckConnectionTracking(false, profiler, cmdString, false);
306+
}
307+
308+
private class CurrentDbProfiler : IDbProfiler
309+
{
310+
private Func<IDbProfiler> GetProfiler { get; }
311+
public CurrentDbProfiler(Func<IDbProfiler> getProfiler) => GetProfiler = getProfiler;
312+
313+
public bool IsActive => ((IDbProfiler)MiniProfiler.Current)?.IsActive ?? false;
314+
315+
public void ExecuteFinish(IDbCommand profiledDbCommand, SqlExecuteType executeType, DbDataReader reader) =>
316+
GetProfiler()?.ExecuteFinish(profiledDbCommand, executeType, reader);
317+
318+
public void ExecuteStart(IDbCommand profiledDbCommand, SqlExecuteType executeType) =>
319+
GetProfiler()?.ExecuteStart(profiledDbCommand, executeType);
320+
321+
public void OnError(IDbCommand profiledDbCommand, SqlExecuteType executeType, Exception exception) =>
322+
GetProfiler()?.OnError(profiledDbCommand, executeType, exception);
323+
324+
public void ReaderFinish(IDataReader reader) =>
325+
GetProfiler()?.ReaderFinish(reader);
326+
}
327+
295328
private void CheckConnectionTracking(bool track, MiniProfiler profiler, string command, bool async)
296329
{
297330
Assert.NotNull(profiler.Root.CustomTimings);
@@ -312,7 +345,7 @@ private void CheckConnectionTracking(bool track, MiniProfiler profiler, string c
312345
}
313346
}
314347

315-
private ProfiledDbConnection GetUnopenedConnection(MiniProfiler profiler) => new ProfiledDbConnection(Fixture.GetConnection(), profiler);
348+
private ProfiledDbConnection GetUnopenedConnection(IDbProfiler profiler) => new ProfiledDbConnection(Fixture.GetConnection(), profiler);
316349

317350
private CountingConnection GetConnection()
318351
{

0 commit comments

Comments
 (0)