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);
        }
Пример #2
0
        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);
        }