示例#1
0
        public void FrameTableManipulation()
        {
            FrameTable table = new FrameTable();

            table.AddColumn <int>("Id");
            table.AddColumn <DateTime?>("Birthdate");
            table.AddColumns <string>("FirstName", "LastName");
            Assert.IsTrue(table.Columns.Count == 4);

            // Index lookup should work
            Assert.IsTrue(table.GetColumnIndex("Birthdate") >= 0);
            Assert.IsTrue(table.GetColumnIndex("None") < 0);

            // Add rows
            Assert.IsTrue(table.Rows.Count == 0);
            table.AddRow(1, DateTime.Parse("1990-01-01"), "a", "p");
            table.AddRow(2, DateTime.Parse("2000-02-02"), null, null);
            table.AddRow(new Dictionary <string, object>()
            {
                { "Id", 3 }, { "Birthdate", null }, { "FirstName", "c" }, { "LastName", "r" }
            });
            Assert.IsTrue(table.Rows.Count == 3);

            // Adding rows with the wrong types and/or entries should fail
            // Careful, some of these will leave the table in a bad state
            //try {
            //    table.AddRow(4, DateTime.Parse("2010-04-04"), 1.0, "s");
            //    Assert.Fail();
            //} catch (Exception) { }
            try {
                table.AddRow(4, DateTime.Parse("2010-04-04"));
                Assert.Fail();
            } catch (Exception) { }
            //try {
            //    table.AddRow(new Dictionary<string, object>() {
            //        {"Id", 4}, { "FirstName", "d" }, { "LastName", "r" }
            //    });
            //    Assert.Fail();
            //} catch (Exception) { }
            //try {
            //    table.AddRow(new Dictionary<string, object>() {
            //        {"Id", 4}, { "Birthdate", null }, { "FirstName", "d" }, { "LastName", "r" }, { "MiddleName", "u" }
            //    });
            //    Assert.Fail();
            //} catch (Exception) { }

            // Adding a new column with the wrong length should fail
            try {
                table.AddColumn <double>("Score");
                Assert.Fail();
            } catch (Exception) { }
            Assert.IsTrue(table.GetColumnIndex("Score") < 0);

            // Adding a new column with the right length should work
            List <double> scores = new List <double>()
            {
                1.1, 1.2, 1.3
            };

            table.AddColumn("Score", scores);
            Assert.IsTrue(table.GetColumnIndex("Score") >= 0);

            // Adding a new computed column should work
            table.AddComputedColumn <TimeSpan?>("Age", r => {
                DateTime?b = (DateTime?)r["Birthdate"];
                if (b.HasValue)
                {
                    return(DateTime.Now - b.Value);
                }
                else
                {
                    return(null);
                }
            });
            Assert.IsTrue(table.GetColumnIndex("Age") >= 0);

            // Changing a value should change the result of the computed column that depends on it
            int      birthdateIndex = table.GetColumnIndex("Birthdate");
            int      ageIndex       = table.GetColumnIndex("Age");
            TimeSpan age1           = (TimeSpan)table[0, ageIndex];

            table[0, birthdateIndex] = DateTime.Parse("2010-01-01");
            TimeSpan age2 = (TimeSpan)table[0, ageIndex];

            Assert.IsTrue(age2 != age1);

            // Clearing a table should work
            table.Clear();
            Assert.IsTrue(table.Columns.Count > 0);
            Assert.IsTrue(table.Rows.Count == 0);
        }