public string Write <TEntity>(IEnumerable <CsvRecord <TEntity> > records, Action <CsvRecord <TEntity> > onMap = null) where TEntity : CsvRecord <TEntity> { if (records == null) { throw new ArgumentNullException(nameof(records)); } CsvRecord <TEntity> .Field[] fields = CsvRecord <TEntity> .GetFields(); return(CsvRow.BeginRows(fields.Select(x => x.Name).ToArray()) .Configure(configure => configure .ReturnHeaderAsRow() .ChangeDelimiter("\t")) .FromUsingMapper(records, (mapper, record) => { foreach (CsvRecord <TEntity> .Field field in fields) { onMap?.Invoke(record); mapper.Map(field.Name, field.Value(record)); } }) .ToString()); }
public void Create_Csv_Add_Using_Mapper_Wrong_Header_Throws() { CsvRow.ICsvRowBuilder csv = CsvRow.BeginRows("Name"); KeyNotFoundException exception = Assert.Throws <KeyNotFoundException>(() => csv.AddUsingMapper(x => x.Map("Age", "30"))); Assert.That(exception.Message, Does.Contain("Age")); }
public void Create_Csv_Null_Value_ToString() { string csv = CsvRow .BeginRows("Name", "Age") .Add("John Doe", "30") .Add("Jane Doe", null) .ToString(); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "John Doe;30", "Jane Doe;"))); }
public void Row_IsEmpty_Multiple() { CsvRow[] rows = CsvRow.BeginRows() .Add("John", null) .Add("", null) .Add(null, null) .ToRows(); Assert.That(rows[0].IsEmpty, Is.False); Assert.That(rows[1].IsEmpty, Is.True); Assert.That(rows[2].IsEmpty, Is.True); }
public void Create_Csv_From_List_Using_Mapper() { CsvRow[] rows = CsvRow.BeginRows("Name") .FromUsingMapper(new[] { "John", "Jane" }, (m, x) => m.Map("Name", x)) .ToRows(); var csv = string.Join(Environment.NewLine, rows.Select(x => x.ToString())); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "John", "Jane"))); }
public void Create_Csv_Add_Using_Mapper() { string csv = CsvRow .BeginRows("Name", "Age") .AddUsingMapper(x => x.Map("Age", "30").Map("Name", "John Doe")) .AddUsingMapper(x => x.Map("Age", "31").Map("Name", "Jane Doe")) .ToString(); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "John Doe;30", "Jane Doe;31"))); }
public void Create_Csv_No_Headers() { CsvRow[] rows = CsvRow .BeginRows() .Add("John Doe", "30") .Add("Jane Doe", "31") .ToRows(); var csv = string.Join(Environment.NewLine, rows.Select(x => x.ToString())); Assert.That(csv, Is.EqualTo($"John Doe;30{Environment.NewLine}Jane Doe;31")); }
public void Create_Csv_From_List() { CsvRow[] rows = CsvRow.BeginRows("Name") .From(new[] { "John", "Jane" }, x => new[] { x }) .ToRows(); var csv = string.Join(Environment.NewLine, rows.Select(x => x.ToString())); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "John", "Jane"))); }
public void Create_Csv_Mismatch_Headers_Data_Throws() { CsvRow.ICsvRowBuilderFinisher builder = CsvRow .BeginRows("Name", "Age") .Add("John Doe", "30"); ArgumentException exception = Assert.Throws <ArgumentException>(() => builder.Add("Jane Doe", "31", "New column")); Assert.That(exception.Message, Does.Contain("Row #2")); Assert.That(exception.Message, Does.Contain("has 3 columns")); Assert.That(exception.Message, Does.Contain("expected 2 columns")); }
public void Create_Csv_Tab_Delimiter() { string csv = CsvRow .BeginRows("Name", "Age") .Configure(x => x.ReturnHeaderAsRow().ChangeDelimiter("\t")) .Add("John Doe", "30") .Add("Jane Doe", "31") .ToString(); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "Name Age", "John Doe 30", "Jane Doe 31"))); }
public void Escape_LineBreak_Quote() { CsvRow.ICsvRowBuilderAdder builder = CsvRow.BeginRows("Name", "Age") .Configure(x => x .ReturnHeaderAsRow()); builder.AddUsingMapper(x => x .Map("Name", $"John{Environment.NewLine}Doe") .Map("Age", "30")); string csv = builder.ToString(); Assert.That(csv, Is.EqualTo($@"Name;Age{Environment.NewLine}""John{Environment.NewLine}Doe"";30")); }
public void Escape_Header_Content_Quote_Delimiter() { CsvRow.ICsvRowBuilderAdder builder = CsvRow.BeginRows("\"Name\"", "Age") .Configure(x => x .ReturnHeaderAsRow() .ChangeDelimiter(",")); builder.AddUsingMapper(x => x .Map("\"Name\"", "John,Doe") .Map("Age", "30\"")); string csv = builder.ToString(); Assert.That(csv, Is.EqualTo($@"""""""Name"""""",Age{Environment.NewLine}""John,Doe"",""30""""""")); }
public static string QueryToCsv(this IDbSession session, string sql, dynamic param = null, Action <CsvRow.ICsvRowBuilderConfiguration> configuration = null) { if (session == null) { throw new ArgumentNullException(nameof(session)); } if (string.IsNullOrWhiteSpace(sql)) { throw new ArgumentException(@"Value cannot be null or empty.", nameof(sql)); } IEnumerable <dynamic> query = session.Query(sql, param); string[] headers = null; return (CsvRow.BeginRows() .Configure(x => { if (configuration != null) { configuration(x); } else { x.ReturnHeaderAsRow(); } }) .From(query, data => { var row = (IDictionary <string, object>)data; if (headers == null) { headers = row.Keys.ToArray(); } return row.Values.Select(x => x?.ToString()).ToArray(); }) .Headers(headers) .ToString()); }
public void Parse_MultiLine_BuildBy_CsvRow() { string csv = CsvRow.BeginRows("Id", "Text") .Configure(configure => configure.ReturnHeaderAsRow()) .AddUsingMapper(mapper => mapper.Map("Id", "1").Map("Text", "SingleLine-1")) .AddUsingMapper(mapper => mapper.Map("Id", "2").Map("Text", $"Multi{Environment.NewLine}Line-2")) .AddUsingMapper(mapper => mapper.Map("Id", "3").Map("Text", "SingleLine-3")) .ToString(); CsvRow[] rows = Parse(csv); Assert.That(rows.Length, Is.EqualTo(3)); Assert.That(rows[0]["Id"], Is.EqualTo("1")); Assert.That(rows[1]["Id"], Is.EqualTo("2")); Assert.That(rows[2]["Id"], Is.EqualTo("3")); Assert.That(rows[0]["Text"], Is.EqualTo("SingleLine-1")); Assert.That(rows[1]["Text"], Is.EqualTo($"Multi{Environment.NewLine}Line-2")); Assert.That(rows[2]["Text"], Is.EqualTo("SingleLine-3")); }
public void Create_Csv_With_Headers() { CsvRow[] rows = CsvRow .BeginRows("Name", "Age") .Configure(x => x.ReturnHeaderAsRow()) .Add("John Doe", "30") .Add("Jane Doe", "31") .ToRows(); Assert.That(rows.Length, Is.EqualTo(3)); Assert.That(rows[0].Meta.LineNumber, Is.EqualTo(1)); Assert.That(rows[1].Meta.LineNumber, Is.EqualTo(2)); Assert.That(rows[2].Meta.LineNumber, Is.EqualTo(3)); var csv = string.Join(Environment.NewLine, rows.Select(x => x.ToString())); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "Name;Age", "John Doe;30", "Jane Doe;31"))); }
public void Create_Complex_Build() { CsvRow.ICsvRowBuilderAdder builder = CsvRow.BeginRows("Name", "Age") .Configure(x => x .ReturnHeaderAsRow() .ChangeDelimiter(",")); builder.AddUsingMapper(x => x .Map("Name", "John Doe") .Map("Age", "30")); builder.FromUsingMapper(new[] { "Jane Doe" }, (m, x) => m.Map("Name", x)); Assert.That(builder.DataRowCount, Is.EqualTo(2)); string csv = builder.ToString(); Assert.That(csv, Is.EqualTo(string.Join(Environment.NewLine, "Name,Age", "John Doe,30", "Jane Doe,"))); }