From cc91adf5997c09344c5102500b8e120132ee51be Mon Sep 17 00:00:00 2001 From: Nick Craver Date: Thu, 23 Aug 2018 19:20:31 -0400 Subject: [PATCH] Add IDbProfiler shimming example to tests See #316 --- tests/MiniProfiler.Tests/DbProfilerTests.cs | 35 ++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/MiniProfiler.Tests/DbProfilerTests.cs b/tests/MiniProfiler.Tests/DbProfilerTests.cs index fca7ab893..106e56d8b 100644 --- a/tests/MiniProfiler.Tests/DbProfilerTests.cs +++ b/tests/MiniProfiler.Tests/DbProfilerTests.cs @@ -292,6 +292,39 @@ public async Task TrackingOptionsAsync(bool track) CheckConnectionTracking(track, profiler, cmdString, true); } + [Fact] + public void ShimProfiler() + { + var options = new MiniProfilerTestOptions {}; + var profiler = options.StartProfiler("Shimming"); + var currentDbProfiler = new CurrentDbProfiler(() => profiler); + + const string cmdString = "Select 1"; + GetUnopenedConnection(currentDbProfiler).Query(cmdString); + + CheckConnectionTracking(false, profiler, cmdString, false); + } + + private class CurrentDbProfiler : IDbProfiler + { + private Func GetProfiler { get; } + public CurrentDbProfiler(Func getProfiler) => GetProfiler = getProfiler; + + public bool IsActive => ((IDbProfiler)MiniProfiler.Current)?.IsActive ?? false; + + public void ExecuteFinish(IDbCommand profiledDbCommand, SqlExecuteType executeType, DbDataReader reader) => + GetProfiler()?.ExecuteFinish(profiledDbCommand, executeType, reader); + + public void ExecuteStart(IDbCommand profiledDbCommand, SqlExecuteType executeType) => + GetProfiler()?.ExecuteStart(profiledDbCommand, executeType); + + public void OnError(IDbCommand profiledDbCommand, SqlExecuteType executeType, Exception exception) => + GetProfiler()?.OnError(profiledDbCommand, executeType, exception); + + public void ReaderFinish(IDataReader reader) => + GetProfiler()?.ReaderFinish(reader); + } + private void CheckConnectionTracking(bool track, MiniProfiler profiler, string command, bool async) { Assert.NotNull(profiler.Root.CustomTimings); @@ -312,7 +345,7 @@ private void CheckConnectionTracking(bool track, MiniProfiler profiler, string c } } - private ProfiledDbConnection GetUnopenedConnection(MiniProfiler profiler) => new ProfiledDbConnection(Fixture.GetConnection(), profiler); + private ProfiledDbConnection GetUnopenedConnection(IDbProfiler profiler) => new ProfiledDbConnection(Fixture.GetConnection(), profiler); private CountingConnection GetConnection() {