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 ExecuteJob_OutputFormatSpecified_CommandExecutedWithWriter() { // setup string connString = Guid.NewGuid().ToString(); ConnectionSetting connSetting = new ConnectionSetting(Guid.NewGuid().ToString(), connString); ReportJob job = new ReportJob(); job.Command = Guid.NewGuid().ToString(); job.OutputFormat = Guid.NewGuid().ToString(); job.OutputFileName = Guid.NewGuid().ToString(); job.OutputFilePath = Environment.CurrentDirectory; job.Delimiter = Guid.NewGuid().ToString(); IDbConnection dbConn = Substitute.For <IDbConnection>(); _dbConnectionFactory.CreateConnection(connString, true).Returns(dbConn); IDbCommand cmd = Substitute.For <IDbCommand>(); dbConn.CreateCommand().Returns(cmd); IReportWriter reportWriter = Substitute.For <IReportWriter>(); _reportWriterFactory.GetReportWriter(job.OutputFormat).Returns(reportWriter); IDataReader reader = Substitute.For <IDataReader>(); reader.GetSchemaTable().Returns(new DataTable()); cmd.ExecuteReader().Returns(reader); // execute ReportJobResult result = _reportExecutor.ExecuteJob(connSetting, job); // assert cmd.DidNotReceive().ExecuteNonQuery(); cmd.Received(1).ExecuteReader(); reportWriter.Received(1).Initialise(Arg.Any <String>(), job.Delimiter); reportWriter.Received(1).WriteHeader(Arg.Any <IEnumerable <String> >()); }
public void GetReportWriter_SupportedReportFormat_ReturnsCorrectType(string reportFormat, Type expectedType) { IReportWriter writer = _reportWriterFactory.GetReportWriter(reportFormat); Assert.IsInstanceOf(expectedType, writer); }