Skip to content

Commit

Permalink
Add IDbProfiler shimming example to tests
Browse files Browse the repository at this point in the history
See #316
  • Loading branch information
NickCraver committed Aug 23, 2018
1 parent bee2740 commit cc91adf
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tests/MiniProfiler.Tests/DbProfilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDbProfiler> GetProfiler { get; }
public CurrentDbProfiler(Func<IDbProfiler> 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);
Expand All @@ -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()
{
Expand Down

0 comments on commit cc91adf

Please sign in to comment.