public void TestDbProfiler() { var mockStorage = new Mock<IProfilingStorage>(); var profiler = new Profiler("test", mockStorage.Object, null); var target = new DbProfiler(profiler) as IDbProfiler; var stepId = Guid.NewGuid(); ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId = stepId; var executeCalled = false; var mockCommand = new Mock<IDbCommand>(); mockCommand.Setup(cmd => cmd.CommandText).Returns("test"); var mockReader = new Mock<IDataReader>(); // test execute reader target.ExecuteDbCommand(DbExecuteType.Reader, mockCommand.Object, () => { executeCalled = true; return mockReader.Object; }, new[] { "tag1" }); Assert.IsTrue(executeCalled); Assert.AreEqual(0, (profiler as IProfiler).CustomTimings.Count()); target.DataReaderFinished(new ProfiledDbDataReader(mockReader.Object, target)); Assert.AreEqual(1, (profiler as IProfiler).CustomTimings.Count()); Assert.AreEqual("TAG1", (profiler as IProfiler).CustomTimings.Last().Tags.First()); // test execute nonquery executeCalled = false; target.ExecuteDbCommand(DbExecuteType.NonQuery, mockCommand.Object, () => { executeCalled = true; return null; }, null); Assert.IsTrue(executeCalled); Assert.AreEqual(2, (profiler as IProfiler).CustomTimings.Count()); // test DataReaderFinished with invalid reader, it should not throw exception target.DataReaderFinished(null); }
public DataSet LoadActiveDemoDataWithDataAdapter() { using (ProfilingSession.Current.Step("Data.LoadActiveDemoDataWithDataAdapter")) { using (var conn = new SqlConnection(@"Server=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\SimpleDemoDB.mdf;Database=SimpleDemoDB;Trusted_Connection=Yes;")) { conn.Open(); using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.Text; cmd.CommandText = "select Id, Name from [Table] where IsActive = @IsActive"; cmd.Parameters.Add(new SqlParameter("@IsActive", 1)); var dataAdapter = new SqlDataAdapter(cmd); var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler); var profiledDataAdapter = new ProfiledDbDataAdapter(dataAdapter, dbProfiler); var ds = new DataSet("SimpleDemoDB"); profiledDataAdapter.Fill(ds); } } } return null; }
private IDbConnection GetConnection() { var conn = new SqlConnection(@"Server=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\SimpleDemoDB.mdf;Database=SimpleDemoDB;Trusted_Connection=Yes;"); if (ProfilingSession.Current == null) { return conn; } var dbProfiler = new DbProfiler(ProfilingSession.Current.Profiler); return new ProfiledDbConnection(conn, dbProfiler); }
public void TestDbProfiler_ExecuteDbCommand_InvalidCommand() { var mockStorage = new Mock<IProfilingStorage>(); var profiler = new Profiler("test", mockStorage.Object, null); var target = new DbProfiler(profiler) as IDbProfiler; var executeCalled = false; target.ExecuteDbCommand(DbExecuteType.Reader, null, () => { executeCalled = true; return null; }, null); Assert.IsTrue(executeCalled); }
public void TestDbProfiler_ExecuteDbCommand_InvalidExecute() { var mockStorage = new Mock<IProfilingStorage>(); var profiler = new Profiler("test", mockStorage.Object, null); var mockCommand = new Mock<IDbCommand>(); var target = new DbProfiler(profiler) as IDbProfiler; // execute empty execute should not throw exception target.ExecuteDbCommand(DbExecuteType.Reader, mockCommand.Object, null, null); }