public void ExcelReportWriterTest_CheckFileContents() { const string delimiter = ","; // set up the data reader to return columns ColumnMetaData[] headers = { new ColumnMetaData("Name", "varchar", 100), new ColumnMetaData("Surname", "varchar", 100), new ColumnMetaData("Age", "varchar", 32), }; object[][] data = { new object[] { "Matt", "Salmon", 41 }, new object[] { "John", "Doe", 61 }, new object[] { "Jane", "Smith", 25 } }; IDataReader reader = Substitute.For <IDataReader>(); reader.FieldCount.Returns(3); reader.Read().Returns(true, true, true, false); reader.GetValue(0).Returns(data[0][0], data[1][0], data[2][0]); reader.GetValue(1).Returns(data[0][1], data[1][1], data[2][1]); reader.GetValue(2).Returns(data[0][2], data[1][2], data[2][2]); // execute _reportWriter.Initialise(_filePath, delimiter); _reportWriter.WriteHeader(headers.Select(x => x.Name)); foreach (object[] line in data) { _reportWriter.WriteLine(reader, headers); } _reportWriter.Dispose(); // assert Assert.IsTrue(File.Exists(_filePath)); List <string[]> lines = ReadSpreadsheetLines(_filePath); // check that the header is correct string expectedHeader = String.Join(delimiter, headers.Select(x => x.Name)); Assert.AreEqual(expectedHeader, String.Join(delimiter, lines[0])); // make sure each of the lines is written correctly for (int i = 0; i < data.Length; i++) { object[] line = data[i]; string expectedLine = String.Join(delimiter, data[i]); string actualLine = String.Join(delimiter, lines[i + 1]); Assert.AreEqual(expectedLine, actualLine); } _excelRangeFormatter.Received(data.Length * data[0].Length).FormatCell(Arg.Any <ExcelRange>(), Arg.Any <object>(), Arg.Any <Type>()); }
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); } }
/// <summary> /// The write content. /// </summary> /// <param name="w">The w.</param> public override void WriteContent(IReportWriter w) { w.WriteHeader(this); }
public void WriteHeader_WithoutInitialise_ThrowsException() { TestDelegate del = () => _reportWriter.WriteHeader(new string[] { }); Assert.Throws(typeof(InvalidOperationException), del); }
/// <summary> /// The write content. /// </summary> /// <param name="w"> /// The w. /// </param> public override void WriteContent(IReportWriter w) { w.WriteHeader(this); }
public void DelimitedReportWriterTest_CheckFileContents(string delimiter) { // set up the data reader to return columns ColumnMetaData[] headers = { new ColumnMetaData("Name", "varchar", 100), new ColumnMetaData("Surname", "varchar", 100), new ColumnMetaData("Age", "varchar", 32), }; object[][] data = { new object[] { "Matt", "Salmon", 41 }, new object[] { "John", "Doe", 61 }, new object[] { "Jane", "Smith", 25 } }; IDataReader reader = Substitute.For <IDataReader>(); reader.Read().Returns(true, true, true, false); reader.GetValue(0).Returns(data[0][0], data[1][0], data[2][0]); reader.GetValue(1).Returns(data[0][1], data[1][1], data[2][1]); reader.GetValue(2).Returns(data[0][2], data[1][2], data[2][2]); reader.GetFieldType(Arg.Any <int>()).Returns(typeof(String)); // set up the text formatter to return the value supplied _textFormatter.FormatText(Arg.Any <object>(), Arg.Any <Type>()).Returns((c) => { return(c.ArgAt <object>(0).ToString()); }); // execute _reportWriter.Initialise(_filePath, delimiter); _reportWriter.WriteHeader(headers.Select(x => x.Name)); foreach (object[] line in data) { _reportWriter.WriteLine(reader, headers); } _reportWriter.Dispose(); // assert Assert.IsTrue(File.Exists(_filePath)); List <string> lines = File.ReadLines(_filePath).ToList(); // check that the header is correct string expectedHeader = String.Join(delimiter, headers.Select(x => x.Name)); Assert.AreEqual(expectedHeader, lines[0]); // make sure each of the lines is written correctly for (int i = 0; i < data.Length; i++) { object[] line = data[i]; string expectedLine = String.Join(delimiter, data[i]); string actualLine = lines[i + 1]; Assert.AreEqual(expectedLine, actualLine); } // make sure all the data items are formatted foreach (object[] line in data) { foreach (object item in line) { _textFormatter.Received(1).FormatText(item, typeof(String)); } } }