Exemplo n.º 1
0
        public async Task fill_data_table_async_works_with_large_csv_input()
        {
            var csv = string.Empty;

            using (var stringWriter = new StringWriter())
            using (var writer = new CsvWriter(stringWriter))
            {
                writer.WriteRecord("Header1", "Header2");

                for (var i = 0; i < 1000; ++i)
                {
                    writer.WriteRecord("value0_" + i, "value1_" + i);
                }

                writer.Flush();
                csv = stringWriter.ToString();
            }

            // read less than all available records
            using (var reader = CsvReader.FromCsvString(csv))
            {
                var table = new DataTable();
                reader.ReadHeaderRecord();

                Assert.Equal(913, await table.FillAsync(reader, 913));
                Assert.Equal(913, table.Rows.Count);
                Assert.True(reader.HasMoreRecords);
            }

            // read exactly available records
            using (var reader = CsvReader.FromCsvString(csv))
            {
                var table = new DataTable();
                reader.ReadHeaderRecord();

                Assert.Equal(1000, await table.FillAsync(reader, 1000));
                Assert.Equal(1000, table.Rows.Count);
                Assert.False(reader.HasMoreRecords);
            }

            // attempt to read more than available records
            using (var reader = CsvReader.FromCsvString(csv))
            {
                var table = new DataTable();
                reader.ReadHeaderRecord();

                Assert.Equal(1000, await table.FillAsync(reader, 1500));
                Assert.Equal(1000, table.Rows.Count);
                Assert.False(reader.HasMoreRecords);
            }
        }