public void TestNotEnoughtItems()
 {
     using (var reader = new CsvStreamReader <TestModel>(StreamHelper.GenerateStream("name,type,cost,id,date\nClaws,Attachment,10,\"34.5")))
     {
         reader.AsEnumerable().ToList();
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Method is meant to parse a csv string.
 /// </summary>
 /// <param name="csv">The csv string that needs to be parsed.</param>
 /// <param name="options">The options that should be sent off to the stream.</param>
 /// <typeparam name="TModel">The model the parser needs to turn into.</typeparam>
 /// <returns>The list of objects that the parser comes back at.</returns>
 public static List <TModel> Parse <TModel>(string csv, CsvStreamOptions options)
     where TModel : class, new()
 {
     using (var reader = new CsvStreamReader <TModel>(GenerateStream(csv), options))
     {
         return(reader.AsEnumerable().ToList());
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Method is meant to deal with reading csv files from the system.
 /// </summary>
 /// <param name="path">The file path to the csv file that needs to be parsed.</param>
 /// <param name="options">The options that should be sent off to the stream.</param>
 /// <typeparam name="TModel">The model the parser needs to turn into.</typeparam>
 /// <returns>The list of objects that the parser comes back at.</returns>
 public static List <TModel> ParseFile <TModel>(string path, CsvStreamOptions options)
     where TModel : class, new()
 {
     using (var reader = new CsvStreamReader <TModel>(path, options))
     {
         return(reader.AsEnumerable().ToList());
     }
 }
 public void TestNoDefaults()
 {
     using (var reader = new CsvStreamReader <SecondTestModel>(StreamHelper.GenerateStream("Claws,,10,\"34.5\",03/27/1987"), new CsvStreamOptions()
     {
         AllowDefaults = false, ParseHeaders = false, DataRow = 0
     }))
     {
         reader.AsEnumerable().ToList();
     }
 }
 public void ProcessLargeFile()
 {
     using (var reader = new CsvStreamReader <LargeModel>("large.csv", new CsvStreamOptions()
     {
         ParseHeaders = false, RowDelimiter = "\n"
     }))
     {
         var count = 0;
         Parallel.ForEach(reader.AsEnumerable(), (x, state, index) => {
             count++;
         });
     }
 }
Esempio n. 6
0
        public void TestStringParser()
        {
            using (var reader = new CsvStreamReader <TestModel>(StreamHelper.GenerateStream("name,type,cost,id,date\r\nClaws,Attachment,10,\"34.5\",03/27/1987")))
            {
                var result = reader.AsEnumerable().FirstOrDefault();

                Assert.AreEqual(result.Name, "Claws");
                Assert.AreEqual(result.Type, TestType.Attachment);
                Assert.AreEqual(result.Cost, 10);
                Assert.AreEqual(result.Id, 34.5);
                Assert.AreEqual(result.Date, DateTime.Parse("03/27/1987"));
            }
        }
Esempio n. 7
0
        public void TestNoDefaults()
        {
            using (var reader = new CsvStreamReader <TestModel>(StreamHelper.GenerateStream("name,type,cost,id,date\nClaws,Attachment,10,\"34.5\",03/27/1987"), new CsvStreamOptions()
            {
                AllowDefaults = false, RowDelimiter = "\n", HeaderRowDelimiter = "\n"
            }))
            {
                var result = reader.AsEnumerable().FirstOrDefault();

                Assert.AreEqual(result.Name, "Claws");
                Assert.AreEqual(result.Type, TestType.Attachment);
                Assert.AreEqual(result.Cost, 10);
                Assert.AreEqual(result.Id, 34.5);
                Assert.AreEqual(result.Date, DateTime.Parse("03/27/1987"));
            }
        }
Esempio n. 8
0
        public void TestNoHeaders()
        {
            using (var reader = new CsvStreamReader <SecondTestModel>(StreamHelper.GenerateStream("Claws,Effect,10,60.05,9-5-1029"), new CsvStreamOptions()
            {
                ParseHeaders = false, DataRow = 0
            }))
            {
                var result = reader.AsEnumerable().FirstOrDefault();

                Assert.AreEqual(result.Name, "Claws");
                Assert.AreEqual(result.Type, TestType.Effect);
                Assert.AreEqual(result.Cost, 10);
                Assert.AreEqual(result.Id, 60.05);
                Assert.AreEqual(result.Date, DateTime.Parse("9-5-1029"));
            }
        }
Esempio n. 9
0
        public void TestSkipHeaderAndData()
        {
            var stream = StreamHelper.GenerateStream("This is an example message\r\n \r\nname,type,cost,id,date\r\n \r\nThe Data follows this:\r\nClaws,Attachment,10,\"34.5\",03/27/1987");

            using (var reader = new CsvStreamReader <TestModel>(stream, new CsvStreamOptions()
            {
                DataRow = 5, HeaderRow = 2
            }))
            {
                var result = reader.AsEnumerable().FirstOrDefault();

                Assert.AreEqual(result.Name, "Claws");
                Assert.AreEqual(result.Type, TestType.Attachment);
                Assert.AreEqual(result.Cost, 10);
                Assert.AreEqual(result.Id, 34.5);
                Assert.AreEqual(result.Date, DateTime.Parse("03/27/1987"));
            }
        }
Esempio n. 10
0
        public void TestWithNoWrapper()
        {
            var stream = StreamHelper.GenerateStream("name,type,cost,id,date\r\nClaws,Attachment,10,34.5,03/27/1987");

            using (var reader = new CsvStreamReader <TestModel>(stream, new CsvStreamOptions()
            {
                Wrapper = null
            }))
            {
                var result = reader.AsEnumerable().FirstOrDefault();

                Assert.AreEqual(result.Name, "Claws");
                Assert.AreEqual(result.Type, TestType.Attachment);
                Assert.AreEqual(result.Cost, 10);
                Assert.AreEqual(result.Id, 34.5);
                Assert.AreEqual(result.Date, DateTime.Parse("03/27/1987"));
            }
        }
Esempio n. 11
0
        public void TestCustomDelimiterAndWrapper()
        {
            var stream = StreamHelper.GenerateStream("name;type;cost;id;date\n*Claws*;Spell;50;50.55;*6-19-2012*");

            using (var reader = new CsvStreamReader <TestModel>(stream, new CsvStreamOptions()
            {
                Delimiter = ";", Wrapper = '*', RowDelimiter = "\n", HeaderRowDelimiter = "\n", HeaderDelimiter = ";"
            }))
            {
                var result = reader.AsEnumerable().FirstOrDefault();

                Assert.AreEqual(result.Name, "Claws");
                Assert.AreEqual(result.Type, TestType.Spell);
                Assert.AreEqual(result.Cost, 50);
                Assert.AreEqual(result.Id, 50.55);
                Assert.AreEqual(result.Date, DateTime.Parse("6-19-2012"));
            }
        }
Esempio n. 12
0
 public static List <TModel> ParseFile <TModel>(Stream stream, CsvStreamOptions csvStreamOptions)
     where TModel : class, new()
 {
     using var reader = new CsvStreamReader <TModel>(stream, csvStreamOptions);
     return(reader.AsEnumerable().ToList());
 }
Esempio n. 13
0
        public void TestTypes()
        {
            using (var reader = new CsvStreamReader <TypeModel>("type.csv"))
            {
                var results = reader.AsEnumerable().ToList();

                Assert.AreEqual(results[0].Boolean, true);
                Assert.AreEqual(results[1].Boolean, false);
                Assert.AreEqual(results[2].Boolean, default(bool));

                Assert.AreEqual(results[0].Char, 't');
                Assert.AreEqual(results[1].Char, 'h');
                Assert.AreEqual(results[2].Char, default(char));

                Assert.AreEqual(results[0].SByte, (sbyte)1);
                Assert.AreEqual(results[1].SByte, (sbyte)1);
                Assert.AreEqual(results[2].SByte, default(sbyte));

                Assert.AreEqual(results[0].Byte, (byte)5);
                Assert.AreEqual(results[1].Byte, (byte)2);
                Assert.AreEqual(results[2].Byte, default(byte));

                Assert.AreEqual(results[0].Int16, (short)1);
                Assert.AreEqual(results[1].Int16, (short)16);
                Assert.AreEqual(results[2].Int16, default(short));

                Assert.AreEqual(results[0].UInt16, (ushort)5);
                Assert.AreEqual(results[1].UInt16, (ushort)116);
                Assert.AreEqual(results[2].UInt16, default(ushort));

                Assert.AreEqual(results[0].Int32, (int)1);
                Assert.AreEqual(results[1].Int32, (int)32);
                Assert.AreEqual(results[2].Int32, default(int));

                Assert.AreEqual(results[0].UInt32, (uint)5);
                Assert.AreEqual(results[1].UInt32, (uint)332);
                Assert.AreEqual(results[2].UInt32, default(uint));

                Assert.AreEqual(results[0].Int64, (long)1);
                Assert.AreEqual(results[1].Int64, (long)64);
                Assert.AreEqual(results[2].Int64, default(int));

                Assert.AreEqual(results[0].UInt64, (ulong)5);
                Assert.AreEqual(results[1].UInt64, (ulong)664);
                Assert.AreEqual(results[2].UInt64, default(ulong));

                Assert.AreEqual(results[0].Single, (Single)1);
                Assert.AreEqual(results[1].Single, (Single)111);
                Assert.AreEqual(results[2].Single, default(Single));

                Assert.AreEqual(results[0].Double, (double)5.55);
                Assert.AreEqual(results[1].Double, (double)34.86);
                Assert.AreEqual(results[2].Double, default(double));

                Assert.AreEqual(results[0].Decimal, (decimal)1.88);
                Assert.AreEqual(results[1].Decimal, (decimal)176.88);
                Assert.AreEqual(results[2].Decimal, default(decimal));

                Assert.AreEqual(results[0].DateTime, DateTime.Parse("01/03/1994"));
                Assert.AreEqual(results[1].DateTime, DateTime.Parse("9-12-2005"));
                Assert.AreEqual(results[2].DateTime, default(DateTime));

                Assert.AreEqual(results[0].String, "This is a string for you");
                Assert.AreEqual(results[1].String, "Special String, for \"you\"");
                Assert.AreEqual(results[2].String, string.Empty);
            }
        }