Friday, March 22, 2013

C# - Data Reader For Test Result Validation

This is a two class solution to reading data from the database for data scenario validation.

//CLASS 1

using System;
using System.Data.SqlClient;

namespace Thing_Automation.Helpers
{
    public class DBUtils
    {
        private static string SQL_DB_SOURCE = "SERVERNAMESOMETHING";
        private static string SQL_DB_CATALOG = "DATABASESOMETHING";
        private static string connectionString = "Data Source=" + SQL_DB_SOURCE + "; Initial Catalog=" + SQL_DB_CATALOG + "; Integrated Security=true; MultipleActiveResultSets=True";
     
        /// <summary>
        /// Get the score relative to the most recent completion
        /// </summary>
        /// <param name="Thing_Type">name of the Thing_ from the Thing_ table used by the client e.g. Thing_ 10, Thing_ 13</param>
        /// <param name="TestTime">Time the test started. Used to filter out Thing_ score relevant to this test context</param>
        /// <param name="UserName">User login for this test context</param>
        /// <returns>a decimal representing the actual Thing_ score</returns>

        public decimal ActualRawScore = 887;
        public int ActualThing_Level = 888;
        public decimal ActualThing_Score = 889;

        public Tuple<decimal,int,decimal> GetThing_Score(string Thing_Type, DateTime TestTime, string UserName)
        {

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    string queryString = "USE " + SQL_DB_CATALOG
                                         + " SELECT SRP_RawScore.[Value], SRP_Thing_Lvl.Value, SRP_Thing_Score.Value FROM Thing_ S"
                                         + " INNER JOIN [Thing_AnswerSet_] SR ON S.Thing_ID = SR.Thing_ID "
                                         + " INNER JOIN Loging A ON A.PersonID = SR.PersonID "
                                         + " INNER JOIN [Thing_AnswerSet_Profile] SRP_RawScore ON SR.Thing_AnswerSet_ID = SRP_RawScore.Thing_AnswerSet_ID  AND SRP_RawScore.Thing_AnswerSet_ProfileTypeID = 1 "
                                         + " INNER JOIN [Thing_AnswerSet_Profile] SRP_Thing_Lvl ON SR.Thing_AnswerSet_ID = SRP_Thing_Lvl.Thing_AnswerSet_ID  AND SRP_Thing_Lvl.Thing_AnswerSet_ProfileTypeID = 2 "
                                         + " INNER JOIN [Thing_AnswerSet_Profile] SRP_Thing_Score ON SR.Thing_AnswerSet_ID = SRP_Thing_Score.Thing_AnswerSet_ID  AND SRP_Thing_Score.Thing_AnswerSet_ProfileTypeID = 3 "
                                         + " WHERE S.Name = '" + Thing_Type + "'"
                                         + " AND CompletedDateTime > '" + TestTime + "'"
                                         + " AND A.LoginName = '" + UserName + "'";

                    SqlCommand spcmd = new SqlCommand(queryString, connection);

                    //try
                    //{
                        SqlDataReader reader = null;
                        connection.Open();
                        reader = spcmd.ExecuteReader();
                        reader.Read();
                        ActualRawScore = decimal.Parse(reader[0].ToString());
                        ActualThing_Level = int.Parse(reader[1].ToString());
                        ActualThing_Score = decimal.Parse(reader[2].ToString());
                    //}
                    //catch (Exception e)
                    //{
                    //    ActualThing_Score = 999;
                    //}
               }
            return new Tuple<decimal, int, decimal>(ActualRawScore,ActualThing_Level,ActualThing_Score);
            }
        }
    }




//CONSUMING CLASS
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Thing_Automation.Helpers;
using Thing_Automation.Pages;


namespace Thing_Automation.Tests
{

    [TestClass]
    public class Thingie_10Thing_ScoringTest : Thingie_Thing_Page
    {

        private decimal ActualRawScore;
        private int ActualThingie_Level;
        private decimal ActualThingie_Score;

        //TODO: move this stuff to the app.config
        private const string ClientSetupFile = "StLukesClientSetups.xml";
        private const string Thingie_Type = "Thingie_ 10";

        DBUtils utils = new DBUtils();

        /// <summary>
        /// method used to test Thingie_ score. references a Thingie_ score method with special joins in dbutil.
        /// inherits the registration and navigation for the UI from the registration and Thingie_Thing_ page classes
        /// </summary>
        /// <param name="regFile">The name of the registration file you want to pass in. Any valid age/gender scenario will work for Thingie_ 10 scoring</param>
        /// <param name="ClientSetupFile">The name of the access code file for the client you are passing.</param>
        /// <param name="Thingie_Thing_ScoreFile">The name of the file containing the answer set you want to pass into the scoring engine. These values are the ids for the answers rendered in the DOM/UI</param>
        /// <param name="ExpectedRawScore">The raw score you expect from the answers you are passing in</param>
        /// <param name="ExpectedThingie_Level">The Thingie_ level you expect from the answers you are passing in</param>
        /// <param name="ExpectedThingie_Score">The activation score you expect from the answers you are passing in</param>
        public void Thingie_10Thing_ScoreTest(string regFile, string ClientSetupFile, string Thingie_Thing_ScoreFile, decimal ExpectedRawScore, int ExpectedThingie_Level, decimal ExpectedThingie_Score)
        {
            CompleteThingie_10Thing_(regFile, ClientSetupFile, Thingie_Thing_ScoreFile);
            SignOut();
            ConfirmOnLoginPage();

            utils.GetThingie_Score(Thingie_Type, TestTime, UserName);
            ActualRawScore = utils.ActualRawScore;
            ActualThingie_Level = utils.ActualThingie_Level;
            ActualThingie_Score = utils.ActualThingie_Score;

            Assert.AreEqual(ExpectedRawScore, ActualRawScore);
            Assert.AreEqual(ExpectedThingie_Level, ActualThingie_Level);
            Assert.AreEqual(ExpectedThingie_Score, ActualThingie_Score);

            TearDown();
        }


       [TestMethod]
        public void Thingie_10Thing_AllNA()
    {
        Thingie_10Thing_ScoreTest("RegistrationMale58YearsOld.xml", ClientSetupFile, "Thingie_10Thing_AllNA.xml", 0, 2, 0);
    }

        [TestMethod]
        public void Thingie_10Thing_AllStrongA()
        {
            Thingie_10Thing_ScoreTest("RegistrationMale58YearsOld.xml", ClientSetupFile, "Thingie_10Thing_AllStrongA.xml", 40, 4, 99.1m);
        }
//...more methods
    }
}