示例#1
0
        public void TestTagCollection()
        {
            var target = new TagCollection(new[] {"t1", "", ",", "t2"});
            Assert.AreEqual("T1,T2", target.ToString());

            var deserialized = TagCollection.FromString("T1,T2");
            Assert.AreEqual(target.Count, deserialized.Count);
            Assert.AreEqual(target.First(), deserialized.First());
            Assert.AreEqual(target.Last(), deserialized.Last());

            Assert.IsNull(TagCollection.FromString(null));
        }
示例#2
0
        public void TestProfiledDbCommandWithIDbCommand()
        {
            var mockCommand    = new Mock <IDbCommand>();
            var mockDbProfiler = new Mock <IDbProfiler>();

            var tags   = new TagCollection(new[] { "test" });
            var target = new ProfiledDbCommand(mockCommand.Object, mockDbProfiler.Object, tags) as IDbCommand;

            // test Cancel()
            var cancelCalled = false;

            mockCommand.Setup(cmd => cmd.Cancel()).Callback(() => cancelCalled = true);
            target.Cancel();
            Assert.IsTrue(cancelCalled);

            // test CommandText
            var sql            = "test sql;";
            var sql2           = "test sql 2";
            var commandTextSet = false;

            mockCommand.Setup(cmd => cmd.CommandText).Returns(sql);
            mockCommand.SetupSet(cmd => cmd.CommandText = It.IsAny <string>()).Callback <string>(a =>
            {
                Assert.AreEqual(sql2, a);
                commandTextSet = true;
            });
            Assert.AreEqual(sql, target.CommandText);
            target.CommandText = sql2;
            Assert.IsTrue(commandTextSet);

            // test CommandTimeout
            var timeout1          = 1;
            var timeout2          = 2;
            var commandTimeoutSet = false;

            mockCommand.Setup(cmd => cmd.CommandTimeout).Returns(timeout1);
            mockCommand.SetupSet(cmd => cmd.CommandTimeout = It.IsAny <int>()).Callback <int>(a =>
            {
                Assert.AreEqual(timeout2, a);
                commandTimeoutSet = true;
            });
            Assert.AreEqual(timeout1, target.CommandTimeout);
            target.CommandTimeout = timeout2;
            Assert.IsTrue(commandTimeoutSet);

            // test CommandType
            var cmdType1       = CommandType.StoredProcedure;
            var cmdType2       = CommandType.Text;
            var commandTypeSet = false;

            mockCommand.Setup(cmd => cmd.CommandType).Returns(cmdType1);
            mockCommand.SetupSet(cmd => cmd.CommandType = It.IsAny <CommandType>()).Callback <CommandType>(a =>
            {
                Assert.AreEqual(cmdType2, a);
                commandTypeSet = true;
            });
            Assert.AreEqual(cmdType1, target.CommandType);
            target.CommandType = cmdType2;
            Assert.IsTrue(commandTypeSet);

            // test CreateDbParameter()
            var mockParameter = new Mock <DbParameter>();
            var parameterName = "p1";

            mockParameter.Setup(p => p.ParameterName).Returns(parameterName);
            mockCommand.Setup(cmd => cmd.CreateParameter()).Returns(mockParameter.Object);
            var parameter = target.CreateParameter();

            Assert.AreNotEqual(mockParameter.Object, parameter);
            Assert.AreEqual(parameterName, parameter.ParameterName);

            // test DbConnection
            var mockConnection = new Mock <IDbConnection>();
            var connStr        = "test conn str";

            mockConnection.Setup(c => c.ConnectionString).Returns(connStr);
            mockCommand.Setup(cmd => cmd.Connection).Returns(mockConnection.Object);
            var connection = target.Connection;

            Assert.AreEqual(mockConnection.Object, connection);
            Assert.AreEqual(connection, target.Connection);
            Assert.AreEqual(connStr, connection.ConnectionString);

            // test DbParameterCollection
            Assert.IsNull(target.Parameters);
            // ReSharper disable HeuristicUnreachableCode
            var mockParameterCollection = new Mock <IDataParameterCollection>();

            mockParameterCollection.Setup(c => c.Count).Returns(1);
            mockCommand.Setup(cmd => cmd.Parameters).Returns(mockParameterCollection.Object);
            var parameterCollection = target.Parameters;

            Assert.IsFalse(ReferenceEquals(mockParameterCollection.Object, parameterCollection));
            Assert.AreEqual(parameterCollection, target.Parameters);
            Assert.AreEqual(mockParameterCollection.Object.Count, parameterCollection.Count);

            // test DbTransaction
            var mockTransaction = new Mock <IDbTransaction>();
            var isoLevel        = IsolationLevel.Chaos;

            mockTransaction.Setup(t => t.IsolationLevel).Returns(isoLevel);
            mockCommand.Setup(cmd => cmd.Transaction).Returns(mockTransaction.Object);
            var transaction = target.Transaction;

            Assert.AreEqual(mockTransaction.Object, transaction);
            Assert.AreEqual(transaction, target.Transaction);
            Assert.AreEqual(isoLevel, transaction.IsolationLevel);

            //test ExecuteDbDataReader()
            var mockReader = new Mock <IDataReader>();

            mockReader.Setup(r => r.Depth).Returns(1);
            var executeDbCommandCalled = false;

            mockDbProfiler.Setup(p => p.ExecuteDbCommand(It.IsAny <DbExecuteType>(), It.IsAny <IDbCommand>(), It.IsAny <Func <IDataReader> >(), It.IsAny <TagCollection>()))
            .Callback <DbExecuteType, IDbCommand, Func <IDataReader>, IEnumerable <string> >((a, b, c, d) =>
            {
                Assert.AreEqual(mockCommand.Object, b);
                Assert.AreEqual(tags.First(), d.First());
                c();
                executeDbCommandCalled = true;
            });
            var cmdBehavior = CommandBehavior.CloseConnection;

            mockCommand.Setup(c => c.ExecuteReader(cmdBehavior)).Returns(mockReader.Object);
            var reader = target.ExecuteReader(cmdBehavior);

            Assert.IsTrue(executeDbCommandCalled);
            Assert.AreNotEqual(mockReader.Object, reader);
            Assert.AreEqual(1, reader.Depth);

            // test ExecuteNonQuery()
            executeDbCommandCalled = false;
            var executeNonQueryCalled = false;
            var executeNonQueryResult = 1;

            mockCommand.Setup(c => c.ExecuteNonQuery()).Callback(() => executeNonQueryCalled = true).Returns(executeNonQueryResult);
            Assert.AreEqual(executeNonQueryResult, target.ExecuteNonQuery());
            Assert.IsTrue(executeDbCommandCalled);
            Assert.IsTrue(executeNonQueryCalled);

            // test ExecuteScalar()
            executeDbCommandCalled = false;
            var executeScalarCalled = false;
            var executeScalarResult = new object();

            mockCommand.Setup(c => c.ExecuteScalar()).Callback(() => executeScalarCalled = true).Returns(executeScalarResult);
            Assert.AreEqual(executeScalarResult, target.ExecuteScalar());
            Assert.IsTrue(executeDbCommandCalled);
            Assert.IsTrue(executeScalarCalled);

            // test Prepare()
            var prepareCalled = false;

            mockCommand.Setup(c => c.Prepare()).Callback(() => prepareCalled = true);
            target.Prepare();
            Assert.IsTrue(prepareCalled);

            // test UpdatedRowSource
            var updateRowSource1      = UpdateRowSource.Both;
            var updateRowSource2      = UpdateRowSource.FirstReturnedRecord;
            var updateRowSourceCalled = false;

            mockCommand.Setup(cmd => cmd.UpdatedRowSource).Returns(updateRowSource1);
            mockCommand.SetupSet(cmd => cmd.UpdatedRowSource = It.IsAny <UpdateRowSource>()).Callback <UpdateRowSource>(a =>
            {
                Assert.AreEqual(updateRowSource2, a);
                updateRowSourceCalled = true;
            });
            Assert.AreEqual(updateRowSource1, target.UpdatedRowSource);
            target.UpdatedRowSource = updateRowSource2;
            Assert.IsTrue(updateRowSourceCalled);

            // ReSharper restore HeuristicUnreachableCode
        }