Before we begin running tests against totals from the reports, let’s add a login method to our class for Crystal Reports. Add to the custom Crystal Object class the following login method that I use to log into a database from a Crystal Report Document. The method is named ApplyCRLogInfo. See the code in Listing 4 to see what I’ve added.
Listing 5: ApplyCRLogin Method
private void ApplyCRLogin()
{
CrystalDecisions.CrystalReports.Engine.Database oCRDb = currDoc.Database;
CrystalDecisions.CrystalReports.Engine.Tables oCRTables = oCRDb.Tables;
CrystalDecisions.Shared.TableLogOnInfo oCRTableLogonInfo;
CrystalDecisions.Shared.ConnectionInfo oCRConnectionInfo =
new CrystalDecisions.Shared.ConnectionInfo();
oCRConnectionInfo.DatabaseName = _dataBase;
oCRConnectionInfo.ServerName = _serverName;
oCRConnectionInfo.UserID = _userID;
oCRConnectionInfo.Password =_userPass;
foreach (CrystalDecisions.CrystalReports.Engine.Table oCRTable in oCRTables)
{
oCRTableLogonInfo = oCRTable.LogOnInfo;
oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo;
oCRTable.ApplyLogOnInfo(oCRTableLogonInfo);
}
}
I have added this private method within the ReportObjects class . The method is called from the Load method that is already used in another test. In LoadReport, if the UserID, Password, DatabaseName, and ServerName properties are filled, then call the ApplyCRLogin method.
The test is simple. Add a section in the SalesByYearResults test to use this part of the object. This is enacted by populating the UserID, Password, DatabaseName, and ServerName properties. See Listing 6 for the code I have added to the test.
Listing 6: Extended SalesByYearResults Test
[Test]
public void SalesByYearResults()
{
// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "SELECT Orders.ShippedDate, Orders.OrderID, [Order Subtotals].Subtotal,
DATENAME(yy,ShippedDate) AS Year
FROM Orders INNER JOIN [Order Subtotals] ON Orders.OrderID = [Order Subtotals].OrderID
WHERE Orders.ShippedDate Between '01/01/1998' And '12/31/1999'";
DBCommandWrapper dbCommandWrapper = db.GetSqlStringCommandWrapper(sqlCommand);
// DataSet that will hold the returned results
DataSet salesDataSetQuery = db.ExecuteDataSet(dbCommandWrapper);
Assert.IsTrue(salesDataSetQuery.Tables[0].Rows.Count>0, "No results was returned");
sqlCommand = "Sales By Year";
dbCommandWrapper = db.GetStoredProcCommandWrapper(sqlCommand);
// Retrieve products from the specified category.
dbCommandWrapper.AddInParameter("@Beginning_Date", DbType.DateTime,
System.Convert.ToDateTime("01/01/1998"));
dbCommandWrapper.AddInParameter("@Ending_Date", DbType.DateTime,
System.Convert.ToDateTime("12/31/1999"));
// DataSet that will hold the returned results
DataSet salesDataSetSP = db.ExecuteDataSet(dbCommandWrapper);
Assert.IsTrue(salesDataSetQuery.Tables[0].Rows.Count== salesDataSetSP.Tables[0].Rows.Count,
"Does not contain the same data.");
CrystalObjects.ReportObjects oCrObjs = new CrystalObjects.ReportObjects();
oCrObjs.ServerName="(local)";
oCrObjs.UserName="NorthWind";
oCrObjs.UserPassWord="NorthWind";
oCrObjs.Database="NorthWind";
oCrObjs.LoadReport(@"C:\Dev\UnitTestBICrystal\SalesReportToTest.rpt");
}
Now run the SalesByYearResults test to get a green bar. Next, the TestLoadReport should return a green bar as well. Now it is on to writing the meat of the tests so to speak. These are the tests comparing results from a database query to results in a report. I'll explore those tests in Part II of this series.