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 AsDataTable_Empty() { IDynamicTable table = new DynamicTable(DynamicTableType.Expandable); DataTable dataTable; //set columns table.PreDefineColumns( new List<IDynamicTableColumn>() { new DynamicTableColumn<string>("FirstName", ""), new DynamicTableColumn<string>("LastName"), new DynamicTableColumn<int>("Age", -1) }); //get csv 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(0, dataTable.Rows.Count); }
public void AsDataTable_NotInitialized() { IDynamicTable table = new DynamicTable(DynamicTableType.Expandable); DataTable dataTable; //get csv dataTable = table.AsDataTable(); //compare Assert.AreEqual(null, dataTable); }
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); }