[Test] public void RowFilter()
{
//note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
// this test also check DataView.Count property
DataRowView[] drvResult = null;
ArrayList al = new ArrayList();
//create the source datatable
DataTable dt = DataProvider.CreateChildDataTable();
//create the dataview for the table
DataView dv = new DataView(dt);
//-------------------------------------------------------------
//Get excpected result
al.Clear();
foreach (DataRow dr in dt.Rows )
{
if ((int)dr["ChildId"] == 1)
{
al.Add(dr);
}
}
// RowFilter = 'ChildId=1', check count
dv.RowFilter = "ChildId=1";
Assert.AreEqual(al.Count , dv.Count , "DV70");
// RowFilter = 'ChildId=1', check rows
drvResult = new DataRowView[dv.Count];
dv.CopyTo(drvResult,0);
//check that the filterd rows exists
bool Succeed = true;
for (int i=0; i<drvResult.Length ; i++)
{
Succeed = al.Contains(drvResult[i].Row);
if (!Succeed) break;
}
Assert.AreEqual(true, Succeed , "DV71");
//-------------------------------------------------------------
//-------------------------------------------------------------
//Get excpected result
al.Clear();
foreach (DataRow dr in dt.Rows )
if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" )
al.Add(dr);
// RowFilter - ChildId=1 and String1='1-String1'
dv.RowFilter = "ChildId=1 and String1='1-String1'";
Assert.AreEqual(al.Count , dv.Count , "DV72");
// RowFilter = ChildId=1 and String1='1-String1', check rows
drvResult = new DataRowView[dv.Count];
dv.CopyTo(drvResult,0);
//check that the filterd rows exists
Succeed = true;
for (int i=0; i<drvResult.Length ; i++)
{
Succeed = al.Contains(drvResult[i].Row);
if (!Succeed) break;
}
Assert.AreEqual(true, Succeed , "DV73");
//-------------------------------------------------------------
//EvaluateException
// RowFilter - check EvaluateException
try
{
dv.RowFilter = "Col=1";
Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
}
catch (EvaluateException) {}
catch (AssertionException exc) {throw exc;}
catch (Exception exc)
{
Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
}
//SyntaxErrorException 1
// RowFilter - check SyntaxErrorException 1
try
{
dv.RowFilter = "sum('something')";
Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
}
catch (SyntaxErrorException) {}
catch (AssertionException exc) {throw exc;}
catch (Exception exc)
{
Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
}
//SyntaxErrorException 2
// RowFilter - check SyntaxErrorException 2
try
{
dv.RowFilter = "HH**!";
Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
}
catch (SyntaxErrorException) {}
catch (AssertionException exc) {throw exc;}
catch (Exception exc)
{
Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
}
}