This is a migrated thread and some comments may be shown as answers.

Unit test for Sorting & Filtering - RadGridView

1 Answer 984 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Karandeep Singh
Top achievements
Rank 1
Karandeep Singh asked on 24 Apr 2012, 06:09 PM
Hi,

I want to write automated unit tests for Sort and filter functionality in RadGridView. 
In my test method I want to bind 1000 mock records to the GridView and run the sort and filter. 
Is that possible? If Yes throw some light please.


Karandeep Singh

1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 27 Apr 2012, 12:44 PM
Hi Karandeep,

Here is a sample code with unit tests for the filtering and sorting operations:

TestMethod]
public void TestLikeOperatorWithWildcard()
{
    using (Form form = new Form())
    {
        form.Size = new System.Drawing.Size(640, 480);
        RadGridView grid = new RadGridView();
        grid.Dock = System.Windows.Forms.DockStyle.Fill;
        grid.EnableFiltering = true;
        form.Controls.Add(grid);
 
        DataTable table = new DataTable("table");
        table.Columns.Add("ID");
        table.Columns.Add("Name");
        table.Columns.Add("City");
        table.Columns.Add("Value", typeof(byte));
 
        grid.DataSource = table;
        form.Show();
 
        grid.Columns[1].HeaderText = "Test3";
        grid.Columns[1].Name = "Test3";
 
        grid.Rows.Add("111", "Nancy Davolio", "Seattle", 12);
        grid.Rows.Add("222", "Andrew Fuller", "Tacoma", 4);
        grid.Rows.Add("333", "Janet Leverling", "Kirkland", 6);
        grid.Rows.Add("444", "Margaret \"Peacock", "Redmond", 8);
        grid.Rows.Add("555", "Steven Buchanan", "London", 2);
 
        //apply filter descriptor with LIKE operator
        grid.FilterDescriptors.Add(new FilterDescriptor("City", FilterOperator.IsLike, "%ond"));
 
        Assert.AreEqual(grid.MasterView.ChildRows.Count, 1, "Wrong filtered rows number with LIKE operator");
 
        //apply filter descriptor with NOT LIKE operator
        grid.FilterDescriptors.Clear();
        grid.FilterDescriptors.Add(new FilterDescriptor("City", FilterOperator.IsNotLike, "Londo%"));
 
        Assert.AreEqual(grid.MasterView.ChildRows.Count, 4, "Wrong filtered rows number with NOT LIKE operator");
    }
}
 
 
[TestMethod]
public void RowIndexAfterSortingTest()
{
    using (Form form = new Form())
    {
        RadGridView gridView = UnitTestHelper.AttachGrid(form);
        form.Show();
 
        gridView.ColumnCount = 2;
        gridView.Rows.Add("Ivan", 1);
        gridView.Rows.Add("Petur", 1);
        gridView.Rows.Add("Dragomir", 1);
        gridView.Rows.Add("Veselin", 2);
        gridView.Rows.Add("Kocho", 2);
        gridView.Rows.Add("Gocho", 3);
        gridView.Rows.Add("Tashko", 3);
 
        gridView.SortDescriptors.Expression = "column2 desc";
 
        Assert.AreEqual("Gocho", gridView.ChildRows[0].Cells[0].Value);
        Assert.AreEqual(0, gridView.ChildRows[0].Index);
 
        Assert.AreEqual("Tashko", gridView.ChildRows[1].Cells[0].Value);
        Assert.AreEqual(1, gridView.ChildRows[1].Index);
 
        Assert.AreEqual("Veselin", gridView.ChildRows[2].Cells[0].Value);
        Assert.AreEqual(2, gridView.ChildRows[2].Index);
 
        Assert.AreEqual("Kocho", gridView.ChildRows[3].Cells[0].Value);
        Assert.AreEqual(3, gridView.ChildRows[3].Index);
 
        Assert.AreEqual("Ivan", gridView.ChildRows[4].Cells[0].Value);
        Assert.AreEqual(4, gridView.ChildRows[4].Index);
 
        Assert.AreEqual("Petur", gridView.ChildRows[5].Cells[0].Value);
        Assert.AreEqual(5, gridView.ChildRows[5].Index);
 
        Assert.AreEqual("Dragomir", gridView.ChildRows[6].Cells[0].Value);
        Assert.AreEqual(6, gridView.ChildRows[6].Index);
    }
}

I hope this helps.

Regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Karandeep Singh
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or