public void AddRow_Error_AdditionalElement_DefineOnce()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.DefineOnce);
            dynamic row;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            try
            {
                row = new ExpandoObject();
                row.FirstName = "Hans";
                row.LastName = "Meier";
                row.Age = 30;
                row.Street = "Main street";
                table.AddRow(row);

                Assert.Fail();
            }
            catch (ArgumentException)
            {
            }
        }
        public void AddRows_Error_Rollback()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            dynamic rowB;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            try
            {
                row = new ExpandoObject();
                row.FirstName = "Hans";
                row.LastName = "Meier";
                row.Age = 30;

                rowB = new ExpandoObject();
                rowB.LastName = 50;
                rowB.Street = "Main street";

                table.AddRows(new List<dynamic>(){ row, rowB});

                Assert.Fail();
            }
            catch (ArgumentException)
            {
            }

            //compare
            Assert.AreEqual(1, table.Rows.Count);
        }
        public void AsDataTable_Standard_PreDefineColumns()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            DataTable dataTable;

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

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

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

            //get data table
            dataTable = table.AsDataTable();

            //compare
            Assert.AreEqual(3, dataTable.Columns.Count);
            Assert.AreEqual("FirstName", dataTable.Columns[0].ColumnName);
            Assert.AreEqual("LastName", dataTable.Columns[1].ColumnName);
            Assert.AreEqual("Age", dataTable.Columns[2].ColumnName);
            Assert.AreEqual("System.String", dataTable.Columns[0].DataType.ToString());
            Assert.AreEqual("System.String", dataTable.Columns[1].DataType.ToString());
            Assert.AreEqual("System.Int32", dataTable.Columns[2].DataType.ToString());

            Assert.AreEqual(2, dataTable.Rows.Count);

            Assert.AreEqual("Hans", dataTable.Rows[0].ItemArray[0]);
            Assert.AreEqual("Mueller", dataTable.Rows[0].ItemArray[1]);
            Assert.AreEqual(30, dataTable.Rows[0].ItemArray[2]);

            Assert.AreEqual("", dataTable.Rows[1].ItemArray[0]);
            Assert.AreEqual("Meier", dataTable.Rows[1].ItemArray[1]);
            Assert.AreEqual(-1, dataTable.Rows[1].ItemArray[2]);
        }
        public void AsDataTable_Standard()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            DataTable dataTable;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            row = new ExpandoObject();
            row.LastName = "Meier";
            row.Street = "Main street";
            row.Birthday = new DateTime(2001, 12, 20);
            table.AddRow(row);

            //get data table
            dataTable = table.AsDataTable();

            //compare
            Assert.AreEqual(5, dataTable.Columns.Count);
            Assert.AreEqual("FirstName", dataTable.Columns[0].ColumnName);
            Assert.AreEqual("LastName", dataTable.Columns[1].ColumnName);
            Assert.AreEqual("Age", dataTable.Columns[2].ColumnName);
            Assert.AreEqual("Street", dataTable.Columns[3].ColumnName);
            Assert.AreEqual("Birthday", dataTable.Columns[4].ColumnName);
            Assert.AreEqual("System.String", dataTable.Columns[0].DataType.ToString());
            Assert.AreEqual("System.String", dataTable.Columns[1].DataType.ToString());
            Assert.AreEqual("System.Int32", dataTable.Columns[2].DataType.ToString());
            Assert.AreEqual("System.String", dataTable.Columns[3].DataType.ToString());
            Assert.AreEqual("System.DateTime", dataTable.Columns[4].DataType.ToString());

            Assert.AreEqual(2, dataTable.Rows.Count);

            Assert.AreEqual("Hans", dataTable.Rows[0].ItemArray[0]);
            Assert.AreEqual("Mueller", dataTable.Rows[0].ItemArray[1]);
            Assert.AreEqual(30, dataTable.Rows[0].ItemArray[2]);
            Assert.AreEqual(DBNull.Value, dataTable.Rows[0].ItemArray[3]);
            Assert.AreEqual(new DateTime(0), dataTable.Rows[0].ItemArray[4]);

            Assert.AreEqual(DBNull.Value, dataTable.Rows[1].ItemArray[0]);
            Assert.AreEqual("Meier", dataTable.Rows[1].ItemArray[1]);
            Assert.AreEqual(0, dataTable.Rows[1].ItemArray[2]);
            Assert.AreEqual("Main street", dataTable.Rows[1].ItemArray[3]);
            Assert.AreEqual(new DateTime(2001, 12, 20), dataTable.Rows[1].ItemArray[4]);
        }
        public void AsCsv_Standard()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            string csvContent;
            string expectedContent;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

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

            //get csv
            csvContent = table.AsCsv();

            //compare
            expectedContent = "\"Hans\",\"Mueller\",30,\"\"" + Environment.NewLine +
                 "\"\",\"Meier\",0,\"Main street\"" + Environment.NewLine;

            Assert.AreEqual(expectedContent, csvContent);
        }
        public void AsCsv_Semicolon_WithoutHeaders_WithoutQuotes()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            string csvContent;
            string expectedContent;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

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

            //get csv
            csvContent = table.AsCsv(false, ';', false);

            //compare
            expectedContent = "Hans;Mueller;30;" + Environment.NewLine +
                 ";Meier;0;Main street" + Environment.NewLine;

            Assert.AreEqual(expectedContent, csvContent);
        }
        public void ResetTable_Standard()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            //compare
            Assert.AreEqual(1, table.Rows.Count);
            Assert.AreEqual(3, table.Columns.Count);

            //reset
            table.ResetTable();

            //compare
            Assert.AreEqual(null, table.Rows);
            Assert.AreEqual(null, table.Columns);
        }
        public void GetColumn_Standard()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            IDynamicTableColumn column;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            //compare
            column = table.GetColumn("LastName");
            Assert.AreEqual("LastName", column.Name);

            column = table.GetColumn("Age");
            Assert.AreEqual("Age", column.Name);
        }
        public void UseTable_PreDefinedColumns_Expandeable()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

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

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            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(4, table.Columns.Count);

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

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

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            try
            {
                row = new ExpandoObject();
                row.FirstName = "Hans";
                row.LastName = "Meier";
                table.AddRow(row);

                Assert.Fail();
            }
            catch (ArgumentException)
            {
            }
        }
Example #11
0
        static void Main(string[] args)
        {
            dynamic element = new ExpandoObject();
            element.FirstName = "John";
            element.LastName = "Doe";
            element.Age = 30;

            using (StreamWriter writer = new StreamWriter("test.csv"))
            {
                writer.Write(ExpandoObjectSerializer.AsCsv(element));
            }

            ImportElementCsvFile();

            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            row = new ExpandoObject();
            row.FirstName = "John";
            row.LastName = "Doe";
            row.Age = 30;
            table.AddRow(row);

            row = new ExpandoObject();
            row.FirstName = "Jane";
            row.LastName = "Doe";
            row.Street = "Main street";
            table.AddRow(row);

            foreach (dynamic actualRow in table.Rows)
            {
                Console.WriteLine(
                    string.Format("{0} {1} is {2} years old.",
                        actualRow.FirstName,
                        actualRow.LastName,
                        actualRow.Age));
            }

            using (StreamWriter writer = new StreamWriter("test.csv"))
            {
                writer.Write(table.AsCsv());
            }

            using (StreamWriter writer = new StreamWriter("test.xml"))
            {
                writer.Write(table.AsXml());
            }

            ImportTableCsvFile();

            Console.ReadKey();
        }
        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);
        }
        public void UseTable_WithoutColumnDefinition_WellFormet()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.WellFormed);
            dynamic row;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            row = new ExpandoObject();
            row.FirstName = "Sarah";
            row.LastName = "Meier";
            row.Age = 22;
            table.AddRow(row);

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

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

            row = table.Rows[1];
            Assert.AreEqual("Sarah", row.FirstName);
            Assert.AreEqual("Meier", row.LastName);
            Assert.AreEqual(22, row.Age);
        }
        public void UseTable_WithoutColumnDefinition_Expandeable()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            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(4, table.Columns.Count);

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

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

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

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            row = new ExpandoObject();
            row.FirstName = "Sarah";
            row.LastName = "Meier";
            row.Age = 50;
            table.AddRow(row);

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

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

            row = table.Rows[1];
            Assert.AreEqual("Sarah", row.FirstName);
            Assert.AreEqual("Meier", row.LastName);
            Assert.AreEqual(50, row.Age);
        }
        public void AsXml_Standard()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            string csvContent;
            string expectedContent;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

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

            //get csv
            csvContent = table.AsXml();

            //compare
            expectedContent =
                "<DynamicTable>" + Environment.NewLine +
                "  <Rows>" + Environment.NewLine +
                "    <Row>" + Environment.NewLine +
                "      <FirstName value=\"Hans\" />" + Environment.NewLine +
                "      <LastName value=\"Mueller\" />" + Environment.NewLine +
                "      <Age value=\"30\" />" + Environment.NewLine +
                "      <Street value=\"\" />" + Environment.NewLine +
                "    </Row>" + Environment.NewLine +
                "    <Row>" + Environment.NewLine +
                "      <FirstName value=\"\" />" + Environment.NewLine +
                "      <LastName value=\"Meier\" />" + Environment.NewLine +
                "      <Age value=\"0\" />" + Environment.NewLine +
                "      <Street value=\"Main street\" />" + Environment.NewLine +
                "    </Row>" + Environment.NewLine +
                "  </Rows>" + Environment.NewLine +
                "</DynamicTable>";

            Assert.AreEqual(expectedContent, csvContent);
        }
        public void GetColumn_NotExisting()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;

            IDynamicTableColumn column;

            //add values
            row = new ExpandoObject();
            row.FirstName = "Hans";
            row.LastName = "Mueller";
            row.Age = 30;
            table.AddRow(row);

            //compare
            column = table.GetColumn("xxxxxxx");
            Assert.AreEqual(null, column);
        }
        public void ExportAndImport_DataTable()
        {
            IDynamicTable table = new DynamicTable(DynamicTableType.Expandable);
            dynamic row;
            DataTable data;

            //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
            data = table.AsDataTable();

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

            //import
            table.FromDataTable(data);

            //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(null, row.Street);

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