Example #1
0
        static void QuickTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoManifoldReader(reader))
                        {
                            parser.Configuration.RecordSelector         = (l) => typeof(ExpandoObject);
                            parser.Configuration[typeof(ExpandoObject)] = new ChoCSVRecordConfiguration();

                            writer.WriteLine("Id,Name,Salary");
                            writer.WriteLine("1,Carl,1000");
                            writer.WriteLine("2,Mark,2000");
                            writer.WriteLine("3,Tom,3000");

                            writer.Flush();
                            stream.Position = 0;

                            object rec;
                            while ((rec = parser.Read()) != null)
                            {
                                Console.WriteLine(rec.ToStringEx());
                            }
                        }
        }
Example #2
0
        //[Test]
        public static void MasterDetailTest()
        {
            List <object> expected = new List <object>
            {
                new KeyValuePair <Recipe, RecipeLineItem>(new Recipe {
                    Id = 1704, Name = "Birthday cake"
                }, new RecipeLineItem {
                    Index = 1, Amount = 25
                }),
                new KeyValuePair <Recipe, RecipeLineItem>(new Recipe {
                    Id = 1704, Name = "Birthday cake"
                }, new RecipeLineItem {
                    Index = 2, Amount = 25
                }),
                new KeyValuePair <Recipe, RecipeLineItem>(new Recipe {
                    Id = 1704, Name = "Birthday cake"
                }, new RecipeLineItem {
                    Index = 3
                }),
                new KeyValuePair <Recipe, RecipeLineItem>(new Recipe {
                    Id = 1804, Name = "Wedding cake"
                }, new RecipeLineItem {
                    Index = 1, Amount = 25
                }),
                new KeyValuePair <Recipe, RecipeLineItem>(new Recipe {
                    Id = 1804, Name = "Wedding cake"
                }, new RecipeLineItem {
                    Index = 2, Amount = 25
                }),
                new KeyValuePair <Recipe, RecipeLineItem>(new Recipe {
                    Id = 1804, Name = "Wedding cake"
                }, new RecipeLineItem {
                    Index = 3, Amount = 50
                }),
            };
            List <object> actual = new List <object>();

            using (var parser = new ChoManifoldReader(FileNameMasterDetailTXT)
                                .WithCustomRecordSelector((v) =>
            {
                string l = v as string;
                if (l.SplitNTrim(';')[0].CastTo <int>() > 1700)
                {
                    return(typeof(Recipe));
                }
                else
                {
                    return(typeof(RecipeLineItem));
                }
            })
                   )
            {
                foreach (var r in parser.ToMasterDetail <Recipe, RecipeLineItem>())
                {
                    actual.Add(r);
                }
            }

            CollectionAssert.AreEqual(expected, actual);
        }
Example #3
0
        public IEnumerator GetEnumerator()
        {
            ChoManifoldReader reader = new ChoManifoldReader(_streamReader, Configuration as ChoManifoldRecordConfiguration).WithRecordSelector(0, 1, typeof(ChoNACHABatchHeaderRecord), typeof(ChoNACHABatchControlRecord),
                                                                                                                                                typeof(ChoNACHAFileHeaderRecord), typeof(ChoNACHAFileControlRecord), typeof(ChoNACHAEntryDetailRecord), typeof(ChoNACHAAddendaRecord));

            reader.Configuration.ObjectValidationMode = ChoObjectValidationMode.ObjectLevel;
            object state = null;

            return(ChoNACHAEnumeratorWrapper.BuildEnumerable <object>(() => (state = reader.Read()) != null, () => state).GetEnumerator());
        }
Example #4
0
        static void LoadTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoManifoldReader(reader).WithFirstLineHeader())
                        {
                            parser.WithCustomRecordSelector((value) =>
                            {
                                string recordLine = value as string;
                                if (recordLine.Length == 0)
                                {
                                    return(null);
                                }

                                if (Char.IsLetter(recordLine[0]))
                                {
                                    return(typeof(Customer));
                                }
                                else if (recordLine.Length == 14)
                                {
                                    return(typeof(SampleType));
                                }
                                else
                                {
                                    return(typeof(Orders));
                                }
                            });
                            //parser.Configuration[typeof(ExpandoObject)] = new ChoCSVRecordConfiguration();

                            writer.WriteLine("Header");
                            writer.WriteLine("10248|VINET|5|04071996|01081996|16071996|3|32.38  ");
                            writer.WriteLine("10249|TOMSP|6|05071996|16081996|10071996|1|11.61");
                            writer.WriteLine("ALFKI;Alfreds Futterkiste;Maria Anders;Sales Representative;Obere Str. 57;Berlin;Germany");
                            writer.WriteLine("ANATR;Ana Trujillo Emparedados y helados;Ana Trujillo;Owner;Avda. de la Constitución 2222;México D.F.;Mexico");
                            writer.WriteLine("10250|HANAR|4|08071996|05081996|12071996|2|65.83");
                            writer.WriteLine("10111314012345");
                            writer.WriteLine("11101314123456");
                            writer.WriteLine("10251|VICTE|3|08071996|05081996|15071996|1|41.34");
                            writer.WriteLine("11121314901234");
                            writer.WriteLine("10101314234567");
                            writer.WriteLine("ANTON;Antonio Moreno Taquería;Antonio Moreno;Owner;Mataderos  2312;México D.F.;Mexico");
                            writer.WriteLine("BERGS;Berglunds snabbköp;Christina Berglund;Order Administrator;Berguvsvägen  8;Luleå;Sweden");

                            writer.Flush();
                            stream.Position = 0;

                            object rec;
                            while ((rec = parser.Read()) != null)
                            {
                                Console.WriteLine(rec.ToStringEx());
                            }
                        }
        }
Example #5
0
        //[Test]
        public static void QuickTest()
        {
            List <object> expected = new List <object>
            {
                new ChoDynamicObject {
                    { "Id", "1" }, { "Name", "Carl" }, { "Salary", "1000" }
                },
                new ChoDynamicObject {
                    { "Id", "2" }, { "Name", "Mark" }, { "Salary", "2000" }
                },
                new ChoDynamicObject {
                    { "Id", "3" }, { "Name", "Tom" }, { "Salary", "3000" }
                }
            };
            List <object> actual = new List <object>();

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoManifoldReader(reader))
                        {
                            parser.Configuration.RecordSelector         = (l) => typeof(ExpandoObject);
                            parser.Configuration[typeof(ExpandoObject)] = new ChoCSVRecordConfiguration();

                            writer.WriteLine("Id,Name,Salary");
                            writer.WriteLine("1,Carl,1000");
                            writer.WriteLine("2,Mark,2000");
                            writer.WriteLine("3,Tom,3000");

                            writer.Flush();
                            stream.Position = 0;

                            object rec;
                            while ((rec = parser.Read()) != null)
                            {
                                actual.Add(rec);
                            }
                        }

            CollectionAssert.AreEqual(expected, actual);
        }
Example #6
0
        //[Test]
        public static void QuickPOCOTest()
        {
            List <object> expected = new List <object>
            {
                new EmployeeRecWithCurrency {
                    Id = 1, Name = "Carl", Salary = new ChoCurrency(1000)
                },
                new EmployeeRecWithCurrency {
                    Id = 2, Name = "Mark", Salary = new ChoCurrency(2000)
                },
                new EmployeeRecWithCurrency {
                    Id = 3, Name = "Tom", Salary = new ChoCurrency(3000)
                }
            };
            List <object> actual = new List <object>();

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoManifoldReader(reader))
                        {
                            parser.Configuration.RecordSelector = (l) => typeof(EmployeeRecWithCurrency);
                            //parser.Configuration[typeof(ExpandoObject)] = new ChoCSVRecordConfiguration();

                            writer.WriteLine("1,Carl,1000");
                            writer.WriteLine("2,Mark,2000");
                            writer.WriteLine("3,Tom,3000");

                            writer.Flush();
                            stream.Position = 0;

                            object rec;
                            while ((rec = parser.Read()) != null)
                            {
                                actual.Add(rec);
                            }
                        }

            CollectionAssert.AreEqual(expected, actual);
        }
Example #7
0
 static void MasterDetailTest()
 {
     using (var parser = new ChoManifoldReader("MasterDetail.txt")
                         .WithCustomRecordSelector((l) =>
     {
         if (l.SplitNTrim(';')[0].CastTo <int>() > 1700)
         {
             return(typeof(Recipe));
         }
         else
         {
             return(typeof(RecipeLineItem));
         }
     })
            )
     {
         foreach (var r in parser.ToMasterDetail <Recipe, RecipeLineItem>())
         {
             Console.WriteLine(r.ToStringEx());
         }
     }
 }
Example #8
0
        ////[Test]
        public static void LoadTest()
        {
            List <object> expected = new List <object>
            {
                new Orders {
                    OrderID = 10248, CustomerID = "VINET", EmployeeID = 5, OrderDate = new DateTime(1996, 7, 4), RequiredDate = "01081996", ShippedDate = "16071996", ShipVia = 3, Freight = (decimal)32.38
                },
                new Orders {
                    OrderID = 10249, CustomerID = "TOMSP", EmployeeID = 6, OrderDate = new DateTime(1996, 7, 5), RequiredDate = "16081996", ShippedDate = "10071996", ShipVia = 1, Freight = (decimal)11.61
                },
                new Customer {
                    CustomerID = "ALFKI", CompanyName = "Alfreds Futterkiste", ContactName = "Maria Anders", ContactTitle = "Sales Representative", Address = "Obere Str. 57", City = "Berlin", Country = "Germany"
                },
                new Customer {
                    CustomerID = "ANATR", CompanyName = "Ana Trujillo Emparedados y helados", ContactName = "Ana Trujillo", ContactTitle = "Owner", Address = "Avda. de la Constitución 2222", City = "México D.F.;", Country = "Mexico"
                },
                new Orders {
                    OrderID = 10250, CustomerID = "HANAR", EmployeeID = 4, OrderDate = new DateTime(1996, 7, 8), RequiredDate = "05081996", ShippedDate = "12071996", ShipVia = 2, Freight = (decimal)65.83
                },
                new SampleType {
                    Field1 = "10111314", Field2 = "012", Field3 = 345
                },
                new SampleType {
                    Field1 = "11101314", Field2 = "123", Field3 = 456
                },
                new Orders {
                    OrderID = 10251, CustomerID = "VICTE", EmployeeID = 3, OrderDate = new DateTime(1996, 7, 8), RequiredDate = "05081996", ShippedDate = "15071996", ShipVia = 1, Freight = (decimal)41.34
                },
                new SampleType {
                    Field1 = "11121314", Field2 = "901", Field3 = 234
                },
                new SampleType {
                    Field1 = "10101314", Field2 = "234", Field3 = 567
                },
                new Customer {
                    CustomerID = "ANTON", CompanyName = "Antonio Moreno Taquería", ContactName = "Antonio Moreno", ContactTitle = "Owner", Address = "Mataderos  2312", City = "México D.F.", Country = "Mexico"
                },
                new Customer {
                    CustomerID = "BERGS", CompanyName = "Berglunds snabbköp", ContactName = "Christina Berglund", ContactTitle = "Order Administrator", Address = "Berguvsvägen  8", City = "Luleå", Country = "Sweden"
                }
            };
            List <object> actual = new List <object>();

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var parser = new ChoManifoldReader(reader).WithFirstLineHeader())
                        {
                            parser.WithCustomRecordSelector((value) =>
                            {
                                string recordLine = value as string;
                                if (recordLine.Length == 0)
                                {
                                    return(null);
                                }

                                if (Char.IsLetter(recordLine[0]))
                                {
                                    return(typeof(Customer));
                                }
                                else if (recordLine.Length == 14)
                                {
                                    return(typeof(SampleType));
                                }
                                else
                                {
                                    return(typeof(Orders));
                                }
                            });
                            //parser.Configuration[typeof(ExpandoObject)] = new ChoCSVRecordConfiguration();

                            writer.WriteLine("Header");
                            writer.WriteLine("10248|VINET|5|04071996|01081996|16071996|3|32.38  ");
                            writer.WriteLine("10249|TOMSP|6|05071996|16081996|10071996|1|11.61");
                            writer.WriteLine("ALFKI;Alfreds Futterkiste;Maria Anders;Sales Representative;Obere Str. 57;Berlin;Germany");
                            writer.WriteLine("ANATR;Ana Trujillo Emparedados y helados;Ana Trujillo;Owner;Avda. de la Constitución 2222;México D.F.;Mexico");
                            writer.WriteLine("10250|HANAR|4|08071996|05081996|12071996|2|65.83");
                            writer.WriteLine("10111314012345");
                            writer.WriteLine("11101314123456");
                            writer.WriteLine("10251|VICTE|3|08071996|05081996|15071996|1|41.34");
                            writer.WriteLine("11121314901234");
                            writer.WriteLine("10101314234567");
                            writer.WriteLine("ANTON;Antonio Moreno Taquería;Antonio Moreno;Owner;Mataderos  2312;México D.F.;Mexico");
                            writer.WriteLine("BERGS;Berglunds snabbköp;Christina Berglund;Order Administrator;Berguvsvägen  8;Luleå;Sweden");

                            writer.Flush();
                            stream.Position = 0;

                            object rec;
                            while ((rec = parser.Read()) != null)
                            {
                                actual.Add(rec);
//                    Console.WriteLine(rec.ToStringEx());
                            }
                        }

            CollectionAssert.AreEqual(expected, actual);
        }