blob: b127478de23ff7d8417784c0e679abc5490d89c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
using CsvHelper.Configuration;
using CsvHelper.Configuration.Attributes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace CsvHelper.Tests.Reading
{
public class BadDataTests
{
[Fact]
public void GetRecord_BadDataCountNotDuplicted()
{
var errorCount = 0;
var csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
ReadingExceptionOccurred = args => false,
BadDataFound = args =>
{
++errorCount;
},
Delimiter = ";",
};
var csv = "SKU;Min quantity;List price;Sale price\r\nTestSku1;2;10.99;9.99\r\nTestSku2;2;10.99;9\r\nXXX;\"9;10.9;9";
var stream = new MemoryStream();
using (var writer = new StreamWriter(stream, leaveOpen: true))
{
writer.Write(csv);
writer.Flush();
stream.Position = 0;
}
var textReader = new StreamReader(stream, leaveOpen: true);
var csvReader = new CsvReader(textReader, csvConfiguration);
while (csvReader.Read())
{
csvReader.GetRecord<CsvPrice>();
}
Assert.Equal(1, errorCount);
}
public sealed class CsvPrice
{
[Name("SKU")]
public string Sku { get; set; }
[Name("Min quantity")]
public int MinQuantity { get; set; }
[Name("List price")]
public decimal ListPrice { get; set; }
[Name("Sale price")]
public decimal? SalePrice { get; set; }
}
}
}
|