Example #1
0
        private static void ImportTableCsvFile()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);

            //pre define columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age"),
                    new DynamicTableColumn<string>("Street")
                });

            //import
            using (StreamReader reader = new StreamReader("test.csv"))
            {
                table.FromCsv(ReadFile(reader));
            }

            foreach (dynamic actualRow in table.Rows)
            {
                Console.WriteLine(
                    string.Format("{0} {1} is {2} years old.",
                        actualRow.FirstName,
                        actualRow.LastName,
                        actualRow.Age));
            }
        }
        public void FromCsv_WrongNumberOfElements()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);

            List<string> csvContent = new List<string>()
            {
                "\"Hans\",\"Mueller\",30,\"\"",
                 "\"\",\"Meier\",0"
            };

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            try
            {
                table.FromCsv(csvContent);
                Assert.Fail();
            }
            catch (FormatException)
            {
            }

            //compare
            Assert.AreEqual(0, table.Rows.Count);
        }
        public void FromCsv_WrongColumnNames()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);

            List<string> csvContent = new List<string>()
            {
                "FirstName,Abc,Age,Street",
                "\"Hans\",\"Mueller\",30,\"\"",
                 "\"\",\"Meier\",0,\"Main street\""
            };

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            try
            {
                table.FromCsv(csvContent, true, ',', true);
                Assert.Fail();
            }
            catch (FormatException)
            {
            }
        }
        public void FromCsv_WithHeaders_OtherSortOrderInFile()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            List<string> csvContent = new List<string>()
            {
                 "Street,FirstName,Age,LastName",
                 ",Hans,30,Mueller",
                 "Main street,,0,Meier"
            };

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            table.FromCsv(csvContent, true, ',', false);

            //compare
            Assert.AreEqual(2, table.Rows.Count);

            row = table.Rows[0];
            Assert.AreEqual("Hans", row.FirstName);
            Assert.AreEqual("Mueller", row.LastName);
            Assert.AreEqual(30, row.Age);
            Assert.AreEqual("", row.Street);

            row = table.Rows[1];
            Assert.AreEqual("", row.FirstName);
            Assert.AreEqual("Meier", row.LastName);
            Assert.AreEqual(0, row.Age);
            Assert.AreEqual("Main street", row.Street);
        }
        public void FromCsv_Standard()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            List<string> csvContent = new List<string>()
            {
                "\"Hans\",\"Mueller\",30,\"\"",
                 "\"\",\"Meier\",0,\"Main street\""
            };

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            table.FromCsv(csvContent);

            //compare
            Assert.AreEqual(2, table.Rows.Count);

            row = table.Rows[0];
            Assert.AreEqual("Hans", row.FirstName);
            Assert.AreEqual("Mueller", row.LastName);
            Assert.AreEqual(30, row.Age);
            Assert.AreEqual("", row.Street);

            row = table.Rows[1];
            Assert.AreEqual("", row.FirstName);
            Assert.AreEqual("Meier", row.LastName);
            Assert.AreEqual(0, row.Age);
            Assert.AreEqual("Main street", row.Street);
        }
        public void FromCsv_Semicolon_WithoutHeaders_WithoutQuotes()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            List<string> csvContent = new List<string>()
            {
                 "Hans;Mueller;30;",
                 ";Meier;0;Main street"
            };

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            table.FromCsv(csvContent, false, ';', false);

            //compare
            Assert.AreEqual(2, table.Rows.Count);

            row = table.Rows[0];
            Assert.AreEqual("Hans", row.FirstName);
            Assert.AreEqual("Mueller", row.LastName);
            Assert.AreEqual(30, row.Age);
            Assert.AreEqual("", row.Street);

            row = table.Rows[1];
            Assert.AreEqual("", row.FirstName);
            Assert.AreEqual("Meier", row.LastName);
            Assert.AreEqual(0, row.Age);
            Assert.AreEqual("Main street", row.Street);
        }
        public void FromCsv_Null()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);

            List<string> csvContent = new List<string>();

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            table.FromCsv(null);

            //compare
            Assert.AreEqual(0, table.Rows.Count);
        }
        public void FromCsv_NoColumns()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);

            List<string> csvContent = new List<string>()
            {
                "\"Hans\",\"Mueller\",30,\"\"",
                 "\"\",\"Meier\",0,\"Main street\""
            };

            //import
            try
            {
                table.FromCsv(csvContent);
                Assert.Fail();
            }
            catch (NotSupportedException)
            {
            }
        }
        public void FromCsv_AlreadyContainsRows()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);

            List<string> csvContent = new List<string>()
            {
                "\"Hans\",\"Mueller\",30,\"\"",
                 "\"\",\"Meier\",0,\"Main street\""
            };

            //columns
            table.PreDefineColumns(
                new List<IDynamicTableColumn>()
                {
                    new DynamicTableColumn<string>("FirstName"),
                    new DynamicTableColumn<string>("LastName"),
                    new DynamicTableColumn<int>("Age", -1),
                    new DynamicTableColumn<string>("Street"),
                });

            //import
            table.FromCsv(csvContent);

            //compare
            Assert.AreEqual(2, table.Rows.Count);

            //import again
            try
            {
                table.FromCsv(csvContent);
                Assert.Fail();
            }
            catch (NotSupportedException)
            {
            }
        }
        public void ExportAndImport_CSV_Semicolon_WithoutHeader_WithoutQuotes()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            string csvExport;

            string fileName = _assemblyDirectory + @"\CsvTest.txt";

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            row.TimeStamp = new DateTime(2012, 12, 24, 1, 2, 3);
            table.AddRow(row);

            row = new ExpandoObject();
            row.LastName = "Meier";
            row.Street = "Main street";
            table.AddRow(row);

            //compare
            Assert.AreEqual(2, table.Rows.Count);
            Assert.AreEqual(5, table.Columns.Count);

            //export
            csvExport = table.AsCsv(false, ';', false);

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(csvExport);
            }

            //remove rows
            table.RemoveAllRows();
            Assert.AreEqual(0, table.Rows.Count);
            Assert.AreEqual(5, table.Columns.Count);

            //import
            using (StreamReader reader = new StreamReader(fileName))
            {
                table.FromCsv(ReadFile(reader), false, ';', false);
            }

            //compare
            row = table.Rows[0];
            Assert.AreEqual("Hans", row.FirstName);
            Assert.AreEqual("Mueller", row.LastName);
            Assert.AreEqual(30, row.Age);

            Assert.AreEqual(2012, row.TimeStamp.Year);
            Assert.AreEqual(12, row.TimeStamp.Month);
            Assert.AreEqual(24, row.TimeStamp.Day);
            Assert.AreEqual(1, row.TimeStamp.Hour);
            Assert.AreEqual(2, row.TimeStamp.Minute);
            Assert.AreEqual(3, row.TimeStamp.Second);

            Assert.AreEqual("", row.Street);

            row = table.Rows[1];
            Assert.AreEqual("", row.FirstName);
            Assert.AreEqual("Meier", row.LastName);
            Assert.AreEqual(0, row.Age);
            Assert.AreEqual(0, row.TimeStamp.Ticks);
            Assert.AreEqual("Main street", row.Street);
        }