public void TestEmbeddedDelimiters() { string fileName = WriteTextToFile(String.Format( "\"Wilbur Smith\",\"Go, go, go\"{0}\"Willem Wikkelspies\",\"Romeo said \"\"jump\"\"\"", "\r\n")); var fields = new TextFileReaderFields(); fields.Delimiter = DelimiterType.Comma; fields.TextQualifier = TextQualifierType.DoubleQuotes; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Field1" }); fieldCollection.Add(new Afield { Name = "Field2" }); fieldCollection.Add(new Afield { Name = "Field3" }); fieldCollection.Add(new Afield { Name = "Field4" }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); List <NextResult> lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(2, lines.Count); Assert.AreEqual("Wilbur Smith", lines[0].Value.LineContents.Field1); Assert.AreEqual("Go, go, go", lines[0].Value.LineContents.Field2); Assert.AreEqual("Willem Wikkelspies", lines[1].Value.LineContents.Field1); Assert.AreEqual("Romeo said \"jump\"", lines[1].Value.LineContents.Field2); }
public void TestFieldsAsStringsSpecificCase() { string fileName = WriteTextToFile(String.Format("'abc'|'123'|'01-12-2013'{0}def|' 456'|' 25-12-2013'", "\r\n")); var fields = new TextFileReaderFields(); fields.Delimiter = DelimiterType.Other; fields.OtherDelimiter = "|"; fields.TextQualifier = TextQualifierType.SingleQuotes; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Field1" }); fieldCollection.Add(new Afield { Name = "Field2" }); fieldCollection.Add(new Afield { Name = "Field3" }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); var lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(2, lines.Count); Assert.AreEqual("abc", lines[0].Value.LineContents.Field1); Assert.AreEqual("123", lines[0].Value.LineContents.Field2); Assert.AreEqual("01-12-2013", lines[0].Value.LineContents.Field3); Assert.AreEqual("def", lines[1].Value.LineContents.Field1); Assert.AreEqual(" 456", lines[1].Value.LineContents.Field2); Assert.AreEqual(" 25-12-2013", lines[1].Value.LineContents.Field3); }
public void TestFieldsSkip() { string fileName = WriteTextToFile(String.Format("a,b,c{0}d,e,f{0}g,h,i", "\r\n")); var fields = new TextFileReaderFields(); fields.Delimiter = DelimiterType.Comma; fields.TextQualifier = TextQualifierType.None; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Field1" }); fieldCollection.Add(new Afield { Name = "Field2", Skip = true }); fieldCollection.Add(new Afield { Name = "Field3" }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); var lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(3, lines.Count); Assert.That(() => lines[0].Value.LineContents.Field2, Throws.Exception.TypeOf <RuntimeBinderException>()); Assert.AreEqual("a", lines[0].Value.LineContents.Field1); Assert.AreEqual("c", lines[0].Value.LineContents.Field3); Assert.AreEqual("d", lines[1].Value.LineContents.Field1); Assert.AreEqual("f", lines[1].Value.LineContents.Field3); Assert.AreEqual("g", lines[2].Value.LineContents.Field1); Assert.AreEqual("i", lines[2].Value.LineContents.Field3); }
public void TestFixedLengthFieldsWithLessChars() { string fileName = WriteTextToFile(String.Format("100456NBTPurchase{0}100566BTXPurchase{0}100890ANLPurchase", "\r\n")); var fields = new TextFileReaderFields(); fields.TextFileType = FileType.FixedLength; fields.TextQualifier = TextQualifierType.None; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Client", Type = new FieldType(typeof(string)), Length = 6 }); fieldCollection.Add(new Afield { Name = "Security", Type = new FieldType(typeof(string)), Length = 3 }); fieldCollection.Add(new Afield { Name = "Transaction", Type = new FieldType(typeof(string)), Length = 10 }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); var lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(3, lines.Count); Assert.AreEqual("100456", lines[0].Value.LineContents.Client); Assert.AreEqual("NBT", lines[0].Value.LineContents.Security); Assert.AreEqual("Purchase", lines[0].Value.LineContents.Transaction); }
public void TestFieldFormat() { string fileName = WriteTextToFile( String.Format( "Client100456,1312,100,145203,10 21-2002,True,0,50.0{0}Client100566,99.55,1500,145201,10 18-2002,true,0,55{0}Client100890,866.00,1050,145204,10 21-2002,false,0,70", "\r\n")); var fields = new TextFileReaderFields(); fields.Delimiter = DelimiterType.Comma; fields.TextQualifier = TextQualifierType.None; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Client", Type = new FieldType(typeof(string)) }); fieldCollection.Add(new Afield { Name = "Price", Type = new FieldType(typeof(decimal)) }); fieldCollection.Add(new Afield { Name = "Quantity", Type = new FieldType(typeof(int)) }); fieldCollection.Add(new Afield { Name = "OrderNo", Type = new FieldType(typeof(string)) }); fieldCollection.Add(new Afield { Name = "OrderDate", Type = new FieldType(typeof(DateTime)), Format = "MM dd-yyyy" }); fieldCollection.Add(new Afield { Name = "BoolVal", Type = new FieldType(typeof(bool)) }); fieldCollection.Add(new Afield { Name = "ByteVal", Type = new FieldType(typeof(byte)) }); fieldCollection.Add(new Afield { Name = "DoubleVal", Type = new FieldType(typeof(double)) }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); List <NextResult> lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(3, lines.Count); Assert.AreEqual("Client100456", lines[0].Value.LineContents.Client); Assert.AreEqual(1312, lines[0].Value.LineContents.Price); Assert.AreEqual(100, lines[0].Value.LineContents.Quantity); Assert.AreEqual("145203", lines[0].Value.LineContents.OrderNo); Assert.AreEqual(2002, ((DateTime)lines[0].Value.LineContents.OrderDate).Year); Assert.AreEqual(10, ((DateTime)lines[0].Value.LineContents.OrderDate).Month); Assert.AreEqual(21, ((DateTime)lines[0].Value.LineContents.OrderDate).Day); Assert.IsTrue(lines[0].Value.LineContents.BoolVal); Assert.AreEqual(byte.MinValue, lines[0].Value.LineContents.ByteVal); Assert.AreEqual(50, lines[0].Value.LineContents.DoubleVal); }
private AfieldCollection CopyFieldList() { AfieldCollection newFieldList = new AfieldCollection(); foreach (var wrapperField in FieldList) { newFieldList.Add(wrapperField.ToAfield()); } return(newFieldList); }
public void TestFieldsAsStringsFieldsMismatch() { string fileName = WriteTextToFile(String.Format("a,b,c{0}d,e,f{0}g,h,i", "\r\n")); var fields = new TextFileReaderFields(); fields.Delimiter = DelimiterType.Comma; fields.TextQualifier = TextQualifierType.None; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Field1" }); fieldCollection.Add(new Afield { Name = "Field2" }); fieldCollection.Add(new Afield { Name = "Field3" }); fieldCollection.Add(new Afield { Name = "Field4" }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); var lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(3, lines.Count); Assert.AreEqual("a", lines[0].Value.LineContents.Field1); Assert.AreEqual("b", lines[0].Value.LineContents.Field2); Assert.AreEqual("c", lines[0].Value.LineContents.Field3); Assert.AreEqual("", lines[0].Value.LineContents.Field4); Assert.AreEqual("d", lines[1].Value.LineContents.Field1); Assert.AreEqual("e", lines[1].Value.LineContents.Field2); Assert.AreEqual("f", lines[1].Value.LineContents.Field3); Assert.AreEqual("", lines[1].Value.LineContents.Field4); Assert.AreEqual("g", lines[2].Value.LineContents.Field1); Assert.AreEqual("h", lines[2].Value.LineContents.Field2); Assert.AreEqual("i", lines[2].Value.LineContents.Field3); Assert.AreEqual("", lines[2].Value.LineContents.Field4); }
public void TestTrimOutputExceptTextQualifier() { string fileName = WriteTextToFile(String.Format("john, smith,\" ferrari \",sandton{0}debbie, smuts,\" bugatti \",rosebank", "\r\n")); var fields = new TextFileReaderFields(); fields.Delimiter = DelimiterType.Comma; fields.TextQualifier = TextQualifierType.DoubleQuotes; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Field1" }); fieldCollection.Add(new Afield { Name = "Field2" }); fieldCollection.Add(new Afield { Name = "Field3" }); fieldCollection.Add(new Afield { Name = "Field4" }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); var lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(2, lines.Count); Assert.AreEqual("john", lines[0].Value.LineContents.Field1); Assert.AreEqual("smith", lines[0].Value.LineContents.Field2); Assert.AreEqual(" ferrari ", lines[0].Value.LineContents.Field3); Assert.AreEqual("sandton", lines[0].Value.LineContents.Field4); Assert.AreEqual("debbie", lines[1].Value.LineContents.Field1); Assert.AreEqual("smuts", lines[1].Value.LineContents.Field2); Assert.AreEqual(" bugatti ", lines[1].Value.LineContents.Field3); Assert.AreEqual("rosebank", lines[1].Value.LineContents.Field4); }
public void TestFixedLengthFieldsTextQualifier() { string fileName = WriteTextToFile( String.Format("100456NBT'Purchase'131True 050{0}100566BTX'Purchase'099true 055{0}100890ANL'Purchase'866false070", "\r\n")); var fields = new TextFileReaderFields(); fields.TextFileType = FileType.FixedLength; fields.TextQualifier = TextQualifierType.SingleQuotes; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Client", Type = new FieldType(typeof(string)), Length = 6 }); fieldCollection.Add(new Afield { Name = "Security", Type = new FieldType(typeof(string)), Length = 3 }); fieldCollection.Add(new Afield { Name = "Transaction", Type = new FieldType(typeof(string)), Length = 10 }); fieldCollection.Add(new Afield { Name = "Price", Type = new FieldType(typeof(double)), Length = 3 }); fieldCollection.Add(new Afield { Name = "BoolVal", Type = new FieldType(typeof(bool)), Length = 5 }); fieldCollection.Add(new Afield { Name = "IntVal", Type = new FieldType(typeof(int)), Length = 3 }); fields.FieldList = fieldCollection; dynamic output = Execute(fileName, FileReadOptions.LineByLine, fields); var lines = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); Assert.AreEqual(3, lines.Count); Assert.AreEqual("100456", lines[0].Value.LineContents.Client); Assert.AreEqual("NBT", lines[0].Value.LineContents.Security); Assert.AreEqual("Purchase", lines[0].Value.LineContents.Transaction); Assert.AreEqual(131, lines[0].Value.LineContents.Price); Assert.AreEqual(true, lines[0].Value.LineContents.BoolVal); Assert.AreEqual(50, lines[0].Value.LineContents.IntVal); }
private void FieldsAsStringsTester( string textQualifierInFile, TextQualifierType textQualifierForComponent, Tuple <string, string, string>[] expectedResults, DelimiterType delimiterType, string otherDelimiter = "", bool loopResults = false) { string delimiter = ""; switch (delimiterType) { case DelimiterType.Comma: delimiter = ","; break; case DelimiterType.Tab: delimiter = "\t"; break; case DelimiterType.Other: delimiter = otherDelimiter; break; default: throw new Exception("Unexpected DelimiterType " + delimiterType); } string fileName = WriteTextToFile(String.Format("{1}a{1}{0}b{0}c\r\n{1}d{1}{0}e{0}f\r\n{1}g{1}{0}h{0}{1}{1}", delimiter, textQualifierInFile)); var fields = new TextFileReaderFields(); fields.Delimiter = delimiterType; if (delimiterType == DelimiterType.Other) { fields.OtherDelimiter = otherDelimiter; } fields.TextQualifier = textQualifierForComponent; var fieldCollection = new AfieldCollection(); fieldCollection.Add(new Afield { Name = "Field1" }); fieldCollection.Add(new Afield { Name = "Field2" }); fieldCollection.Add(new Afield { Name = "Field3" }); fields.FieldList = fieldCollection; FunctionResult output = Execute(fileName, loopResults ? FileReadOptions.LineByLine : FileReadOptions.ListOfLines, fields); if (loopResults) { List <NextResult> resultList = ((IEnumerable <NextResult>)output.ExecutionPathResult).ToList(); for (int i = 0; i < expectedResults.Length; i++) { Assert.AreEqual(expectedResults[i].Item1, resultList[i].Value.LineContents.Field1); Assert.AreEqual(expectedResults[i].Item2, resultList[i].Value.LineContents.Field2); Assert.AreEqual(expectedResults[i].Item3, resultList[i].Value.LineContents.Field3); } } else { dynamic results = output.Value; for (int i = 0; i < expectedResults.Length; i++) { Assert.AreEqual(expectedResults[i].Item1, results[i].Field1); Assert.AreEqual(expectedResults[i].Item2, results[i].Field2); Assert.AreEqual(expectedResults[i].Item3, results[i].Field3); } } }