public static DbConnection GetConnection() { var conn = new SqlConnection(@"Server=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\SimpleDemoDB.mdf;Database=SimpleDemoDB;Trusted_Connection=Yes;"); // wrap the connection with ProfiledDbConnection that tracks timings var dbProfiler = new ProfiledDbConnection(conn, new DbProfiler(ProfilingSession.Current.Profiler)); return dbProfiler; }
public void TestProfiledDbConnectionWithIDbConnection() { var mockConnection = new Mock<IDbConnection>(); var mockDbProfiler = new Mock<IDbProfiler>(); var target = new ProfiledDbConnection(mockConnection.Object, mockDbProfiler.Object); // test BeginDbTransaction() var beginTransCalled = false; var isoLevel = IsolationLevel.Chaos; var mockTransaction = new Mock<IDbTransaction>(); mockTransaction.Setup(t => t.IsolationLevel).Returns(isoLevel); mockConnection.Setup(c => c.BeginTransaction(isoLevel)).Callback<IsolationLevel>(a => beginTransCalled = true).Returns(mockTransaction.Object); var transaction = target.BeginTransaction(isoLevel); Assert.AreNotEqual(mockTransaction.Object, transaction); Assert.AreEqual(isoLevel, transaction.IsolationLevel); Assert.IsTrue(beginTransCalled); // test ChangeDatabase() var dbName = "test db"; var changeDatabaseCalled = false; mockConnection.Setup(c => c.ChangeDatabase(dbName)).Callback<string>(a => changeDatabaseCalled = true); target.ChangeDatabase(dbName); Assert.IsTrue(changeDatabaseCalled); // test Close() var closeCalled = false; mockConnection.Setup(c => c.Close()).Callback(() => closeCalled = true); target.Close(); Assert.IsTrue(closeCalled); // test ConnectionString var connStr1 = "test 1;"; var connStr2 = "test 2"; var connectionStringSet = false; mockConnection.Setup(c => c.ConnectionString).Returns(connStr1); mockConnection.SetupSet(c => c.ConnectionString = It.IsAny<string>()).Callback<string>(a => { Assert.AreEqual(connStr2, a); connectionStringSet = true; }); Assert.AreEqual(connStr1, target.ConnectionString); target.ConnectionString = connStr2; Assert.IsTrue(connectionStringSet); // test CreateDbCommand() var createDbCommandCalled = false; var mockCommand = new Mock<IDbCommand>(); var sql = "test sql"; mockCommand.Setup(c => c.CommandText).Returns(sql); mockConnection.Setup(c => c.CreateCommand()).Callback(() => createDbCommandCalled = true).Returns(mockCommand.Object); var command = target.CreateCommand(); Assert.AreNotEqual(mockCommand.Object, command); Assert.AreEqual(sql, command.CommandText); Assert.IsTrue(createDbCommandCalled); // test Database mockConnection.Setup(c => c.Database).Returns(dbName); Assert.AreEqual(dbName, target.Database); // test Open() var openCalled = false; mockConnection.Setup(c => c.Open()).Callback(() => openCalled = true); target.Open(); Assert.IsTrue(openCalled); // test State var connState = ConnectionState.Executing; mockConnection.Setup(c => c.State).Returns(connState); Assert.AreEqual(connState, target.State); // test ConnectionTimeout var timeout = 1; mockConnection.Setup(c => c.ConnectionTimeout).Returns(timeout); Assert.AreEqual(timeout, target.ConnectionTimeout); }
public void TestProfiledDbConnectionWithProfiledDbConnection() { var mockConnection = new Mock<DbConnection>(); var mockDbProfiler = new Mock<IDbProfiler>(); var target = new ProfiledDbConnection(mockConnection.Object, mockDbProfiler.Object); // test BeginDbTransaction() var beginTransCalled = false; var isoLevel = IsolationLevel.Chaos; var mockTransaction = new Mock<IDbTransaction>(); var profiledTransaction = new ProfiledDbTransaction(mockTransaction.Object, mockDbProfiler.Object); mockConnection.Protected().Setup<DbTransaction>("BeginDbTransaction", isoLevel).Callback<IsolationLevel>(a => beginTransCalled = true).Returns(profiledTransaction); var transaction = target.BeginTransaction(isoLevel); Assert.AreEqual(profiledTransaction, transaction); Assert.IsTrue(beginTransCalled); // test CreateDbCommand() var createDbCommandCalled = false; var mockCommand = new Mock<IDbCommand>(); var profiledCommand = new ProfiledDbCommand(mockCommand.Object, mockDbProfiler.Object); mockConnection.Protected().Setup<DbCommand>("CreateDbCommand").Callback(() => createDbCommandCalled = true).Returns(profiledCommand); var command = target.CreateCommand(); Assert.AreEqual(profiledCommand, command); Assert.IsTrue(createDbCommandCalled); // test DataSource var dataSource = "test"; mockConnection.Setup(c => c.DataSource).Returns(dataSource); Assert.AreEqual(dataSource, target.DataSource); // test ServerVersion var serverVersion = "test"; mockConnection.Setup(c => c.ServerVersion).Returns(serverVersion); Assert.AreEqual(serverVersion, target.ServerVersion); // test Dispose() var disposeCalled = false; mockConnection.Protected().Setup("Dispose", true).Callback<bool>(a => disposeCalled = true); mockConnection.Setup(c => c.State).Returns(ConnectionState.Executing); target.Dispose(); Assert.IsTrue(disposeCalled); }
public void TestProfiledDbCommandWithProfiledDbCommand() { var mockCommand = new Mock<DbCommand>(); var mockDbProfiler = new Mock<IDbProfiler>(); var target = new ProfiledDbCommand(mockCommand.Object, mockDbProfiler.Object); // test CreateDbParameter() var mockParameter = new Mock<DbParameter>(); mockCommand.Protected().Setup<DbParameter>("CreateDbParameter").Returns(mockParameter.Object); Assert.AreEqual(mockParameter.Object, target.CreateParameter()); // test DbConnection var mockConnection = new Mock<DbConnection>(); var profiledConnection = new ProfiledDbConnection(mockConnection.Object, mockDbProfiler.Object); mockCommand.Protected().Setup<DbConnection>("DbConnection").Returns(profiledConnection); var connection = target.Connection; Assert.AreEqual(profiledConnection, connection); // test DbParameterCollection var mockParameterCollection = new Mock<DbParameterCollection>(); mockCommand.Protected().Setup<DbParameterCollection>("DbParameterCollection").Returns(mockParameterCollection.Object); var parameterCollection = target.Parameters; Assert.AreEqual(mockParameterCollection.Object, parameterCollection); // test DbTransaction var mockTransaction = new Mock<DbTransaction>(); var profiledTransaction = new ProfiledDbTransaction(mockTransaction.Object, mockDbProfiler.Object); mockCommand.Protected().Setup<DbTransaction>("DbTransaction").Returns(profiledTransaction); var transaction = target.Transaction; Assert.AreEqual(profiledTransaction, transaction); // test DesignTimeVisible var designTimeVisible1 = true; var designTimeVisible2 = false; var designTimeVisibleSet = false; mockCommand.Setup(cmd => cmd.DesignTimeVisible).Returns(designTimeVisible1); mockCommand.SetupSet(cmd => cmd.DesignTimeVisible = It.IsAny<bool>()).Callback<bool>(a => { Assert.AreEqual(designTimeVisible2, a); designTimeVisibleSet = true; }); Assert.AreEqual(designTimeVisible1, target.DesignTimeVisible); target.DesignTimeVisible = designTimeVisible2; Assert.IsTrue(designTimeVisibleSet); // test ExecuteDbDataReader() var mockReader = new Mock<DbDataReader>(); var profiledReader = new ProfiledDbDataReader(mockReader.Object, mockDbProfiler.Object); var cmdBehavior = CommandBehavior.CloseConnection; mockCommand.Protected().Setup<DbDataReader>("ExecuteDbDataReader", cmdBehavior).Returns(profiledReader); var executeDbCommandCalled = false; mockDbProfiler.Setup(p => p.ExecuteDbCommand(It.IsAny<DbExecuteType>(), It.IsAny<IDbCommand>(), It.IsAny<Func<IDataReader>>(), It.IsAny<IEnumerable<string>>())) .Callback<DbExecuteType, IDbCommand, Func<IDataReader>, IEnumerable<string>>((a, b, c, d) => { Assert.AreEqual(mockCommand.Object, b); c(); executeDbCommandCalled = true; }); var reader = target.ExecuteReader(cmdBehavior); Assert.IsTrue(executeDbCommandCalled); Assert.AreEqual(profiledReader, reader); // test Dispose() var disposeCalled = false; mockCommand.Protected().Setup("Dispose", true).Callback<bool>(a => disposeCalled = true); target.Dispose(); Assert.IsTrue(disposeCalled); }