public void TestReaderMapPropertiesWithFunkyDate() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("Field1,Field2,Field3,Field4,Field5\r\n"); writer.Write("value1,100,true,\"20161231\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnIndex(0); dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnIndex(1); dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnIndex(2); dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnIndex(3).TypeConverter(new DateTimeConverter() { Format = "yyyyMMdd" }); dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnIndex(4); dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderGetRecordCustomFieldsDefaultValues() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("Field1,Field2,Field3,Field4,Field5\r\n"); writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = false; dtReader.ShouldSkipRecord = (row => row[0] == "Field1"); dtReader.AddColumn("F1"); dtReader.AddColumn("F2"); dtReader.AddColumn("F3"); dtReader.AddColumn("F4"); dtReader.AddColumn("F5"); dtReader.AddColumn("F6"); dtReader.AddColumn("F7", "defaultVal"); dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); Assert.Equal("", trecord.Field6); Assert.Equal("defaultVal", trecord.Field7); Assert.Equal("F1", dtReader.FieldHeaders[0]); } }
public void TestReaderMapPropertiesWitoutHeaderRow() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = false; dtReader.MapProperty <TestRecord>(m => m.Field1).MappedColumnIndex = 0; dtReader.MapProperty <TestRecord>(m => m.Field4).MappedColumnIndex = 3; dtReader.MapProperty <TestRecord>(m => m.Field2).MappedColumnIndex = 1; dtReader.MapProperty <TestRecord>(m => m.Field5).MappedColumnIndex = 4; dtReader.MapProperty <TestRecord>(m => m.Field3).MappedColumnIndex = 2; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderMapPropertiesByName() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("X1|X2|X3|X4|X5\r\n"); writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnName("X1"); dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnName("X4"); dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnName("X2"); dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnName("X5"); dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnName("X3"); dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderGetRecordShouldSkipRecord() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("HDR,AAAA11111XXX000-FFF\r]n"); writer.Write("Field1,Field2,Field3,Field4,Field5\r\n"); writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.ShouldSkipRecord = (row => row[0] == "HDR" || row[0] == "TRL"); dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderGetRecordPropertiesOutnumberFields() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("Field1,Field2\r\n"); writer.Write("value1,100\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); } }
public void TestReaderGetRecordWithoutFieldHeadersOrPropertyMappingsNestedObject() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = false; dtReader.Read(); TestRecordData trecord = dtReader.GetRecord <TestRecordData>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Data1); Assert.Equal(100, trecord.Data2); Assert.True(trecord.Data4); } }
public void TestReaderGetField() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("Field1,Field2,Field3,Field4,Field5\r\n"); writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.Read(); Assert.Equal("value1", dtReader.GetField <string>(0)); Assert.Equal(100, dtReader.GetField <int>(1)); Assert.True(dtReader.GetField <bool>(2)); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), dtReader.GetField <DateTime>(3).ToShortDateString()); Assert.Equal(25.76M, dtReader.GetField <decimal>(4)); } }
public void TestReaderGetRecordWithoutFieldHeadersOrPropertyMappings() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = false; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void GetRecordCaseInsensitiveTest() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("field1,field2,Field3,Field4,Field5\r\n"); writer.Write("value1,100,true,\"12/31/2016\", 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderMapPropertiesByUnknownName() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("X1|X2|X3|X4|X5\r\n"); writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.MapProperty <TestRecord>(m => m.Field1).ColumnName("X1"); dtReader.MapProperty <TestRecord>(m => m.Field4).ColumnName("FOO"); dtReader.MapProperty <TestRecord>(m => m.Field2).ColumnName("X2"); dtReader.MapProperty <TestRecord>(m => m.Field5).ColumnName("X5"); dtReader.MapProperty <TestRecord>(m => m.Field3).ColumnName("X3"); dtReader.Read(); TestRecord trecord; Assert.Throws <DelimitedTextReaderMappingException>(() => trecord = dtReader.GetRecord <TestRecord>()); } }
public void TestReaderGetRecordPropertiesNotInFields() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader)) { writer.Write("Field1,Field2,Field3,Field5\r\n"); writer.Write("value1,100,true, 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.MinValue.ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void TestReaderPipeDelimitedGetRecord() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) using (var reader = new StreamReader(stream)) using (var dtReader = new DelimitedTextReader(reader, '|')) { writer.Write("Field1|Field2|Field3|Field4|Field5\r\n"); writer.Write("value1|100|true|\"12/31/2016\"| 25.76\r\n"); writer.Flush(); stream.Position = 0; dtReader.FirstRowIsHeader = true; dtReader.Read(); TestRecord trecord = dtReader.GetRecord <TestRecord>(); Assert.NotNull(trecord); Assert.Equal("value1", trecord.Field1); Assert.Equal(100, trecord.Field2); Assert.True(trecord.Field3); Assert.Equal(DateTime.Parse("12/31/2016").ToShortDateString(), trecord.Field4.ToShortDateString()); Assert.Equal(25.76M, trecord.Field5); } }
public void Read(string file) { Assert.IsNotNull(file); Assert.IsTrue(file.Trim().Length <= 0); if (file == null) { return; } if (file.Trim().Length <= 0) { return; } DelimitedTextReader textReader = new DelimitedTextReader(); // CreateTextReader(args); textReader.FieldDelimiter = ","; textReader.RecordDelimiter = System.Environment.NewLine; textReader.HeaderRecordIndex = 0; //if(textReader == null) //{ // WriteLine("ERROR - textReader == null"); // return; //} WriteLine(""); WriteLine("textReader.FieldDelimiter=" + textReader.FieldDelimiter); WriteLine("textReader.RecordDelimiter=" + textReader.RecordDelimiter); WriteLine("textReader.HeaderRecordIndex=" + textReader.HeaderRecordIndex); WriteLine("textReader.LeftEnclosure=" + textReader.LeftEnclosure); WriteLine("textReader.RightEnclosure=" + textReader.RightEnclosure); WriteLine("textReader.CurrentEncoding=" + textReader.CurrentEncoding); WriteLine(""); string OrderBy = "Gender DESC"; // GetSortOrderBy(args); //if(OrderBy == null) OrderBy = ""; //string file = GetFile(args); //if(file == null) file = ""; WriteLine("File=" + file); WriteLine("OrderBy=" + OrderBy); WriteLine(""); ////• Output 1 – sorted by gender (females before males) then by last name ascending. ////• Output 2 – sorted by birth date, ascending. ////• Output 3 – sorted by last name, descending. ////Display dates in the format M/D/YYYY if (file == null || file.Trim().Length <= 0) { WriteLine("ERROR file is null"); return; } WriteLine(""); WriteLine("textReader.SetFieldTypeMapping..."); WriteLine(""); textReader.SetFieldTypeMapping("DOB", typeof(DateTime)); WriteLine(""); WriteLine("Reading file:" + file); WriteLine(""); textReader.Read(file); WriteLine(""); WriteLine("textReader.GenerateDataTable..."); WriteLine(""); // // textReader.GenerateDataTable("Table 1"); textReader.InitializeDataTable("CURRENT_TABLE"); System.Data.DataTable dataTable = textReader.CurrentDataTable; Assert.IsNotNull(dataTable); WriteLine(""); WriteLine("textReader.SortDataTable..."); WriteLine(""); ////string OrderBy = GetSortOrderBy(args); dataTable = textReader.SortDataTable(dataTable, OrderBy); Assert.IsNotNull(dataTable); WriteLine(""); WriteLine("textReader.PrintDataTable..."); WriteLine(""); //textReader.PrintDataTable(dataTable); textReader.PrintDataTable(dataTable, System.Environment.NewLine, "^", Console.Out); WriteLine(""); WriteLine("END - MainBody"); WriteLine(""); }