예제 #1
0
        public async Task TestEntityReaderWithAlternativeHeaders()
        {
            var csvData =
                "A;B1;C2;D" + Environment.NewLine +
                "22;Text;true;01.01.2010";

            using var csvReader = new CsvReader(csvData);

            var csvTableReader = new CsvTableReader(csvReader);

            csvTableReader.SetFormats <DateTime>("dd.MM.yyyy");
            csvTableReader.SetTrueFalseString <bool>("true", "false");

            Assert.NotNull(csvTableReader);

            await csvTableReader.ReadHeadersAsync();

            Assert.Equal(4, csvTableReader.Headers.Count);
            Assert.Equal("A", csvTableReader.Headers[0]);
            Assert.Equal("B1", csvTableReader.Headers[1]);
            Assert.Equal("C2", csvTableReader.Headers[2]);
            Assert.Equal("D", csvTableReader.Headers[3]);

            await csvTableReader.ReadAsync();

            Assert.Equal(new SampleObject()
            {
                A = 22, B = "Text", C = true, D = new DateTime(2010, 1, 1)
            }, csvTableReader.Get <SampleObject>());
        }
예제 #2
0
        public async Task TestTypedValues()
        {
            var csvData =
                "A;B;C;D;E" + Environment.NewLine +
                "22;Text;true;01.01.2010;A" + Environment.NewLine +
                "-31;A long text;false;20.01.2050;B" + Environment.NewLine +
                "55;\"A text with ;\";;31.07.1971;C";

            using var csvReader = new CsvReader(csvData);

            var csvTableReader = new CsvTableReader(csvReader);

            Assert.NotNull(csvTableReader);

            csvTableReader.SetFormats <DateTime>("dd.MM.yyyy");

            await csvTableReader.ReadHeadersAsync();

            Assert.Equal(5, csvTableReader.Headers.Count);
            Assert.Equal("A", csvTableReader.Headers[0]);
            Assert.Equal("B", csvTableReader.Headers[1]);
            Assert.Equal("C", csvTableReader.Headers[2]);
            Assert.Equal("D", csvTableReader.Headers[3]);
            Assert.Equal("E", csvTableReader.Headers[4]);

            await csvTableReader.ReadAsync();

            Assert.Equal(22, csvTableReader.GetValue <int>("A"));
            Assert.Equal("Text", csvTableReader.GetValue <string>("B"));
            Assert.True(csvTableReader.GetValue <bool>("C"));
            Assert.Equal(new DateTime(2010, 1, 1), csvTableReader.GetValue <DateTime>("D"));
            Assert.Equal(SampleEnum.A, csvTableReader.GetValue <SampleEnum>("E"));

            await csvTableReader.ReadAsync();

            Assert.Equal(-31, csvTableReader.GetValue <int>("A"));
            Assert.Equal("A long text", csvTableReader.GetValue <string>("B"));
            Assert.False(csvTableReader.GetValue <bool>("C"));
            Assert.Equal(new DateTime(2050, 1, 20), csvTableReader.GetValue <DateTime>("D"));
            Assert.Equal(SampleEnum.B, csvTableReader.GetValue <SampleEnum?>("E"));

            await csvTableReader.ReadAsync();

            Assert.True(csvTableReader.TryGetValue <int>("A", out var a));
            Assert.Equal(55, a);
            Assert.True(csvTableReader.TryGetValue <string>("B", out var b));
            Assert.Equal("A text with ;", b);
            Assert.True(csvTableReader.TryGetValue <bool?>("C", out var c));
            Assert.Null(c);
            Assert.True(csvTableReader.TryGetValue <DateTime>("D", out var d));
            Assert.Equal(new DateTime(1971, 7, 31), d);
        }
예제 #3
0
        public async Task TestEntityReader()
        {
            var csvData =
                "A;B;C;D;G" + Environment.NewLine +
                "22;Text;true;01.01.2010;A" + Environment.NewLine +
                "-31;A long text;false;20.01.2050;B" + Environment.NewLine +
                "55;\"A text with ;\";;31.07.1971;C";

            using var csvReader = new CsvReader(csvData);

            var csvTableReader = new CsvTableReader(csvReader);

            csvTableReader.SetFormats <DateTime>("dd.MM.yyyy");
            csvTableReader.SetTrueFalseString <bool>("true", "false");

            Assert.NotNull(csvTableReader);

            await csvTableReader.ReadHeadersAsync();

            Assert.Equal(5, csvTableReader.Headers.Count);
            Assert.Equal("A", csvTableReader.Headers[0]);
            Assert.Equal("B", csvTableReader.Headers[1]);
            Assert.Equal("C", csvTableReader.Headers[2]);
            Assert.Equal("D", csvTableReader.Headers[3]);
            Assert.Equal("G", csvTableReader.Headers[4]);

            await csvTableReader.ReadAsync();

            Assert.Equal(new SampleObject()
            {
                A = 22, B = "Text", C = true, D = new DateTime(2010, 1, 1), G = SampleEnum.A
            }, csvTableReader.Get <SampleObject>());

            await csvTableReader.ReadAsync();

            Assert.Equal(new SampleObject()
            {
                A = -31, B = "A long text", C = false, D = new DateTime(2050, 1, 20), G = SampleEnum.B
            }, csvTableReader.Get <SampleObject>());

            await csvTableReader.ReadAsync();

            Assert.Equal(new SampleObject()
            {
                A = 55, B = "A text with ;", C = null, D = new DateTime(1971, 7, 31), G = SampleEnum.C
            }, csvTableReader.Get <SampleObject>());
        }