public void ExecuteJob_NoDbParameters_CommandParametersNotSet() { // setup string connString = Guid.NewGuid().ToString(); ConnectionSetting connSetting = new ConnectionSetting(Guid.NewGuid().ToString(), connString); ReportJob job = new ReportJob(); job.Command = Guid.NewGuid().ToString(); job.Parameters = Guid.NewGuid().ToString(); IDbConnection dbConn = Substitute.For <IDbConnection>(); _dbConnectionFactory.CreateConnection(connString, true).Returns(dbConn); IDbCommand cmd = Substitute.For <IDbCommand>(); dbConn.CreateCommand().Returns(cmd); _dbParameterUtility.ConvertXmlToDbParameters(job.Parameters).Returns(new SqlParameter[] { }); // execute _reportExecutor.ExecuteJob(connSetting, job); // assert var temp = cmd.DidNotReceive().Parameters; }
public ReportJobResult ExecuteJob(ConnectionSetting connection, ReportJob job) { using (IDbConnection conn = _dbConnectionFactory.CreateConnection(connection.ConnectionString)) { int rowCount = 0; DateTime start = DateTime.UtcNow; using (var command = conn.CreateCommand()) { command.CommandTimeout = 0; command.CommandText = job.Command; if (!String.IsNullOrEmpty(job.CommandType) && job.CommandType.ToLower() == Constants.CommandType.StoredProcedure) { command.CommandType = System.Data.CommandType.StoredProcedure; } // add the parameters to the command SqlParameter[] parameters = _dbParameterUtility.ConvertXmlToDbParameters(job.Parameters); foreach (IDataParameter p in parameters) { command.Parameters.Add(p); } // make sure an output format has been specified - if it hasn't then just execute the command if (String.IsNullOrEmpty(job.OutputFormat)) { command.ExecuteNonQuery(); } else { using (var reader = command.ExecuteReader()) { string destinationFile = Path.Combine(job.OutputFilePath, job.OutputFileName); using (IReportWriter reportWriter = _reportWriterFactory.GetReportWriter(job.OutputFormat)) { ColumnMetaData[] columnInfo = GetColumnInfo(reader).ToArray(); reportWriter.Initialise(destinationFile, job.Delimiter); reportWriter.WriteHeader(columnInfo.Select(x => x.Name)); while (reader.Read()) { reportWriter.WriteLine(reader, columnInfo); rowCount++; } } } } } ReportJobResult result = new ReportJobResult(); result.RowCount = rowCount; result.ExecutionTime = DateTime.UtcNow.Subtract(start); return(result); } }
public void ConvertXmlToDbParameters_NullOrEmptyXml_ReturnsEmptyArray(string xml) { SqlParameter[] result = _dbParameterUtility.ConvertXmlToDbParameters(xml); Assert.AreEqual(0, result.Length); }