[TestMethod]
public void TestAccessDirectly()
{
//assign the value to input parameters
string inMovieID = "g06";
string inMovieTitle = "F is for Function";
int inMovieRunTime = 96;
decimal inMoviePrice = 66.6600M;
//connect sql server
SqlConnection sc = new SqlConnection(connString);
sc.Open();
// get both tables into a DataSet as a expected outcome
DataSet tmp = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from tblMain", sc);
sda.Fill(tmp, "tblMain");
//insert the input parameters into DataSet
DataRow tblMainDataRow = tmp.Tables[0].NewRow();
tblMainDataRow["MovID"] = inMovieID;
tblMainDataRow["MovTitle"] = inMovieTitle;
tblMainDataRow["MovRunTime"] = inMovieRunTime;
tmp.Tables[0].Rows.Add(tblMainDataRow);
DataView dv = new DataView();
dv = tmp.Tables[0].DefaultView;
dv.Sort = "MovID ASC";
DataSet expected = new DataSet();
expected.Tables.Add(dv.ToTable());
sda = new SqlDataAdapter("select * from tblPrices", sc);
sda.Fill(expected, "tblPrices");
DataRow tblPricesDataRow = expected.Tables[1].NewRow();
tblPricesDataRow["MovID"] = inMovieID;
tblPricesDataRow["MovPrice"] = inMoviePrice;
expected.Tables[1].Rows.Add(tblPricesDataRow);
// execute the procedure usp_AddMovie
SqlCommand cmd = new SqlCommand("usp_AddMovie", sc);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = cmd.Parameters.Add("@movID", SqlDbType.Char, 3);
p1.Direction = ParameterDirection.Input;
p1.Value = inMovieID;
SqlParameter p2 = cmd.Parameters.Add("@movTitle", SqlDbType.VarChar, 35);
p2.Direction = ParameterDirection.Input;
p2.Value = inMovieTitle;
SqlParameter p3 = cmd.Parameters.Add("@movRunTime", SqlDbType.Int);
p3.Direction = ParameterDirection.Input;
p3.Value = inMovieRunTime;
SqlParameter p4 = cmd.Parameters.Add("@movPrice", SqlDbType.Money);
p4.Direction = ParameterDirection.Input;
p4.Value = inMoviePrice;
cmd.ExecuteScalar();
// get both tables into a DataSet as a actual outcome
DataSet actual = expected.Clone();
sda = new SqlDataAdapter("select * from tblMain", sc);
sda.Fill(actual, "tblMain");
sda = new SqlDataAdapter("select * from tblPrices", sc);
sda.Fill(actual, "tblPrices");
bool test = TestData.TestDataTable(expected.Tables[0], actual.Tables[0]) && TestData.TestDataTable(expected.Tables[1], actual.Tables[1]);
Assert.AreEqual(true, test, "");
sc.Close();
} |