public void FileLocationNullWhenStreamConstructorUsed() { var streamInput = StringToStream("Test String"); var parser = new DelimitedFileParser(streamInput, ","); Assert.IsTrue(parser._fileLocation == null); }
public void ValidateAllDataIsAddedToDataReader2() { /*Creates csv formatted text for input: * Column1,Column2,Column3 * Data1,Data2,Data3 */ String[] columns = { "Column1", "Column2", "Column3" }; String[] data = { "Data1", "Data2", "Data3" }; var delimiter = ","; var input = String.Join(delimiter, columns) + Environment.NewLine + String.Join(delimiter, data); var streamInput = StringToStream(input); var parser = new DelimitedFileParser(streamInput, ","); var parsedData = parser.ParseData(); var isClosed = parsedData.IsClosed; var fieldCount = parsedData.FieldCount; Object[] row = new Object[parsedData.FieldCount]; Assert.AreEqual(data.Length, row.Length); while (parsedData.Read()) { parsedData.GetValues(row); foreach (var item in data) { Assert.IsTrue(row.Contains(item)); } } }
public void DelimitedFileParser_ToString() { var aStr = "a,b,c\n1,2,3\n4,5,6"; var a = new DelimitedFileParser(); a.ParseString(aStr); Assert.AreEqual(aStr, a.ToString(true)); }
public void ReadSingleDataRow() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("1,2,3"); DelimitedFileParser parser = new DelimitedFileParser(",", "\r\n"); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "1", "2", "3" }); }
public void ValidateColumnParsing() { var input = @"""Column1"", ""Column2"", ""Column3"""; var streamInput = StringToStream(input); var parser = new DelimitedFileParser(streamInput, ","); var result = parser.ParseColumns(); Assert.AreEqual(result.Count(), 3); }
public void FileStreamNullWhenLocationConstructorUsed() { var location = CreateFile("Sample Text for temp file"); using (var parser = new DelimitedFileParser(location, ",")) { Assert.IsTrue(parser._fileStream == null); } DeleteFile(location); }
public void ColumnNameInFirstRowTest() { DelimitedFileParser target = new DelimitedFileParser(",", "\r\n"); bool expected = false; bool actual; target.ColumnNameInFirstRow = expected; actual = target.ColumnNameInFirstRow; Assert.AreEqual(expected, actual); }
public void TextQualifierTest() { DelimitedFileParser target = new DelimitedFileParser(",", "\r\n"); string expected = "\""; string actual; target.TextQualifier = expected; actual = target.TextQualifier; Assert.AreEqual(expected, actual); }
public void DataRowsToSkipTest() { DelimitedFileParser target = new DelimitedFileParser(",", "\r\n"); int expected = 17; int actual; target.DataRowsToSkip = expected; actual = target.DataRowsToSkip; Assert.AreEqual(expected, actual); }
public void ValidateDuplicateColumnsNotAllowed() { var input = "Hello, this is a test!"; var streamInput = StringToStream(input); var parser = new DelimitedFileParser(streamInput, ","); String[] columns = { "Column1", "Column1", "Column2" }; Assert.Throws <Exceptions.DuplicateColumnsExistExcepetion>(() => parser.ValidateColumns(columns)); }
public void SourceExistsValidationDetectsfile() { var location = CreateFile("Sample Text for temp file"); using (var parser = new DelimitedFileParser(location, ",")) { Assert.DoesNotThrow(() => parser.SourceExists()); } DeleteFile(location); }
public void HeaderRowDelimiterTest() { DelimitedFileParser target = new DelimitedFileParser(",", "\r\n"); string expected = "\r\n"; string actual; target.HeaderRowDelimiter = expected; actual = target.HeaderRowDelimiter; Assert.AreEqual(expected, actual); }
public void DelimitedFileParser_GetAt() { var aStr = "a,b,c\n1,2,3\n4,5,6"; var a = new DelimitedFileParser(); a.ParseString(aStr); var dataMap = a.GetMap(); Assert.AreEqual("3", dataMap[0]["c"]); }
public void ReadColumnNamesTest() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("A,B,C\r\n1,2,3"); DelimitedFileParser parser = new DelimitedFileParser(",", "\r\n"); parser.ColumnNameInFirstRow = true; parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "A", "B", "C" }); }
public void DelimitedFileParser_GetAtIndexOutOfRange() { var aStr = "a,b,c\n1,2,3\n4,5,6"; var a = new DelimitedFileParser(); a.ParseString(aStr); var dataMap = a.GetMap(); Assert.Throws <KeyNotFoundException>(() => Console.WriteLine(dataMap[0]["f"])); }
public void ReadSingleColumnRows() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("1,2,3"); DelimitedFileParser parser = new DelimitedFileParser(string.Empty, ","); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "1" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "2" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "3" }); }
public void GetRecordsBasicTestFromString(string inputline, string expected) { //Arrange var parser = new DelimitedFileParser(inputline, true); Func <string[], string> getFullName = (input) => $"{input[1]} {input[0]}"; //Act var result = parser.GetRecords <string>(getFullName); //Assert Assert.Single(result); Assert.Equal(expected, result[0]); }
public void ReadSingleColumnRows2() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("ABC\r\nDEF\r\nGHI"); DelimitedFileParser parser = new DelimitedFileParser(string.Empty, "\r\n"); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "ABC" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "DEF" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "GHI" }); }
public void ReadColumnNamesAfterHeaderTest() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("\r\n\r\nHeader text\r\nA,B,C\r\n1,2,3"); DelimitedFileParser parser = new DelimitedFileParser(",", "\r\n"); parser.ColumnNameInFirstRow = true; parser.HeaderRowDelimiter = "\r\n"; parser.HeaderRowsToSkip = 3; parser.SkipHeaderRows(reader); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "A", "B", "C" }); }
public void DelimitedFileParser_PrintAgelent() { var aStr = "###Formula\tMass\tCompound name\tKEGG\tCAS\tPolarity\tIon Species\tCCS\tZ\tGas\tCCS Standard\tNotes\n" + "#Formula\tMass\tCpd\tKEGG\tCAS\tPolarity\tIon Species\tCCS\tZ\tGas\tCCS Standard\tNotes\n" + "C5H9NO3\t131.0576\tTrans-4-Hydroxy-L-proline\tC01157\t51-35-4\tpositive\t(M+H)+\t130.23\t\tN2\t\t\n" + "C6H10N2O4\t174.0634\tN-Alpha-Acetyl-L-Asparagine\t\t4033-40-3\tnegative\t(M-H)-\t137.87\t\tN2\t\t\n"; var bStr = "main_class\tsubclass\tCatalog\tCompany\tkegg\tCID\tInChi\tNeutral Name\tcas\tformula\tmass\tmPlusH\tmPlusHCCS\tmPlusHRsd\tmPlusNa\tmPlusNaCCS\tmPlusNaRsd\tmMinusH\tmMinusHCCS\tmMinusHRsd\tmPlusDot\tmPlusDotCCS\tmPlusDotRSD\n" + "Primary Metabolite\tAmino Acid\tH54409\tSigma-Aldrich\tC01157\t5810\tPMMYEEVYMWASQN-DMTCNVIQSA-N\tTrans-4-Hydroxy-L-proline\t51-35-4\tC5H9NO3\t131.0576\t132.0655\t130.23\t0.51\t154.0473\tN/A\tN/A\t130.0496\tN/A\tN/A\t\t\t\n" + "Primary Metabolite\tAmino Acid\tsc-215594\tSanta Cruz Biotechnology\t\t99715\tHXFOXFJUNFFYMO-BYPYZUCNSA-N\tN-Alpha-Acetyl-L-Asparagine\t4033-40-3\tC6H10N2O4\t174.0634\t175.0713\tN/A\tN/A\t197.0532\tN/A\tN/A\t173.0555\t137.87\t0.45\t\t\t"; var a = new DelimitedFileParser(); a.ParseString(bStr, '\t'); Assert.AreEqual(aStr, a.PrintAgilent()); }
public void DelimitedFileParser_Concat() { var aStr = "a,b,c\n1,2,3\n4,5,6"; var bStr = "a,b,c\n7,8,9\n10,11,12"; var fStr = "a,b,c\n1,2,3\n4,5,6\n7,8,9\n10,11,12"; var a = new DelimitedFileParser(); var b = new DelimitedFileParser(); a.ParseString(aStr); b.ParseString(bStr); var success = a.Concat(b); Assert.IsTrue(success); Assert.AreEqual(fStr, a.ToString(true)); }
public void ReadSingleColumnRows3() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("\"ABC\"\r\n\"DE\"F\r\n\"G\"HI"); DelimitedFileParser parser = new DelimitedFileParser(string.Empty, "\r\n"); parser.TextQualifier = "\""; parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "ABC" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "DEF" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "GHI" }); }
public void ReadMultipleDataRows() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("1,1,1,1\r\n2,2,2,2\r\n3,3,3,3\r\n4,4,4,4"); DelimitedFileParser parser = new DelimitedFileParser(",", "\r\n"); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "1", "1", "1", "1" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "2", "2", "2", "2" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "3", "3", "3", "3" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "4", "4", "4", "4" }); }
public void DelimitedFileParser_Headers() { var aStr = "a,b,c\n1,2,3\n4,5,6"; var a = new DelimitedFileParser(); var b = new DelimitedFileParser(); a.ParseString(aStr); b.ParseString(aStr); var size = a.GetHeaders().Count; for (var i = 0; i < size; i++) { Assert.AreEqual(b.GetHeaders()[i], a.GetHeaders()[i]); } }
public void TestSkippingTooManyHeaderRows() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("\r\n\r\nHeader\r\nA,B,C,D\r\n1,1,1,1\r\n2,2,2,2\r\n3,3,3,3\r\n4,4,4,4\r\n"); DelimitedFileParser parser = new DelimitedFileParser(",", "\r\n"); parser.HeaderRowDelimiter = "\r\n"; parser.HeaderRowsToSkip = 12; parser.DataRowsToSkip = 15; parser.ColumnNameInFirstRow = true; parser.SkipInitialRows(reader); Assert.AreEqual <bool>(true, reader.IsEOF); }
public bool AddFromFile(string filename) { if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } var parser = new DelimitedFileParser(filename); List <Person> people = parser.GetRecords <Person>(_stringsToPerson); foreach (var person in people) { _recordsRepository.AddRecord(person); } return(true); }
public void DelimitedFileParser_ConcatFail() { var aStr = "a,b\n1,2,3\n4,5,6"; var aStrReturned = "a,b\n1,2\n4,5"; var bStr = "a,b,c\n7,8,9\n10,11,12"; var a = new DelimitedFileParser(); var b = new DelimitedFileParser(); a.ParseString(aStr); b.ParseString(bStr); var success = a.Concat(b); Assert.IsFalse(success); Assert.AreEqual(aStrReturned, a.ToString(true)); }
public void ReadMultipleDataRowsAfterHeaderWithMessyParsing() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("\r\n\r\nHeader text\r\n A)::-)brr\"-:-)\r\n-\r\nasdfghj\"-\"\"-\":-)\"-\":-)\"-\"\"-\"\"-\":--\r"); DelimitedFileParser parser = new DelimitedFileParser(":-)", "-\r\n"); parser.HeaderRowDelimiter = "\r\n"; parser.HeaderRowsToSkip = 3; parser.TextQualifier = "\"-\""; parser.SkipHeaderRows(reader); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { " A):", "brr\"-", "\r\n" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "asdfghj\"-\"\"-\"", ":-)\"-\":-" }); }
public void TestSkippingRows() { RowData rowData = new RowData(); IFileReader reader = FileReaderTest.GetReader("\r\n\r\nHeader\r\nA,B,C,D\r\n1,1,1,1\r\n2,2,2,2\r\n3,3,3,3\r\n4,4,4,4\r\n"); DelimitedFileParser parser = new DelimitedFileParser(",", "\r\n"); parser.HeaderRowDelimiter = "\r\n"; parser.HeaderRowsToSkip = 3; parser.DataRowsToSkip = 3; parser.ColumnNameInFirstRow = true; parser.SkipInitialRows(reader); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[] { "4", "4", "4", "4" }); parser.ParseNextRow(reader, rowData); RowDataTest.VerifyParsedRow(rowData, new string[0]); Assert.AreEqual <bool>(true, reader.IsEOF); }
private async Task <IParser> CreateParserAsync(FileStream downloadedFile, List <ImportJobError> violations) { return(await Task.Run(() => { DelimitedFileParser retVal = null; try { retVal = new DelimitedFileParser(downloadedFile, Constant.CommaSeparator); } catch (Exception ex) { var error = new ImportJobError() { Message = ex.Message, Details = ex.ToString(), Type = Constant.ImportUtilityJob.ErrorType.FileLevel, LineNumber = null }; violations.Add(error); } return retVal; })); }