Exemplo n.º 1
0
        public void CaseInsensitiveColumnNames()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            Assert.AreEqual(0, reader.GetOrdinal("header1"));
            Assert.AreEqual(1, reader.GetOrdinal("HEADER2"));
            Assert.AreEqual(2, reader.GetOrdinal("HeaDER3"));
            reader.Dispose();
        }
Exemplo n.º 2
0
        public void HeadersParse()
        {
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt");

            Assert.AreEqual(0, reader.GetOrdinal("Header1"));
            Assert.AreEqual(1, reader.GetOrdinal("Header2"));
            Assert.AreEqual(2, reader.GetOrdinal("Header3"));
            reader.Dispose();
        }
Exemplo n.º 3
0
        public void AddStaticValue()
        {
            Dictionary <String, String> staticColumns = new Dictionary <String, String>();

            staticColumns.Add("Column1", "Value");
            CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt", staticColumns);

            Assert.AreEqual(3, reader.GetOrdinal("Column1"));
            while (reader.Read())
            {
                Assert.AreEqual("Value", reader.GetValue(reader.GetOrdinal("Column1")));
            }
            reader.Close();
            reader.Dispose();
        }
Exemplo n.º 4
0
 public void GetOrdinalFailure()
 {
     using (CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt"))
     {
         int i = reader.GetOrdinal("ZZZZ");
     }
     //Assert.Fail();
     //reader.Dispose();
 }
Exemplo n.º 5
0
        public void GetValuesTest()
        {
            var s = new StringBuilder();

            s.AppendLine("Boolean,Byte,Bytes,Char,Chars,DateTime,Decimal,Double,Float,Guid,Short,Int,Long,Null");
            s.AppendLine("true,1,0x0102,a,ab,1/1/2019,1.23,4.56,7.89,eca0c8c6-9a2a-4e6c-8599-3561abda13f1,1,2,3,null");
            using (var reader = new StringReader(s.ToString()))
                using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                {
                    csv.Configuration.Delimiter = ",";
                    csv.Configuration.TypeConverterOptionsCache.GetOptions <string>().NullValues.Add("null");
                    var dataReader = new CsvDataReader(csv);
                    dataReader.Read();

                    Assert.AreEqual(true, dataReader.GetBoolean(0));
                    Assert.AreEqual(1, dataReader.GetByte(1));

                    byte[] byteBuffer = new byte[2];
                    dataReader.GetBytes(2, 0, byteBuffer, 0, byteBuffer.Length);
                    Assert.AreEqual(0x1, byteBuffer[0]);
                    Assert.AreEqual(0x2, byteBuffer[1]);

                    Assert.AreEqual('a', dataReader.GetChar(3));

                    char[] charBuffer = new char[2];
                    dataReader.GetChars(4, 0, charBuffer, 0, charBuffer.Length);
                    Assert.AreEqual('a', charBuffer[0]);
                    Assert.AreEqual('b', charBuffer[1]);

                    Assert.IsNull(dataReader.GetData(0));
                    Assert.AreEqual(DateTime.Parse("1/1/2019"), dataReader.GetDateTime(5));
                    Assert.AreEqual(typeof(string).Name, dataReader.GetDataTypeName(0));
                    Assert.AreEqual(1.23m, dataReader.GetDecimal(6));
                    Assert.AreEqual(4.56d, dataReader.GetDouble(7));
                    Assert.AreEqual(typeof(string), dataReader.GetFieldType(0));
                    Assert.AreEqual(7.89f, dataReader.GetFloat(8));
                    Assert.AreEqual(Guid.Parse("eca0c8c6-9a2a-4e6c-8599-3561abda13f1"), dataReader.GetGuid(9));
                    Assert.AreEqual(1, dataReader.GetInt16(10));
                    Assert.AreEqual(2, dataReader.GetInt32(11));
                    Assert.AreEqual(3, dataReader.GetInt64(12));
                    Assert.AreEqual("Boolean", dataReader.GetName(0));
                    Assert.AreEqual(0, dataReader.GetOrdinal("Boolean"));

                    Assert.AreEqual("true", dataReader.GetString(0));
                    Assert.AreEqual("true", dataReader.GetValue(0));

                    var objectBuffer = new object[14];
                    dataReader.GetValues(objectBuffer);
                    Assert.AreEqual("true", objectBuffer[0]);
                    Assert.AreEqual(DBNull.Value, objectBuffer[13]);
                    Assert.IsTrue(dataReader.IsDBNull(13));
                }
        }
Exemplo n.º 6
0
        public void GetOrdinalCaseInsensitiveTest()
        {
            var parser = new ParserMock
            {
                { "Id", "Name" },
                { "1", "one" },
                null,
            };

            using (var csv = new CsvReader(parser))
            {
                using (var dr = new CsvDataReader(csv))
                {
                    var ordinal = dr.GetOrdinal("name");

                    Assert.Equal(1, ordinal);
                }
            }
        }
Exemplo n.º 7
0
        public void GetOrdinalMissingTest()
        {
            var parser = new ParserMock
            {
                { "Id", "Name" },
                { "1", "one" },
                null,
            };

            using (var csv = new CsvReader(parser))
            {
                using (var dr = new CsvDataReader(csv))
                {
                    Assert.Throws <IndexOutOfRangeException>(() =>
                    {
                        dr.GetOrdinal("Foo");
                    });
                }
            }
        }
Exemplo n.º 8
0
        public void GetOrdinalCaseInsensitiveTest()
        {
            var data = new List <string[]>
            {
                new[] { "Id", "Name" },
                new[] { "1", "one" },
                null,
            };
            var queue  = new Queue <string[]>(data);
            var parser = new ParserMock(queue);

            using (var csv = new CsvReader(parser))
            {
                using (var dr = new CsvDataReader(csv))
                {
                    var ordinal = dr.GetOrdinal("name");

                    Assert.AreEqual(1, ordinal);
                }
            }
        }
Exemplo n.º 9
0
        public void GetOrdinalMissingTest()
        {
            var data = new List <string[]>
            {
                new[] { "Id", "Name" },
                new[] { "1", "one" },
                null,
            };
            var queue  = new Queue <string[]>(data);
            var parser = new ParserMock(queue);

            using (var csv = new CsvReader(parser))
            {
                using (var dr = new CsvDataReader(csv))
                {
                    Assert.ThrowsException <IndexOutOfRangeException>(() =>
                    {
                        dr.GetOrdinal("Foo");
                    });
                }
            }
        }