Example #1
0
        public void NextRecords_CsvWithCsvProfileEmptyCell_CorrectResults(string text, char fieldSeparator, string recordSeparator, string emptyCell)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(text);
                writer.Flush();

                stream.Position = 0;
                var reader    = new CsvReaderProxy();
                var dataTable = reader.Read(stream, Encoding.UTF8, 0, false, recordSeparator, fieldSeparator, '\"', '\"', emptyCell, "_");

                Assert.That(dataTable.Rows[0].ItemArray[0], Is.EqualTo("a"));
                Assert.That(dataTable.Rows[0].ItemArray[1], Is.EqualTo("b"));
                Assert.That(dataTable.Rows[0].ItemArray[2], Is.EqualTo("c"));

                Assert.That(dataTable.Rows[1].ItemArray[0], Is.EqualTo("a"));
                Assert.That(dataTable.Rows[1].ItemArray[1], Is.EqualTo("?"));
                Assert.That(dataTable.Rows[1].ItemArray[2], Is.EqualTo("c"));

                Assert.That(dataTable.Rows[2].ItemArray[0], Is.EqualTo("?"));
                Assert.That(dataTable.Rows[2].ItemArray[1], Is.EqualTo("b"));
                Assert.That(dataTable.Rows[2].ItemArray[2], Is.EqualTo("c"));

                Assert.That(dataTable.Rows[3].ItemArray[0], Is.EqualTo("?"));
                Assert.That(dataTable.Rows[3].ItemArray[1], Is.EqualTo("b"));
                Assert.That(dataTable.Rows[3].ItemArray[2], Is.EqualTo("?"));

                writer.Dispose();
            }
        }
Example #2
0
        public void Read_MissingValue_MatchWithNullValue()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write("a;b;c\r\na;b\r\na;b;c");
                    writer.Flush();

                    stream.Position = 0;

                    var profile   = new CsvProfile(';', '"', "\r\n", false, true, 512, string.Empty, "(null)");
                    var reader    = new CsvReaderProxy(profile);
                    var dataTable = reader.Read(stream);
                    Assert.That(dataTable.Rows[1][2], Is.EqualTo("(null)"));
                }
            }
        }
Example #3
0
        public void Read_EmptyValue_MatchWithEmpty()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write("a;;c");
                    writer.Flush();

                    stream.Position = 0;

                    var profile   = CsvProfile.SemiColumnDoubleQuote;
                    var reader    = new CsvReaderProxy(profile);
                    var dataTable = reader.Read(stream);
                    Assert.That(dataTable.Rows[0][1], Is.EqualTo(string.Empty));
                }
            }
        }
Example #4
0
        public void Read_MissingValue_MatchWithNullValue()
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write("a;b;c\r\na;b\r\na;b;c");
                    writer.Flush();

                    stream.Position = 0;

                    var profile   = CsvProfile.SemiColumnDoubleQuote;
                    var reader    = new CsvReaderProxy(profile);
                    var dataTable = reader.Read(stream);
                    Assert.That(dataTable.Rows[1][2], Is.EqualTo("(null)"));
                }
            }
        }
Example #5
0
        public void Read_MoreFieldThanExpected_ExceptionMessage(string text, int rowNumber, int moreField)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(text);
                    writer.Flush();

                    stream.Position = 0;

                    var profile = CsvProfile.SemiColumnDoubleQuote;
                    var reader  = new CsvReaderProxy(profile);

                    var ex = Assert.Throws <InvalidDataException>(delegate { reader.Read(stream); });
                    Assert.That(ex.Message, Is.StringContaining(string.Format("record {0} ", rowNumber + 1)));
                    Assert.That(ex.Message, Is.StringContaining(string.Format("{0} more", moreField)));
                }
            }
        }
Example #6
0
        public void Read_Comment_CommentedLinesSkipped(string content)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(content);
                    writer.Flush();

                    stream.Position = 0;

                    var profile = new CsvProfile(new CsvDialectDescriptor {
                        Header = false, Delimiter = ';', CommentChar = '#', DoubleQuote = false
                    });
                    var reader    = new CsvReaderProxy(profile);
                    var dataTable = reader.Read(stream);
                    Assert.That(dataTable.Rows.Count, Is.EqualTo(2));
                    Assert.That(dataTable.Columns.Count, Is.EqualTo(3));
                }
            }
        }
Example #7
0
        public void Read_Csv_CorrectResult(string text, int bufferSize, int columnCount)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(text);
                writer.Flush();

                stream.Position = 0;

                var reader    = new CsvReaderProxy(new CsvProfile(';', '\"', "\r\n", false, false, "(empty)", "(null)"), bufferSize);
                var dataTable = reader.Read(stream);
                Assert.That(dataTable.Rows, Has.Count.EqualTo(4));
                Assert.That(dataTable.Columns, Has.Count.EqualTo(columnCount));
                foreach (DataRow row in dataTable.Rows)
                {
                    foreach (var cell in row.ItemArray)
                    {
                        Assert.That(cell.ToString(), Has.Length.EqualTo(3).Or.EqualTo("(empty)").Or.EqualTo("(null)"));
                    }
                }
                writer.Dispose();
            }
        }