public void ReadSingleRowWithInParameters()
        {
            var dataContainer = new TestDataContainer();
            var dataReader    = MockDatabaseFactory.CreateDbDataReader(dataContainer);
            var query         = new SQLQuery(sqlInString, SQLQueryType.DataReader)
            {
                ProcessRow = dataContainer.ProcessRow
            };

            query.Parameters.Add("param1", "value1");
            query.Parameters.Add("param2", "value2");
            query.Parameters.Add("param3", 333);
            var inList = new List <object>();

            inList.AddRange(new string[] { "val1", "val2", "val3", "val4" });
            query.InParameters.Add("inParam", inList);
            queryRunner.RunQuery(query);

            dataContainer.AssertData();
            VerifyLogging(sqlInStringAfterProcessing);

            Assert.AreEqual(sqlInStringAfterProcessing, MockDatabaseFactory.DbCommand.Object.CommandText);
            MockDatabaseFactory.DbConnection.VerifySet(dbc => dbc.ConnectionString = connectionString, Times.Exactly(1));
            MockDatabaseFactory.Parameters.Verify(p => p.Add(It.IsAny <DbParameter>()), Times.Exactly(7));
            MockDatabaseFactory.DbCommand.Protected()
            .Verify <Task <DbDataReader> >("ExecuteDbDataReaderAsync", Times.Exactly(1), It.IsAny <CommandBehavior>(), It.IsAny <System.Threading.CancellationToken>());
            Assert.AreEqual(1, query.RowCount);
            dataReader.Verify(dr => dr.Read(), Times.Exactly(2));
        }
        public void ReadSingleRowWithParametersAsync()
        {
            var dataContainer = new TestDataContainer();
            var dataReader    = MockDatabaseFactory.CreateDbDataReader(dataContainer);
            var query         = new SQLQuery(sqlString, SQLQueryType.DataReader)
            {
                ProcessRow = dataContainer.ProcessRow
            };

            query.Parameters.Add("param1", "value1");
            query.Parameters.Add("param2", "value2");
            query.Parameters.Add("param3", 333);
            var task = queryRunner.RunQueryAsync(query);

            task.Wait();

            dataContainer.AssertData();
            VerifyLogging(sqlString);
            Assert.AreEqual(sqlString, MockDatabaseFactory.DbCommand.Object.CommandText);
            MockDatabaseFactory.DbConnection.VerifySet(dbc => dbc.ConnectionString = connectionString, Times.Exactly(1));
            MockDatabaseFactory.Parameters.Verify(p => p.Add(It.IsAny <DbParameter>()), Times.Exactly(3));
            MockDatabaseFactory.DbCommand.Protected()
            .Verify <Task <DbDataReader> >("ExecuteDbDataReaderAsync", Times.Exactly(1), It.IsAny <CommandBehavior>(), It.IsAny <System.Threading.CancellationToken>());
            MockDatabaseFactory.DbCommand.Protected()
            .VerifyGet <DbParameterCollection>("DbParameterCollection", Times.Exactly(3));
            Assert.AreEqual(1, query.RowCount);
            dataReader.Verify(dr => dr.Read(), Times.Exactly(2));
        }
Exemple #3
0
        public static Mock <MoqDataReader> CreateDbDataReader(TestDataContainer dataContainer)
        {
            var dataReader = new Mock <MoqDataReader>(dataContainer.dataRow);

            dataReader.CallBase = true;
            dataReader.Setup(dr => dr.Close());
            MockDatabaseFactory.DbCommand = MockDatabaseFactory.CreateDbCommand(dataReader.Object);
            return(dataReader);
        }
        public void ReadSingleRowNoParameters()
        {
            var dataContainer = new TestDataContainer();
            var dataReader    = MockDatabaseFactory.CreateDbDataReader(dataContainer);
            var query         = new SQLQuery(sqlString, SQLQueryType.DataReader)
            {
                ProcessRow = dataContainer.ProcessRow
            };

            queryRunner.RunQuery(query);

            dataContainer.AssertData();
            VerifyLogging(sqlString);
            MockDatabaseFactory.DbConnection.VerifySet(dbc => dbc.ConnectionString = connectionString, Times.Exactly(1));
            MockDatabaseFactory.Parameters.Verify(p => p.Add(It.IsAny <DbParameter>()), Times.Exactly(0));
            MockDatabaseFactory.DbCommand.Protected()
            .Verify <Task <DbDataReader> >("ExecuteDbDataReaderAsync", Times.Exactly(1), It.IsAny <CommandBehavior>(), It.IsAny <System.Threading.CancellationToken>());
            Assert.AreEqual(1, query.RowCount);
            dataReader.Verify(dr => dr.Read(), Times.Exactly(2));
        }