public void TestGetCurrentLine([Values(true, false)] bool withHeader) { const int count = 10; StringBuilder sb = new StringBuilder(); for (int i = 0; i < count; i++) { sb.Append("a\r\n"); } using (ICsvReader reader = CreateCsvReader(sb.ToString(), withHeader)) { int expectedLine = -1; Assert.IsTrue(reader.GetCurrentLine() == expectedLine, "No the expected current line"); // for (int i = 0; i < (count - (withHeader ? 1 : 0)); i++) { Assert.IsTrue(reader.Read(), "Can't read!"); expectedLine++; Assert.IsTrue(reader.GetCurrentLine() == expectedLine, "No the expected current line"); } Assert.IsFalse(reader.Read(), "Should be end of file"); Assert.IsTrue(reader.GetCurrentLine() == expectedLine, "Current must not change after and of file"); } }
public void TestEscapingWithSpecialCharacters() { string[] specialChars = { " ", "_", "\t", "\r", "\n", "\r\n", ",", "\"" }; StringBuilder sb = new StringBuilder(); for (int index = 0; index < specialChars.Length; index++) { string s = specialChars[index]; if (index != 0) { sb.Append(','); } sb.AppendFormat("\"azerty{0}azerty\"", s.Replace("\"", "\"\"")); } using (ICsvReader reader = CreateCsvReader(sb.ToString(), false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); for (int index = 0; index < specialChars.Length; index++) { Assert.AreEqual(string.Format("azerty{0}azerty", specialChars[index]), reader.GetValue(index), "Not the excepted value for column {0}", index); } Assert.IsFalse(reader.Read(), "Must be end of file"); } }
public void LoadReferenceData() { _csvReader.Read(); _csvReader.ReadHeader(); while (_csvReader.Read()) { var record = _csvReader.GetRecord <AddressReferenceDataRow>(); _locationDictionary.Add(record.PracticeCode, record.Postcode); } }
public void TestNoEscapingWhenFirstCharIsNotEscapingChare() { const string input = " \"azerty\",t\"\",\t\"\n"; using (ICsvReader reader = CreateCsvReader(input, false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.AreEqual(" \"azerty\"", reader.GetValue(0), "Not the excepted value for column 0"); Assert.AreEqual("t\"\"", reader.GetValue(1), "Not the excepted value for column 1"); Assert.AreEqual("\t\"", reader.GetValue(2), "Not the excepted value for column 2"); Assert.IsFalse(reader.Read(), "Must be end of file"); } }
public void TestCheckNumberOfColumns([Values(true, false)] bool withHeader) { const string input = "azerty\nazerty,azerty"; using (ICsvReader reader = CreateCsvReader(input, withHeader)) { if (!withHeader) { Assert.IsTrue(reader.Read(), "Can't read first line"); } Assert.Throws <CsvReaderWrongNumberOfColumnsException>(() => reader.Read(), "Read should throw CsvReaderWrongNumberOfColumnsException when line has not the expected number of colums"); TestErrorState(reader, "CsvReaderWrongNumberOfColumnsException"); } }
public void TestCheckNoRead([Values(true, false)] bool withHeader) { const string input = "azerty\nazerty"; using (ICsvReader reader = CreateCsvReader(input, withHeader)) { Assert.DoesNotThrow(() => { bool b = reader.WithHeader; }, "Access to WithHeader should not throw exception before call to Read"); Assert.DoesNotThrow(() => { char c = reader.Separator; }, "Access to Separator should not throw exception before call to Read"); Assert.DoesNotThrow(() => reader.GetCurrentLine(), "Access to GetCurrentLine() should not throw exception before call to Read"); if (withHeader) { Assert.DoesNotThrow(() => reader.GetColumnsCount(), "Access to GetColumnsCount should not throw exception before call to Read"); } else { Assert.Throws <CsvReaderNoReadCallException>(() => reader.GetColumnsCount(), "Access to GetColumnsCount should throw CsvReaderNoReadCallException before call to Read"); } Assert.DoesNotThrow(() => reader.GetHeaders(), "Access to GetHeaders() should not throw exception before call to Read"); Assert.Throws <CsvReaderNoReadCallException>(() => reader.GetValue(0), "Access to GetValue should throw CsvReaderNoReadCallException before call to Read"); Assert.DoesNotThrow(() => { bool b = reader.EndOfStream; }, "Access to EndOfStream should not throw exception before call to Read"); Assert.DoesNotThrow(() => reader.Read(), "Call to Read() should not throw exception"); Assert.DoesNotThrow(() => { bool b = reader.WithHeader; }, "Access to WithHeader should not throw exception after call to Read"); Assert.DoesNotThrow(() => { char c = reader.Separator; }, "Access to Separator should not throw exception after call to Read"); Assert.DoesNotThrow(() => reader.GetCurrentLine(), "Access to GetCurrentLine() should not throw exception after call to Read"); Assert.DoesNotThrow(() => reader.GetColumnsCount(), "Access to GetColumnsCount should not throw exception after call to Read"); Assert.DoesNotThrow(() => reader.GetHeaders(), "Access to GetHeaders() should not throw exception after call to Read"); Assert.DoesNotThrow(() => reader.GetValue(0), "Access to GetValue should not throw exception after call to Read"); Assert.DoesNotThrow(() => { bool b = reader.EndOfStream; }, "Access to EndOfStream should not throw exception before after to Read"); } }
private void GameReset() { _isActive = true; _roundActive = false; _items = _csvReader.Read("wordList.csv"); _players = new List <Player>(); }
public void TestEscapingSimple() { const string input = "azerty00,\"azerty01\",\"azerty02\"\r\"azerty10\",azerty11,\"azerty12\"\n\"azerty20\",\"azerty21\",azerty22\r\n\"azerty30\",\"azerty31\",\"azerty32\""; using (ICsvReader reader = CreateCsvReader(input, false)) { for (int l = 0; l < 4; l++) { Assert.IsTrue(reader.Read(), "Can't read line {0}", l); for (int f = 0; f < 3; f++) { Assert.AreEqual(string.Format("azerty{0}{1}", l, f), reader.GetValue(f), "Not the excepted value for line {0} column {1}", l, f); } } Assert.IsFalse(reader.Read(), "Must be end of file"); } }
public IActionResult ImportCsv([FromForm] UploadFileModel model) { using var stream = model.FormFile.OpenReadStream(); var products = _productCsvReader.Read(stream); // TODO: import to database return(Ok(products)); }
public void TestAllKindOfEndLineAndFile([Values("\n", "\r", "\r\n")] string eol1, [Values("\n", "\r", "\r\n")] string eol2, [Values("\n", "\r", "\r\n", null, "")] string eof) { string input = string.Format("azerty,azerty2{0}tttt,tttt2{1}aaaaa,aaaaa2{2}", eol1, eol2, eof); using (ICsvReader reader = CreateCsvReader(input, false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.AreEqual("azerty", reader.GetValue(0), "Not the excepted value for line 0 column 0"); Assert.AreEqual("azerty2", reader.GetValue(1), "Not the excepted value for line 0 column 1"); Assert.IsTrue(reader.Read(), "Can't read second line"); Assert.AreEqual("tttt", reader.GetValue(0), "Not the excepted value for line 1 column 0"); Assert.AreEqual("tttt2", reader.GetValue(1), "Not the excepted value for line 1 column 1"); Assert.IsTrue(reader.Read(), "Can't read third line"); Assert.AreEqual("aaaaa", reader.GetValue(0), "Not the excepted value for line 2 column 0"); Assert.AreEqual("aaaaa2", reader.GetValue(1), "Not the excepted value for line 2 column 1"); Assert.IsFalse(reader.Read(), "Must be end of file"); } }
private static IEnumerable <IEnumerable <string> > ReadAllRecords(ICsvReader reader, bool header) { reader.Configuration.HasHeaderRecord = header; while (reader.Read()) { yield return(reader.CurrentRecord); } }
private static List<string[]> ReadRows(ICsvReader csvReader) { var records = new List<string[]>(); while (csvReader.Read()) { records.Add(csvReader.CurrentRecord); } return records; }
public bool Read(string column1, string column2) { if (_csvReader == null) { throw new ApplicationException("File is closed or hasn't been opened in read mode."); } var columns = _csvReader.Read(); if (columns == null) { return(false); } column1 = columns.Column1; column2 = columns.Column2; return(true); }
public void TestUnclosedEscape() { const string input = "azerty,azerty,\"azert"; using (ICsvReader reader = CreateCsvReader(input, false)) { Assert.Throws <CsvReaderUnclosedEscapeException>(() => reader.Read(), "Read() should thrown CsvReaderUnclosedEscapeException"); TestErrorState(reader, "CsvReaderUnclosedEscapeException"); } }
public sealed override void Build(ICsvReader reader, IDataTableContainer container) { var elements = new List <T>(); while (reader.Read()) { elements.Add(this.ConstructElement(reader, container)); } this.BuildIndex(elements, container); }
public void ParseCsvFile() { while (_csvReader.Read()) { var record = _csvReader.GetRecord <TRowType>() as IDataRow; var dataRowMessage = new DataRowMessage(typeof(TRowType), record); Sender.Tell(dataRowMessage, Self); } Sender.Tell(new FileAnalysisFinishedMessage()); _logger.Info($"Finished parsing {_sourcePath}"); }
public void TestEscapingMultiQuote() { // '"' using (ICsvReader reader = CreateCsvReader("\"", false)) { Assert.Throws <CsvReaderUnclosedEscapeException>(() => reader.Read(), "Read() should thrown CsvReaderUnclosedEscapeException for 1 DQuote"); } // '""' using (ICsvReader reader = CreateCsvReader("\"\"", false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.AreEqual(string.Empty, reader.GetValue(0), "Not the excepted value"); Assert.IsFalse(reader.Read(), "Must be end of file"); } // '"""' using (ICsvReader reader = CreateCsvReader("\"\"\"", false)) { Assert.Throws <CsvReaderUnclosedEscapeException>(() => reader.Read(), "Read() should thrown CsvReaderUnclosedEscapeException for 3 DQuote"); } // '""""' using (ICsvReader reader = CreateCsvReader("\"\"\"\"", false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.AreEqual("\"", reader.GetValue(0), "Not the excepted value"); Assert.IsFalse(reader.Read(), "Must be end of file"); } // '"""""' using (ICsvReader reader = CreateCsvReader("\"\"\"\"\"", false)) { Assert.Throws <CsvReaderUnclosedEscapeException>(() => reader.Read(), "Read() should thrown CsvReaderUnclosedEscapeException for 5 DQuote"); } // '""""""' using (ICsvReader reader = CreateCsvReader("\"\"\"\"\"\"", false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.AreEqual("\"\"", reader.GetValue(0), "Not the excepted value"); Assert.IsFalse(reader.Read(), "Must be end of file"); } // ReSharper restore UnusedVariable }
public void TestGetValueIndex() { const string input = "azerty,azerty"; using (ICsvReader reader = CreateCsvReader(input, false)) { Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.Throws <IndexOutOfRangeException>(() => reader.GetValue(-1), "GetValue(-1) should thrown IndexOutOfRangeException"); Assert.DoesNotThrow(() => reader.GetValue(0), "GetValue(0) should not thrown IndexOutOfRangeException"); Assert.DoesNotThrow(() => reader.GetValue(1), "GetValue(1) should not thrown IndexOutOfRangeException"); Assert.Throws <IndexOutOfRangeException>(() => reader.GetValue(2), "GetValue(2) should thrown IndexOutOfRangeException"); } }
public bool Read(out string column1, out string column2) { var isReadOk = _csvReader.Read(); if (!isReadOk) { column1 = null; column2 = null; return(false); } column1 = _csvReader.GetField <string>(0); column2 = _csvReader.GetField <string>(1); return(true); }
static IEnumerable readCsvRows(ICsvReader csvReader, IDictionary <string, int> fldToIdx) { while (csvReader.Read()) { if (csvReader.FieldHeaders != null && fldToIdx.Count == 0) { for (int i = 0; i < csvReader.FieldHeaders.Length; i++) { fldToIdx[csvReader.FieldHeaders[i]] = i; //Console.WriteLine("Column #{0}: {1}", i, csvReader.FieldHeaders[i]); } } yield return(csvReader.CurrentRecord); } }
public void TestGetColumnsCount([Values(true, false)] bool withHeader) { const string input = "azerty,azerty\r\ntttt,ttttt"; using (ICsvReader reader = CreateCsvReader(input, withHeader)) { if (withHeader) { Assert.AreEqual(2, reader.GetColumnsCount(), "Not the excepted ColumnsCount before call to Read"); } else { Assert.Throws <CsvReaderNoReadCallException>(() => reader.GetColumnsCount(), "Access to GetColumnsCount should throw CsvReaderNoReadCallException before call to Read"); } Assert.IsTrue(reader.Read(), "Can't read first line"); Assert.AreEqual(2, reader.GetColumnsCount(), "Not the excepted ColumnsCount after call to Read"); } }
public void TestGetHeaders([Values(true, false)] bool withHeader) { const string input = "azerty,azerty\r\ntttt,ttttt"; string[] exceptedResult = withHeader ? new[] { "azerty", "azerty" } : Array.Empty <string>(); using (ICsvReader reader = CreateCsvReader(input, withHeader)) { string[] header = reader.GetHeaders(); Assert.AreEqual(exceptedResult.Length, header.Length, "Not the excepted length"); for (int i = 0; i < exceptedResult.Length; i++) { Assert.AreEqual(exceptedResult[i], header[i], "Not the excepted value at i={0}", i); } Assert.IsTrue(reader.Read(), "Can't read first line"); header = reader.GetHeaders(); Assert.AreEqual(exceptedResult.Length, header.Length, "Not the excepted length after read"); for (int i = 0; i < exceptedResult.Length; i++) { Assert.AreEqual(exceptedResult[i], header[i], "Not the excepted value at i={0} after read", i); } } }
public static CsvTable ReadFrom(ICsvReader reader) => ReadFrom(() => reader.Read());
private void TestErrorState(ICsvReader reader, string exceptionName) { Assert.Throws <CsvReaderErrorStateException>(() => reader.Read(), "Read should throw CsvReaderErrorStateException when call Read after {0}", exceptionName); Assert.Throws <CsvReaderErrorStateException>(() => reader.GetValue(0), "GetValue() should throw CsvReaderErrorStateException when call Read after {0}", exceptionName); }
/// <summary> /// Runs the custom importer /// </summary> public void ImportData() { _csvReader.Read(); }